|
| 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 | +} |
0 commit comments