Skip to content

Commit 5fcdb02

Browse files
committed
Added versioning to executable generation.
1 parent 675f5b5 commit 5fcdb02

File tree

10 files changed

+213
-171
lines changed

10 files changed

+213
-171
lines changed

version.sh

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,15 @@ case ${answer:0:1} in
6565
;;
6666
esac
6767

68-
# Check if at least bumping the minor version, because then we freeze the xsuite env
68+
# Two cases:
69+
# patch: Only internal changes (interface to xboinc and server etc)
70+
# No changes to the underlying C -> xsuite versions remain pinned
71+
# minor: Changes to the underlying C -> new xsuite versions are pinned
72+
# This is when we want to create a new executable for the server
6973
current_ver=(${current_ver//./ })
7074
current_ver=${current_ver[0]}.${current_ver[1]}
7175
minor_ver=(${expected_ver//./ })
76+
exec_ver=$(( 1000*${minor_ver[0]} + ${minor_ver[1]} ))
7277
minor_ver=${minor_ver[0]}.${minor_ver[1]}
7378
if [[ "$minor_ver" != "$current_ver" ]]
7479
then
@@ -87,10 +92,11 @@ then
8792
echo "Fatal error: poetry --dry-run expected $expected_ver, but result is $new_ver..."
8893
exit 1
8994
fi
90-
sed -i "s/\(__version__ =\).*/\1 '"${new_ver}"'/" xboinc/general.py
91-
sed -i "s/\(assert __version__ ==\).*/\1 '"${new_ver}"'/" tests/test_version.py
95+
sed -i "s/\(int64_t xboinc_exec_version =\).*/\1 "${exec_ver}";/" xboinc/executable/main.c
96+
sed -i "s/\(__version__ =\).*/\1 '"${new_ver}"'/" xboinc/general.py
97+
sed -i "s/\(assert __version__ ==\).*/\1 '"${new_ver}"'/" tests/test_version.py
9298
git reset
93-
git add pyproject.toml xboinc/general.py tests/test_version.py
99+
git add pyproject.toml xboinc/general.py tests/test_version.py xboinc/executable/main.c
94100
git commit -m "Updated version number to v"${new_ver}"."
95101
git push
96102

xboinc/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
from .register import register
1313
from .submit import SubmitJobs
1414

15-
from .simulation_data import build_input_file
16-
17-
from .generate_executable_source import generate_executable_source, generate_executable
15+
from .simulation_io import SimState, SimConfig
16+
from .executable import generate_executable_source, generate_executable
1817

1918

2019
# Check that the active environment has the correct pinned versions

xboinc/executable/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# copyright ############################### #
2+
# This file is part of the Xboinc Package. #
3+
# Copyright (c) CERN, 2023. #
4+
# ######################################### #
5+
6+
from .generate_executable_source import generate_executable_source, generate_executable
File renamed without changes.

xboinc/generate_executable_source.py renamed to xboinc/executable/generate_executable_source.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import xobjects as xo
22
from .default_tracker import get_default_tracker
3-
from .simulation_data import SimState, SimConfig
4-
from .general import _pkg_root
3+
from .simulation_io import SimState, SimConfig
4+
from .general import _pkg_root, __version__
55

66
from pathlib import Path
77
import shutil
@@ -16,7 +16,6 @@
1616

1717
def generate_executable_source(write_source_files=True,
1818
_context=None):
19-
2019
assert _context is None
2120
assert write_source_files
2221

@@ -52,21 +51,24 @@ def generate_executable_source(write_source_files=True,
5251

5352

5453
def generate_executable(keep_source=False):
55-
5654
main = Path.cwd() / "main.c"
5755
config = Path.cwd() / "sim_config.h"
5856
tracker = Path.cwd() / "xtrack_tracker.h"
5957
if not main.exists() or not config.exists() or not tracker.exists():
6058
source_files = generate_executable_source()
61-
59+
6260
if shutil.which("gcc") is not None:
6361
compiler = "gcc"
6462
elif shutil.which("clang") is not None:
6563
compiler = "clang"
6664
else:
6765
raise RuntimeError("Neither clang or gcc are found. Install a C compiler.")
68-
69-
cmd = subprocess.run([compiler, 'main.c', '-o', 'xboinc_executable', '-lm'],
66+
67+
tag = '.'.join(__version__.split('.')[:2])
68+
cmd = subprocess.run(['uname', '-ps'], stdout=subprocess.PIPE)
69+
if cmd.returncode == 0:
70+
tag += '-' + cmd.stdout.decode('UTF-8').strip().lower().replace(' ','-')
71+
cmd = subprocess.run([compiler, 'main.c', '-o', f'xboinc_{tag}', '-lm'],
7072
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
7173
if cmd.returncode != 0:
7274
stderr = cmd.stderr.decode('UTF-8').strip().split('\n')

xboinc/executable/main.c

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#include <math.h>
2+
3+
#include "xtrack_tracker.h"
4+
#include "sim_config.h"
5+
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
9+
10+
// Do not change
11+
// ==================================
12+
// version XXX.YYY as int (no patch)
13+
int64_t xboinc_exec_version = 0;
14+
// ==================================
15+
16+
17+
int8_t* file_to_buffer(char *filename, int8_t* buf_in){
18+
19+
FILE *sim_fid;
20+
int8_t *buf;
21+
22+
// Get buffer
23+
sim_fid = fopen(filename, "rb");
24+
if (!sim_fid){
25+
return NULL;
26+
}
27+
fseek(sim_fid, 0L, SEEK_END);
28+
unsigned long filesize = ftell(sim_fid);
29+
fseek(sim_fid, 0L, SEEK_SET);
30+
if (buf_in){
31+
printf("Reusing buffer\n");
32+
buf = buf_in;
33+
}
34+
else{
35+
buf = malloc(filesize*sizeof(int8_t));
36+
}
37+
fread(buf, sizeof(int8_t), filesize, sim_fid);
38+
fclose(sim_fid);
39+
40+
return (buf);
41+
}
42+
43+
44+
int main(){
45+
46+
int8_t* sim_buffer = file_to_buffer("./xboinc_input.bin", NULL);
47+
if (!sim_buffer){
48+
printf("Error: could not read simulation input file\n");
49+
return -1;
50+
}
51+
52+
// Get sim config
53+
SimConfig sim_config = (SimConfig) sim_buffer;
54+
SimVersion sim_ver = SimConfig_getp_sim_state_version(sim_config);
55+
int64_t input_version = SimVersionData_get_xboinc_version(sim_ver);
56+
// Compatible if major and minor versions match (no new executables are made at patches)
57+
input_version = input_version/1000;
58+
if (input_version != xboinc_exec_version){
59+
printf("Error: Xboinc version of executable and input file do not match!\n");
60+
return -1; // error
61+
}
62+
63+
const int64_t num_turns = SimConfig_get_num_turns(sim_config);
64+
const int64_t num_elements = SimConfig_get_num_elements(sim_config);
65+
printf("num_turns: %d\n", (int) num_turns);
66+
printf("num_elements: %d\n", (int) num_elements);
67+
68+
ParticlesData particles = SimConfig_getp_sim_state_particles(sim_config);
69+
SimStateData sim_state = SimConfig_getp_sim_state(sim_config);
70+
int64_t checkpoint_every = SimConfig_get_checkpoint_every(sim_config);
71+
72+
// Check if checkpoint exists
73+
printf("sim_state: %p\n", (int8_t*) sim_state);
74+
int8_t* loaded = file_to_buffer("./checkpoint.bin", (int8_t*) sim_state);
75+
if (loaded){
76+
printf("Loaded checkpoint\n");
77+
}
78+
else{
79+
printf("No checkpoint found\n");
80+
}
81+
82+
int64_t step_turns;
83+
if (checkpoint_every>0){
84+
step_turns = checkpoint_every;
85+
} else {
86+
step_turns = num_elements;
87+
}
88+
89+
int64_t current_turn = SimStateData_get_i_turn(sim_state);
90+
while (current_turn < num_turns){
91+
if (current_turn > num_turns - checkpoint_every){
92+
step_turns = num_turns - checkpoint_every;
93+
}
94+
track_line(
95+
sim_buffer, // int8_t* buffer,
96+
SimConfig_getp_line_metadata(sim_config), // ElementRefData elem_ref_data
97+
particles, // ParticlesData particles,
98+
step_turns, // int num_turns,
99+
0, // int ele_start,
100+
(int) num_elements, // int num_ele_track,
101+
1, // int flag_end_turn_actions,
102+
1, // int flag_reset_s_at_end_turn,
103+
0, // int flag_monitor,
104+
0, // int64_t num_ele_line, (needed only for backtracking)
105+
0.0, // double line_length, (needed only for backtracking)
106+
NULL, // int8_t* buffer_tbt_monitor,
107+
0, // int64_t offset_tbt_monitor
108+
NULL // int8_t* io_buffer,
109+
);
110+
SimStateData_set_i_turn(sim_state, SimStateData_get_i_turn(sim_state) + step_turns);
111+
112+
if (checkpoint_every>0){
113+
printf("Checkpointing turn %d!\n", (int) SimStateData_get_i_turn(sim_state));
114+
FILE *chkp_fid;
115+
chkp_fid = fopen("./checkpoint.bin", "wb");
116+
fwrite(SimConfig_getp_sim_state(sim_config), sizeof(int8_t),
117+
SimConfig_get_sim_state_size(sim_config), chkp_fid);
118+
fclose(chkp_fid);
119+
}
120+
}
121+
122+
// Quick check
123+
//for (int ii=0; ii<ParticlesData_get__capacity(particles); ii++){
124+
//printf("s[%d] = %e\n", ii, ParticlesData_get_s(particles, (int64_t) ii));
125+
//}
126+
127+
// Save output
128+
FILE *out_fid;
129+
out_fid = fopen("./sim_state_out.bin", "wb");
130+
fwrite(SimConfig_getp_sim_state(sim_config), sizeof(int8_t),
131+
SimConfig_get_sim_state_size(sim_config), out_fid);
132+
fclose(out_fid);
133+
134+
// Remove checkpoint
135+
if (remove("./checkpoint.bin") != 0){
136+
printf("Error: could not remove checkpoint file\n");
137+
return -1; // error
138+
}
139+
else{
140+
printf("Checkpoint file removed\n");
141+
}
142+
143+
return 0;
144+
}

xboinc/executable_src/main.c

Lines changed: 0 additions & 120 deletions
This file was deleted.

xboinc/general.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
_pkg_root = Path(__file__).parent.absolute()
99

10+
11+
# ====================================================================
12+
# Do not change
13+
# ====================================================================
14+
1015
__version__ = '0.0.3'
1116

1217
# These are the xsuite modules that are used by boinc and the versions they are tied to.
@@ -21,3 +26,5 @@
2126
'xfields' : '0.13.1',
2227
'xcoll' : '0.2.5',
2328
}
29+
30+
# ====================================================================

0 commit comments

Comments
 (0)