|
| 1 | +//This example demonstrates the simulator using the ADSim API directly with multiple ranks and multiple threads per rank |
| 2 | +//Anomalies are explicitly tagged |
| 3 | +#include<mpi.h> |
| 4 | +#include<sim.hpp> |
| 5 | +#include<random> |
| 6 | + |
| 7 | +using namespace chimbuko_sim; |
| 8 | + |
| 9 | +int main(int argc, char **argv){ |
| 10 | + MPI_Init(&argc, &argv); |
| 11 | + |
| 12 | + int window_size = 5; //number of events to record around an anomaly in the provenance data |
| 13 | + int pid = 0; //program index |
| 14 | + unsigned long program_start = 100; |
| 15 | + unsigned long step_freq = 1000; |
| 16 | + |
| 17 | + //Setup the "AD" instances |
| 18 | + int n_ranks = 4; |
| 19 | + int threads_per_rank = 2; |
| 20 | + |
| 21 | + std::vector<ADsim> ad; |
| 22 | + for(int r=0;r<n_ranks;r++) |
| 23 | + ad.push_back(ADsim(window_size, pid, r, program_start, step_freq)); |
| 24 | + |
| 25 | + //Setup some functions |
| 26 | + registerFunc("main", 500, 50, 100); |
| 27 | + registerFunc("child", 250, 25, 100); |
| 28 | + |
| 29 | + int anom_runtimes[3] = {500,750,1000}; |
| 30 | + double anom_scores[3] = {2,3,4}; |
| 31 | + |
| 32 | + //Setup some traces |
| 33 | + int nexec = 500; |
| 34 | + int anom_freq = 50; |
| 35 | + |
| 36 | + for(int rank=0;rank<n_ranks;rank++){ |
| 37 | + for(int thread=0;thread<threads_per_rank;thread++){ |
| 38 | + int anom_off = 0; |
| 39 | + |
| 40 | + unsigned long child_start = 200; |
| 41 | + |
| 42 | + unsigned long time = program_start; |
| 43 | + auto main = ad[rank].addExec(thread, "main", time, 0); //will update the end time later |
| 44 | + for(int i=0;i<nexec;i++){ |
| 45 | + CallListIterator_t child; |
| 46 | + unsigned long runtime; |
| 47 | + if(i!=0 && i % anom_freq == 0){ //an anomaly! |
| 48 | + runtime = anom_runtimes[anom_off]; |
| 49 | + double score = anom_scores[anom_off]; |
| 50 | + child = ad[rank].addExec(thread, "child", time, runtime, true, score); |
| 51 | + |
| 52 | + std::cout << rank << " " << thread << " " << i << " anomaly " << runtime << std::endl; |
| 53 | + |
| 54 | + anom_off = (anom_off + 1) % 3; //cycle through 3 times |
| 55 | + }else{ |
| 56 | + runtime = 250; |
| 57 | + child = ad[rank].addExec(thread, "child", time, runtime); |
| 58 | + |
| 59 | + std::cout << rank << " " << thread << " " << i << " normal " << runtime << std::endl; |
| 60 | + } |
| 61 | + ad[rank].bindParentChild(main, child); |
| 62 | + |
| 63 | + time = time + runtime + 1; |
| 64 | + } |
| 65 | + ad[rank].updateRuntime(main, time - program_start); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + //Run the simulation |
| 70 | + int nstep = ad[0].largest_step() + 1; |
| 71 | + for(int r=1;r<n_ranks;r++) |
| 72 | + nstep = std::max( (unsigned long)nstep, ad[r].largest_step() + 1); |
| 73 | + |
| 74 | + std::cout << "Got data for " << nstep << " steps" << std::endl; |
| 75 | + |
| 76 | + for(int i=0;i<nstep;i++){ |
| 77 | + for(int r=0;r<n_ranks;r++){ |
| 78 | + std::cout << "Rank " << r << " step " << i << " contains " << ad[r].nStepExecs(i) << " execs" << std::endl; |
| 79 | + ad[r].step(i); |
| 80 | + } |
| 81 | + //PServer dump its output |
| 82 | + getPserver().writeStreamingOutput(); |
| 83 | + } |
| 84 | + |
| 85 | + MPI_Finalize(); |
| 86 | + std::cout << "Done" << std::endl; |
| 87 | + return 0; |
| 88 | +} |
0 commit comments