signal_handlers.c 174 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993
  1. #include "signal_handlers.h"
  2. extern int num_idags, node_id ,my_idag, num_apps_terminated;//, fd_log;
  3. extern FILE *log_file, *app_log_file;
  4. extern core_states state, pending_state;
  5. extern inter_list **core_inter_head,**core_inter_tail,*init_pending_head, *init_pending_tail;
  6. extern app my_app;
  7. extern app init_app;
  8. extern app far_req_app;
  9. extern metrics my_stats, total_stats;
  10. extern int far_req_or_sender;//far_req_max_man_cores, far_req_max_man, far_req_max_man_count,
  11. extern int *idag_mask, *idag_id_arr;
  12. extern int *Cl_x_max_arr, *Cl_y_max_arr;
  13. extern int DDS_count, my_cores_count, max_cores_count;
  14. extern DDS_list *DDS, *DDS_tail;
  15. extern core_list *my_cores, *my_cores_tail;
  16. extern offer_list *init_man_offers, *selfopt_man_offers;
  17. extern offer_list *far_man_offers, *far_man_offers_tail;
  18. extern int far_list_count, far_man_agent_count;
  19. extern int my_agent, time_worked;
  20. extern int debug_global;
  21. extern time_t cur_time;
  22. extern struct tm *cur_t;
  23. extern struct sigevent sev;
  24. extern struct itimerspec its, chk_timer;
  25. extern timer_t timerid;
  26. extern long int selfopt_time_rem;//-1 means it is not set
  27. extern long int upper_work_bound;
  28. extern int time_for_farman;
  29. extern char scen_num[4];
  30. extern int chk_rem_count, chk_rem_num, sum_rem_time;
  31. extern float old_Speedup, my_Speedup;
  32. extern int nodes_ended_cnt, app_terminated, nodes_initialised, stats_replied;
  33. extern int init_DDS_replies, selfopt_DDS_replies, init_DDS_idags, selfopt_DDS_idags, selfopt_interval, init_idags_areas_replies, init_near_areas_num;
  34. extern target_list *init_targets_head, *init_targets_tail;
  35. extern target_list *selfopt_targets_head, *selfopt_targets_tail;
  36. extern application_states app_state;
  37. extern region far_reg;
  38. extern my_time_stamp init_app_times[2], my_app_times[2];
  39. extern int *sig_array, *data_array, NUES, idags_replied;
  40. extern RCCE_FLAG flag_signals_enabled,flag_data_written;
  41. /*void send_init_reqs (int sender_id) {
  42. int agent_id, i;
  43. inter_list *tmp_inter_list;
  44. target_list *tmp_target_list;
  45. for (tmp_target_list = init_targets_head; tmp_target_list != NULL; tmp_target_list = tmp_target_list->next){
  46. agent_id = tmp_target_list->target;
  47. for (tmp_inter_list = core_inter_head[agent_id]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
  48. if (tmp_inter_list->type == FAR_INIT_REQ || tmp_inter_list->type == FAR_REQ_MAN) break;
  49. if (tmp_inter_list == NULL) {
  50. if (core_inter_head[agent_id] == NULL){
  51. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  52. core_inter_tail[agent_id] = core_inter_head[agent_id];
  53. } else {
  54. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  55. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  56. }
  57. core_inter_tail[agent_id]->type = AGENT_REQ_CORES_PENDING;
  58. core_inter_tail[agent_id]->data.reg_arr.num_of_regions = tmp_target_list->num_of_regions;
  59. core_inter_tail[agent_id]->data.reg_arr.region_arr = (region *) malloc(tmp_target_list->num_of_regions * sizeof(region));
  60. if (tmp_target_list->target == node_id) printf("Why is this here node_id=%d\n",node_id);
  61. fprintf(log_file,"Init req target = %d, num_of_regions = %d. Αreas:",tmp_target_list->target,tmp_target_list->num_of_regions);
  62. for (i=0; i<tmp_target_list->num_of_regions; i++) {
  63. core_inter_tail[agent_id]->data.reg_arr.region_arr[i] = tmp_target_list->region_arr[i];
  64. fprintf(log_file," (%d,%d),",tmp_target_list->region_arr[i].C,tmp_target_list->region_arr[i].r);
  65. }
  66. fprintf(log_file,"\n");
  67. fflush(log_file);
  68. core_inter_tail[agent_id]->next = NULL;
  69. //kill(pid_num[agent_id], SIG_REQ_CORES);
  70. if (core_inter_head[agent_id]->next == NULL) {
  71. kill(pid_num[agent_id], SIG_REQ_CORES);
  72. my_stats.msg_count++;
  73. my_stats.distance += distance(node_id,agent_id);
  74. } else if (agent_id == sender_id && core_inter_head[agent_id]->next->type == AGENT_REQ_CORES_PENDING) { //den exei fugei apo to free alla einai wra na stalei
  75. kill(pid_num[agent_id], SIG_REQ_CORES);
  76. my_stats.msg_count++;
  77. my_stats.distance += distance(node_id,agent_id);
  78. } else printf("This init fucker is not NULL interaction=%d interaction2=%d\n",core_inter_head[agent_id]->type,core_inter_head[agent_id]->next->type);
  79. } else
  80. printf("I am %d and i did not send local requests to %d because he is my far manager\n",node_id,agent_id);
  81. }
  82. its.it_value.tv_nsec = INIT_NODE_INTERVAL * MS;//750000000;// * MS;
  83. if (timer_settime(timerid, 0, &its, NULL) == -1) perror("timer_settime error412\n");
  84. }
  85. void send_selfopt_reqs (int sender_id) {
  86. int agent_id, i;
  87. target_list *tmp_target_list;
  88. for (tmp_target_list = selfopt_targets_head; tmp_target_list != NULL; tmp_target_list = tmp_target_list->next){
  89. agent_id = tmp_target_list->target;
  90. if (core_inter_head[agent_id] == NULL){
  91. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  92. core_inter_tail[agent_id] = core_inter_head[agent_id];
  93. } else {
  94. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  95. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  96. }
  97. core_inter_tail[agent_id]->type = SELFOPT_REQ_CORES_PENDING;
  98. core_inter_tail[agent_id]->data.reg_arr.num_of_regions = tmp_target_list->num_of_regions;
  99. core_inter_tail[agent_id]->data.reg_arr.region_arr = (region *) malloc(tmp_target_list->num_of_regions * sizeof(region));
  100. if (tmp_target_list->target == node_id) printf("selfopt Why is this here node_id=%d\n",node_id);
  101. fprintf(log_file,"Selfopt req target = %d, num_of_regions = %d. Αreas:",tmp_target_list->target,tmp_target_list->num_of_regions);
  102. for (i=0; i<tmp_target_list->num_of_regions; i++) {
  103. core_inter_tail[agent_id]->data.reg_arr.region_arr[i] = tmp_target_list->region_arr[i];
  104. fprintf(log_file," (%d,%d),",tmp_target_list->region_arr[i].C,tmp_target_list->region_arr[i].r);
  105. }
  106. fprintf(log_file,"\n");
  107. fflush(log_file);
  108. core_inter_tail[agent_id]->next = NULL;
  109. if (core_inter_head[agent_id]->next == NULL) {
  110. kill(pid_num[agent_id], SIG_REQ_CORES);
  111. my_stats.msg_count++;
  112. my_stats.distance += distance(node_id,agent_id);
  113. } else if (agent_id == sender_id && core_inter_head[agent_id]->next->type == SELFOPT_REQ_CORES_PENDING) {
  114. kill(pid_num[agent_id], SIG_REQ_CORES);
  115. my_stats.msg_count++;
  116. my_stats.distance += distance(node_id,agent_id);
  117. } else printf("This selfopt fucker is not NULL interaction=%d interaction2=%d\n",core_inter_head[agent_id]->type,core_inter_head[agent_id]->next->type);
  118. }
  119. }
  120. */
  121. void sig_TERMINATE_handler(int sender_id)
  122. {
  123. //metrics some_stats;
  124. inter_list *tmp_inter_list;
  125. int data_array_local[LINE_SIZE];
  126. int i, error, str_len;
  127. //RCCE_FLAG_STATUS receiver_status;
  128. char error_str[64];
  129. signals_disable();
  130. cur_time = time(NULL);
  131. cur_t = localtime(&cur_time);
  132. fprintf(log_file, "[%d:%d:%d]: I entered sig_TERMINATE_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  133. fflush(log_file);
  134. if (my_idag != -1 && core_inter_head[sender_id] != NULL && core_inter_head[sender_id]->type != TERMINATION_STATS)
  135. while (core_inter_head[sender_id] != NULL && core_inter_head[sender_id]->type != TERMINATION_STATS) {
  136. fprintf(log_file,"I am still doing smth with my agent %d interaction = %d\n",sender_id,core_inter_head[sender_id]->type);
  137. fflush(log_file);
  138. tmp_inter_list = core_inter_head[sender_id];
  139. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  140. free(tmp_inter_list);
  141. }
  142. else if (my_idag == -1 && core_inter_head[sender_id] != NULL && core_inter_head[sender_id]->type != TERMINATION_STATS)
  143. while (core_inter_head[sender_id] != NULL && core_inter_head[sender_id]->type != TERMINATION_STATS) {
  144. fprintf(log_file,"I am still doing smth with my node %d interaction = %d\n",sender_id,core_inter_head[sender_id]->type);
  145. fflush(log_file);
  146. tmp_inter_list = core_inter_head[sender_id];
  147. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  148. free(tmp_inter_list);
  149. }
  150. if (core_inter_head[sender_id] == NULL) {
  151. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  152. core_inter_tail[sender_id] = core_inter_head[sender_id];
  153. core_inter_tail[sender_id]->type = REP_STATISTICS;
  154. core_inter_tail[sender_id]->next = NULL;
  155. if (my_idag != -1) {
  156. core_inter_tail[sender_id]->data.stats = my_stats;
  157. //kill(info->si_pid, SIG_TERMINATE);
  158. scc_kill(sender_id, SIG_TERMINATE);
  159. } else
  160. state = IDAG_ENDING;
  161. } else if (core_inter_head[sender_id]->type == TERMINATION_STATS) {
  162. //kill(info->si_pid, SIG_ACK);
  163. //scc_kill(sender_id, SIG_ACK);
  164. //printf("I come through here\n");
  165. //read(fd_r, &some_stats, sizeof(metrics));
  166. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  167. scc_kill(sender_id, SIG_ACK);
  168. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  169. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), LINE_SIZE * sizeof(int), node_id);
  170. if (error != RCCE_SUCCESS) {
  171. RCCE_error_string(error, error_str, &str_len);
  172. printf("I am %d and i got an error in get data in sig_TERMINATE_handler from %d with descr %s\n",node_id,sender_id,error_str);
  173. }
  174. total_stats.msg_count += data_array_local[0];//some_stats.msg_count;
  175. total_stats.message_size += data_array_local[1];//some_stats.message_size;
  176. total_stats.distance += data_array_local[2];//some_stats.distance;
  177. total_stats.app_turnaround += data_array_local[3];//some_stats.app_turnaround;
  178. total_stats.comp_effort += data_array_local[4];//some_stats.comp_effort;
  179. total_stats.cores_utilized += data_array_local[5];//some_stats.cores_utilized;
  180. total_stats.times_accessed += data_array_local[6];//some_stats.times_accessed;
  181. stats_replied++;
  182. fprintf(log_file,"I am %d and my node %d replied stats stats_replied = %d my_cores_count = %d msg_count=%d\n",node_id,sender_id,stats_replied,my_cores_count,data_array_local[0]);
  183. fflush(log_file);
  184. tmp_inter_list = core_inter_head[sender_id];
  185. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  186. free(tmp_inter_list);
  187. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  188. else printf("I am %d in sig_terminate and after free i am still doing smth with my agent interaction = %d\n",node_id,core_inter_head[sender_id]->type);
  189. } else fprintf(log_file,"I am in sig_terminate and i am still doing smth with my agent %d interaction = %d\n",sender_id,core_inter_head[sender_id]->type);
  190. cur_time = time(NULL);
  191. cur_t = localtime(&cur_time);
  192. fprintf(log_file, "[%d:%d:%d]: I ended sig_TERMINATE_handler with sender = %d state=%d stats_replied=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state,stats_replied);
  193. fflush(log_file);
  194. signals_enable();
  195. }
  196. void sig_INIT_APP_handler(int sender_id)
  197. {
  198. int data_array_local[LINE_SIZE];
  199. int i, error, str_len;
  200. //RCCE_FLAG_STATUS receiver_status;
  201. char error_str[64];
  202. signals_disable();
  203. inter_list *tmp_inter_list, *tmp_inter_prev;
  204. cur_time = time(NULL);
  205. cur_t = localtime(&cur_time);
  206. fprintf(log_file, "[%d:%d:%d]: I entered sig_INIT_APP_handler with sender=%d state=%d pending_state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state,pending_state);
  207. fflush(log_file);
  208. if (node_id == 0) {
  209. tmp_inter_prev = NULL;
  210. for (tmp_inter_list = init_pending_head; tmp_inter_list != NULL; tmp_inter_list = tmp_inter_list->next) {
  211. if (tmp_inter_list->data.new_app.num_of_cores == sender_id) break;
  212. tmp_inter_prev = tmp_inter_list;
  213. }
  214. if (tmp_inter_list != NULL) {
  215. fprintf(log_file,"I am sending an aborted init_app\n");
  216. fflush(log_file);
  217. if (core_inter_head[sender_id] == NULL){
  218. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  219. core_inter_tail[sender_id] = core_inter_head[sender_id];
  220. } else {
  221. core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
  222. core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
  223. }
  224. core_inter_tail[sender_id]->type = INIT_APP;
  225. core_inter_tail[sender_id]->data.new_app = tmp_inter_list->data.new_app;
  226. core_inter_tail[sender_id]->data.new_app.num_of_cores = 0;
  227. core_inter_tail[sender_id]->next = NULL;
  228. if (core_inter_head[sender_id]->next == NULL) {
  229. //kill(pid_num[sender_id],SIG_INIT_APP);
  230. scc_kill(sender_id,SIG_INIT_APP);
  231. my_stats.msg_count++;
  232. my_stats.distance += distance(node_id,sender_id);
  233. }
  234. if (tmp_inter_prev == NULL) {
  235. init_pending_head = init_pending_head->next;
  236. //free(tmp_inter_list);
  237. } else {
  238. tmp_inter_prev->next = tmp_inter_list->next;
  239. if (tmp_inter_prev->next == NULL) init_pending_tail = tmp_inter_prev;
  240. }
  241. free(tmp_inter_list);
  242. }
  243. } else if (state == INIT_MANAGER || state == INIT_MANAGER_SEND_OFFERS || state == IDLE_INIT_MAN || state == INIT_MAN_CHK_OFFERS || state == WORKING_NODE_IDLE_INIT) {
  244. printf("I have to reject sig_INIT_APP sender_id=%d node_id=%d state=%d\n",sender_id,node_id,state);
  245. fprintf(log_file,"I have to reject sig_INIT_APP sender_id=%d\n",sender_id);
  246. fflush(log_file);
  247. //kill(info->si_pid, SIG_REJECT);
  248. scc_kill(sender_id, SIG_REJECT);
  249. my_stats.msg_count++;
  250. my_stats.distance += distance(node_id,sender_id);
  251. } else {
  252. //kill(info->si_pid, SIG_ACK);
  253. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  254. scc_kill(sender_id, SIG_ACK);
  255. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  256. //scc_kill(sender_id, SIG_ACK);
  257. my_stats.msg_count++;
  258. my_stats.distance += distance(node_id,sender_id);
  259. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), LINE_SIZE * sizeof(int), node_id);
  260. if (error != RCCE_SUCCESS) {
  261. RCCE_error_string(error, error_str, &str_len);
  262. printf("I am %d and i got an error in get data in sig_INIT_APP from %d with descr %s\n",node_id,sender_id,error_str);
  263. }
  264. init_app.id = data_array_local[0];
  265. memcpy(&init_app.A,&data_array_local[1],sizeof(int));
  266. memcpy(&init_app.var,&data_array_local[2],sizeof(int));
  267. memcpy(&init_app.workld,&data_array_local[3],sizeof(int));
  268. init_app.num_of_cores = data_array_local[4];
  269. //read(fd_r, &init_app, sizeof(app));
  270. fprintf(log_file,"My app is A = %f, var = %f, cores = %d\n",init_app.A,init_app.var,init_app.num_of_cores);
  271. fflush(log_file);
  272. cur_time = time(NULL);
  273. cur_t = localtime(&cur_time);
  274. init_app_times[0].tm_sec = cur_t->tm_sec;
  275. init_app_times[0].tm_min = cur_t->tm_min;
  276. init_app_times[0].tm_hour = cur_t->tm_hour;
  277. //printf("I am %d and about to do an init app with my state before change = %d\n",node_id,state);
  278. if (state == IDLE_AGENT_WAITING_OFF || state == AGENT_SELF_CHK_OFFERS || state == AGENT_ZOMBIE || state == AGENT_ENDING)
  279. pending_state = INIT_MANAGER;
  280. else if (state == IDLE_AGENT) {
  281. if (timer_gettime(timerid, &chk_timer) == -1) perror("timer_gettime error init\n");
  282. else selfopt_time_rem = chk_timer.it_value.tv_nsec;
  283. its.it_value.tv_nsec = 0;
  284. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error init\n");
  285. pending_state = IDLE_AGENT;
  286. state = INIT_MANAGER;
  287. } else if (state == WORKING_NODE || state == AGENT_SELF_OPT) {
  288. pending_state = state;
  289. state = INIT_MANAGER;
  290. } else state = INIT_MANAGER;
  291. //state = INIT_MANAGER;
  292. }
  293. cur_time = time(NULL);
  294. cur_t = localtime(&cur_time);
  295. fprintf(log_file, "[%d:%d:%d]: I ended sig_INIT_APP_handler with sender=%d state=%d pending_state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state,pending_state);
  296. fflush(log_file);
  297. signals_enable();
  298. }
  299. void sig_TIMER_handler(int signo, siginfo_t *info, void *context)
  300. {
  301. cur_time = time(NULL);
  302. cur_t = localtime(&cur_time);
  303. fprintf(log_file, "[%d:%d:%d]: Alarm went off state=%d pending_state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,state,pending_state);
  304. fflush(log_file);
  305. if (state == IDLE_INIT_MAN) state = INIT_MAN_CHK_OFFERS;
  306. else if (state == IDLE_FAR_MAN) state = FAR_MAN_CHK_OFFERS;
  307. else if (state == IDLE_AGENT_WAITING_OFF) state = AGENT_SELF_CHK_OFFERS;
  308. else if (state == IDLE_AGENT) state = AGENT_SELF_OPT;
  309. else if (state == IDLE_CHK_APP_FILE) state = CHK_APP_FILE;
  310. else if (state == IDLE_INIT_MAN_SELFOPT_PENDING) state = INIT_MAN_CHK_OFFERS_SELFOPT_PENDING;
  311. else if (state == IDLE_INIT_MAN_WORK_PENDING) state = INIT_MAN_CHK_OFFERS_WORK_PENDING;
  312. else if (state == WORKING_NODE_IDLE_INIT) {
  313. state = INIT_MAN_CHK_OFFERS;
  314. pending_state = WORKING_NODE;
  315. }
  316. else if (state == AGENT_INIT_STATE) {
  317. if (pending_state == IDLE_INIT_MAN) pending_state = INIT_MAN_CHK_OFFERS;
  318. }
  319. else printf("i am %d, timer went off and i don't know what to do. My state is %d\n",node_id,state);
  320. cur_time = time(NULL);
  321. cur_t = localtime(&cur_time);
  322. fprintf(log_file, "[%d:%d:%d]: End of timer handler state=%d pending_state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,state,pending_state);
  323. fflush(log_file);
  324. }
  325. void sig_INIT_handler(int sender_id)
  326. {
  327. int data_array_local[LINE_SIZE];
  328. int error, str_len;
  329. //RCCE_FLAG_STATUS receiver_status;
  330. char error_str[64];
  331. signals_disable();
  332. //kill(info->si_pid, SIG_ACK);
  333. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  334. scc_kill(sender_id, SIG_ACK);
  335. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  336. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), LINE_SIZE * sizeof(int), node_id);
  337. if (error != RCCE_SUCCESS) {
  338. RCCE_error_string(error, error_str, &str_len);
  339. printf("I am %d and i got an error in get data in sig_INIT from %d with descr %s\n",node_id,sender_id,error_str);
  340. } else my_idag = data_array_local[0];
  341. my_stats.msg_count++; //gia to sig_ACK
  342. my_stats.distance += distance(node_id,my_idag);
  343. printf("I am node with id %d my idle agent is %d\n",node_id,my_idag);
  344. cur_time = time(NULL);
  345. cur_t = localtime(&cur_time);
  346. fprintf(log_file, "[%d:%d:%d]: my idle agent is %d and my pid is %d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,my_idag,getpid());
  347. fflush(log_file);
  348. signals_enable();
  349. }
  350. void sig_ACK_handler(int sender_id)
  351. {
  352. int clear=1, i, j, data_array_local[3 * LINE_SIZE], k;//, fd_r;//fd_r, ; clear is 1 if node is to be removed
  353. inter_list *tmp_inter_list;
  354. DDS_list *tmp_DDS;//, *tmp_inter_prev=NULL;
  355. offer_list *tmp_offer_list;
  356. int error, str_len;
  357. //RCCE_FLAG_STATUS receiver_status;
  358. char error_str[64];
  359. signals_disable();
  360. cur_time = time(NULL);
  361. cur_t = localtime(&cur_time);
  362. fprintf(log_file, "[%d:%d:%d]: I entered sig_ACK_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  363. fflush(log_file);
  364. if (core_inter_head[sender_id] == NULL){
  365. fprintf(log_file,"We were fucked in sig_ACK! sender_id = %d\n",sender_id);
  366. fflush(log_file);
  367. } else {
  368. //cur_time = time(NULL);
  369. //cur_t = localtime(&cur_time);
  370. fprintf(log_file, "Type=%d\n",core_inter_head[sender_id]->type);
  371. fflush(log_file);
  372. tmp_inter_list = core_inter_head[sender_id];
  373. if (tmp_inter_list->type == INIT_CORE){
  374. //write_res = write(fd_w, &node_id, sizeof(int));
  375. data_array_local[0] = node_id;
  376. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), LINE_SIZE * sizeof(int), sender_id);
  377. if (error != RCCE_SUCCESS) {
  378. RCCE_error_string(error, error_str, &str_len);
  379. printf("I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  380. }
  381. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  382. fprintf(log_file, "node_id=%d\n",node_id);
  383. fflush(log_file);
  384. my_stats.message_size += sizeof(int);
  385. nodes_initialised++;
  386. } else if (tmp_inter_list->type == INIT_APP){
  387. //write_res = write(fd_w, &tmp_inter_list->data.new_app, sizeof(app));
  388. //float tmpf;
  389. data_array_local[0] = tmp_inter_list->data.new_app.id;
  390. memcpy(&data_array_local[1],&tmp_inter_list->data.new_app.A,sizeof(int));
  391. memcpy(&data_array_local[2],&tmp_inter_list->data.new_app.var,sizeof(int));
  392. memcpy(&data_array_local[3],&tmp_inter_list->data.new_app.workld,sizeof(int));
  393. data_array_local[4] = tmp_inter_list->data.new_app.num_of_cores;
  394. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), LINE_SIZE * sizeof(int), sender_id);
  395. if (error != RCCE_SUCCESS) {
  396. RCCE_error_string(error, error_str, &str_len);
  397. printf("I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  398. }
  399. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  400. /*memcpy(&tmpf,&data_array_local[1],sizeof(int));
  401. fprintf(log_file, "A=%0.2f\n",tmpf);
  402. fflush(log_file);
  403. memcpy(&tmpf,&data_array_local[2],sizeof(int));
  404. fprintf(log_file, "var=%0.2f\n",tmpf);
  405. fflush(log_file);
  406. memcpy(&tmpf,&data_array_local[3],sizeof(int));
  407. fprintf(log_file, "workld=%0.2f\n",tmpf);
  408. fflush(log_file);*/
  409. my_stats.message_size += sizeof(app);
  410. } else if (tmp_inter_list->type == IDAG_FIND_IDAGS_PENDING || tmp_inter_list->type == SELFOPT_IDAG_FIND_IDAGS_PENDING) {//I am the requesting common node
  411. data_array_local[0] = tmp_inter_list->data.reg.C;
  412. data_array_local[1] = tmp_inter_list->data.reg.r;
  413. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), LINE_SIZE * sizeof(int), sender_id);
  414. if (error != RCCE_SUCCESS) {
  415. RCCE_error_string(error, error_str, &str_len);
  416. printf("I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  417. }
  418. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  419. //write_res = write(fd_w, &tmp_inter_list->data.reg, sizeof(region));
  420. fprintf(log_file, "C=%d r=%d\n",tmp_inter_list->data.reg.C,tmp_inter_list->data.reg.r);
  421. fflush(log_file);
  422. if (tmp_inter_list->type == IDAG_FIND_IDAGS_PENDING) tmp_inter_list->type = IDAG_FIND_IDAGS;
  423. else if (tmp_inter_list->type == SELFOPT_IDAG_FIND_IDAGS_PENDING) tmp_inter_list->type = SELFOPT_IDAG_FIND_IDAGS;
  424. my_stats.message_size += sizeof(region);
  425. clear = 0;
  426. } else if (core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING || core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS_PENDING
  427. || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING || core_inter_head[sender_id]->type == DEBUG_IDAG_REQ_DDS){
  428. data_array_local[0] = tmp_inter_list->data.reg.C;
  429. data_array_local[1] = tmp_inter_list->data.reg.r;
  430. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), LINE_SIZE * sizeof(int), sender_id);
  431. if (error != RCCE_SUCCESS) {
  432. RCCE_error_string(error, error_str, &str_len);
  433. printf("I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  434. }
  435. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  436. //write_res = write(fd_w, &tmp_inter_list->data.reg, sizeof(region));
  437. fprintf(log_file, "C=%d r=%d\n",tmp_inter_list->data.reg.C,tmp_inter_list->data.reg.r);
  438. fflush(log_file);
  439. my_stats.message_size += sizeof(region);
  440. if (core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING) core_inter_head[sender_id]->type = IDAG_REQ_DDS;
  441. else if (core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS_PENDING) core_inter_head[sender_id]->type = FAR_REQ_IDAG_REQ_DDS;
  442. else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING) core_inter_head[sender_id]->type = SELFOPT_IDAG_REQ_DDS;
  443. clear = 0;
  444. } else if (tmp_inter_list->type == REP_IDAG_FIND_IDAGS) {//I am the idag
  445. //write_res = write(fd_w, &tmp_inter_list->data.idags_in_reg[num_idags_x*num_idags_y], sizeof(int)); //number of idags in region
  446. data_array_local[0] = tmp_inter_list->data.idags_in_reg[num_idags_x*num_idags_y];
  447. fprintf(log_file, "num_of_idags=%d\n",tmp_inter_list->data.idags_in_reg[num_idags_x*num_idags_y]);
  448. fflush(log_file);
  449. my_stats.message_size += sizeof(int);
  450. j=1;
  451. for (i=0; i<num_idags_x*num_idags_y; i++)
  452. if (tmp_inter_list->data.idags_in_reg[i]) {
  453. //write_res = write(fd_w, &idag_id_arr[i], sizeof(int)); //matching of increasing number to node_id
  454. data_array_local[j] = idag_id_arr[i];
  455. j++;
  456. fprintf(log_file, "idag=%d\n",idag_id_arr[i]);
  457. fflush(log_file);
  458. my_stats.message_size += sizeof(int);
  459. }
  460. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), LINE_SIZE * sizeof(int), sender_id);
  461. if (error != RCCE_SUCCESS) {
  462. RCCE_error_string(error, error_str, &str_len);
  463. printf("I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  464. }
  465. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  466. } else if (tmp_inter_list->type == REP_IDAG_REQ_DDS) {//I am the idag
  467. if (tmp_inter_list->data.agents_in_reg == NULL){//debugging
  468. fprintf(log_file, "In null rep_idag_dds with sender %d and DDS_count %d\n",sender_id,DDS_count);
  469. fflush(log_file);
  470. //write_res = write(fd_w, &DDS_count, sizeof(int)); //number of idags in region
  471. data_array_local[0] = DDS_count;
  472. data_array_local[1] = 0;
  473. i=2;
  474. tmp_DDS = DDS;
  475. while(tmp_DDS != NULL){
  476. //write_res = write(fd_w, &tmp_DDS->agent_id, sizeof(int));
  477. //write_res = write(fd_w, &tmp_DDS->num_of_cores, sizeof(int));
  478. data_array_local[i++] = tmp_DDS->agent_id;
  479. data_array_local[i++] = tmp_DDS->num_of_cores;
  480. tmp_DDS = tmp_DDS->next;
  481. }
  482. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), 2 * LINE_SIZE * sizeof(int), sender_id);
  483. if (error != RCCE_SUCCESS) {
  484. RCCE_error_string(error, error_str, &str_len);
  485. fprintf(log_file,"I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  486. fflush(log_file);
  487. }
  488. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  489. } else {
  490. //write_res = write(fd_w, &tmp_inter_list->data.agents_in_reg[0], sizeof(int));
  491. data_array_local[0] = tmp_inter_list->data.agents_in_reg[0];
  492. data_array_local[1] = 0;
  493. fprintf(log_file, "num_of_agents=%d\n",tmp_inter_list->data.agents_in_reg[0]);
  494. fflush(log_file);
  495. my_stats.message_size += sizeof(int);
  496. j=2;
  497. for (i=1; i<=2*tmp_inter_list->data.agents_in_reg[0]; i+=2){
  498. /*write_res = write(fd_w, &tmp_inter_list->data.agents_in_reg[i], sizeof(int));
  499. fprintf(log_file, "agent=%d size=%d write_res=%d\n",tmp_inter_list->data.agents_in_reg[i],sizeof(int),write_res);
  500. fflush(log_file);
  501. write_res = write(fd_w, &tmp_inter_list->data.agents_in_reg[i+1], sizeof(int));//offset may be different!!!!!!
  502. fprintf(log_file, "cores=%d size=%d write_res=%d\n",tmp_inter_list->data.agents_in_reg[i+1],sizeof(int),write_res);
  503. fflush(log_file);*/
  504. fprintf(log_file, "agent=%d cores=%d\n",tmp_inter_list->data.agents_in_reg[i],tmp_inter_list->data.agents_in_reg[i+1]);
  505. fflush(log_file);
  506. data_array_local[j++] = tmp_inter_list->data.agents_in_reg[i];
  507. data_array_local[j++] = tmp_inter_list->data.agents_in_reg[i+1];
  508. my_stats.message_size += 2 * sizeof(int);
  509. }
  510. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), 2 * LINE_SIZE * sizeof(int), sender_id);
  511. if (error != RCCE_SUCCESS) {
  512. RCCE_error_string(error, error_str, &str_len);
  513. fprintf(log_file,"I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  514. fflush(log_file);
  515. }
  516. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  517. }
  518. } else if (tmp_inter_list->type == AGENT_REQ_CORES_PENDING){
  519. //write_res = write(fd_w, &init_app, sizeof(app));
  520. fprintf(log_file, "A=%0.2f\n",init_app.A);
  521. fflush(log_file);
  522. my_stats.message_size += sizeof(app);
  523. data_array_local[0] = init_app.id;
  524. memcpy(&data_array_local[1],&init_app.A,sizeof(int));
  525. memcpy(&data_array_local[2],&init_app.var,sizeof(int));
  526. memcpy(&data_array_local[3],&init_app.workld,sizeof(int));
  527. data_array_local[4] = init_app.num_of_cores;
  528. //write_res = write(fd_w, &tmp_inter_list->data.reg_arr.num_of_regions, sizeof(int));
  529. data_array_local[5] = tmp_inter_list->data.reg_arr.num_of_regions;
  530. fprintf(log_file, "num_of_regions=%d\n",tmp_inter_list->data.reg_arr.num_of_regions);
  531. fflush(log_file);
  532. for (i=0; i<tmp_inter_list->data.reg_arr.num_of_regions; i++) {
  533. //write_res = write(fd_w, &tmp_inter_list->data.reg_arr.region_arr[i], sizeof(region));
  534. fprintf(log_file, "C=%d r=%d\n",tmp_inter_list->data.reg_arr.region_arr[i].C,tmp_inter_list->data.reg_arr.region_arr[i].r);
  535. fflush(log_file);
  536. data_array_local[2*i+LINE_SIZE] = tmp_inter_list->data.reg_arr.region_arr[i].C;
  537. data_array_local[2*i+LINE_SIZE+1] = tmp_inter_list->data.reg_arr.region_arr[i].r;
  538. my_stats.message_size += sizeof(region);
  539. }
  540. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]),3 * LINE_SIZE * sizeof(int), sender_id);
  541. if (error != RCCE_SUCCESS) {
  542. RCCE_error_string(error, error_str, &str_len);
  543. fprintf(log_file,"I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  544. fflush(log_file);
  545. }
  546. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  547. tmp_inter_list->type = AGENT_REQ_CORES;
  548. free(tmp_inter_list->data.reg_arr.region_arr);
  549. clear = 0;
  550. } else if (tmp_inter_list->type == SELFOPT_REQ_CORES_PENDING) {
  551. //write_res = write(fd_w, &my_app, sizeof(app));
  552. fprintf(log_file, "A=%0.2f num_of_cores = %d\n",my_app.A,my_app.num_of_cores);
  553. fflush(log_file);
  554. my_stats.message_size += sizeof(app);
  555. data_array_local[0] = my_app.id;
  556. memcpy(&data_array_local[1],&my_app.A,sizeof(int));
  557. memcpy(&data_array_local[2],&my_app.var,sizeof(int));
  558. memcpy(&data_array_local[3],&my_app.workld,sizeof(int));
  559. data_array_local[4] = my_app.num_of_cores;
  560. //write_res = write(fd_w, &tmp_inter_list->data.reg_arr.num_of_regions, sizeof(int));
  561. data_array_local[5] = tmp_inter_list->data.reg_arr.num_of_regions;
  562. fprintf(log_file, "num_of_regions=%d\n",tmp_inter_list->data.reg_arr.num_of_regions);
  563. fflush(log_file);
  564. for (i=0; i<tmp_inter_list->data.reg_arr.num_of_regions; i++) {
  565. //write_res = write(fd_w, &tmp_inter_list->data.reg_arr.region_arr[i], sizeof(region));
  566. data_array_local[2*i+LINE_SIZE] = tmp_inter_list->data.reg_arr.region_arr[i].C;
  567. data_array_local[2*i+LINE_SIZE+1] = tmp_inter_list->data.reg_arr.region_arr[i].r;
  568. fprintf(log_file, "C=%d r=%d\n",tmp_inter_list->data.reg_arr.region_arr[i].C,tmp_inter_list->data.reg_arr.region_arr[i].r);
  569. fflush(log_file);
  570. my_stats.message_size += sizeof(region);
  571. }
  572. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]),3 * LINE_SIZE * sizeof(int), sender_id);
  573. if (error != RCCE_SUCCESS) {
  574. RCCE_error_string(error, error_str, &str_len);
  575. fprintf(log_file,"I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  576. fflush(log_file);
  577. }
  578. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  579. tmp_inter_list->type = SELFOPT_REQ_CORES;
  580. free(tmp_inter_list->data.reg_arr.region_arr);
  581. clear = 0;
  582. /*} else if (tmp_inter_list->type == FAR_REQ_CORES_PENDING) {//I am the requesting common node
  583. write_res = write(fd_w, &far_req_app, sizeof(app));
  584. fprintf(log_file, "A=%0.2f size=%d write_res=%d\n",far_req_app.A,sizeof(app),write_res);
  585. fflush(log_file);
  586. my_stats.message_size += sizeof(app);
  587. i = 1;
  588. write_res = write(fd_w, &i, sizeof(int));
  589. fprintf(log_file, "num_of_regions=%d size=%d write_res=%d\n",i,sizeof(int),write_res);
  590. fflush(log_file);
  591. my_stats.message_size += sizeof(int);
  592. write_res = write(fd_w, &tmp_inter_list->data.reg, sizeof(region));
  593. fprintf(log_file, "C=%d r=%d size=%d write_res=%d\n",tmp_inter_list->data.reg.C,tmp_inter_list->data.reg.r,sizeof(region),write_res);
  594. fflush(log_file);
  595. my_stats.message_size += sizeof(region);
  596. tmp_inter_list->type = FAR_REQ_CORES;
  597. clear = 0;*/
  598. } else if (tmp_inter_list->type == REP_AGENT_REQ_CORES) {//I am the agent
  599. //write_res = write(fd_w, &tmp_inter_list->data.off_arr.num_of_offers, sizeof(int));
  600. data_array_local[0] = tmp_inter_list->data.off_arr.num_of_offers;
  601. fprintf(log_file, "num_of_offers=%d\n",tmp_inter_list->data.off_arr.num_of_offers);
  602. fflush(log_file);
  603. my_stats.message_size += sizeof(int);
  604. k=1;
  605. if (tmp_inter_list->data.off_arr.num_of_offers > 0) {
  606. for (j=0; j<tmp_inter_list->data.off_arr.num_of_offers; j++) {
  607. //write_res = write(fd_w, &tmp_inter_list->data.off_arr.offer_arr[j].num_of_cores, sizeof(int));
  608. data_array_local[k++] = tmp_inter_list->data.off_arr.offer_arr[j].num_of_cores;
  609. fprintf(log_file, "num_of_cores=%d\n",tmp_inter_list->data.off_arr.offer_arr[j].num_of_cores);
  610. fflush(log_file);
  611. my_stats.message_size += sizeof(int);
  612. //write_res = write(fd_w, &tmp_inter_list->data.off_arr.offer_arr[j].spd_loss, sizeof(float));
  613. memcpy(&data_array_local[k++],&tmp_inter_list->data.off_arr.offer_arr[j].spd_loss,sizeof(int));
  614. fprintf(log_file, "spd_loss=%0.2f\n",tmp_inter_list->data.off_arr.offer_arr[j].spd_loss);
  615. fflush(log_file);
  616. my_stats.message_size += sizeof(float);
  617. for (i=0; i<tmp_inter_list->data.off_arr.offer_arr[j].num_of_cores; i++) {
  618. data_array_local[k++] = tmp_inter_list->data.off_arr.offer_arr[j].offered_cores[i];
  619. //write_res = write(fd_w, &tmp_inter_list->data.off_arr.offer_arr[j].offered_cores[i], sizeof(int));
  620. fprintf(log_file, "core=%d\n",tmp_inter_list->data.off_arr.offer_arr[j].offered_cores[i]);
  621. fflush(log_file);
  622. my_stats.message_size += sizeof(int);
  623. }
  624. }
  625. fprintf(log_file, "k=%d\n",k);
  626. fflush(log_file);
  627. core_inter_head[sender_id]->type = AGENT_OFFER_SENT;
  628. clear = 0;
  629. }
  630. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), 3 * LINE_SIZE * sizeof(int), sender_id);
  631. if (error != RCCE_SUCCESS) {
  632. RCCE_error_string(error, error_str, &str_len);
  633. fprintf(log_file,"I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  634. fflush(log_file);
  635. }
  636. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  637. } else if (tmp_inter_list->type == REP_AGENT_OFFER_SENT) {
  638. fprintf(log_file, "I have to reply %d for %d offers\n",sender_id,tmp_inter_list->data.offer_acc_array[0]);
  639. fflush(log_file);
  640. my_stats.message_size += sizeof(int);
  641. for (i=1; i<=tmp_inter_list->data.offer_acc_array[0]; i++){
  642. //write_res = write(fd_w, &tmp_inter_list->data.offer_acc_array[i], sizeof(int));
  643. data_array_local[i-1] = tmp_inter_list->data.offer_acc_array[i];
  644. fprintf(log_file, "offer_ans=%d\n",tmp_inter_list->data.offer_acc_array[i]);
  645. fflush(log_file);
  646. my_stats.message_size += sizeof(int);
  647. }
  648. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), LINE_SIZE * sizeof(int), sender_id);
  649. if (error != RCCE_SUCCESS) {
  650. RCCE_error_string(error, error_str, &str_len);
  651. fprintf(log_file,"I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  652. fflush(log_file);
  653. }
  654. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  655. fflush(log_file);
  656. free(tmp_inter_list->data.offer_acc_array);
  657. } else if (tmp_inter_list->type == INIT_AGENT) {
  658. //write_res = write(fd_w, &init_app, sizeof(app));
  659. data_array_local[0] = init_app.id;
  660. memcpy(&data_array_local[1],&init_app.A,sizeof(int));
  661. memcpy(&data_array_local[2],&init_app.var,sizeof(int));
  662. memcpy(&data_array_local[3],&init_app.workld,sizeof(int));
  663. data_array_local[4] = init_app.num_of_cores;
  664. fprintf(log_file, "A=%0.2f\n",init_app.A);
  665. fflush(log_file);
  666. my_stats.message_size += sizeof(app);
  667. for (i=1; i<=init_app.num_of_cores; i++){
  668. //write_res = write(fd_w, &tmp_inter_list->data.app_cores[i], sizeof(int));
  669. data_array_local[LINE_SIZE+i-1] = tmp_inter_list->data.app_cores[i];
  670. fprintf(log_file, "core=%d\n",tmp_inter_list->data.app_cores[i]);
  671. fflush(log_file);
  672. my_stats.message_size += sizeof(int);
  673. }
  674. data_array_local[2*LINE_SIZE] = init_app_times[0].tm_sec;
  675. data_array_local[2*LINE_SIZE+1] = init_app_times[0].tm_min;
  676. data_array_local[2*LINE_SIZE+2] = init_app_times[0].tm_hour;
  677. data_array_local[2*LINE_SIZE+3] = init_app_times[1].tm_sec;
  678. data_array_local[2*LINE_SIZE+4] = init_app_times[1].tm_min;
  679. data_array_local[2*LINE_SIZE+5] = init_app_times[1].tm_hour;
  680. //write_res = write(fd_w, &init_app_times[0], sizeof(my_time_stamp));
  681. fprintf(log_file, "sec=%d min=%d hours=%d\n",init_app_times[0].tm_sec,init_app_times[0].tm_min,init_app_times[0].tm_hour);
  682. fflush(log_file);
  683. //write_res = write(fd_w, &init_app_times[1], sizeof(my_time_stamp));
  684. fprintf(log_file, "sec=%d min=%d hours=%d\n",init_app_times[1].tm_sec,init_app_times[1].tm_min,init_app_times[1].tm_hour);
  685. fflush(log_file);
  686. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]),3 * LINE_SIZE * sizeof(int), sender_id);
  687. if (error != RCCE_SUCCESS) {
  688. RCCE_error_string(error, error_str, &str_len);
  689. fprintf(log_file,"I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  690. fflush(log_file);
  691. }
  692. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  693. init_app.A=-1.0;
  694. init_app.var=-1.0;
  695. init_app.num_of_cores=-1;
  696. init_app_times[0].tm_sec = 0;
  697. init_app_times[0].tm_min = 0;
  698. init_app_times[0].tm_hour = 0;
  699. init_app_times[1].tm_sec = 0;
  700. init_app_times[1].tm_min = 0;
  701. init_app_times[1].tm_hour = 0;
  702. //kill(pid_num[0], SIG_INIT_APP);
  703. scc_kill(0, SIG_INIT_APP);
  704. my_stats.msg_count++;
  705. my_stats.distance += distance(node_id,0);
  706. free(tmp_inter_list->data.app_cores);
  707. } else if (tmp_inter_list->type == IDAG_ADD_CORES_DDS || tmp_inter_list->type == IDAG_REM_CORES_DDS) {// || tmp_inter_list->type == REMOVE_APP
  708. //fprintf(log_file, "I am in add/remove/remove_app to %d with %d cores\n",sender_id,tmp_inter_list->data.app_cores[0]);
  709. //fflush(log_file);
  710. //write_res = write(fd_w, &tmp_inter_list->data.app_cores[0], sizeof(int));
  711. data_array_local[0] = tmp_inter_list->data.app_cores[0];
  712. fprintf(log_file, "app_cores=%d\n",tmp_inter_list->data.app_cores[0]);
  713. fflush(log_file);
  714. my_stats.message_size += sizeof(int);
  715. for (i=1; i<=tmp_inter_list->data.app_cores[0]; i++){
  716. //write_res = write(fd_w, &tmp_inter_list->data.app_cores[i], sizeof(int));
  717. data_array_local[LINE_SIZE+i-1] = tmp_inter_list->data.app_cores[i];
  718. fprintf(log_file, "core=%d\n",tmp_inter_list->data.app_cores[i]);
  719. fflush(log_file);
  720. my_stats.message_size += sizeof(int);
  721. }
  722. //I am an idag and i have to send to other idags my original sender
  723. if (my_idag == -1) {//idag_id != -1
  724. //write_res = write(fd_w, &tmp_inter_list->data.app_cores[i], sizeof(int));
  725. data_array_local[1] = tmp_inter_list->data.app_cores[i];
  726. fprintf(log_file, "orig_sender=%d\n",tmp_inter_list->data.app_cores[i]);
  727. fflush(log_file);
  728. my_stats.message_size += sizeof(int);
  729. }
  730. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]),2 * LINE_SIZE * sizeof(int), sender_id);
  731. if (error != RCCE_SUCCESS) {
  732. RCCE_error_string(error, error_str, &str_len);
  733. fprintf(log_file,"I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  734. fflush(log_file);
  735. }
  736. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  737. } else if (tmp_inter_list->type == REMOVE_APP) {
  738. //fprintf(log_file, "I am in add/remove/remove_app to %d with %d cores\n",sender_id,tmp_inter_list->data.app_cores[0]);
  739. //fflush(log_file);
  740. if (my_idag != -1) {
  741. //write_res = write(fd_w, &tmp_inter_list->data.app_cores[0], sizeof(int));
  742. data_array_local[0] = tmp_inter_list->data.app_cores[0];
  743. fprintf(log_file, "app_cores=%d\n",tmp_inter_list->data.app_cores[0]);
  744. fflush(log_file);
  745. my_stats.message_size += sizeof(int);
  746. for (i=1; i<=tmp_inter_list->data.app_cores[0]; i++){
  747. //write_res = write(fd_w, &tmp_inter_list->data.app_cores[i], sizeof(int));
  748. data_array_local[i] = tmp_inter_list->data.app_cores[i];//LINE_SIZE+i-1
  749. fprintf(log_file, "core=%d\n",tmp_inter_list->data.app_cores[i]);
  750. fflush(log_file);
  751. my_stats.message_size += sizeof(int);
  752. }
  753. } else {
  754. //I am an idag and i have to send to other idags my original sender
  755. //write_res = write(fd_w, &tmp_inter_list->data.agent_ended, sizeof(int));
  756. data_array_local[0] = tmp_inter_list->data.agent_ended;
  757. fprintf(log_file, "orig_sender=%d\n",tmp_inter_list->data.agent_ended);
  758. fflush(log_file);
  759. my_stats.message_size += sizeof(int);
  760. }
  761. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), 2 * LINE_SIZE * sizeof(int), sender_id);
  762. if (error != RCCE_SUCCESS) {
  763. RCCE_error_string(error, error_str, &str_len);
  764. fprintf(log_file,"I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  765. fflush(log_file);
  766. }
  767. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  768. /*} else if (tmp_inter_list->type == FAR_INIT_REQ) {//I am the requesting common node
  769. write_res = write(fd_w, &init_app, sizeof(app));
  770. fprintf(log_file, "A=%0.2f size=%d write_res=%d\n",init_app.A,sizeof(app),write_res);
  771. fflush(log_file);
  772. my_stats.message_size += sizeof(app);
  773. write_res = write(fd_w, &tmp_inter_list->data.reg, sizeof(region));
  774. fprintf(log_file, "C=%d r=%d size=%d write_res=%d\n",tmp_inter_list->data.reg.C,tmp_inter_list->data.reg.r,sizeof(region),write_res);
  775. fflush(log_file);
  776. my_stats.message_size += sizeof(region);
  777. clear = 0;
  778. } else if (tmp_inter_list->type == FAR_REQ_MAN_APPOINT) {//I am the requesting common node
  779. //fprintf(log_file, "I am appointing %d for far manager of %d with region (%d,%d)\n",
  780. // sender_id,tmp_inter_list->data.far_req.orig_sender,tmp_inter_list->data.far_req.reg.C,tmp_inter_list->data.far_req.reg.r);
  781. //fflush(log_file);
  782. write_res = write(fd_w, &tmp_inter_list->data.far_req.orig_sender, sizeof(int));
  783. fprintf(log_file, "orig_sender=%d size=%d write_res=%d\n",tmp_inter_list->data.far_req.orig_sender,sizeof(int),write_res);
  784. fflush(log_file);
  785. my_stats.message_size += sizeof(int);
  786. write_res = write(fd_w, &tmp_inter_list->data.far_req.far_app, sizeof(app));
  787. fprintf(log_file, "A=%0.2f size=%d write_res=%d\n",tmp_inter_list->data.far_req.far_app.A,sizeof(app),write_res);
  788. fflush(log_file);
  789. my_stats.message_size += sizeof(app);
  790. write_res = write(fd_w, &tmp_inter_list->data.far_req.reg, sizeof(region));
  791. fprintf(log_file, "C=%d r=%d size=%d write_res=%d\n",tmp_inter_list->data.far_req.reg.C,tmp_inter_list->data.far_req.reg.r,sizeof(region),write_res);
  792. fflush(log_file);
  793. my_stats.message_size += sizeof(region);
  794. if (tmp_inter_list->next != NULL && tmp_inter_list->next->type == FAR_REQ_MAN_APPOINT_PENDING) { //gia na thrhthei h seira k na mhn skaei to apo katw
  795. fprintf(log_file, "I enter this case\n");
  796. fflush(log_file);
  797. core_inter_head[sender_id] = tmp_inter_list->next;
  798. free(tmp_inter_list);
  799. for (tmp_inter_list = core_inter_head[sender_id]; tmp_inter_list != NULL; tmp_inter_list = tmp_inter_list->next)
  800. fprintf (log_file, "Interaction is %d\n",tmp_inter_list->type);
  801. fflush(log_file);
  802. clear = 0;
  803. }
  804. } else if (tmp_inter_list->type == REP_FAR_INIT_REQ){
  805. write_res = write(fd_w, &tmp_inter_list->data.far_req_man, sizeof(int));
  806. fprintf(log_file, "far manager=%d size=%d write_res=%d\n",tmp_inter_list->data.far_req_man,sizeof(int),write_res);
  807. fflush(log_file);
  808. my_stats.message_size += sizeof(int);
  809. //printf("i am %d in asdfwe and far_req_man is %d\n",node_id,tmp_inter_list->data.far_req_man);
  810. //if (core_inter_head[tmp_inter_list->data.far_req_man] == NULL) printf("kariolares\n");
  811. if (tmp_inter_list->data.far_req_man != node_id && core_inter_head[tmp_inter_list->data.far_req_man] != NULL &&
  812. core_inter_head[tmp_inter_list->data.far_req_man]->type == FAR_REQ_MAN_APPOINT_PENDING) {//next == NULL) {
  813. kill(pid_num[tmp_inter_list->data.far_req_man], SIG_FAR_REQ);
  814. core_inter_head[tmp_inter_list->data.far_req_man]->type = FAR_REQ_MAN_APPOINT;
  815. fprintf(log_file, "I sent it\n");
  816. fflush(log_file);
  817. my_stats.msg_count++;
  818. my_stats.distance += distance(node_id,tmp_inter_list->data.far_req_man);
  819. } else {
  820. fprintf(log_file, "far_man=%d\n",tmp_inter_list->data.far_req_man);
  821. if (core_inter_head[tmp_inter_list->data.far_req_man] != NULL) fprintf(log_file, "interaction is =%d\n",core_inter_head[tmp_inter_list->data.far_req_man]->type);
  822. else fprintf(log_file, "no interaction\n");
  823. fflush(log_file);
  824. }
  825. } else if (tmp_inter_list->type == ABORT_FAR_MAN) {
  826. write_res = write(fd_w, &tmp_inter_list->data.far_req_man, sizeof(int));
  827. fprintf(log_file, "far_req_man=%d size=%d write_res=%d\n",tmp_inter_list->data.far_req_man,sizeof(int),write_res);
  828. fflush(log_file);
  829. my_stats.message_size += sizeof(int);
  830. //printf("i am %d in asdfwe and far_req_man is %d\n",node_id,tmp_inter_list->data.far_req_man);
  831. //if (core_inter_head[tmp_inter_list->data.far_req_man] == NULL) printf("kariolares\n");
  832. } else if (tmp_inter_list->type == FAR_REQ_OFFER) {//I am the agent
  833. //printf("far_list_count = %d\n",far_list_count);
  834. write_res = write(fd_w, &far_list_count, sizeof(int));
  835. fprintf(log_file, "far_list_count=%d size=%d write_res=%d\n",far_list_count,sizeof(int),write_res);
  836. fflush(log_file);
  837. my_stats.message_size += sizeof(int);
  838. if (far_list_count > 0) {
  839. tmp_offer_list = far_man_offers;
  840. while (tmp_offer_list != NULL){
  841. write_res = write(fd_w, &tmp_offer_list->off.num_of_cores, sizeof(int));
  842. fprintf(log_file, "num_of_cores=%d size=%d write_res=%d\n",tmp_offer_list->off.num_of_cores,sizeof(int),write_res);
  843. fflush(log_file);
  844. my_stats.message_size += sizeof(int);
  845. write_res = write(fd_w, &tmp_offer_list->off.spd_loss, sizeof(float));
  846. fprintf(log_file, "spd_loss=%0.2f size=%d write_res=%d\n",tmp_offer_list->off.spd_loss,sizeof(float),write_res);
  847. fflush(log_file);
  848. my_stats.message_size += sizeof(float);
  849. for (i=0; i<tmp_offer_list->off.num_of_cores; i++) {
  850. //printf ("Offering core = %d\n",tmp_inter_list->data.my_offer.offered_cores[i]);
  851. write_res = write(fd_w, &tmp_offer_list->off.offered_cores[i], sizeof(int));
  852. fprintf(log_file, "core=%d size=%d write_res=%d\n",tmp_offer_list->off.offered_cores[i],sizeof(int),write_res);
  853. fflush(log_file);
  854. my_stats.message_size += sizeof(int);
  855. }
  856. tmp_offer_list = tmp_offer_list->next;
  857. }
  858. core_inter_head[sender_id]->type = FAR_REQ_OFFER_SENT;
  859. clear = 0;
  860. } else {
  861. far_req_app.A=-1.0;
  862. far_req_app.var=-1.0;
  863. far_req_app.num_of_cores=-1;
  864. far_req_or_sender = -1;
  865. far_reg.C = -1;
  866. far_reg.r = -1;
  867. }
  868. } else if (tmp_inter_list->type == REP_FAR_REQ_OFFER_SENT) {
  869. fprintf(log_file, "num_of_offers=%d\n",tmp_inter_list->data.offer_acc_array[0]);
  870. fflush(log_file);
  871. for (i=1; i<=tmp_inter_list->data.offer_acc_array[0]; i++) {
  872. write_res = write(fd_w, &tmp_inter_list->data.offer_acc_array[i], sizeof(int));
  873. fprintf(log_file, "offer_ans=%d size=%d write_res=%d\n",tmp_inter_list->data.offer_acc_array[i],sizeof(int),write_res);
  874. fflush(log_file);
  875. my_stats.message_size += sizeof(int);
  876. }*/
  877. } else if (tmp_inter_list->type == INIT_WORK_NODE) {
  878. if (tmp_inter_list->data.work_time != -1) {
  879. //i=1;
  880. //write_res = write(fd_w, &i, sizeof(int));
  881. data_array_local[0] = 1;
  882. fprintf(log_file, "i=%d\n",data_array_local[0]);
  883. fflush(log_file);
  884. //write_res = write(fd_w, &node_id, sizeof(int));
  885. data_array_local[1] = node_id;
  886. fprintf(log_file, "node_id=%d\n",node_id);
  887. fflush(log_file);
  888. //write_res = write(fd_w, &tmp_inter_list->data.work_time, sizeof(int));
  889. data_array_local[2] = tmp_inter_list->data.work_time;
  890. fprintf(log_file, "work_time=%d\n",tmp_inter_list->data.work_time);
  891. fflush(log_file);
  892. my_stats.message_size += 3 * sizeof(int);
  893. } else {
  894. //i=0;
  895. //write_res = write(fd_w, &i, sizeof(int));
  896. data_array_local[0] = 0;
  897. fprintf(log_file, "i=%d\n",data_array_local[0]);
  898. fflush(log_file);
  899. my_stats.message_size += sizeof(int);
  900. }
  901. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]),LINE_SIZE * sizeof(int), sender_id);
  902. if (error != RCCE_SUCCESS) {
  903. RCCE_error_string(error, error_str, &str_len);
  904. fprintf(log_file,"I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  905. fflush(log_file);
  906. }
  907. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  908. } else if (tmp_inter_list->type == APPOINT_WORK_NODE) {
  909. if (tmp_inter_list->data.work_time != -1) {
  910. /*i=1;
  911. write_res = write(fd_w, &i, sizeof(int));
  912. fprintf(log_file, "i=%d size=%d write_res=%d\n",i,sizeof(int),write_res);
  913. fflush(log_file);
  914. write_res = write(fd_w, &tmp_inter_list->data.work_time, sizeof(int));
  915. fprintf(log_file, "work_time=%d size=%d write_res=%d\n",tmp_inter_list->data.work_time,sizeof(int),write_res);
  916. fflush(log_file);*/
  917. data_array_local[0] = 1;
  918. fprintf(log_file, "i=%d\n",data_array_local[0]);
  919. fflush(log_file);
  920. //write_res = write(fd_w, &node_id, sizeof(int));
  921. //data_array_local[1] = node_id;
  922. //fprintf(log_file, "node_id=%d\n",node_id);
  923. data_array_local[2] = tmp_inter_list->data.work_time;
  924. fprintf(log_file, "work_time=%d\n",tmp_inter_list->data.work_time);
  925. fflush(log_file);
  926. my_stats.message_size += 2 * sizeof(int);
  927. } else {
  928. /*i=0;
  929. write_res = write(fd_w, &i, sizeof(int));
  930. fprintf(log_file, "i=%d size=%d write_res=%d\n",i,sizeof(int),write_res);
  931. fflush(log_file);
  932. my_stats.message_size += sizeof(int);*/
  933. data_array_local[0] = 0;
  934. fprintf(log_file, "i=%d\n",data_array_local[0]);
  935. fflush(log_file);
  936. my_stats.message_size += sizeof(int);
  937. }
  938. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]),LINE_SIZE * sizeof(int), sender_id);
  939. if (error != RCCE_SUCCESS) {
  940. RCCE_error_string(error, error_str, &str_len);
  941. fprintf(log_file,"I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  942. fflush(log_file);
  943. }
  944. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  945. } else if (tmp_inter_list->type == REP_CHK_REM_TIME) {
  946. //write_res = write(fd_w, &tmp_inter_list->data.work_time, sizeof(int));
  947. data_array_local[0] = tmp_inter_list->data.work_time;
  948. fprintf(log_file, "work_time=%d\n",tmp_inter_list->data.work_time);
  949. fflush(log_file);
  950. my_stats.message_size += sizeof(int);
  951. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]),LINE_SIZE * sizeof(int), sender_id);
  952. if (error != RCCE_SUCCESS) {
  953. RCCE_error_string(error, error_str, &str_len);
  954. fprintf(log_file,"I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  955. fflush(log_file);
  956. }
  957. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  958. } else if (tmp_inter_list->type == REP_STATISTICS) {
  959. //write_res = write(fd_w, &tmp_inter_list->data.stats, sizeof(metrics));
  960. data_array_local[0] = tmp_inter_list->data.stats.msg_count;
  961. data_array_local[1] = tmp_inter_list->data.stats.message_size;
  962. data_array_local[2] = tmp_inter_list->data.stats.distance;
  963. data_array_local[3] = tmp_inter_list->data.stats.app_turnaround;
  964. data_array_local[4] = tmp_inter_list->data.stats.comp_effort;
  965. data_array_local[5] = tmp_inter_list->data.stats.cores_utilized;
  966. data_array_local[6] = tmp_inter_list->data.stats.times_accessed;
  967. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), LINE_SIZE * sizeof(int), sender_id);
  968. if (error != RCCE_SUCCESS) {
  969. RCCE_error_string(error, error_str, &str_len);
  970. printf("I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  971. }
  972. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  973. state = TERMINATED;
  974. } else printf("We were fucked inside ACK! node_id = %d sender_id = %d\n",node_id,sender_id);
  975. if (clear){
  976. core_inter_head[sender_id] = tmp_inter_list->next;
  977. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  978. else send_next_signal(core_inter_head[sender_id],sender_id);
  979. free(tmp_inter_list);
  980. }
  981. }
  982. cur_time = time(NULL);
  983. cur_t = localtime(&cur_time);
  984. fprintf(log_file, "[%d:%d:%d]: I ended sig_ACK_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  985. fflush(log_file);
  986. signals_enable();
  987. }
  988. void sig_IDAG_FIND_IDAGS_handler(int sender_id)
  989. {
  990. int num_of_idags, i, one_idag, error, str_len;//, idags_read=0;
  991. region cur_reg;
  992. inter_list *tmp_inter_list;
  993. int data_array_local[LINE_SIZE];
  994. //RCCE_FLAG_STATUS receiver_status;
  995. char error_str[64];
  996. signals_disable();
  997. cur_time = time(NULL);
  998. cur_t = localtime(&cur_time);
  999. fprintf(log_file, "[%d:%d:%d]: I entered sig_IDAG_FIND_IDAGS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  1000. fflush(log_file);
  1001. if (core_inter_head[sender_id] == NULL || core_inter_head[sender_id]->type == REP_IDAG_FIND_IDAGS){ //I am the idag
  1002. //kill(info->si_pid, SIG_ACK);
  1003. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  1004. scc_kill(sender_id, SIG_ACK);
  1005. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  1006. my_stats.msg_count++;
  1007. my_stats.distance += distance(node_id,sender_id);
  1008. //read(fd_r, &cur_reg, sizeof(region));
  1009. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), LINE_SIZE * sizeof(int), node_id);
  1010. if (error != RCCE_SUCCESS) {
  1011. RCCE_error_string(error, error_str, &str_len);
  1012. printf("I am %d and i got an error in get data in sig_IDAG_FIND_IDAGS_handler from %d with descr %s\n",node_id,sender_id,error_str);
  1013. }
  1014. cur_reg.C = data_array_local[0];
  1015. cur_reg.r = data_array_local[1];
  1016. if (core_inter_head[sender_id] == NULL){
  1017. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  1018. core_inter_tail[sender_id] = core_inter_head[sender_id];
  1019. } else {
  1020. core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
  1021. core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
  1022. }
  1023. core_inter_tail[sender_id]->type = REP_IDAG_FIND_IDAGS;
  1024. core_inter_tail[sender_id]->data.idags_in_reg = (int *) malloc((num_idags_x*num_idags_y+1)*sizeof(int));
  1025. core_inter_tail[sender_id]->next = NULL;
  1026. get_reg_idags(cur_reg, core_inter_tail[sender_id]->data.idags_in_reg);
  1027. if (core_inter_head[sender_id]->next == NULL) {
  1028. //kill(info->si_pid, SIG_IDAG_FIND_IDAGS);
  1029. scc_kill(sender_id, SIG_IDAG_FIND_IDAGS);
  1030. my_stats.msg_count++;
  1031. my_stats.distance += distance(node_id,sender_id);
  1032. }
  1033. } else if (core_inter_head[sender_id]->type == IDAG_FIND_IDAGS && state != IDLE_INIT_MAN && state != WORKING_NODE_IDLE_INIT) {
  1034. //IDLE_INIT_MAN_SELFOPT_PENDING && state != IDLE_INIT_MAN_WORK_PENDING) {
  1035. printf("I am %d and i think i cought a stray init idag_find_idags reply from %d. My current state is %d\n",node_id,sender_id,state);
  1036. fprintf(log_file,"I am %d and i think i cought a stray init idag_find_idags reply from %d. My current state is %d\n",node_id,sender_id,state);
  1037. fflush(log_file);
  1038. //kill(info->si_pid, SIG_REJECT);
  1039. scc_kill(sender_id, SIG_REJECT);
  1040. my_stats.msg_count++;
  1041. my_stats.distance += distance(node_id,sender_id);
  1042. tmp_inter_list = core_inter_head[sender_id];
  1043. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1044. free(tmp_inter_list);
  1045. if(core_inter_head[sender_id] == NULL) {
  1046. if (core_inter_tail[sender_id] == NULL){
  1047. printf("Malaka gamietai to core_inter_tail\n");
  1048. core_inter_tail[sender_id] = NULL;
  1049. }
  1050. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1051. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_FIND_IDAGS && state != IDLE_AGENT_WAITING_OFF) {
  1052. printf("I am %d and i think i cought a stray selfopt idag_find_idags reply from %d. My current state is %d\n",node_id,sender_id,state);
  1053. fprintf(log_file,"I am %d and i think i cought a stray selfopt idag_find_idags reply from %d. My current state is %d\n",node_id,sender_id,state);
  1054. fflush(log_file);
  1055. //kill(info->si_pid, SIG_REJECT);
  1056. scc_kill(sender_id, SIG_REJECT);
  1057. my_stats.msg_count++;
  1058. my_stats.distance += distance(node_id,sender_id);
  1059. tmp_inter_list = core_inter_head[sender_id];
  1060. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1061. free(tmp_inter_list);
  1062. if(core_inter_head[sender_id] == NULL) {
  1063. if (core_inter_tail[sender_id] == NULL){
  1064. printf("Malaka gamietai to core_inter_tail\n");
  1065. core_inter_tail[sender_id] = NULL;
  1066. }
  1067. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1068. } else if (core_inter_head[sender_id]->type == IDAG_FIND_IDAGS || core_inter_head[sender_id]->type == SELFOPT_IDAG_FIND_IDAGS) { //I am the requesting common node
  1069. //kill(info->si_pid, SIG_ACK);
  1070. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  1071. scc_kill(sender_id, SIG_ACK);
  1072. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  1073. my_stats.msg_count++;
  1074. my_stats.distance += distance(node_id,sender_id);
  1075. //read(fd_r, &num_of_idags, sizeof(int));
  1076. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), LINE_SIZE * sizeof(int), node_id);
  1077. if (error != RCCE_SUCCESS) {
  1078. RCCE_error_string(error, error_str, &str_len);
  1079. printf("I am %d and i got an error in get data in sig_IDAG_FIND_IDAGS_handler from %d with descr %s\n",node_id,sender_id,error_str);
  1080. }
  1081. num_of_idags = data_array_local[0];
  1082. fprintf(log_file,"Number of agents in region %d\n",num_of_idags);
  1083. fflush(log_file);
  1084. //if (core_inter_head[sender_id]->type == FAR_INIT_IDAG_FIND_IDAGS) far_req_max_man_count = num_of_idags;
  1085. if (core_inter_head[sender_id]->type == IDAG_FIND_IDAGS) {
  1086. init_idags_areas_replies++;
  1087. init_DDS_idags += num_of_idags;
  1088. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_FIND_IDAGS) selfopt_DDS_idags += num_of_idags;
  1089. for (i=0; i<num_of_idags; i++){ //max 4 idags
  1090. //read(fd_r, &one_idag, sizeof(int));
  1091. one_idag = data_array_local[i+1];
  1092. fprintf(log_file,"In the region I have idag with id %d\n",one_idag);
  1093. fflush(log_file);
  1094. if (core_inter_head[one_idag] == NULL){
  1095. core_inter_head[one_idag] = (inter_list *) malloc(sizeof(inter_list));
  1096. core_inter_tail[one_idag] = core_inter_head[one_idag];
  1097. } else {
  1098. for (tmp_inter_list = core_inter_head[one_idag]; tmp_inter_list != NULL; tmp_inter_list = tmp_inter_list->next)
  1099. if (tmp_inter_list->type == FAR_REQ_MAN) break;
  1100. if (tmp_inter_list != NULL) {
  1101. fprintf(log_file,"I dismissed node %d in rep_idag_find_idags\n",one_idag);
  1102. fflush(log_file);
  1103. init_DDS_idags--;
  1104. continue;
  1105. }
  1106. core_inter_tail[one_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  1107. core_inter_tail[one_idag] = core_inter_tail[one_idag]->next;
  1108. }
  1109. if (core_inter_head[sender_id]->type == IDAG_FIND_IDAGS) core_inter_tail[one_idag]->type = IDAG_REQ_DDS_PENDING;
  1110. else if (core_inter_head[sender_id]->type == FAR_REQ_IDAG_FIND_IDAGS) core_inter_tail[one_idag]->type = FAR_REQ_IDAG_REQ_DDS_PENDING;
  1111. else core_inter_tail[one_idag]->type = SELFOPT_IDAG_REQ_DDS_PENDING;
  1112. core_inter_tail[one_idag]->data.reg.C = core_inter_head[sender_id]->data.reg.C;
  1113. core_inter_tail[one_idag]->data.reg.r = core_inter_head[sender_id]->data.reg.r;
  1114. core_inter_tail[one_idag]->next = NULL;
  1115. if (core_inter_head[one_idag]->next == NULL) {
  1116. //kill(pid_num[one_idag], SIG_REQ_DDS); //newly created
  1117. scc_kill(one_idag, SIG_REQ_DDS);
  1118. my_stats.msg_count++;
  1119. my_stats.distance += distance(node_id,one_idag);
  1120. } else {
  1121. fprintf(log_file,"I did not sent req_dds to %d with interaction = %d inter 2=%d\n",one_idag,core_inter_head[one_idag]->type,core_inter_head[one_idag]->next->type);
  1122. fflush(log_file);
  1123. }
  1124. }
  1125. tmp_inter_list = core_inter_head[sender_id];
  1126. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1127. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  1128. else send_next_signal(core_inter_head[sender_id], sender_id);
  1129. free(tmp_inter_list);
  1130. } else if (core_inter_head[sender_id]->type == IDAG_FIND_IDAGS_PENDING || core_inter_head[sender_id]->type == SELFOPT_IDAG_FIND_IDAGS_PENDING) {
  1131. printf("I am %d and deadlock was prevented in sig_find_idags\n",node_id);
  1132. tmp_inter_list = core_inter_head[sender_id];
  1133. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1134. free(tmp_inter_list);
  1135. if(core_inter_head[sender_id] == NULL) {
  1136. if (core_inter_tail[sender_id] == NULL){
  1137. printf("Malaka gamietai to core_inter_tail\n");
  1138. core_inter_tail[sender_id] = NULL;
  1139. }
  1140. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1141. } else {
  1142. printf("I have to reject sig_IDAG_FIND_IDAGS_handler sender_id=%d node_id=%d interaction=%d\n",sender_id,node_id,core_inter_head[sender_id]->type);
  1143. fprintf(log_file,"I have to reject sig_IDAG_FIND_IDAGS_handler sender_id=%d interaction=%d\n",sender_id,core_inter_head[sender_id]->type);
  1144. fflush(log_file);
  1145. //kill(info->si_pid, SIG_REJECT);
  1146. scc_kill(sender_id, SIG_REJECT);
  1147. my_stats.msg_count++;
  1148. my_stats.distance += distance(node_id,sender_id);
  1149. }
  1150. cur_time = time(NULL);
  1151. cur_t = localtime(&cur_time);
  1152. fprintf(log_file, "[%d:%d:%d]: I ended sig_IDAG_FIND_IDAGS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  1153. fflush(log_file);
  1154. signals_enable();
  1155. }
  1156. void sig_REQ_DDS_handler(int sender_id)
  1157. {
  1158. int num_of_agents, i, agent_id, num_of_cores, j, agents_read=0;
  1159. region cur_reg;
  1160. core_list *tmp_cores_list;
  1161. inter_list *tmp_inter_list;
  1162. DDS_list *tmp_DDS;
  1163. target_list *tmp_target_list;
  1164. int data_array_local[LINE_SIZE], error, str_len;
  1165. //RCCE_FLAG_STATUS receiver_status;
  1166. char error_str[64];
  1167. signals_disable();
  1168. cur_time = time(NULL);
  1169. cur_t = localtime(&cur_time);
  1170. fprintf(log_file, "[%d:%d:%d]: I entered sig_REQ_DDS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  1171. fflush(log_file);
  1172. if (core_inter_head[sender_id] == NULL){ //I am the idag
  1173. //kill(info->si_pid, SIG_ACK);
  1174. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  1175. scc_kill(sender_id, SIG_ACK);
  1176. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  1177. my_stats.msg_count++;
  1178. my_stats.distance += distance(node_id,sender_id);
  1179. //read(fd_r, &cur_reg, sizeof(region));
  1180. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), LINE_SIZE * sizeof(int), node_id);
  1181. if (error != RCCE_SUCCESS) {
  1182. RCCE_error_string(error, error_str, &str_len);
  1183. printf("I am %d and i got an error in get data in sig_REQ_DDS_handler from %d with descr %s\n",node_id,sender_id,error_str);
  1184. }
  1185. cur_reg.C = data_array_local[0];
  1186. cur_reg.r = data_array_local[1];
  1187. //read(fd_r, &cur_reg, sizeof(region));
  1188. fprintf(log_file,"I am to investigate region C=%d r=%d for %d\n",cur_reg.C,cur_reg.r,sender_id);
  1189. fflush(log_file);
  1190. if (core_inter_head[sender_id] == NULL){
  1191. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  1192. core_inter_tail[sender_id] = core_inter_head[sender_id];
  1193. } else {
  1194. core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
  1195. core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
  1196. }
  1197. core_inter_tail[sender_id]->type = REP_IDAG_REQ_DDS;
  1198. core_inter_tail[sender_id]->next = NULL;
  1199. if (cur_reg.C > -1) {//gia na zhtaei o node 0 dds
  1200. core_inter_tail[sender_id]->data.agents_in_reg = (int *) malloc((2*DDS_count+1)*sizeof(int));
  1201. core_inter_tail[sender_id]->data.agents_in_reg[0] = 0;
  1202. for (i=1; i<=2*DDS_count; i+=2){
  1203. core_inter_tail[sender_id]->data.agents_in_reg[i] = -1;
  1204. core_inter_tail[sender_id]->data.agents_in_reg[i+1] = 0;
  1205. }
  1206. tmp_cores_list = my_cores;
  1207. while (tmp_cores_list != NULL){
  1208. if (distance(tmp_cores_list->core_id, cur_reg.C) <= cur_reg.r){
  1209. if (tmp_cores_list->offered_to == -1) agent_id=node_id;
  1210. else {
  1211. agent_id=tmp_cores_list->offered_to;
  1212. for (tmp_DDS = DDS->next; tmp_DDS!=NULL; tmp_DDS=tmp_DDS->next) if (tmp_DDS->agent_id == agent_id) break;
  1213. if (tmp_DDS == NULL) agent_id = node_id;
  1214. }
  1215. for (i=1; i<=2*DDS_count; i+=2)
  1216. if (core_inter_tail[sender_id]->data.agents_in_reg[i] == agent_id) break;
  1217. else if (core_inter_tail[sender_id]->data.agents_in_reg[i] == -1){
  1218. core_inter_tail[sender_id]->data.agents_in_reg[0]++;
  1219. core_inter_tail[sender_id]->data.agents_in_reg[i] = agent_id;
  1220. break;
  1221. }
  1222. core_inter_tail[sender_id]->data.agents_in_reg[i+1]++;
  1223. }
  1224. tmp_cores_list = tmp_cores_list->next;
  1225. }
  1226. } else core_inter_tail[sender_id]->data.agents_in_reg = NULL;
  1227. //kill(info->si_pid, SIG_REQ_DDS);
  1228. scc_kill(sender_id, SIG_REQ_DDS);
  1229. my_stats.msg_count++;
  1230. my_stats.distance += distance(node_id,sender_id);
  1231. } else if (core_inter_head[sender_id]->type == IDAG_REQ_DDS && state != IDLE_INIT_MAN && state != WORKING_NODE_IDLE_INIT) {
  1232. //IDLE_INIT_MAN_SELFOPT_PENDING && state != IDLE_INIT_MAN_WORK_PENDING) {
  1233. printf("I am %d and i think i cought a stray init IDAG_REQ_DDS reply from %d. My current state is %d\n",node_id,sender_id,state);
  1234. fprintf(log_file,"I am %d and i think i cought a stray init IDAG_REQ_DDS reply from %d. My current state is %d\n",node_id,sender_id,state);
  1235. fflush(log_file);
  1236. //kill(info->si_pid, SIG_REJECT);
  1237. scc_kill(sender_id, SIG_REJECT);
  1238. my_stats.msg_count++;
  1239. my_stats.distance += distance(node_id,sender_id);
  1240. tmp_inter_list = core_inter_head[sender_id];
  1241. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1242. free(tmp_inter_list);
  1243. if(core_inter_head[sender_id] == NULL) {
  1244. if (core_inter_tail[sender_id] == NULL){
  1245. printf("Malaka gamietai to core_inter_tail\n");
  1246. core_inter_tail[sender_id] = NULL;
  1247. }
  1248. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1249. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS && state != IDLE_AGENT_WAITING_OFF) {
  1250. printf("I am %d and i think i cought a stray selfopt SELFOPT_IDAG_REQ_DDS reply from %d. My current state is %d\n",node_id,sender_id,state);
  1251. fprintf(log_file,"I am %d and i think i cought a stray selfopt SELFOPT_IDAG_REQ_DDS reply from %d. My current state is %d\n",node_id,sender_id,state);
  1252. fflush(log_file);
  1253. //kill(info->si_pid, SIG_REJECT);
  1254. scc_kill(sender_id, SIG_REJECT);
  1255. my_stats.msg_count++;
  1256. my_stats.distance += distance(node_id,sender_id);
  1257. tmp_inter_list = core_inter_head[sender_id];
  1258. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1259. free(tmp_inter_list);
  1260. if(core_inter_head[sender_id] == NULL) {
  1261. if (core_inter_tail[sender_id] == NULL){
  1262. printf("Malaka gamietai to core_inter_tail\n");
  1263. core_inter_tail[sender_id] = NULL;
  1264. }
  1265. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1266. } else if (core_inter_head[sender_id]->type == IDAG_REQ_DDS || core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS){ //I am the requesting common node
  1267. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  1268. scc_kill(sender_id, SIG_ACK);
  1269. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  1270. my_stats.msg_count++;
  1271. my_stats.distance += distance(node_id,sender_id);
  1272. //read(fd_r, &num_of_idags, sizeof(int));
  1273. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), LINE_SIZE * sizeof(int), node_id);
  1274. if (error != RCCE_SUCCESS) {
  1275. RCCE_error_string(error, error_str, &str_len);
  1276. printf("I am %d and i got an error in get data in sig_REQ_DDS_handler from %d with descr %s\n",node_id,sender_id,error_str);
  1277. }
  1278. if (core_inter_head[sender_id]->type == IDAG_REQ_DDS) {
  1279. init_DDS_replies++;
  1280. fprintf(log_file,"init_DDS_idags = %d, init_DDS_replies = %d init_idags_areas_replies=%d\n",init_DDS_idags,init_DDS_replies,init_idags_areas_replies);
  1281. fflush(log_file);
  1282. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS) {
  1283. selfopt_DDS_replies++;
  1284. fprintf(log_file,"selfopt_DDS_idags = %d, selfopt_DDS_replies = %d\n",selfopt_DDS_idags,selfopt_DDS_replies);
  1285. fflush(log_file);
  1286. }
  1287. //read(fd_r, &num_of_agents, sizeof(int));
  1288. num_of_agents = data_array_local[0];
  1289. while (num_of_agents > X_max*Y_max) {
  1290. printf("I am %d and in req_dds i got shit num_of_agents %d from %d\n",node_id,num_of_agents,sender_id);
  1291. //read(fd_r, &num_of_agents, sizeof(int));
  1292. }
  1293. fprintf(log_file, "Number of agents in region = %d of %d reg = (%d,%d)\n",num_of_agents,sender_id,core_inter_head[sender_id]->data.reg.C,core_inter_head[sender_id]->data.reg.r);
  1294. fflush(log_file);
  1295. //for (i=0; i<num_of_agents; i++)
  1296. i = 2; //bypass second element of array to produce cleaner code
  1297. while (agents_read < num_of_agents) {
  1298. //read(fd_r, &agent_id, sizeof(int));
  1299. //read(fd_r, &num_of_cores, sizeof(int));
  1300. if (i > LINE_SIZE - 1) {
  1301. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[LINE_SIZE]), LINE_SIZE * sizeof(int), node_id);
  1302. if (error != RCCE_SUCCESS) {
  1303. RCCE_error_string(error, error_str, &str_len);
  1304. printf("I am %d and i got an error in get data in sig_IDAG_FIND_IDAGS_handler from %d with descr %s\n",node_id,sender_id,error_str);
  1305. }
  1306. i=0;
  1307. }
  1308. agent_id = data_array_local[i++];
  1309. num_of_cores = data_array_local[i++];
  1310. agents_read++;
  1311. fprintf(log_file, "there is an agent with id %d and %d cores\n",agent_id,num_of_cores);
  1312. fflush(log_file);
  1313. if (agent_id == node_id) continue;//((node_id == -1 && agent_id == idag_id) || node_id == agent_id)
  1314. if (core_inter_head[sender_id]->type == IDAG_REQ_DDS) {
  1315. tmp_target_list = init_targets_head;
  1316. while (tmp_target_list != NULL && tmp_target_list->target != agent_id)
  1317. tmp_target_list = tmp_target_list->next;
  1318. if (tmp_target_list == NULL) {
  1319. if (init_targets_head == NULL) {
  1320. init_targets_head = (target_list *) malloc(sizeof(target_list));
  1321. init_targets_tail = init_targets_head;;
  1322. } else {
  1323. init_targets_tail->next = (target_list *) malloc(sizeof(target_list));
  1324. init_targets_tail = init_targets_tail->next;
  1325. }
  1326. init_targets_tail->next = NULL;
  1327. init_targets_tail->target = agent_id;
  1328. init_targets_tail->num_of_regions = 1;
  1329. init_targets_tail->region_arr[0] = core_inter_head[sender_id]->data.reg;
  1330. } else {
  1331. fprintf(log_file, "in here num_of_regions = %d\n",tmp_target_list->num_of_regions);
  1332. fflush(log_file);
  1333. for (j=0; j<tmp_target_list->num_of_regions; j++)
  1334. if (tmp_target_list->region_arr[j].C == core_inter_head[sender_id]->data.reg.C && tmp_target_list->region_arr[j].r == core_inter_head[sender_id]->data.reg.r){
  1335. fprintf(log_file, "fucking area allready exists\n");
  1336. fflush(log_file);
  1337. break;
  1338. }
  1339. if (j == tmp_target_list->num_of_regions) {
  1340. tmp_target_list->region_arr[tmp_target_list->num_of_regions++] = core_inter_head[sender_id]->data.reg;
  1341. //fprintf(log_file, "new area added\n");
  1342. //fflush(log_file);
  1343. }
  1344. }
  1345. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS){
  1346. tmp_target_list = selfopt_targets_head;
  1347. while (tmp_target_list != NULL && tmp_target_list->target != agent_id)
  1348. tmp_target_list = tmp_target_list->next;
  1349. if (tmp_target_list == NULL) {
  1350. if (selfopt_targets_head == NULL) {
  1351. selfopt_targets_head = (target_list *) malloc(sizeof(target_list));
  1352. selfopt_targets_tail = selfopt_targets_head;;
  1353. } else {
  1354. selfopt_targets_tail->next = (target_list *) malloc(sizeof(target_list));
  1355. selfopt_targets_tail = selfopt_targets_tail->next;
  1356. }
  1357. selfopt_targets_tail->next = NULL;
  1358. selfopt_targets_tail->target = agent_id;
  1359. selfopt_targets_tail->num_of_regions = 1;
  1360. selfopt_targets_tail->region_arr[0] = core_inter_head[sender_id]->data.reg;
  1361. } else {
  1362. for (j=0; j<tmp_target_list->num_of_regions; j++)
  1363. if (tmp_target_list->region_arr[j].C == core_inter_head[sender_id]->data.reg.C && tmp_target_list->region_arr[j].r == core_inter_head[sender_id]->data.reg.r){
  1364. break;
  1365. }
  1366. if (j == tmp_target_list->num_of_regions)
  1367. tmp_target_list->region_arr[tmp_target_list->num_of_regions++] = core_inter_head[sender_id]->data.reg;
  1368. }
  1369. } else if (core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS) {
  1370. if (core_inter_head[agent_id] == NULL){
  1371. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  1372. core_inter_tail[agent_id] = core_inter_head[agent_id];
  1373. } else {
  1374. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  1375. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  1376. }
  1377. //if (core_inter_head[sender_id]->type == IDAG_REQ_DDS) core_inter_tail[agent_id]->type = AGENT_REQ_CORES_PENDING;
  1378. //else if (core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS) core_inter_tail[agent_id]->type = FAR_REQ_CORES;
  1379. //else core_inter_tail[agent_id]->type = SELFOPT_REQ_CORES_PENDING;
  1380. core_inter_tail[agent_id]->type = FAR_REQ_CORES_PENDING;
  1381. core_inter_tail[agent_id]->data.reg = core_inter_head[sender_id]->data.reg;
  1382. core_inter_tail[agent_id]->next = NULL;
  1383. if (core_inter_head[agent_id]->next == NULL) {
  1384. //kill(pid_num[agent_id], SIG_REQ_CORES); //newly created, not an idag
  1385. scc_kill(agent_id, SIG_REQ_CORES);
  1386. my_stats.msg_count++;
  1387. my_stats.distance += distance(node_id,agent_id);
  1388. }
  1389. }
  1390. }
  1391. if (core_inter_head[sender_id]->type == IDAG_REQ_DDS && init_DDS_replies == init_DDS_idags && init_idags_areas_replies == init_near_areas_num) {
  1392. for (tmp_target_list = init_targets_head; tmp_target_list != NULL; tmp_target_list = tmp_target_list->next){
  1393. agent_id = tmp_target_list->target;
  1394. for (tmp_inter_list = core_inter_head[agent_id]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
  1395. if (tmp_inter_list->type == FAR_INIT_REQ || tmp_inter_list->type == FAR_REQ_MAN) break;
  1396. if (tmp_inter_list == NULL) {
  1397. if (core_inter_head[agent_id] == NULL){
  1398. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  1399. core_inter_tail[agent_id] = core_inter_head[agent_id];
  1400. } else {
  1401. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  1402. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  1403. }
  1404. core_inter_tail[agent_id]->type = AGENT_REQ_CORES_PENDING;
  1405. core_inter_tail[agent_id]->data.reg_arr.num_of_regions = tmp_target_list->num_of_regions;
  1406. core_inter_tail[agent_id]->data.reg_arr.region_arr = (region *) malloc(tmp_target_list->num_of_regions * sizeof(region));
  1407. if (tmp_target_list->target == node_id) printf("Why is this here node_id=%d\n",node_id);
  1408. fprintf(log_file,"Init req target = %d, num_of_regions = %d. Αreas:",tmp_target_list->target,tmp_target_list->num_of_regions);
  1409. for (i=0; i<tmp_target_list->num_of_regions; i++) {
  1410. core_inter_tail[agent_id]->data.reg_arr.region_arr[i] = tmp_target_list->region_arr[i];
  1411. fprintf(log_file," (%d,%d),",tmp_target_list->region_arr[i].C,tmp_target_list->region_arr[i].r);
  1412. }
  1413. fprintf(log_file,"\n");
  1414. fflush(log_file);
  1415. core_inter_tail[agent_id]->next = NULL;
  1416. if (core_inter_head[agent_id]->next == NULL) {
  1417. //kill(pid_num[agent_id], SIG_REQ_CORES);
  1418. scc_kill(agent_id, SIG_REQ_CORES);
  1419. my_stats.msg_count++;
  1420. my_stats.distance += distance(node_id,agent_id);
  1421. } else if (agent_id == sender_id && core_inter_head[agent_id]->next->type == AGENT_REQ_CORES_PENDING) { //den exei fugei apo to free alla einai wra na stalei
  1422. //kill(pid_num[agent_id], SIG_REQ_CORES);
  1423. scc_kill(agent_id, SIG_REQ_CORES);
  1424. my_stats.msg_count++;
  1425. my_stats.distance += distance(node_id,agent_id);
  1426. } else printf("This init fucker is not NULL interaction=%d interaction2=%d\n",core_inter_head[agent_id]->type,core_inter_head[agent_id]->next->type);
  1427. } else
  1428. printf("I am %d and i did not send local requests to %d because he is my far manager\n",node_id,agent_id);
  1429. }
  1430. its.it_value.tv_nsec = INIT_NODE_INTERVAL * MS;//750000000;// * MS;
  1431. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error4\n");
  1432. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS && selfopt_DDS_replies == selfopt_DDS_idags) {
  1433. for (tmp_target_list = selfopt_targets_head; tmp_target_list != NULL; tmp_target_list = tmp_target_list->next){
  1434. agent_id = tmp_target_list->target;
  1435. if (core_inter_head[agent_id] == NULL){
  1436. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  1437. core_inter_tail[agent_id] = core_inter_head[agent_id];
  1438. } else {
  1439. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  1440. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  1441. }
  1442. core_inter_tail[agent_id]->type = SELFOPT_REQ_CORES_PENDING;
  1443. core_inter_tail[agent_id]->data.reg_arr.num_of_regions = tmp_target_list->num_of_regions;
  1444. core_inter_tail[agent_id]->data.reg_arr.region_arr = (region *) malloc(tmp_target_list->num_of_regions * sizeof(region));
  1445. if (tmp_target_list->target == node_id) printf("selfopt Why is this here node_id=%d\n",node_id);
  1446. fprintf(log_file,"Selfopt req target = %d, num_of_regions = %d. Αreas:",tmp_target_list->target,tmp_target_list->num_of_regions);
  1447. for (i=0; i<tmp_target_list->num_of_regions; i++) {
  1448. core_inter_tail[agent_id]->data.reg_arr.region_arr[i] = tmp_target_list->region_arr[i];
  1449. fprintf(log_file," (%d,%d),",tmp_target_list->region_arr[i].C,tmp_target_list->region_arr[i].r);
  1450. }
  1451. fprintf(log_file,"\n");
  1452. fflush(log_file);
  1453. core_inter_tail[agent_id]->next = NULL;
  1454. if (core_inter_head[agent_id]->next == NULL) {
  1455. //kill(pid_num[agent_id], SIG_REQ_CORES);
  1456. scc_kill(agent_id, SIG_REQ_CORES);
  1457. my_stats.msg_count++;
  1458. my_stats.distance += distance(node_id,agent_id);
  1459. } else if (agent_id == sender_id && core_inter_head[agent_id]->next->type == SELFOPT_REQ_CORES_PENDING) {
  1460. //kill(pid_num[agent_id], SIG_REQ_CORES);
  1461. scc_kill(agent_id, SIG_REQ_CORES);
  1462. my_stats.msg_count++;
  1463. my_stats.distance += distance(node_id,agent_id);
  1464. } else printf("This selfopt fucker is not NULL interaction=%d interaction2=%d\n",core_inter_head[agent_id]->type,core_inter_head[agent_id]->next->type);
  1465. }
  1466. its.it_value.tv_nsec = INIT_NODE_INTERVAL * MS;// 750000000;
  1467. selfopt_time_rem = its.it_value.tv_nsec;
  1468. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error7\n");
  1469. if (state != IDLE_AGENT_WAITING_OFF) {
  1470. printf("I am %d and about to set my alarm for selfopt check and my state before that was %d\n",node_id,state);
  1471. state = IDLE_AGENT_WAITING_OFF;
  1472. }
  1473. }
  1474. tmp_inter_list = core_inter_head[sender_id];
  1475. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1476. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  1477. else if (core_inter_head[sender_id]->type != AGENT_REQ_CORES_PENDING && core_inter_head[sender_id]->type != SELFOPT_REQ_CORES_PENDING)//far_req_max_man != sender_id &&
  1478. send_next_signal(core_inter_head[sender_id], sender_id);
  1479. free(tmp_inter_list);
  1480. } else if (core_inter_head[sender_id]->type == DEBUG_IDAG_REQ_DDS){ //I am the requesting common node
  1481. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  1482. scc_kill(sender_id, SIG_ACK);
  1483. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  1484. //read(fd_r, &num_of_idags, sizeof(int));
  1485. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), LINE_SIZE * sizeof(int), node_id);
  1486. if (error != RCCE_SUCCESS) {
  1487. RCCE_error_string(error, error_str, &str_len);
  1488. fprintf(log_file,"I am %d and i got an error in get data in sig_REQ_DDS_handler from %d with descr %s\n",node_id,sender_id,error_str);
  1489. fflush(log_file);
  1490. }
  1491. //read(fd_r, &num_of_agents, sizeof(int));
  1492. num_of_agents = data_array_local[0];
  1493. printf("\nNumber of agents in region = %d\n",num_of_agents);
  1494. fprintf(log_file,"I come here a\n");
  1495. fflush(log_file);
  1496. //for (i=0; i<num_of_agents; i++)
  1497. i = 2; //bypass second element of array to produce cleaner code
  1498. while (agents_read < num_of_agents) {
  1499. //read(fd_r, &agent_id, sizeof(int));
  1500. //read(fd_r, &num_of_cores, sizeof(int));
  1501. if (i > LINE_SIZE - 1) {
  1502. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[LINE_SIZE]), LINE_SIZE * sizeof(int), node_id);
  1503. if (error != RCCE_SUCCESS) {
  1504. RCCE_error_string(error, error_str, &str_len);
  1505. printf("I am %d and i got an error in get data in sig_IDAG_FIND_IDAGS_handler from %d with descr %s\n",node_id,sender_id,error_str);
  1506. }
  1507. i=0;
  1508. }
  1509. agent_id = data_array_local[i++];
  1510. num_of_cores = data_array_local[i++];
  1511. agents_read++;
  1512. printf("Agent no %d is %d with %d cores\n",i,agent_id,num_of_cores);
  1513. }
  1514. fprintf(log_file,"I come here b\n");
  1515. fflush(log_file);
  1516. idags_replied++;
  1517. tmp_inter_list = core_inter_head[sender_id];
  1518. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1519. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  1520. else send_next_signal(core_inter_head[sender_id], sender_id);
  1521. free(tmp_inter_list);
  1522. } else if (core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING || core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS_PENDING
  1523. || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING){
  1524. printf("I am %d and deadlock was prevented in sig_req_dds\n",node_id);
  1525. tmp_inter_list = core_inter_head[sender_id];
  1526. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1527. free(tmp_inter_list);
  1528. if(core_inter_head[sender_id] == NULL) {
  1529. if (core_inter_tail[sender_id] == NULL){
  1530. printf("Malaka gamietai to core_inter_tail\n");
  1531. core_inter_tail[sender_id] = NULL;
  1532. }
  1533. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1534. } else {
  1535. //printf("I am %d and We were fucked in sig_REQ_DDS_handler from %d interaction is = %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  1536. printf("I am %d and i have to reject req_dds from %d. Our interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  1537. fprintf(log_file,"I am %d and i have to reject req_dds from %d. Our interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  1538. fflush(log_file);
  1539. //kill(info->si_pid, SIG_REJECT);
  1540. scc_kill(sender_id, SIG_REJECT);
  1541. my_stats.msg_count++;
  1542. my_stats.distance += distance(node_id,sender_id);
  1543. }
  1544. cur_time = time(NULL);
  1545. cur_t = localtime(&cur_time);
  1546. fprintf(log_file, "[%d:%d:%d]: I ended sig_REQ_DDS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  1547. fflush(log_file);
  1548. signals_enable();
  1549. }
  1550. void sig_REQ_CORES_handler(int sender_id)
  1551. {
  1552. int i, tmp_int, num_of_offers, j, non_zero_offers=0, cores_util, k;//num_of_idags, i, one_idag;
  1553. float req_gain;
  1554. region cur_reg;
  1555. app req_app;
  1556. offer one_offer;
  1557. offer_list *tmp_offer_list, *tmp_offer_prev = NULL, *tmp_head, *chosen_node;
  1558. inter_list *tmp_inter_list;
  1559. offer_array off_arr;
  1560. DDS_list *tmp_DDS;
  1561. int data_array_local[3 * LINE_SIZE], error, str_len;
  1562. //RCCE_FLAG_STATUS receiver_status;
  1563. char error_str[64];
  1564. signals_disable();
  1565. cur_time = time(NULL);
  1566. cur_t = localtime(&cur_time);
  1567. fprintf(log_file, "[%d:%d:%d]: I entered sig_REQ_CORES_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  1568. fflush(log_file);
  1569. if (core_inter_head[sender_id] == NULL && my_cores != NULL) {
  1570. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  1571. scc_kill(sender_id, SIG_ACK);
  1572. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  1573. my_stats.msg_count++;
  1574. my_stats.distance += distance(node_id,sender_id);
  1575. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), 3 * LINE_SIZE * sizeof(int), node_id);
  1576. if (error != RCCE_SUCCESS) {
  1577. RCCE_error_string(error, error_str, &str_len);
  1578. printf("I am %d and i got an error in get data in sig_REQ_CORES_handler from %d with descr %s\n",node_id,sender_id,error_str);
  1579. }
  1580. req_app.id = data_array_local[0];
  1581. memcpy(&req_app.A,&data_array_local[1],sizeof(int));
  1582. memcpy(&req_app.var,&data_array_local[2],sizeof(int));
  1583. memcpy(&req_app.workld,&data_array_local[3],sizeof(int));
  1584. req_app.num_of_cores = data_array_local[4];
  1585. //read(fd_r, &req_app, sizeof(app));
  1586. if (core_inter_head[sender_id] == NULL) {
  1587. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  1588. core_inter_tail[sender_id] = core_inter_head[sender_id];
  1589. } else {
  1590. core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
  1591. core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
  1592. }
  1593. core_inter_tail[sender_id]->type = REP_AGENT_REQ_CORES;
  1594. off_arr.num_of_offers = data_array_local[5];
  1595. //read(fd_r, &off_arr.num_of_offers, sizeof(int));
  1596. /*while (off_arr.num_of_offers > OLD_INIT_AREAS_NUM) {
  1597. printf("I am %d kai fagame skoupidia apo ton %d kai einai %d\n",node_id,sender_id,off_arr.num_of_offers);
  1598. read(fd_r, &off_arr.num_of_offers, sizeof(int));
  1599. }*/
  1600. off_arr.offer_arr = (offer *) malloc(off_arr.num_of_offers * sizeof(offer));
  1601. core_inter_tail[sender_id]->next = NULL;
  1602. for (i=0; i<off_arr.num_of_offers; i++) {
  1603. //read(fd_r, &cur_reg, sizeof(region));
  1604. cur_reg.C = data_array_local[2*i+LINE_SIZE];
  1605. cur_reg.r = data_array_local[2*i+LINE_SIZE+1];
  1606. fprintf(log_file, "C = %d, r = %d\n",cur_reg.C,cur_reg.r);
  1607. fflush(log_file);
  1608. if (my_idag == -1) {
  1609. off_arr.offer_arr[i].offered_cores = (int *) malloc(my_cores_count*sizeof(int));
  1610. tmp_int = offer_cores(my_cores, req_app, cur_reg, off_arr.offer_arr[i].offered_cores, sender_id);
  1611. off_arr.offer_arr[i].num_of_cores = tmp_int;
  1612. my_stats.comp_effort++;
  1613. if (tmp_int > 0) non_zero_offers++;
  1614. off_arr.offer_arr[i].spd_loss = 0.0;
  1615. } else if (my_cores != NULL && my_cores_count>2) {
  1616. off_arr.offer_arr[i].offered_cores = (int *) malloc(my_cores_count*sizeof(int));
  1617. tmp_int = offer_cores(my_cores, req_app, cur_reg, off_arr.offer_arr[i].offered_cores, sender_id);
  1618. off_arr.offer_arr[i].num_of_cores = tmp_int;
  1619. my_stats.comp_effort++;
  1620. //printf("I am %d in i = %d and i offer %d cores\n",node_id,i,tmp_int);
  1621. off_arr.offer_arr[i].spd_loss = Speedup(my_app, my_cores_count) - Speedup(my_app, my_cores_count-tmp_int);
  1622. req_gain = Speedup(req_app,req_app.num_of_cores+tmp_int) - Speedup(req_app,req_app.num_of_cores);
  1623. if (tmp_int > 0) {
  1624. non_zero_offers++;
  1625. fprintf(log_file,"I offered %d cores with spd_loss = %0.2f and %0.2f gain for the req_app\n",tmp_int,off_arr.offer_arr[i].spd_loss,req_gain);
  1626. fflush(log_file);
  1627. fprintf(app_log_file,"I offered %d cores with spd_loss = %0.2f and %0.2f gain for the req_app\n"
  1628. ,tmp_int,off_arr.offer_arr[i].spd_loss,req_gain);
  1629. fflush(app_log_file);
  1630. }
  1631. } else {
  1632. off_arr.offer_arr[i].num_of_cores = 0;
  1633. off_arr.offer_arr[i].spd_loss = 0.0;
  1634. }
  1635. fprintf(log_file, "I offered %d %d cores: ",sender_id,off_arr.offer_arr[i].num_of_cores);
  1636. for (j=0; j<off_arr.offer_arr[i].num_of_cores; j++)
  1637. fprintf(log_file, "%d, ",off_arr.offer_arr[i].offered_cores[j]);
  1638. fprintf(log_file, "\n");
  1639. fflush(log_file);
  1640. }
  1641. fprintf(log_file,"non_zero_offers = %d\n",non_zero_offers);
  1642. core_inter_tail[sender_id]->data.off_arr.num_of_offers = non_zero_offers;
  1643. if (non_zero_offers > 0) core_inter_tail[sender_id]->data.off_arr.offer_arr = (offer *) malloc(non_zero_offers * sizeof(offer));
  1644. else core_inter_tail[sender_id]->data.off_arr.offer_arr = NULL;
  1645. j = 0;
  1646. for (i=0; i<off_arr.num_of_offers; i++)
  1647. if (off_arr.offer_arr[i].num_of_cores > 0) {
  1648. core_inter_tail[sender_id]->data.off_arr.offer_arr[j].num_of_cores = off_arr.offer_arr[i].num_of_cores;
  1649. core_inter_tail[sender_id]->data.off_arr.offer_arr[j].spd_loss = off_arr.offer_arr[i].spd_loss;
  1650. core_inter_tail[sender_id]->data.off_arr.offer_arr[j].offered_cores = (int *) malloc(off_arr.offer_arr[i].num_of_cores * sizeof(int));
  1651. for (tmp_int=0; tmp_int<off_arr.offer_arr[i].num_of_cores; tmp_int++)
  1652. core_inter_tail[sender_id]->data.off_arr.offer_arr[j].offered_cores[tmp_int] = off_arr.offer_arr[i].offered_cores[tmp_int];
  1653. j++;
  1654. }
  1655. if (core_inter_head[sender_id]->next == NULL) {
  1656. //kill(info->si_pid, SIG_REQ_CORES);
  1657. scc_kill(sender_id, SIG_REQ_CORES);
  1658. my_stats.msg_count++;
  1659. my_stats.distance += distance(node_id,sender_id);
  1660. } else printf("Apparently not null interaction=%d\n",core_inter_head[sender_id]->type);
  1661. } else if (core_inter_head[sender_id] == NULL) {
  1662. printf("I am %d and i have to reject req_cores from %d with null interaction\n",node_id,sender_id);
  1663. fprintf(log_file,"i have to reject req_cores from %d. with null interaction\n",sender_id);
  1664. fflush(log_file);
  1665. scc_kill(sender_id, SIG_REJECT);
  1666. my_stats.msg_count++;
  1667. my_stats.distance += distance(node_id,sender_id);
  1668. } else if (core_inter_head[sender_id]->type == AGENT_REQ_CORES && state != IDLE_INIT_MAN && state != WORKING_NODE_IDLE_INIT) {
  1669. //IDLE_INIT_MAN_SELFOPT_PENDING && state != IDLE_INIT_MAN_WORK_PENDING) {
  1670. printf("I am %d and i think i cought a stray init AGENT_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
  1671. fprintf(log_file,"I am %d and i think i cought a stray init AGENT_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
  1672. fflush(log_file);
  1673. scc_kill(sender_id, SIG_REJECT);
  1674. my_stats.msg_count++;
  1675. my_stats.distance += distance(node_id,sender_id);
  1676. tmp_inter_list = core_inter_head[sender_id];
  1677. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1678. free(tmp_inter_list);
  1679. if(core_inter_head[sender_id] == NULL) {
  1680. if (core_inter_tail[sender_id] == NULL){
  1681. printf("Malaka gamietai to core_inter_tail\n");
  1682. core_inter_tail[sender_id] = NULL;
  1683. }
  1684. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1685. } else if (core_inter_head[sender_id]->type == SELFOPT_REQ_CORES && state != IDLE_AGENT_WAITING_OFF) {
  1686. printf("I am %d and i think i cought a stray selfopt SELFOPT_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
  1687. fprintf(log_file,"I am %d and i think i cought a stray selfopt SELFOPT_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
  1688. fflush(log_file);
  1689. scc_kill(sender_id, SIG_REJECT);
  1690. my_stats.msg_count++;
  1691. my_stats.distance += distance(node_id,sender_id);
  1692. tmp_inter_list = core_inter_head[sender_id];
  1693. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1694. free(tmp_inter_list);
  1695. if(core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  1696. else send_next_signal(core_inter_head[sender_id], sender_id);
  1697. } else if (core_inter_head[sender_id]->type == FAR_REQ_CORES && (state == IDLE_IDAG || (node_id == 0 && time_for_farman == -1))) {
  1698. printf("I am %d and i think i cought a stray far_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
  1699. fprintf(log_file,"I am %d and i think i cought a stray far_REQ_CORES reply from %d. My current state is %d\n",node_id,sender_id,state);
  1700. fflush(log_file);
  1701. scc_kill(sender_id, SIG_REJECT);
  1702. my_stats.msg_count++;
  1703. my_stats.distance += distance(node_id,sender_id);
  1704. tmp_inter_list = core_inter_head[sender_id];
  1705. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1706. free(tmp_inter_list);
  1707. if(core_inter_head[sender_id] == NULL) {
  1708. if (core_inter_tail[sender_id] == NULL){
  1709. printf("Malaka gamietai to core_inter_tail\n");
  1710. core_inter_tail[sender_id] = NULL;
  1711. }
  1712. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1713. } else if (core_inter_head[sender_id]->type == AGENT_REQ_CORES || core_inter_head[sender_id]->type == FAR_REQ_CORES || core_inter_head[sender_id]->type == SELFOPT_REQ_CORES){
  1714. //I am the requesting common node
  1715. //kill(info->si_pid, SIG_ACK);
  1716. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  1717. scc_kill(sender_id, SIG_ACK);
  1718. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  1719. my_stats.msg_count++;
  1720. my_stats.distance += distance(node_id,sender_id);
  1721. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), 3 * LINE_SIZE * sizeof(int), node_id);
  1722. if (error != RCCE_SUCCESS) {
  1723. RCCE_error_string(error, error_str, &str_len);
  1724. printf("I am %d and i got an error in get data in sig_REQ_CORES_handler from %d with descr %s\n",node_id,sender_id,error_str);
  1725. }
  1726. if (core_inter_head[sender_id]->type == AGENT_REQ_CORES || core_inter_head[sender_id]->type == SELFOPT_REQ_CORES) {//den erxontai ta far edw
  1727. //read(fd_r, &num_of_offers, sizeof(int));
  1728. num_of_offers = data_array_local[0];
  1729. fprintf(log_file, "num_of_offers = %d\n",num_of_offers);
  1730. fflush(log_file);
  1731. if (num_of_offers > 0) {
  1732. core_inter_head[sender_id]->data.offer_acc_array = (int *) malloc((num_of_offers+1)*sizeof(int));
  1733. core_inter_head[sender_id]->data.offer_acc_array[0] = num_of_offers;
  1734. //core_inter_head[sender_id]->data.offer_accepted = -1;
  1735. //one_offer.offered_cores = NULL;
  1736. k=1;
  1737. for (j=1; j<=num_of_offers; j++) {
  1738. //read(fd_r, &one_offer.num_of_cores, sizeof(int));
  1739. //read(fd_r, &one_offer.spd_loss, sizeof(float));
  1740. one_offer.num_of_cores = data_array_local[k++];
  1741. memcpy(&one_offer.spd_loss,&data_array_local[k++],sizeof(int));
  1742. if (core_inter_head[sender_id]->type == AGENT_REQ_CORES && init_man_offers == NULL) {
  1743. init_man_offers = (offer_list *) malloc(sizeof(offer_list));
  1744. chosen_node = init_man_offers;
  1745. init_man_offers->next = NULL;
  1746. } else if (core_inter_head[sender_id]->type == SELFOPT_REQ_CORES && selfopt_man_offers == NULL){
  1747. selfopt_man_offers = (offer_list *) malloc(sizeof(offer_list));
  1748. chosen_node = selfopt_man_offers;
  1749. selfopt_man_offers->next = NULL;
  1750. } else {
  1751. tmp_offer_prev = NULL;
  1752. if (core_inter_head[sender_id]->type == AGENT_REQ_CORES) tmp_offer_list = init_man_offers;
  1753. else tmp_offer_list = selfopt_man_offers;
  1754. while (tmp_offer_list != NULL && tmp_offer_list->off.num_of_cores >= one_offer.num_of_cores){
  1755. tmp_offer_prev = tmp_offer_list;
  1756. tmp_offer_list = tmp_offer_list->next;
  1757. }
  1758. if (tmp_offer_list == NULL) { //prepei na mpei teleutaio
  1759. tmp_offer_prev->next = (offer_list *) malloc(sizeof(offer_list));
  1760. tmp_offer_list = tmp_offer_prev->next;
  1761. tmp_offer_list->next = NULL;
  1762. chosen_node = tmp_offer_list;
  1763. } else if (tmp_offer_prev == NULL) { //prepei na mpei prwto
  1764. if (core_inter_head[sender_id]->type == AGENT_REQ_CORES) {
  1765. init_man_offers = (offer_list *) malloc(sizeof(offer_list));
  1766. tmp_head = init_man_offers;
  1767. } else {
  1768. selfopt_man_offers = (offer_list *) malloc(sizeof(offer_list));
  1769. tmp_head = selfopt_man_offers;
  1770. }
  1771. chosen_node = tmp_head;
  1772. tmp_head->next = tmp_offer_list;
  1773. } else {
  1774. tmp_offer_prev->next = (offer_list *) malloc(sizeof(offer_list));
  1775. tmp_offer_prev = tmp_offer_prev->next;
  1776. chosen_node = tmp_offer_prev;
  1777. tmp_offer_prev->next = tmp_offer_list;
  1778. }
  1779. }
  1780. chosen_node->off.num_of_cores = one_offer.num_of_cores;
  1781. chosen_node->off.spd_loss = one_offer.spd_loss;
  1782. chosen_node->off.offered_cores = (int *) malloc(one_offer.num_of_cores*sizeof(int));
  1783. for (i=0; i<one_offer.num_of_cores; i++)
  1784. //read(fd_r, &chosen_node->off.offered_cores[i], sizeof(int));
  1785. chosen_node->off.offered_cores[i] = data_array_local[k++];
  1786. /*printf("asdasNode %d is offering %d cores: ",sender_id,chosen_node->off.num_of_cores);
  1787. for (i=0; i<chosen_node->off.num_of_cores; i++)
  1788. printf(" %d,",chosen_node->off.offered_cores[i]);
  1789. printf("\n");*/
  1790. chosen_node->sender = sender_id;
  1791. core_inter_head[sender_id]->data.offer_acc_array[j] = -1;
  1792. chosen_node->answer = &core_inter_head[sender_id]->data.offer_acc_array[j];
  1793. cur_time = time(NULL);
  1794. cur_t = localtime(&cur_time);
  1795. fprintf(log_file, "[%d:%d:%d]: One node successfully added in list type=%d sender_id=%d\n",
  1796. cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,core_inter_head[sender_id]->type,sender_id);
  1797. fflush(log_file);
  1798. }
  1799. core_inter_head[sender_id]->type = REP_AGENT_OFFER_PENDING;
  1800. } else { //no answer is required
  1801. tmp_inter_list = core_inter_head[sender_id];
  1802. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1803. free(tmp_inter_list);
  1804. if(core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  1805. else send_next_signal(core_inter_head[sender_id], sender_id);
  1806. }
  1807. } else {
  1808. fprintf(log_file, "I somehow got into a far request\n");
  1809. fflush(log_file);
  1810. /*read(fd_r, &num_of_offers, sizeof(int));
  1811. fprintf(log_file, "num_of_offers is %d\n",num_of_offers);
  1812. fflush(log_file);
  1813. if (num_of_offers > 0) {
  1814. core_inter_head[sender_id]->data.offer_acc_array = (int *) malloc((num_of_offers+1)*sizeof(int));
  1815. core_inter_head[sender_id]->data.offer_acc_array[0] = num_of_offers;
  1816. core_inter_head[sender_id]->type = REP_AGENT_OFFER_PENDING;
  1817. read(fd_r, &one_offer.num_of_cores, sizeof(int));
  1818. read(fd_r, &one_offer.spd_loss, sizeof(float));
  1819. one_offer.offered_cores = (int *) malloc(one_offer.num_of_cores*sizeof(int));
  1820. for (i=0; i<one_offer.num_of_cores; i++)
  1821. read(fd_r, &one_offer.offered_cores[i], sizeof(int));
  1822. fprintf(log_file, "%d is offering %d cores with speedup loss %f\n",sender_id,one_offer.num_of_cores,one_offer.spd_loss);
  1823. fflush(log_file);
  1824. //core_inter_head[sender_id]->data.offer_accepted = -1;
  1825. core_inter_head[sender_id]->data.offer_acc_array[1] = -1;
  1826. if (far_man_offers == NULL){
  1827. far_man_offers = (offer_list *) malloc(sizeof(offer_list));
  1828. far_man_offers_tail = far_man_offers;
  1829. } else {
  1830. far_man_offers_tail->next = (offer_list *) malloc(sizeof(offer_list));
  1831. far_man_offers_tail = far_man_offers_tail->next;
  1832. }
  1833. far_man_offers_tail->off = one_offer;
  1834. far_man_offers_tail->sender = sender_id;
  1835. far_man_offers_tail->answer = &core_inter_head[sender_id]->data.offer_acc_array[1];//offer_accepted;
  1836. far_man_offers_tail->next = NULL;
  1837. far_list_count++;
  1838. cur_time = time(NULL);
  1839. cur_t = localtime(&cur_time);
  1840. fprintf(log_file, "[%d:%d:%d]: One node successfully added in far_man_offers list %d\n",
  1841. cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,core_inter_head[sender_id]->data.offer_acc_array[0]);
  1842. fflush(log_file);
  1843. } else { //no answer is required
  1844. tmp_inter_list = core_inter_head[sender_id];
  1845. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1846. free(tmp_inter_list);
  1847. if(core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  1848. else send_next_signal(core_inter_head[sender_id], sender_id);
  1849. }*/
  1850. }
  1851. } else if (core_inter_head[sender_id]->type == AGENT_REQ_CORES_PENDING || core_inter_head[sender_id]->type == SELFOPT_REQ_CORES_PENDING
  1852. || core_inter_head[sender_id]->type == FAR_REQ_CORES_PENDING){
  1853. printf("I am %d and deadlock was prevented in req_cores by %d\n",node_id,sender_id);
  1854. tmp_inter_list = core_inter_head[sender_id];
  1855. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1856. free(tmp_inter_list);
  1857. if(core_inter_head[sender_id] == NULL) {
  1858. if (core_inter_tail[sender_id] == NULL){
  1859. printf("Malaka gamietai to core_inter_tail\n");
  1860. core_inter_tail[sender_id] = NULL;
  1861. }
  1862. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1863. } else {
  1864. printf("I am %d and i have to reject req_cores from %d. Our interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  1865. fprintf(log_file,"I am %d and i have to reject req_cores from %d. Our interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  1866. fflush(log_file);
  1867. scc_kill(sender_id, SIG_REJECT);
  1868. my_stats.msg_count++;
  1869. my_stats.distance += distance(node_id,sender_id);
  1870. }
  1871. cur_time = time(NULL);
  1872. cur_t = localtime(&cur_time);
  1873. fprintf(log_file, "[%d:%d:%d]: I ended sig_REQ_CORES_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  1874. fflush(log_file);
  1875. signals_enable();
  1876. }
  1877. void sig_REP_OFFERS_handler(int sender_id)
  1878. {
  1879. int offer_ans, i, one_core, j, old_cores_cnt;//num_of_idags, i, one_idag;
  1880. core_list *tmp_cores, *tmp_cores_prev, *tmp_cores_list;
  1881. inter_list *tmp_inter_list, *tmp_inter_prev;
  1882. offer_list *tmp_offer_list;
  1883. int data_array_local[2 * LINE_SIZE], error, str_len;
  1884. //RCCE_FLAG_STATUS receiver_status;
  1885. char error_str[64];
  1886. signals_disable();
  1887. cur_time = time(NULL);
  1888. cur_t = localtime(&cur_time);
  1889. fprintf(log_file, "[%d:%d:%d]: I entered sig_REP_OFFERS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  1890. fflush(log_file);
  1891. if (core_inter_head[sender_id] == NULL) printf("fail!\n");
  1892. else if (core_inter_head[sender_id]->type == AGENT_OFFER_SENT) {
  1893. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  1894. scc_kill(sender_id, SIG_ACK);
  1895. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  1896. my_stats.msg_count++;
  1897. my_stats.distance += distance(node_id,sender_id);
  1898. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), 2 * LINE_SIZE * sizeof(int), node_id);
  1899. if (error != RCCE_SUCCESS) {
  1900. RCCE_error_string(error, error_str, &str_len);
  1901. printf("I am %d and i got an error in get data in sig_REP_OFFERS_handler from %d with descr %s\n",node_id,sender_id,error_str);
  1902. }
  1903. fprintf(log_file, "Num of offers is = %d\n",core_inter_head[sender_id]->data.off_arr.num_of_offers);
  1904. fflush(log_file);
  1905. //even if i am in a far req offer, my answer will be the first
  1906. for (j=0; j<core_inter_head[sender_id]->data.off_arr.num_of_offers; j++) {
  1907. //read(fd_r, &offer_ans, sizeof(int));
  1908. offer_ans = data_array_local[0];
  1909. fprintf(log_file, "offer_ans = %d\n",offer_ans);
  1910. fflush(log_file);
  1911. while (offer_ans != 0 && offer_ans != 1){
  1912. printf("I am %d and i am getting bizarre answer = %d from %d\n",node_id,offer_ans,sender_id);
  1913. //read(fd_r, &offer_ans, sizeof(int));
  1914. }
  1915. if (offer_ans == 0 && my_cores != NULL) { // && my_cores != NULL
  1916. for (i=0; i<core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores; i++)
  1917. for (tmp_cores = my_cores->next; tmp_cores != NULL; tmp_cores = tmp_cores->next)
  1918. if (tmp_cores->core_id == core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i]) {
  1919. fprintf(log_file,"core id = %d offered_to = %d\n",tmp_cores->core_id,tmp_cores->offered_to);
  1920. fflush(log_file);
  1921. if (tmp_cores->offered_to == sender_id) tmp_cores->offered_to = -1;
  1922. break;
  1923. }
  1924. if (app_state == APP_TERMINATED) {//app_terminated
  1925. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  1926. if (tmp_cores_list->offered_to != -1) break;
  1927. //if (tmp_cores_list != NULL && state ) state = AGENT_ZOMBIE;
  1928. //else state = AGENT_ENDING;
  1929. if (tmp_cores_list == NULL && state == AGENT_ZOMBIE) state = AGENT_ENDING;
  1930. }
  1931. /*tmp_cores = my_cores;
  1932. while (tmp_cores != NULL){
  1933. if (tmp_cores->offered_to == sender_id) tmp_cores->offered_to = -1;
  1934. tmp_cores = tmp_cores->next;
  1935. }*/
  1936. } else if (offer_ans == 1) {//&& (!app_terminated || my_idag == -1) my_cores != NULL inform my idag if I have. If i am an idag i just reduce my DDS core num. I keep my core.
  1937. if (my_idag == -1){ //I am an idag
  1938. DDS->num_of_cores -= core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores;
  1939. } else {//I am common node
  1940. /*first i must get the remaining time from my cores, before i change my core list*/
  1941. if (app_state != APP_TERMINATED) { //!app_terminated
  1942. old_Speedup = my_Speedup;
  1943. old_cores_cnt = my_cores_count;
  1944. my_cores_count -= core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores;
  1945. my_app.num_of_cores = my_cores_count;
  1946. my_Speedup = Speedup(my_app, my_cores_count);
  1947. fprintf(log_file,"Initialising removal new_cores_count = %d app_state = %d\n",my_cores_count,app_state);
  1948. fflush(log_file);
  1949. fprintf(app_log_file,"Initialising removal new_cores_count = %d app_state = %d\n",my_cores_count,app_state);
  1950. fflush(app_log_file);
  1951. if (app_state == RUNNING) {
  1952. chk_rem_num = old_cores_cnt-1;
  1953. chk_rem_count = 0;
  1954. sum_rem_time = 0;
  1955. app_state = RESIZING;
  1956. for(tmp_cores_list=my_cores->next; tmp_cores_list!=NULL; tmp_cores_list=tmp_cores_list->next) {//first is myself
  1957. one_core = tmp_cores_list->core_id;
  1958. if (core_inter_head[one_core] == NULL){
  1959. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  1960. core_inter_tail[one_core] = core_inter_head[one_core];
  1961. } else {
  1962. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  1963. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  1964. }
  1965. core_inter_tail[one_core]->type = APPOINT_WORK_NODE_PENDING;//CHK_REM_TIME;
  1966. core_inter_tail[one_core]->next = NULL;
  1967. if (core_inter_head[one_core]->next == NULL) {
  1968. //kill(pid_num[one_core], SIG_CHECK_REM_TIME);
  1969. scc_kill(one_core, SIG_CHECK_REM_TIME);
  1970. my_stats.msg_count++;
  1971. my_stats.distance += distance(node_id,one_core);
  1972. } else printf("I am %d and I am doing smth else with my working node %d in send SIG_CHECK_REM_TIME in rep offers type = %d\n",
  1973. node_id,one_core,core_inter_head[one_core]->type);
  1974. }
  1975. }
  1976. } else {
  1977. my_cores_count -= core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores;
  1978. my_app.num_of_cores = my_cores_count;
  1979. fprintf(log_file,"Initialising removal app finished new_cores_count = %d\n",my_cores_count);
  1980. fflush(log_file);
  1981. fprintf(app_log_file,"Initialising removal app finished new_cores_count = %d\n",my_cores_count);
  1982. fflush(app_log_file);
  1983. }
  1984. if (core_inter_head[my_idag] == NULL){
  1985. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  1986. core_inter_tail[my_idag] = core_inter_head[my_idag];
  1987. } else {
  1988. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  1989. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  1990. }
  1991. core_inter_tail[my_idag]->type = IDAG_REM_CORES_DDS;
  1992. core_inter_tail[my_idag]->data.app_cores = (int *)malloc((core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores+1)*sizeof(int));
  1993. core_inter_tail[my_idag]->data.app_cores[0] = core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores;
  1994. for(i=1; i<=core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores; i++) {
  1995. one_core = core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i-1];
  1996. if (app_state != APP_TERMINATED) {
  1997. tmp_inter_prev = core_inter_head[one_core];
  1998. tmp_inter_list = core_inter_head[one_core]->next;
  1999. } else {
  2000. tmp_inter_prev = NULL;
  2001. tmp_inter_list = core_inter_head[one_core];
  2002. }
  2003. while (tmp_inter_list != NULL)
  2004. if (tmp_inter_list->type == INIT_WORK_NODE || tmp_inter_list->type == APPOINT_WORK_NODE || tmp_inter_list->type == INIT_WORK_NODE_PENDING
  2005. || tmp_inter_list->type == APPOINT_WORK_NODE_PENDING) {
  2006. //|| (tmp_inter_list->type == APPOINT_WORK_NODE_PENDING && tmp_inter_prev != NULL)) {
  2007. fprintf(log_file, "Removing in rem offers one node of %d with inter = %d\n",one_core,tmp_inter_list->type);
  2008. fflush(log_file);
  2009. if (tmp_inter_prev == NULL) {
  2010. core_inter_head[one_core] = core_inter_head[one_core]->next;
  2011. free(tmp_inter_list);
  2012. tmp_inter_list = core_inter_head[one_core];
  2013. } else {
  2014. tmp_inter_prev->next = tmp_inter_list->next;
  2015. if (tmp_inter_prev->next == NULL) core_inter_tail[one_core] = tmp_inter_prev;
  2016. free(tmp_inter_list);
  2017. tmp_inter_list = tmp_inter_prev->next;
  2018. }
  2019. } else {
  2020. tmp_inter_prev = tmp_inter_list;
  2021. tmp_inter_list = tmp_inter_list->next;
  2022. }
  2023. if (app_state != APP_TERMINATED) {
  2024. if (core_inter_head[one_core] == NULL) {
  2025. fprintf(log_file,"No interaction with %d. Theoritically impossible\n",one_core);
  2026. fflush(log_file);
  2027. } else if (core_inter_head[one_core]->type == INIT_WORK_NODE_PENDING) {
  2028. fprintf(log_file,"I offered my new core %d. I will clear the interaction\n",one_core);
  2029. fflush(log_file);
  2030. tmp_inter_list = core_inter_head[one_core];
  2031. core_inter_head[one_core] = core_inter_head[one_core]->next;
  2032. if (core_inter_head[one_core] == NULL) core_inter_tail[one_core] = NULL;
  2033. else send_next_signal(core_inter_head[one_core], one_core);
  2034. free(tmp_inter_list);
  2035. } else if (core_inter_head[one_core]->type == INIT_WORK_NODE || core_inter_head[one_core]->type == APPOINT_WORK_NODE) {
  2036. fprintf(log_file,"Invalidating %d. Interaction is %d\n",one_core,core_inter_head[one_core]->type);
  2037. fflush(log_file);
  2038. core_inter_head[one_core]->data.work_time = -1;
  2039. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  2040. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  2041. core_inter_tail[one_core]->type = REMOVED_NODE_REM_TIME;//APPOINT_WORK_NODE_PENDING;//CHK_REM_TIME;
  2042. core_inter_tail[one_core]->next = NULL;
  2043. } else if (core_inter_head[one_core]->type == APPOINT_WORK_NODE_PENDING) {
  2044. fprintf(log_file,"Everything ok %d.\n",one_core);
  2045. fflush(log_file);
  2046. core_inter_head[one_core]->type = REMOVED_NODE_REM_TIME;
  2047. } else {
  2048. fprintf(log_file,"Another interaction with %d. Interaction is %d\n",one_core,core_inter_head[one_core]->type);
  2049. fflush(log_file);
  2050. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  2051. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  2052. core_inter_tail[one_core]->type = REMOVED_NODE_REM_TIME;//APPOINT_WORK_NODE_PENDING;//CHK_REM_TIME;
  2053. core_inter_tail[one_core]->next = NULL;
  2054. }
  2055. }
  2056. tmp_cores = my_cores;
  2057. tmp_cores_prev = NULL;
  2058. while (tmp_cores != NULL && tmp_cores->core_id != core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i-1]){
  2059. tmp_cores_prev = tmp_cores;
  2060. tmp_cores = tmp_cores->next;
  2061. }
  2062. if (tmp_cores == NULL) printf("strangely offered core is not in my_cores list\n");
  2063. else {
  2064. if (tmp_cores_prev == NULL) {
  2065. printf("i fucking offered my agent core!!!\n");
  2066. my_cores = my_cores->next;
  2067. } else if (tmp_cores == my_cores_tail){
  2068. my_cores_tail = tmp_cores_prev;
  2069. my_cores_tail->next = NULL;
  2070. } else tmp_cores_prev->next = tmp_cores->next;
  2071. free(tmp_cores);
  2072. }
  2073. core_inter_tail[my_idag]->data.app_cores[i] = core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i-1];
  2074. }
  2075. core_inter_tail[my_idag]->next = NULL;
  2076. if (core_inter_head[my_idag]->next == NULL) {
  2077. //kill(pid_num[my_idag], SIG_REM_CORES_DDS);
  2078. scc_kill(my_idag, SIG_REM_CORES_DDS);
  2079. my_stats.msg_count++;
  2080. my_stats.distance += distance(node_id,my_idag);
  2081. } else printf("I did not send rem signal!\n");
  2082. cur_time = time(NULL);
  2083. cur_t = localtime(&cur_time);
  2084. fprintf(app_log_file, "[%d:%d:%d]: Removal ok. my_cores_count = %d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,my_cores_count);
  2085. fprintf(app_log_file, "my cores are:");
  2086. for (tmp_cores_list=my_cores; tmp_cores_list!=NULL; tmp_cores_list=tmp_cores_list->next) {
  2087. //fprintf(log_file, " %d", tmp_cores_list->core_id);
  2088. fprintf(app_log_file, " %d", tmp_cores_list->core_id);
  2089. //printf(" %d",tmp_cores_list->core_id);
  2090. }
  2091. fprintf(app_log_file, "\n");
  2092. fflush(app_log_file);
  2093. if (app_state == APP_TERMINATED) {//app_terminated
  2094. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  2095. if (tmp_cores_list->offered_to != -1) break;
  2096. if (tmp_cores_list == NULL && state == AGENT_ZOMBIE) state = AGENT_ENDING;
  2097. }
  2098. }
  2099. } else printf("I am %d and my Answer from %d different than 0 or 1 and is %d!!\n",node_id,sender_id,offer_ans);
  2100. }
  2101. /*} else if (core_inter_head[sender_id]->type == FAR_REQ_OFFER_SENT) {
  2102. sem_getvalue(&node_sem[node_id],&i);
  2103. fprintf(log_file, "In 8 Trying to acquire semaphore. Sem value = %d\n",i);
  2104. fflush(log_file);
  2105. kill(info->si_pid, SIG_ACK);
  2106. sem_wait(&node_sem[node_id]);
  2107. sem_getvalue(&node_sem[node_id],&i);
  2108. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  2109. fflush(log_file);
  2110. my_stats.msg_count++;
  2111. my_stats.distance += distance(node_id,sender_id);
  2112. //printf("open9 idag_id=%d node_id=%d fifo_name=%s\n",idag_id,node_id,fifo_name);
  2113. fd_r = open(fifo_name, O_RDONLY);
  2114. if (fd_r == -1) {
  2115. perror("opening reading pipe");
  2116. signals_enable();
  2117. return;
  2118. } else {
  2119. fprintf(log_file, "I went through open\n");
  2120. fflush(log_file);
  2121. }
  2122. if (far_man_offers->sender == node_id) {
  2123. read(fd_r, &offer_ans, sizeof(int));
  2124. printf("I am node %d and my offer answer is %d interaction=%d\n",node_id,offer_ans,core_inter_head[sender_id]->type);
  2125. if (offer_ans == 0) {
  2126. tmp_cores = my_cores;
  2127. while (tmp_cores != NULL){
  2128. if (tmp_cores->offered_to == sender_id) tmp_cores->offered_to = -1;
  2129. tmp_cores = tmp_cores->next;
  2130. }
  2131. } else if (offer_ans == 1) {//inform my idag if I have. If i am an idag i just reduce my DDS core num. I keep my core.
  2132. if (DDS->agent_id != node_id) printf("Fuck i am not first in my DDS list!!\n");
  2133. DDS->num_of_cores -= core_inter_head[sender_id]->data.my_offer.num_of_cores;
  2134. } else printf("I am %d Answer different than 0 or 1 in far from %d!!\n",node_id,sender_id);
  2135. //if (core_inter_head[sender_id]->type == FAR_REQ_OFFER_SENT) {//FAR_REQ_OFFER
  2136. tmp_offer_list = far_man_offers;
  2137. far_man_offers = far_man_offers->next;
  2138. free(tmp_offer_list);
  2139. }
  2140. while (far_man_offers != NULL){
  2141. //printf("kai edw ftamw\n");
  2142. read(fd_r, &offer_ans, sizeof(int));
  2143. printf("I am node %d and far offer answer for node %d is %d\n",node_id,far_man_offers->sender,offer_ans);
  2144. if (offer_ans == 0 || offer_ans == 1){
  2145. *far_man_offers->answer = offer_ans;
  2146. if (core_inter_head[far_man_offers->sender]->type == REP_AGENT_OFFER_PENDING){
  2147. kill(pid_num[far_man_offers->sender],SIG_REP_OFFERS);
  2148. core_inter_head[far_man_offers->sender]->type = REP_AGENT_OFFER_SENT;
  2149. my_stats.msg_count++;
  2150. my_stats.distance += distance(node_id,far_man_offers->sender);
  2151. } else printf("Apparently lists are poutana ola\n");
  2152. } else printf("Far Answer different than 0 or 1!!\n");
  2153. tmp_offer_list = far_man_offers;
  2154. far_man_offers = far_man_offers->next;
  2155. free(tmp_offer_list);
  2156. }
  2157. sem_getvalue(&node_sem_out[sender_id],&i);
  2158. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  2159. fflush(log_file);
  2160. if (sem_post(&node_sem_out[sender_id]) == -1){
  2161. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  2162. perror("sem_post");
  2163. }
  2164. sem_getvalue(&node_sem_out[sender_id],&i);
  2165. fprintf(log_file, "Sem value = %d\n",i);
  2166. fflush(log_file);
  2167. //sem_post(&node_sem[node_id]);
  2168. //sem_post(&node_sem[sender_id]);
  2169. far_req_app.A=-1.0;
  2170. far_req_app.var=-1.0;
  2171. far_req_app.num_of_cores=-1;
  2172. far_req_or_sender = -1;
  2173. far_reg.C = -1;
  2174. far_reg.r = -1; */
  2175. } else printf("I am %d and fail 2\n",node_id);
  2176. tmp_inter_list = core_inter_head[sender_id];
  2177. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  2178. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  2179. else send_next_signal(core_inter_head[sender_id], sender_id);
  2180. free(tmp_inter_list);
  2181. cur_time = time(NULL);
  2182. cur_t = localtime(&cur_time);
  2183. fprintf(log_file, "[%d:%d:%d]: I ended sig_REP_OFFERS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2184. fflush(log_file);
  2185. signals_enable();
  2186. }
  2187. void sig_INIT_AGENT_handler(int sender_id)
  2188. {
  2189. int i, tmp;
  2190. core_list *tmp_core;
  2191. int data_array_local[3 * LINE_SIZE], error, str_len;
  2192. //RCCE_FLAG_STATUS receiver_status;
  2193. char error_str[64];
  2194. signals_disable();
  2195. cur_time = time(NULL);
  2196. cur_t = localtime(&cur_time);
  2197. fprintf(log_file, "[%d:%d:%d]: I entered sig_INIT_AGENT_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2198. fflush(log_file);
  2199. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  2200. scc_kill(sender_id, SIG_ACK);
  2201. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  2202. my_stats.msg_count++;
  2203. my_stats.distance += distance(node_id,sender_id);
  2204. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), 3 * LINE_SIZE * sizeof(int), node_id);
  2205. if (error != RCCE_SUCCESS) {
  2206. RCCE_error_string(error, error_str, &str_len);
  2207. printf("I am %d and i got an error in get data in sig_INIT_AGENT_handler from %d with descr %s\n",node_id,sender_id,error_str);
  2208. }
  2209. if (my_app.num_of_cores != -1) { //Very very very important!!!
  2210. printf ("I am already managing an app!!! Fail!!!!\n");
  2211. while (my_cores != NULL){
  2212. tmp_core = my_cores;
  2213. my_cores = my_cores->next;
  2214. free(tmp_core);
  2215. }
  2216. my_cores_tail = NULL;
  2217. my_cores_count = 0;
  2218. }
  2219. //read(fd_r, &my_app, sizeof(app));
  2220. my_app.id = data_array_local[0];
  2221. memcpy(&my_app.A,&data_array_local[1],sizeof(int));
  2222. memcpy(&my_app.var,&data_array_local[2],sizeof(int));
  2223. memcpy(&my_app.workld,&data_array_local[3],sizeof(int));
  2224. my_app.num_of_cores = data_array_local[4];
  2225. my_cores_count = my_app.num_of_cores;//+1;
  2226. if (my_cores == NULL) {
  2227. my_cores = (core_list *) malloc(sizeof(core_list));
  2228. my_cores_tail = my_cores;
  2229. } else {
  2230. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  2231. my_cores_tail = my_cores_tail->next;
  2232. }
  2233. my_cores_tail->core_id = node_id;
  2234. my_cores_tail->offered_to = -1;
  2235. my_cores_tail->next = NULL;
  2236. //I want myself to be first in my_cores list
  2237. for (i=0; i<my_app.num_of_cores; i++){
  2238. //read(fd_r, &tmp, sizeof(int));
  2239. tmp = data_array_local[i+LINE_SIZE];
  2240. if (tmp != node_id){
  2241. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  2242. my_cores_tail = my_cores_tail->next;
  2243. //read(fd_r, &my_cores_tail->core_id, sizeof(int));
  2244. my_cores_tail->core_id = tmp;
  2245. my_cores_tail->offered_to = -1;
  2246. my_cores_tail->next = NULL;
  2247. }
  2248. }
  2249. //read(fd_r, &my_app_times[0], sizeof(my_time_stamp));
  2250. //read(fd_r, &my_app_times[1], sizeof(my_time_stamp));
  2251. my_app_times[0].tm_sec = data_array_local[2*LINE_SIZE];
  2252. my_app_times[0].tm_min = data_array_local[2*LINE_SIZE+1];
  2253. my_app_times[0].tm_hour = data_array_local[2*LINE_SIZE+2];
  2254. my_app_times[1].tm_sec = data_array_local[2*LINE_SIZE+3];
  2255. my_app_times[1].tm_min = data_array_local[2*LINE_SIZE+4];
  2256. my_app_times[1].tm_hour = data_array_local[2*LINE_SIZE+5];
  2257. printf("I am new agent with id %d and app with A=%.2f, var=%.2f and %d my_cores_count = %d cores: \n"
  2258. ,node_id,my_app.A,my_app.var,my_app.num_of_cores,my_cores_count);
  2259. my_Speedup = Speedup(my_app, my_app.num_of_cores);
  2260. if (core_inter_head[my_idag] == NULL){
  2261. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  2262. core_inter_tail[my_idag] = core_inter_head[my_idag];
  2263. } else {
  2264. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  2265. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  2266. }
  2267. core_inter_tail[my_idag]->type = IDAG_ADD_CORES_DDS;
  2268. core_inter_tail[my_idag]->data.app_cores = (int *)malloc((my_cores_count+1)*sizeof(int));
  2269. core_inter_tail[my_idag]->data.app_cores[0] = my_cores_count;
  2270. tmp_core = my_cores;
  2271. i=1;
  2272. while (tmp_core != NULL){
  2273. core_inter_tail[my_idag]->data.app_cores[i] = tmp_core->core_id;
  2274. tmp_core = tmp_core->next;
  2275. i++;
  2276. }
  2277. core_inter_tail[my_idag]->next = NULL;
  2278. if (core_inter_head[my_idag]->next == NULL) {
  2279. //kill(pid_num[my_idag], SIG_ADD_CORES_DDS);
  2280. scc_kill(my_idag, SIG_ADD_CORES_DDS);
  2281. my_stats.msg_count++;
  2282. my_stats.distance += distance(node_id,my_idag);
  2283. } else printf("I am %d and i didn't call add!! with interaction %d\n",node_id,core_inter_head[my_idag]->type);
  2284. if (my_agent != -1) {
  2285. printf("I am %d and i do this agent switch\n",node_id);
  2286. if (state == WORKING_NODE || state == WORKING_NODE_IDLE_INIT) {
  2287. if (core_inter_head[my_agent] == NULL){
  2288. core_inter_head[my_agent] = (inter_list *) malloc(sizeof(inter_list));
  2289. core_inter_tail[my_agent] = core_inter_head[my_agent];
  2290. } else {
  2291. core_inter_tail[my_agent]->next = (inter_list *) malloc(sizeof(inter_list));
  2292. core_inter_tail[my_agent] = core_inter_tail[my_agent]->next;
  2293. }
  2294. core_inter_tail[my_agent]->type = REP_CHK_REM_TIME;
  2295. core_inter_tail[my_agent]->data.work_time = upper_work_bound - time_worked;
  2296. core_inter_tail[my_agent]->next = NULL;
  2297. }
  2298. upper_work_bound = 0;
  2299. my_agent = -1;
  2300. }
  2301. printf("Init ok!! my_cores_count = %d app_cores=%d app_id=%d\n",my_cores_count,my_app.num_of_cores,my_app.id);
  2302. cur_time = time(NULL);
  2303. cur_t = localtime(&cur_time);
  2304. fprintf(log_file, "[%d:%d:%d]: Init ok!! my_cores_count = %d app_id=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,my_cores_count,my_app.id);
  2305. fprintf(log_file, "my cores are:");
  2306. for (tmp_core=my_cores; tmp_core!=NULL; tmp_core=tmp_core->next) fprintf(log_file, " %d", tmp_core->core_id);
  2307. fprintf(log_file, "\n");
  2308. printf("I am %d and about to do an selfopt agent with my state before change = %d\n",node_id,state);
  2309. if (state == IDLE_INIT_MAN || state == INIT_MANAGER || state == INIT_MANAGER_SEND_OFFERS || state == INIT_MAN_CHK_OFFERS || state == WORKING_NODE_IDLE_INIT) {
  2310. fprintf(log_file,"I have pending init\n");
  2311. pending_state = state;
  2312. state = AGENT_INIT_STATE;
  2313. } else state = AGENT_INIT_STATE;
  2314. cur_time = time(NULL);
  2315. cur_t = localtime(&cur_time);
  2316. fprintf(log_file, "[%d:%d:%d]: I ended sig_INIT_AGENT_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2317. fflush(log_file);
  2318. signals_enable();
  2319. }
  2320. //an mou erthei to shma apo allon idag prepei apla na valw, an mou erthei apo common node tote prepei na upologisw
  2321. void sig_ADD_CORES_DDS_handler(int sender_id)
  2322. {
  2323. int i, is_sender_idag, j, new_agent_id;
  2324. int *nodes_to_process, nodes_cnt=0, processed_cnt=0;
  2325. int *tmp_list, tmp_cnt, tmp_idag;
  2326. char *fifo_name;
  2327. core_list *tmp_cores;//, *tmp_cores_list;
  2328. DDS_list *tmp_DDS;
  2329. int data_array_local[2 * LINE_SIZE], error, str_len;
  2330. //RCCE_FLAG_STATUS receiver_status;
  2331. char error_str[64];
  2332. signals_disable();
  2333. cur_time = time(NULL);
  2334. cur_t = localtime(&cur_time);
  2335. fprintf(log_file, "[%d:%d:%d]: I entered sig_ADD_CORES_DDS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2336. fflush(log_file);
  2337. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  2338. scc_kill(sender_id, SIG_ACK);
  2339. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  2340. my_stats.msg_count++;
  2341. my_stats.distance += distance(node_id,sender_id);
  2342. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), 2 * LINE_SIZE * sizeof(int), node_id);
  2343. if (error != RCCE_SUCCESS) {
  2344. RCCE_error_string(error, error_str, &str_len);
  2345. fprintf(log_file,"I got an error in get data in sig_ADD_CORES_DDS_handler from %d with descr %s\n",sender_id,error_str);
  2346. fflush(log_file);
  2347. }
  2348. is_sender_idag = 0;
  2349. for (i=0; i<num_idags; i++)
  2350. if (idag_id_arr[i] == sender_id){
  2351. is_sender_idag = 1;
  2352. break;
  2353. }
  2354. if (is_sender_idag == 0) {
  2355. //read(fd_r, &nodes_cnt, sizeof(int));
  2356. nodes_cnt = data_array_local[0];
  2357. while (nodes_cnt <=0){
  2358. fprintf(log_file,"i am %d and i read %d in nodes_cnt from %d\n",node_id,nodes_cnt,sender_id);
  2359. fflush(log_file);
  2360. //read(fd_r, &nodes_cnt, sizeof(int));
  2361. }
  2362. nodes_to_process = (int *) malloc(nodes_cnt * sizeof(int));
  2363. for (i=0; i<nodes_cnt; i++)
  2364. //read(fd_r, &nodes_to_process[i], sizeof(int));
  2365. nodes_to_process[i] = data_array_local[i+LINE_SIZE];
  2366. tmp_list = (int *) malloc(nodes_cnt * sizeof(int));
  2367. //printf("I am %d in add cores with sender %d and nodes_cnt = %d\n",node_id,sender_id,nodes_cnt);
  2368. while (processed_cnt < nodes_cnt){
  2369. tmp_cnt = 0;
  2370. tmp_idag = -1;
  2371. for (i=0; i<nodes_cnt; i++){
  2372. if (processed_cnt == nodes_cnt) break;
  2373. if (nodes_to_process[i] == -1) continue;
  2374. else {
  2375. if (tmp_idag == -1) tmp_idag = idag_mask[nodes_to_process[i]];
  2376. if (idag_mask[nodes_to_process[i]] != tmp_idag) continue;
  2377. else {
  2378. tmp_list[tmp_cnt++] = nodes_to_process[i];
  2379. nodes_to_process[i] = -1;
  2380. processed_cnt++;
  2381. }
  2382. }
  2383. }
  2384. if (tmp_idag != node_id){
  2385. if (core_inter_head[tmp_idag] == NULL){
  2386. core_inter_head[tmp_idag] = (inter_list *) malloc(sizeof(inter_list));
  2387. core_inter_tail[tmp_idag] = core_inter_head[tmp_idag];
  2388. } else {
  2389. printf("Adding to DDS not in high priority! Shouldn't I high prioritize? tmp = %d idag = %d\n",tmp_idag,node_id);
  2390. core_inter_tail[tmp_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  2391. core_inter_tail[tmp_idag] = core_inter_tail[tmp_idag]->next;
  2392. }
  2393. //printf("In ADD tmp_idag = %d tmp_cnt = %d sender_id = %d node_id = %d\n",tmp_idag,tmp_cnt,sender_id,node_id);
  2394. core_inter_tail[tmp_idag]->type = IDAG_ADD_CORES_DDS;
  2395. core_inter_tail[tmp_idag]->data.app_cores = (int *)malloc((tmp_cnt+2)*sizeof(int));
  2396. core_inter_tail[tmp_idag]->data.app_cores[0] = tmp_cnt;//+1;
  2397. core_inter_tail[tmp_idag]->data.app_cores[tmp_cnt+1] = sender_id;
  2398. for (j=1; j<= tmp_cnt; j++)
  2399. core_inter_tail[tmp_idag]->data.app_cores[j] = tmp_list[j-1];
  2400. core_inter_tail[tmp_idag]->next = NULL;
  2401. if (core_inter_head[tmp_idag]->next == NULL) {
  2402. //kill(pid_num[tmp_idag], SIG_ADD_CORES_DDS);
  2403. scc_kill(tmp_idag, SIG_ADD_CORES_DDS);
  2404. my_stats.msg_count++;
  2405. my_stats.distance += distance(node_id,tmp_idag);
  2406. }
  2407. } else {
  2408. //printf("In ADD same tmp_idag = %d tmp_cnt = %d nodes_cnt = %d sender_id = %d node_id = %d\n",tmp_idag,tmp_cnt,nodes_cnt,sender_id,node_id);
  2409. new_agent_id = sender_id;
  2410. for (i=0; i<tmp_cnt; i++){//nodes_cnt
  2411. fprintf(log_file,"I am importing node %d\n",tmp_list[i]);
  2412. fflush(log_file);
  2413. tmp_cores = my_cores;
  2414. while (tmp_cores->core_id != tmp_list[i])//nodes_to_process[i]
  2415. tmp_cores = tmp_cores->next;
  2416. if (tmp_cores->offered_to == -1) {
  2417. fprintf(log_file,"Node %d was offered to nobody\n",tmp_list[i]);
  2418. fflush(log_file);
  2419. DDS->num_of_cores--;
  2420. tmp_cores->offered_to = new_agent_id;
  2421. } else if (tmp_cores->offered_to != new_agent_id) {
  2422. fprintf(log_file,"Node %d is offered to %d\n",tmp_list[i],tmp_cores->offered_to);
  2423. fflush(log_file);
  2424. tmp_cores->offered_to = new_agent_id;
  2425. }
  2426. }
  2427. tmp_DDS = DDS;
  2428. while (tmp_DDS != NULL && tmp_DDS->agent_id != new_agent_id)
  2429. tmp_DDS = tmp_DDS->next;
  2430. if (tmp_DDS != NULL)
  2431. tmp_DDS->num_of_cores += nodes_cnt;
  2432. else {
  2433. //printf("I am in here DDSing nodes_cnt=%d\n",nodes_cnt);
  2434. DDS_tail->next = (DDS_list *) malloc(sizeof(DDS_list));
  2435. DDS_tail = DDS_tail->next;
  2436. DDS_tail->agent_id = new_agent_id;
  2437. DDS_tail->num_of_cores = nodes_cnt;
  2438. DDS_tail->next = NULL;
  2439. DDS_count++;
  2440. }
  2441. }
  2442. }
  2443. } else {
  2444. //read(fd_r, &nodes_cnt, sizeof(int));
  2445. nodes_cnt = data_array_local[0];
  2446. new_agent_id = data_array_local[1];
  2447. nodes_to_process = (int *) malloc(nodes_cnt * sizeof(int));
  2448. for (i=0; i<nodes_cnt; i++)
  2449. //read(fd_r, &nodes_to_process[i], sizeof(int));
  2450. nodes_to_process[i] = data_array_local[i+LINE_SIZE];
  2451. //read(fd_r, &new_agent_id, sizeof(int));
  2452. for (i=0; i<nodes_cnt; i++){
  2453. //printf("Node %d is %d\n",i,nodes_to_process[i]);
  2454. fprintf(log_file,"I am importing node %d\n",nodes_to_process[i]);
  2455. fflush(log_file);
  2456. tmp_cores = my_cores;
  2457. while (tmp_cores->core_id != nodes_to_process[i])
  2458. tmp_cores = tmp_cores->next;
  2459. if (tmp_cores->offered_to == -1) {
  2460. fprintf(log_file,"Node %d was offered to nobody\n",nodes_to_process[i]);
  2461. fflush(log_file);
  2462. DDS->num_of_cores--;
  2463. tmp_cores->offered_to = new_agent_id;
  2464. } else if (tmp_cores->offered_to != new_agent_id) {
  2465. fprintf(log_file,"Node %d is offered to %d\n",nodes_to_process[i],tmp_cores->offered_to);
  2466. fflush(log_file);
  2467. tmp_cores->offered_to = new_agent_id;
  2468. }
  2469. //tmp_cores->offered_to = new_agent_id;
  2470. }
  2471. tmp_DDS = DDS;
  2472. while (tmp_DDS != NULL && tmp_DDS->agent_id != new_agent_id)
  2473. tmp_DDS = tmp_DDS->next;
  2474. if (tmp_DDS != NULL)
  2475. tmp_DDS->num_of_cores += nodes_cnt;
  2476. else {
  2477. if (DDS_count == 1 && DDS != DDS_tail) printf("I am %d kai ta hpiame asxhma!\n",node_id);
  2478. DDS_tail->next = (DDS_list *) malloc(sizeof(DDS_list));
  2479. DDS_tail = DDS_tail->next;
  2480. DDS_tail->agent_id = new_agent_id;
  2481. DDS_tail->num_of_cores = nodes_cnt;
  2482. DDS_tail->next = NULL;
  2483. DDS_count++;
  2484. //printf("I did this with node_id = %d, new_agent_id = %d\n",node_id,new_agent_id);
  2485. }
  2486. }
  2487. my_stats.times_accessed++;
  2488. fprintf(log_file,"I am %d Adding ended well with sender_id=%d!\n",node_id,sender_id);
  2489. fprintf(log_file,"Number of agents in region = %d\n",DDS_count);
  2490. fprintf(log_file,"Agent no 0 is %d with %d cores\n",DDS->agent_id,DDS->num_of_cores);
  2491. i=1;
  2492. for (tmp_DDS = DDS->next; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next){
  2493. fprintf(log_file,"Agent no %d is %d with %d cores\n",i,tmp_DDS->agent_id,tmp_DDS->num_of_cores);
  2494. my_stats.cores_utilized += tmp_DDS->num_of_cores;
  2495. i++;
  2496. }
  2497. fflush(log_file);
  2498. cur_time = time(NULL);
  2499. cur_t = localtime(&cur_time);
  2500. fprintf(log_file, "[%d:%d:%d]: I ended sig_ADD_CORES_DDS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2501. fflush(log_file);
  2502. signals_enable();
  2503. }
  2504. void sig_REM_CORES_DDS_handler(int sender_id)
  2505. {
  2506. int i, is_sender_idag, j, new_agent_id;
  2507. int *nodes_to_process, nodes_cnt=0, processed_cnt=0;
  2508. int *tmp_list, tmp_cnt, tmp_idag;
  2509. char *fifo_name;
  2510. DDS_list *tmp_DDS,*tmp_DDS_prev;
  2511. int data_array_local[2 * LINE_SIZE], error, str_len;
  2512. //RCCE_FLAG_STATUS receiver_status;
  2513. char error_str[64];
  2514. signals_disable();
  2515. cur_time = time(NULL);
  2516. cur_t = localtime(&cur_time);
  2517. fprintf(log_file, "[%d:%d:%d]: I entered sig_REM_CORES_DDS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2518. fflush(log_file);
  2519. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  2520. scc_kill(sender_id, SIG_ACK);
  2521. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  2522. my_stats.msg_count++;
  2523. my_stats.distance += distance(node_id,sender_id);
  2524. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), 2 * LINE_SIZE * sizeof(int), node_id);
  2525. if (error != RCCE_SUCCESS) {
  2526. RCCE_error_string(error, error_str, &str_len);
  2527. printf("I am %d and i got an error in get data in sig_REM_CORES_DDS_handler from %d with descr %s\n",node_id,sender_id,error_str);
  2528. }
  2529. is_sender_idag = 0;
  2530. for (i=0; i<num_idags; i++)
  2531. if (idag_id_arr[i] == sender_id){
  2532. is_sender_idag = 1;
  2533. break;
  2534. }
  2535. //printf("I am in rem with is_sender_idag = %d\n",is_sender_idag);
  2536. if (is_sender_idag == 0){
  2537. nodes_cnt = data_array_local[0];
  2538. //read(fd_r, &nodes_cnt, sizeof(int));
  2539. nodes_to_process = (int *) malloc(nodes_cnt * sizeof(int));
  2540. for (i=0; i<nodes_cnt; i++)
  2541. //read(fd_r, &nodes_to_process[i], sizeof(int));
  2542. nodes_to_process[i] = data_array_local[i+LINE_SIZE];
  2543. tmp_list = (int *) malloc(nodes_cnt * sizeof(int));
  2544. while (processed_cnt < nodes_cnt){
  2545. tmp_cnt = 0;
  2546. tmp_idag = -1;
  2547. for (i=0; i<nodes_cnt; i++){
  2548. if (processed_cnt == nodes_cnt) break;
  2549. if (nodes_to_process[i] == -1) continue;
  2550. else {
  2551. if (tmp_idag == -1) tmp_idag = idag_mask[nodes_to_process[i]];
  2552. if (idag_mask[nodes_to_process[i]] != tmp_idag) continue;
  2553. else {
  2554. tmp_list[tmp_cnt++] = nodes_to_process[i];
  2555. nodes_to_process[i] = -1;
  2556. processed_cnt++;
  2557. }
  2558. }
  2559. }
  2560. if (tmp_idag != node_id){
  2561. if (core_inter_head[tmp_idag] == NULL){
  2562. core_inter_head[tmp_idag] = (inter_list *) malloc(sizeof(inter_list));
  2563. core_inter_tail[tmp_idag] = core_inter_head[tmp_idag];
  2564. } else {
  2565. printf("Removing from DDS not in high priority! Shouldn't I high prioritize? tmp = %d idag = %d\n",tmp_idag,node_id);
  2566. core_inter_tail[tmp_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  2567. core_inter_tail[tmp_idag] = core_inter_tail[tmp_idag]->next;
  2568. }
  2569. //printf("In REM tmp_idag = %d tmp_cnt = %d sender_id = %d idag_id = %d\n",tmp_idag,tmp_cnt,sender_id,idag_id);
  2570. core_inter_tail[tmp_idag]->type = IDAG_REM_CORES_DDS;
  2571. core_inter_tail[tmp_idag]->data.app_cores = (int *)malloc((tmp_cnt+2)*sizeof(int));
  2572. core_inter_tail[tmp_idag]->data.app_cores[0] = tmp_cnt;//+1;
  2573. core_inter_tail[tmp_idag]->data.app_cores[tmp_cnt+1] = sender_id;
  2574. for (j=1; j<= tmp_cnt; j++)
  2575. core_inter_tail[tmp_idag]->data.app_cores[j] = tmp_list[j-1];
  2576. core_inter_tail[tmp_idag]->next = NULL;
  2577. if (core_inter_head[tmp_idag]->next == NULL) {
  2578. //kill(pid_num[tmp_idag], SIG_REM_CORES_DDS);
  2579. scc_kill(tmp_idag, SIG_REM_CORES_DDS);
  2580. my_stats.msg_count++;
  2581. my_stats.distance += distance(node_id,tmp_idag);
  2582. }
  2583. } else {
  2584. fprintf(log_file,"In REM same tmp_idag = %d tmp_cnt = %d nodes_cnt = %d sender_id = %d \n",tmp_idag,tmp_cnt,nodes_cnt,sender_id);
  2585. new_agent_id = sender_id;
  2586. tmp_DDS = DDS;
  2587. tmp_DDS_prev = NULL;
  2588. while (tmp_DDS != NULL && tmp_DDS->agent_id != new_agent_id){
  2589. tmp_DDS_prev = tmp_DDS;
  2590. tmp_DDS = tmp_DDS->next;
  2591. }
  2592. if (tmp_DDS == NULL) printf("Agent does not exist in my DDS\n");
  2593. else if (tmp_DDS == DDS) printf("I am removing from myself in REM?\n");
  2594. else {
  2595. tmp_DDS->num_of_cores -= nodes_cnt;
  2596. if (tmp_DDS->num_of_cores == 0){
  2597. DDS_count--;
  2598. if (tmp_DDS == DDS_tail){
  2599. DDS_tail = tmp_DDS_prev;
  2600. DDS_tail->next = NULL;
  2601. } else tmp_DDS_prev->next = tmp_DDS->next;
  2602. free(tmp_DDS);
  2603. }
  2604. }
  2605. }
  2606. }
  2607. } else { //den afairw tous purhnes apo thn core list giati mporei na exoun hdh ginei add
  2608. nodes_cnt = data_array_local[0];
  2609. new_agent_id = data_array_local[1];
  2610. nodes_to_process = (int *) malloc(nodes_cnt * sizeof(int));
  2611. for (i=0; i<nodes_cnt; i++)
  2612. //read(fd_r, &nodes_to_process[i], sizeof(int));
  2613. nodes_to_process[i] = data_array_local[i+LINE_SIZE];
  2614. tmp_DDS = DDS;
  2615. tmp_DDS_prev = NULL;
  2616. while (tmp_DDS != NULL && tmp_DDS->agent_id != new_agent_id){
  2617. tmp_DDS_prev = tmp_DDS;
  2618. tmp_DDS = tmp_DDS->next;
  2619. }
  2620. if (tmp_DDS == NULL) printf("Agent does not exist in my DDS\n");
  2621. else if (tmp_DDS == DDS) printf("I am removing from myself in REM?\n");
  2622. else {
  2623. tmp_DDS->num_of_cores -= nodes_cnt;
  2624. if (tmp_DDS->num_of_cores == 0){
  2625. DDS_count--;
  2626. if (tmp_DDS == DDS_tail){
  2627. DDS_tail = tmp_DDS_prev;
  2628. DDS_tail->next = NULL;
  2629. } else tmp_DDS_prev->next = tmp_DDS->next;
  2630. free(tmp_DDS);
  2631. }
  2632. }
  2633. }
  2634. fprintf(log_file,"I am %d Removing ended well! with sender_id=%d!\n",node_id,sender_id);
  2635. fprintf(log_file,"Number of agents in region = %d\n",DDS_count);
  2636. i=0;
  2637. for (tmp_DDS = DDS; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next){
  2638. fprintf(log_file,"Agent no %d is %d with %d cores\n",i,tmp_DDS->agent_id,tmp_DDS->num_of_cores);
  2639. i++;
  2640. }
  2641. fflush(log_file);
  2642. cur_time = time(NULL);
  2643. cur_t = localtime(&cur_time);
  2644. fprintf(log_file, "[%d:%d:%d]: I ended sig_REM_CORES_DDS_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2645. fflush(log_file);
  2646. signals_enable();
  2647. }
  2648. void sig_APPOINT_WORK_handler(int sender_id)
  2649. {
  2650. int i, valid=0;
  2651. int data_array_local[LINE_SIZE], error, str_len;
  2652. //RCCE_FLAG_STATUS receiver_status;
  2653. char error_str[64];
  2654. signals_disable();
  2655. cur_time = time(NULL);
  2656. cur_t = localtime(&cur_time);
  2657. fprintf(log_file, "[%d:%d:%d]: I entered sig_APPOINT_WORK_handler with sender=%d and state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2658. fflush(log_file);
  2659. if (core_inter_head[sender_id] != NULL) {
  2660. //else {
  2661. printf("I am %d and i in am doing smth else with my agent %d in sig_APPOINT_WORK_handler interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  2662. /*tmp_inter_list = core_inter_head[sender_id];
  2663. while (tmp_inter_list != NULL) {
  2664. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  2665. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  2666. fprintf(log_file, "I dismissed type = %d of sender = %d\n",tmp_inter_list->type,sender_id);
  2667. fflush(log_file);
  2668. free(tmp_inter_list);
  2669. tmp_inter_list = core_inter_head[sender_id];
  2670. }*/
  2671. }
  2672. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  2673. scc_kill(sender_id, SIG_ACK);
  2674. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  2675. my_stats.msg_count++;
  2676. my_stats.distance += distance(node_id,sender_id);
  2677. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]),LINE_SIZE * sizeof(int), node_id);
  2678. if (error != RCCE_SUCCESS) {
  2679. RCCE_error_string(error, error_str, &str_len);
  2680. fprintf(log_file,"I got an error in get data in sig_APPOINT_WORK_handler from %d with descr %s\n",sender_id,error_str);
  2681. fflush(log_file);
  2682. }
  2683. //read(fd_r, &valid, sizeof(int));
  2684. valid = data_array_local[0];
  2685. fprintf(log_file, "Validity of message = %d\n",valid);
  2686. fflush(log_file);
  2687. if (valid == 1) {
  2688. if (my_agent == -1) my_agent = data_array_local[1];//read(fd_r, &my_agent, sizeof(int));// || my_agent != sender_id
  2689. else if (my_agent != sender_id) {
  2690. printf("I am %d and i do in appoint work switch of agent with upper_work_bound = %ld\n",node_id,upper_work_bound);
  2691. fprintf(log_file,"I do in appoint work switch of agent with upper_work_bound = %ld\n",upper_work_bound);
  2692. fflush(log_file);
  2693. //if (upper_work_bound > 0) {//case where my former agent has not collected my remaining work time
  2694. if (state == WORKING_NODE || state == WORKING_NODE_IDLE_INIT) {
  2695. if (core_inter_head[my_agent] == NULL){
  2696. core_inter_head[my_agent] = (inter_list *) malloc(sizeof(inter_list));
  2697. core_inter_tail[my_agent] = core_inter_head[my_agent];
  2698. } else {
  2699. core_inter_tail[my_agent]->next = (inter_list *) malloc(sizeof(inter_list));
  2700. core_inter_tail[my_agent] = core_inter_tail[my_agent]->next;
  2701. }
  2702. core_inter_tail[my_agent]->type = REP_CHK_REM_TIME;
  2703. core_inter_tail[my_agent]->data.work_time = upper_work_bound - time_worked;
  2704. core_inter_tail[my_agent]->next = NULL;
  2705. }
  2706. //read(fd_r, &my_agent, sizeof(int));
  2707. my_agent = data_array_local[1];
  2708. }
  2709. //else if (state != WORKING_NODE) printf("I have been initialied but i am not working\n");
  2710. //read(fd_r, &upper_work_bound, sizeof(long int));
  2711. upper_work_bound = data_array_local[2];
  2712. time_worked=0;
  2713. printf("I am node %d with agent %d I am going to start working for %ld\n",node_id,my_agent,upper_work_bound);
  2714. fprintf(log_file,"I am with agent %d I am going to start working for %ld\n",my_agent,upper_work_bound);
  2715. fflush(log_file);
  2716. if (upper_work_bound > 0) {
  2717. if (state == IDLE_INIT_MAN) state = WORKING_NODE_IDLE_INIT;
  2718. else if (state == INIT_MANAGER || state == INIT_MAN_CHK_OFFERS || state == INIT_MANAGER_SEND_OFFERS) {
  2719. pending_state = WORKING_NODE;
  2720. //printf("I am node %d and sou milaw gia mallia\n",node_id);
  2721. } else if (state != WORKING_NODE_IDLE_INIT) state = WORKING_NODE;
  2722. } else {
  2723. //kill(info->si_pid, SIG_FINISH);
  2724. scc_kill(sender_id, SIG_FINISH);
  2725. my_stats.msg_count++;
  2726. my_stats.distance += distance(node_id,sender_id);
  2727. }
  2728. }
  2729. //} else printf("I am %d and i in am doing smth else with my agent %d in sig_APPOINT_WORK_handler interaction is %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  2730. cur_time = time(NULL);
  2731. cur_t = localtime(&cur_time);
  2732. fprintf(log_file, "[%d:%d:%d]: I ended sig_APPOINT_WORK_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2733. fflush(log_file);
  2734. signals_enable();
  2735. }
  2736. void sig_CHECK_REM_TIME_handler(int sender_id)
  2737. {
  2738. int tmp_rem_time, i, time_per_node, time_left, time_to_work, one_core;
  2739. core_list *tmp_cores_list;
  2740. inter_list *tmp_inter_list;
  2741. float rem_workld;
  2742. int data_array_local[LINE_SIZE], error, str_len;
  2743. //RCCE_FLAG_STATUS receiver_status;
  2744. char error_str[64];
  2745. signals_disable();
  2746. cur_time = time(NULL);
  2747. cur_t = localtime(&cur_time);
  2748. fprintf(log_file, "[%d:%d:%d]: I entered sig_CHECK_REM_TIME_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2749. fflush(log_file);
  2750. if (core_inter_head[sender_id] == NULL && sender_id == my_agent){ //edw tha mpei otan exw allaksei agent
  2751. fprintf(log_file, "I sig_CHECK_REM_TIME_handler a\n");
  2752. fflush(log_file);
  2753. if (core_inter_head[sender_id] == NULL){
  2754. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  2755. core_inter_tail[sender_id] = core_inter_head[sender_id];
  2756. } else {
  2757. core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
  2758. core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
  2759. }
  2760. //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a1\n");
  2761. //fflush(log_file);
  2762. core_inter_tail[sender_id]->type = REP_CHK_REM_TIME;
  2763. core_inter_tail[sender_id]->data.work_time = upper_work_bound - time_worked;
  2764. if (core_inter_tail[sender_id]->data.work_time < 0) core_inter_tail[sender_id]->data.work_time = 0;
  2765. core_inter_tail[sender_id]->next = NULL;
  2766. //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a2\n");
  2767. //fflush(log_file);
  2768. upper_work_bound = 0;
  2769. if (state == WORKING_NODE) state = IDLE_CORE;
  2770. //kill(info->si_pid, SIG_CHECK_REM_TIME);
  2771. scc_kill(sender_id, SIG_CHECK_REM_TIME);
  2772. //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a3\n");
  2773. //fflush(log_file);
  2774. my_stats.msg_count++;
  2775. my_stats.distance += distance(node_id,sender_id);
  2776. //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a4\n");
  2777. //fflush(log_file);
  2778. } else if (core_inter_head[sender_id] == NULL && sender_id != my_agent) {
  2779. //I hope that this is where i end up when my former agent asks me for my remaining work but my original upper_work_bound was 0
  2780. fprintf(log_file, "I sig_CHECK_REM_TIME_handler c\n");
  2781. fflush(log_file);
  2782. if (core_inter_head[sender_id] == NULL){
  2783. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  2784. core_inter_tail[sender_id] = core_inter_head[sender_id];
  2785. } else {
  2786. core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
  2787. core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
  2788. }
  2789. core_inter_tail[sender_id]->type = REP_CHK_REM_TIME;
  2790. core_inter_tail[sender_id]->data.work_time = 0;
  2791. core_inter_tail[sender_id]->next = NULL;
  2792. //kill(info->si_pid, SIG_CHECK_REM_TIME);
  2793. scc_kill(sender_id, SIG_CHECK_REM_TIME);
  2794. my_stats.msg_count++;
  2795. my_stats.distance += distance(node_id,sender_id);
  2796. } else if (core_inter_head[sender_id]->type == REP_CHK_REM_TIME) {
  2797. fprintf(log_file, "I sig_CHECK_REM_TIME_handler b\n");
  2798. fflush(log_file);
  2799. //kill(info->si_pid, SIG_CHECK_REM_TIME);
  2800. scc_kill(sender_id, SIG_CHECK_REM_TIME);
  2801. my_stats.msg_count++;
  2802. my_stats.distance += distance(node_id,sender_id);
  2803. } else if (core_inter_head[sender_id]->type == APPOINT_WORK_NODE_PENDING || core_inter_head[sender_id]->type == REMOVED_NODE_REM_TIME) {//CHK_REM_TIME
  2804. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  2805. scc_kill(sender_id, SIG_ACK);
  2806. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  2807. my_stats.msg_count++;
  2808. my_stats.distance += distance(node_id,sender_id);
  2809. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]),LINE_SIZE * sizeof(int), node_id);
  2810. if (error != RCCE_SUCCESS) {
  2811. RCCE_error_string(error, error_str, &str_len);
  2812. fprintf(log_file,"I got an error in get data in sig_check_Rem_handler from %d with descr %s\n",sender_id,error_str);
  2813. fflush(log_file);
  2814. }
  2815. if (app_state != RESIZING) {
  2816. printf("I am %d in check rem and app_state = %d app_id = %d\n",node_id,app_state,my_app.id);
  2817. fprintf(log_file,"I am in check rem and app_state = %d app_id = %d\n",app_state,my_app.id);
  2818. fflush(log_file);
  2819. }
  2820. tmp_rem_time = data_array_local[0];
  2821. //read(fd_r, &tmp_rem_time, sizeof(int));
  2822. fprintf(app_log_file,"tmp_rem_time = %d sender_id = %d\n",tmp_rem_time,sender_id);
  2823. fflush(app_log_file);
  2824. my_stats.app_turnaround -= tmp_rem_time;
  2825. sum_rem_time += tmp_rem_time;
  2826. if (++chk_rem_count == chk_rem_num){
  2827. nodes_ended_cnt = 0;
  2828. rem_workld = sum_rem_time * old_Speedup;
  2829. time_to_work = (int) roundf(rem_workld / my_Speedup);
  2830. if (my_cores_count > 1) {
  2831. time_per_node = time_to_work / (my_cores_count-1);
  2832. time_left = time_to_work % (my_cores_count-1);
  2833. printf("i am %d in check_rem. sum_rem_time =%d rem_workld = %0.2f time_to_work =%d time_per_node = %d time_left = %d, my_cores_count = %d\n",
  2834. node_id,sum_rem_time,rem_workld, time_to_work,time_per_node,time_left,my_cores_count);
  2835. fprintf(app_log_file,"i am in check_rem. sum_rem_time =%d rem_workld = %0.2f time_to_work =%d time_per_node = %d time_left = %d, my_cores_count = %d\n",
  2836. sum_rem_time,rem_workld, time_to_work,time_per_node,time_left,my_cores_count);
  2837. fflush(app_log_file);
  2838. if (rem_workld > 0.0) {
  2839. if (time_to_work == 0) time_left++;
  2840. tmp_cores_list = my_cores->next;
  2841. while (tmp_cores_list != NULL) {
  2842. one_core = tmp_cores_list->core_id;
  2843. if (core_inter_head[one_core]->type != APPOINT_WORK_NODE_PENDING && core_inter_head[one_core]->type != INIT_WORK_NODE_PENDING) {
  2844. printf("Wrong fucking interaction core = %d interaction = %d\n",one_core,core_inter_head[one_core]->type);
  2845. tmp_inter_list = core_inter_head[one_core];
  2846. while (tmp_inter_list != NULL) {
  2847. if (tmp_inter_list->type == APPOINT_WORK_NODE_PENDING || tmp_inter_list->type == INIT_WORK_NODE_PENDING) break;
  2848. tmp_inter_list = tmp_inter_list->next;
  2849. }
  2850. if (tmp_inter_list == NULL) printf("I am %d and mas ksekwliase o %d\n",node_id,one_core);
  2851. } else tmp_inter_list = core_inter_head[one_core];
  2852. if (tmp_inter_list->type == APPOINT_WORK_NODE_PENDING) tmp_inter_list->type = APPOINT_WORK_NODE;
  2853. else tmp_inter_list->type = INIT_WORK_NODE;
  2854. tmp_inter_list->data.work_time = time_per_node;
  2855. if (time_left > 0) {
  2856. tmp_inter_list->data.work_time++;
  2857. time_left--;
  2858. }
  2859. if (tmp_inter_list->data.work_time > 0) {
  2860. my_stats.app_turnaround += tmp_inter_list->data.work_time;
  2861. if (core_inter_head[one_core]->type == APPOINT_WORK_NODE || core_inter_head[one_core]->type == INIT_WORK_NODE) {//||next == NULL
  2862. //kill(pid_num[one_core], SIG_APPOINT_WORK);
  2863. scc_kill(one_core, SIG_APPOINT_WORK);
  2864. my_stats.msg_count++;
  2865. my_stats.distance += distance(node_id,one_core);
  2866. } else printf("I am %d doing smth else with my working node %d in re appointing inter1 = %d inter2 = %d\n",node_id,one_core,core_inter_head[one_core]->type
  2867. ,core_inter_head[one_core]->next->type);
  2868. } else nodes_ended_cnt++;
  2869. /*core_inter_head[one_core]->data.work_time = time_per_node;
  2870. if (time_left > 0) {
  2871. core_inter_head[one_core]->data.work_time++;
  2872. time_left--;
  2873. }
  2874. if (core_inter_head[one_core]->data.work_time > 0) {
  2875. my_stats.app_turnaround += core_inter_head[one_core]->data.work_time;
  2876. //printf("sending core with id=%d\n",one_core);
  2877. if (core_inter_head[one_core]->next == NULL) {
  2878. kill(pid_num[one_core], SIG_APPOINT_WORK);
  2879. my_stats.msg_count++;
  2880. my_stats.distance += distance(node_id,one_core);
  2881. } else printf("I am %d doing smth else with my working node %d in re appointing inter1 = %d inter2 = %d\n",node_id,one_core,core_inter_head[one_core]->type
  2882. ,core_inter_head[one_core]->next->type);
  2883. } else nodes_ended_cnt++;*/
  2884. tmp_cores_list = tmp_cores_list->next;
  2885. }
  2886. app_state = RUNNING;
  2887. } else {
  2888. //app_terminated = 1;
  2889. app_state = APP_TERMINATED;
  2890. //state = AGENT_ENDING;
  2891. }
  2892. } else {
  2893. printf("I am %d and i am so stupid that i offered my only working core\n",node_id);
  2894. fprintf(app_log_file,"I have only one core state = %d and pending_state = %d\n",state,pending_state);
  2895. fflush(app_log_file);
  2896. if (selfopt_time_rem == -1 && state == IDLE_AGENT) state = AGENT_REWIND_FILE;
  2897. else if (pending_state == IDLE_AGENT) pending_state = AGENT_REWIND_FILE;
  2898. }
  2899. /*if (app_terminated) {
  2900. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  2901. if (core_inter_head[tmp_cores_list->core_id]->type == APPOINT_WORK_NODE_PENDING || core_inter_head[tmp_cores_list->core_id]->type == INIT_WORK_NODE_PENDING){
  2902. tmp_inter_list = core_inter_head[tmp_cores_list->core_id];
  2903. core_inter_head[tmp_cores_list->core_id] = core_inter_head[tmp_cores_list->core_id]->next;
  2904. free(tmp_inter_list);
  2905. printf("I am %d and i pre freed node %d\n",node_id,tmp_cores_list->core_id);
  2906. } else printf("I am %d and i tried to pre free but my interaction with my working node %d is %d\n",
  2907. node_id,tmp_cores_list->core_id,core_inter_head[tmp_cores_list->core_id]->type);
  2908. } else {*/
  2909. if (app_state != APP_TERMINATED) {//!app_terminated
  2910. if (pending_state == INIT_MANAGER) {
  2911. state = INIT_MANAGER;
  2912. pending_state = IDLE_AGENT;
  2913. } else {
  2914. if (selfopt_interval > 0) {
  2915. its.it_value.tv_nsec = selfopt_interval * MS;
  2916. selfopt_time_rem = selfopt_interval;
  2917. if (timer_settime(timerid, 0, &its, NULL) == -1) {
  2918. perror("timer_settime error8");
  2919. printf("I am %d timer_settime error8 selfopt_interval = %d\n",node_id,selfopt_interval);
  2920. }
  2921. } else selfopt_time_rem = -1;
  2922. }
  2923. }
  2924. }
  2925. if (core_inter_head[sender_id]->type == REMOVED_NODE_REM_TIME) {
  2926. fprintf(log_file,"I am with sender_id = %d and a\n",sender_id);
  2927. fflush(log_file);
  2928. tmp_inter_list = core_inter_head[sender_id];
  2929. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  2930. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  2931. else send_next_signal(core_inter_head[sender_id], sender_id);
  2932. free(tmp_inter_list);
  2933. fprintf(log_file,"I am with sender_id = %d and b\n",sender_id);
  2934. fflush(log_file);
  2935. }
  2936. } else if (sender_id == my_agent){
  2937. printf("I am %d and i am doing smth else with my agent %d in sig_CHECK_REM_TIME_handler interaction=%d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  2938. fprintf(log_file,"I am doing smth else with my agent %d in sig_CHECK_REM_TIME_handler interaction=%d\n",sender_id,core_inter_head[sender_id]->type);
  2939. fflush(log_file);
  2940. core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
  2941. core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
  2942. core_inter_tail[sender_id]->type = REP_CHK_REM_TIME;
  2943. core_inter_tail[sender_id]->data.work_time = upper_work_bound - time_worked;
  2944. if (core_inter_tail[sender_id]->data.work_time < 0) core_inter_tail[sender_id]->data.work_time = 0;
  2945. core_inter_tail[sender_id]->next = NULL;
  2946. //fprintf(log_file, "I sig_CHECK_REM_TIME_handler a2\n");
  2947. //fflush(log_file);
  2948. upper_work_bound = 0;
  2949. if (state == WORKING_NODE) state = IDLE_CORE;
  2950. } else {
  2951. printf("I am %d and i am doing smth else with random node %d in sig_CHECK_REM_TIME_handler interaction=%d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  2952. fprintf(log_file,"I am doing smth else with random node %d in sig_CHECK_REM_TIME_handler interaction=%d\n",sender_id,core_inter_head[sender_id]->type);
  2953. fflush(log_file);
  2954. //kill(info->si_pid, SIG_REJECT);
  2955. scc_kill(sender_id, SIG_REJECT);
  2956. my_stats.msg_count++;
  2957. my_stats.distance += distance(node_id,sender_id);
  2958. }
  2959. cur_time = time(NULL);
  2960. cur_t = localtime(&cur_time);
  2961. fprintf(log_file, "[%d:%d:%d]: I ended sig_CHECK_REM_TIME_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2962. fflush(log_file);
  2963. signals_enable();
  2964. }
  2965. void sig_FINISH_handler(int sender_id)
  2966. {
  2967. int i, is_sender_idag, new_agent_id;
  2968. int *nodes_to_process, nodes_cnt=0, processed_cnt=0;
  2969. int *tmp_list, tmp_cnt, tmp_idag;
  2970. core_list *tmp_cores_list;//tmp_cores,
  2971. DDS_list *tmp_DDS,*prev_DDS;
  2972. inter_list *tmp_inter_list;
  2973. int data_array_local[2 * LINE_SIZE], error, str_len;
  2974. //RCCE_FLAG_STATUS receiver_status;
  2975. char error_str[64];
  2976. signals_disable();
  2977. cur_time = time(NULL);
  2978. cur_t = localtime(&cur_time);
  2979. fprintf(log_file, "[%d:%d:%d]: I entered sig_FINISH_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  2980. fflush(log_file);
  2981. //if i am an idag, sig_finish should be proceed instantly, so i am dropping.
  2982. //On the other hand if an common node doesn't set its agent to -1 its not a big deal. (Hopefully)
  2983. if (core_inter_head[sender_id] != NULL && my_idag == -1) {
  2984. //else {
  2985. fprintf(log_file,"I am in sig finish i don't know what to do with interaction %d\n",core_inter_head[sender_id]->type);
  2986. fflush(log_file);
  2987. if (my_idag == -1) {
  2988. is_sender_idag = 0;
  2989. for (i=0; i<num_idags; i++)
  2990. if (idag_id_arr[i] == sender_id){
  2991. is_sender_idag = 1;
  2992. break;
  2993. }
  2994. if (!is_sender_idag) {
  2995. tmp_inter_list = core_inter_head[sender_id];
  2996. while (tmp_inter_list != NULL) {
  2997. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  2998. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  2999. fprintf(log_file, "I dismissed type = %d of sender = %d\n",tmp_inter_list->type,sender_id);
  3000. fflush(log_file);
  3001. free(tmp_inter_list);
  3002. tmp_inter_list = core_inter_head[sender_id];
  3003. }
  3004. }
  3005. }
  3006. }
  3007. if (my_idag != -1){
  3008. if (my_agent != -1 && my_agent == sender_id) my_agent = -1;//working node
  3009. if (++nodes_ended_cnt == (my_cores_count - 1)) app_state = APP_TERMINATED;//app_terminated = 1;
  3010. } else {
  3011. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  3012. scc_kill(sender_id, SIG_ACK);
  3013. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  3014. my_stats.msg_count++;
  3015. my_stats.distance += distance(node_id,sender_id);
  3016. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), 2 * LINE_SIZE * sizeof(int), node_id);
  3017. if (error != RCCE_SUCCESS) {
  3018. RCCE_error_string(error, error_str, &str_len);
  3019. fprintf(log_file,"I got an error in get data in sig_finish_handler from %d with descr %s\n",sender_id,error_str);
  3020. fflush(log_file);
  3021. }
  3022. is_sender_idag = 0;
  3023. for (i=0; i<num_idags; i++)
  3024. if (idag_id_arr[i] == sender_id){
  3025. is_sender_idag = 1;
  3026. break;
  3027. }
  3028. //printf("I am %d in sig finish with is_sender_idag = %d\n",node_id,is_sender_idag);
  3029. if (is_sender_idag == 0) {
  3030. //read(fd_r, &nodes_cnt, sizeof(int));
  3031. if (node_id == 0) {
  3032. num_apps_terminated++;
  3033. fprintf(log_file,"app_terminated = %d sender_id = %d\n",num_apps_terminated,sender_id);
  3034. fflush(log_file);
  3035. }
  3036. nodes_cnt = data_array_local[0];
  3037. nodes_to_process = (int *) malloc(nodes_cnt * sizeof(int));
  3038. for (i=0; i<nodes_cnt; i++)
  3039. //read(fd_r, &nodes_to_process[i], sizeof(int));
  3040. nodes_to_process[i] = data_array_local[i+1];//LINE_SIZE
  3041. tmp_list = (int *) malloc(nodes_cnt * sizeof(int));
  3042. fprintf(log_file,"nodes_cnt = %d\n",nodes_cnt);
  3043. fprintf(log_file,"I am node_id=%d Cores to be processed: ",node_id);
  3044. for (i=0; i<nodes_cnt; i++)
  3045. fprintf(log_file," %d",nodes_to_process[i]);
  3046. fprintf(log_file,"\n");
  3047. //printf("I am node_id=%d nodes_cnt = %d\n",node_id,nodes_cnt);
  3048. //fflush(log_file);
  3049. while (processed_cnt < nodes_cnt){
  3050. tmp_cnt = 0;
  3051. tmp_idag = -1;
  3052. for (i=0; i<nodes_cnt; i++){
  3053. if (processed_cnt == nodes_cnt) break;
  3054. if (nodes_to_process[i] == -1) continue;
  3055. else {
  3056. if (tmp_idag == -1) tmp_idag = idag_mask[nodes_to_process[i]];
  3057. if (idag_mask[nodes_to_process[i]] != tmp_idag) continue;
  3058. else {
  3059. tmp_list[tmp_cnt++] = nodes_to_process[i];
  3060. nodes_to_process[i] = -1;
  3061. processed_cnt++;
  3062. }
  3063. }
  3064. }
  3065. if (tmp_idag != node_id){
  3066. if (core_inter_head[tmp_idag] == NULL){
  3067. core_inter_head[tmp_idag] = (inter_list *) malloc(sizeof(inter_list));
  3068. core_inter_tail[tmp_idag] = core_inter_head[tmp_idag];
  3069. } else {
  3070. printf("finish agent DDS not in high priority! Shouldn't I high prioritize? tmp = %d idag = %d\n",tmp_idag,node_id);
  3071. core_inter_tail[tmp_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  3072. core_inter_tail[tmp_idag] = core_inter_tail[tmp_idag]->next;
  3073. }
  3074. //printf("node_id = %d In finish tmp_idag = %d tmp_cnt = %d sender_id = %d is_sender_idag=%d tmp_list[0]=%d\n",node_id,tmp_idag,tmp_cnt,sender_id,is_sender_idag,tmp_list[0]);
  3075. core_inter_tail[tmp_idag]->type = REMOVE_APP;
  3076. core_inter_tail[tmp_idag]->data.agent_ended = sender_id;
  3077. /*core_inter_tail[tmp_idag]->data.app_cores = (int *)malloc((tmp_cnt+2)*sizeof(int));
  3078. core_inter_tail[tmp_idag]->data.app_cores[0] = tmp_cnt;//+1;
  3079. core_inter_tail[tmp_idag]->data.app_cores[tmp_cnt+1] = sender_id;
  3080. for (j=1; j<= tmp_cnt; j++)
  3081. core_inter_tail[tmp_idag]->data.app_cores[j] = tmp_list[j-1];
  3082. */
  3083. core_inter_tail[tmp_idag]->next = NULL;
  3084. if (core_inter_head[tmp_idag]->next == NULL) {
  3085. //kill(pid_num[tmp_idag], SIG_FINISH);
  3086. scc_kill(tmp_idag, SIG_FINISH);
  3087. my_stats.msg_count++;
  3088. my_stats.distance += distance(node_id,tmp_idag);
  3089. }
  3090. } else {
  3091. fprintf(log_file,"In FINISH same tmp_idag = %d tmp_cnt = %d nodes_cnt = %d sender_id = %d node_id = %d\n",tmp_idag,tmp_cnt,nodes_cnt,sender_id,node_id);
  3092. new_agent_id = sender_id;
  3093. /*for (i=0; i<tmp_cnt; i++){
  3094. tmp_cores = my_cores;
  3095. while (tmp_cores->core_id != tmp_list[i])//nodes_to_process[i]
  3096. tmp_cores = tmp_cores->next;
  3097. if (tmp_cores->offered_to == new_agent_id) tmp_cores->offered_to = -1;
  3098. }
  3099. DDS->num_of_cores += tmp_cnt;*/
  3100. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  3101. if (tmp_cores_list->offered_to == new_agent_id) {
  3102. tmp_cores_list->offered_to = -1;
  3103. DDS->num_of_cores++;
  3104. }
  3105. prev_DDS = DDS;
  3106. tmp_DDS = DDS->next;
  3107. while (tmp_DDS != NULL && tmp_DDS->agent_id != new_agent_id){
  3108. prev_DDS = tmp_DDS;
  3109. tmp_DDS = tmp_DDS->next;
  3110. }
  3111. if (tmp_DDS != NULL){
  3112. DDS_count--;
  3113. prev_DDS->next = tmp_DDS->next;
  3114. if (tmp_DDS->next == NULL) DDS_tail = prev_DDS;
  3115. free(tmp_DDS);
  3116. } else printf("Fuck agent that finished is not in my DDS\n");
  3117. fprintf(log_file,"My removal of agent complete node_id = %d sender_id=%d\n",node_id,sender_id);
  3118. fprintf(log_file,"Number of agents in region = %d\n",DDS_count);
  3119. i=0;
  3120. for (tmp_DDS = DDS; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next){
  3121. fprintf(log_file,"Agent no %d is %d with %d cores\n",i,tmp_DDS->agent_id,tmp_DDS->num_of_cores);
  3122. i++;
  3123. }
  3124. fflush(log_file);
  3125. }
  3126. }
  3127. free(nodes_to_process);
  3128. free(tmp_list);
  3129. } else {
  3130. /*read(fd_r, &nodes_cnt, sizeof(int));
  3131. printf("I am %d and secondary node cnt by %d is %d\n",node_id,sender_id,nodes_cnt);
  3132. nodes_to_process = (int *) malloc(nodes_cnt * sizeof(int));
  3133. for (i=0; i<nodes_cnt; i++)
  3134. read(fd_r, &nodes_to_process[i], sizeof(int));
  3135. read(fd_r, &new_agent_id, sizeof(int));
  3136. ////sem_post(&node_sem[sender_id]);
  3137. printf("Secondary Cores to be processed of %d by %d: ",new_agent_id,node_id);
  3138. for (i=0; i<nodes_cnt; i++)
  3139. printf(" %d",nodes_to_process[i]);
  3140. printf("\n");
  3141. for (i=0; i<nodes_cnt; i++){
  3142. tmp_cores = my_cores;
  3143. while (tmp_cores->core_id != nodes_to_process[i])//nodes_to_process[i]
  3144. tmp_cores = tmp_cores->next;
  3145. if (tmp_cores->offered_to == new_agent_id) tmp_cores->offered_to = -1;
  3146. }
  3147. DDS->num_of_cores += nodes_cnt;
  3148. */
  3149. //read(fd_r, &new_agent_id, sizeof(int));
  3150. new_agent_id = data_array_local[0];
  3151. fprintf(log_file,"I am %d in Secondary sig_finish for %d\n",node_id,new_agent_id);
  3152. fflush(log_file);
  3153. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  3154. if (tmp_cores_list->offered_to == new_agent_id) {
  3155. tmp_cores_list->offered_to = -1;
  3156. DDS->num_of_cores++;
  3157. }
  3158. prev_DDS = DDS;
  3159. tmp_DDS = DDS->next;
  3160. while (tmp_DDS != NULL && tmp_DDS->agent_id != new_agent_id){
  3161. prev_DDS = tmp_DDS;
  3162. tmp_DDS = tmp_DDS->next;
  3163. }
  3164. if (tmp_DDS != NULL){
  3165. DDS_count--;
  3166. prev_DDS->next = tmp_DDS->next;
  3167. if (tmp_DDS->next == NULL) DDS_tail = prev_DDS;
  3168. free(tmp_DDS);
  3169. } else printf("Fuck agent that finished is not in my DDS\n");
  3170. //free(nodes_to_process);
  3171. /*my_stats.times_accessed++;
  3172. printf("I am %d Adding ended well with sender_id=%d!\n",node_id,sender_id);
  3173. printf("Number of agents in region = %d\n",DDS_count);
  3174. printf("Agent no 0 is %d with %d cores\n",DDS->agent_id,DDS->num_of_cores);
  3175. i=1;
  3176. for (tmp_DDS = DDS->next; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next){
  3177. printf("Agent no %d is %d with %d cores\n",i,tmp_DDS->agent_id,tmp_DDS->num_of_cores);
  3178. my_stats.cores_utilized += tmp_DDS->num_of_cores;
  3179. i++;
  3180. }*/
  3181. fprintf(log_file,"Secondary removal of agent complete node_id =%d sender_id=%d\n",node_id,sender_id);
  3182. /*fprintf(log_file,"Number of agents in region = %d\n",DDS_count);
  3183. i=0;
  3184. for (tmp_DDS = DDS; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next){
  3185. fprintf(log_file,"Agent no %d is %d with %d cores\n",i,tmp_DDS->agent_id,tmp_DDS->num_of_cores);
  3186. i++;
  3187. }
  3188. fflush(log_file);*/
  3189. }
  3190. }
  3191. fprintf(log_file,"Number of agents in region = %d\n",DDS_count);
  3192. i=0;
  3193. for (tmp_DDS = DDS; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next){
  3194. fprintf(log_file,"Agent no %d is %d with %d cores\n",i,tmp_DDS->agent_id,tmp_DDS->num_of_cores);
  3195. i++;
  3196. }
  3197. fflush(log_file);
  3198. cur_time = time(NULL);
  3199. cur_t = localtime(&cur_time);
  3200. fprintf(log_file, "[%d:%d:%d]: I ended sig_FINISH_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  3201. fflush(log_file);
  3202. signals_enable();
  3203. }
  3204. void sig_REJECT_handler(int sender_id)
  3205. {
  3206. int agent_id, i, j;
  3207. inter_list *tmp_inter_list;
  3208. core_list *tmp_cores_list;
  3209. target_list *tmp_target_list;
  3210. offer_list *tmp_offer_list;
  3211. signals_disable();
  3212. cur_time = time(NULL);
  3213. cur_t = localtime(&cur_time);
  3214. fprintf(log_file, "[%d:%d:%d]: I entered sig_REJECT_handler with sender=%d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  3215. fflush(log_file);
  3216. if (core_inter_head[sender_id] != NULL){
  3217. fprintf(log_file, "[%d:%d:%d]: Interaction with sender=%d is %d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,core_inter_head[sender_id]->type);
  3218. fflush(log_file);
  3219. }
  3220. if (core_inter_head[sender_id] == NULL) { //edw tha mpei otan exw allaksei agent
  3221. printf("I am %d in sig_reject and i have null interaction with sender %d\n",node_id,sender_id);
  3222. } else if (core_inter_head[sender_id]->type == IDAG_REQ_DDS || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS || core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS
  3223. || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING || core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING) {
  3224. if (core_inter_head[sender_id]->type == IDAG_REQ_DDS || core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING) {
  3225. init_DDS_replies++;
  3226. fprintf(log_file, "One init_req_dds has been rejected by %d!\n",sender_id);
  3227. fflush(log_file);
  3228. /*if (init_areas_num == 1) {
  3229. fprintf(log_file, "My only init area was rejected. my state is %d my pending_state = %d\n",state,pending_state);
  3230. fflush(log_file);
  3231. state = INIT_MANAGER_SEND_OFFERS;
  3232. }*/
  3233. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING) {
  3234. selfopt_DDS_replies++;
  3235. fprintf(log_file, "One selfopt_req_dds has been rejected by %d!\n",sender_id);
  3236. fflush(log_file);
  3237. }
  3238. if ((core_inter_head[sender_id]->type == IDAG_REQ_DDS || core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING)
  3239. && init_DDS_replies == init_DDS_idags && init_idags_areas_replies == init_near_areas_num)
  3240. for (tmp_target_list = init_targets_head; tmp_target_list != NULL; tmp_target_list = tmp_target_list->next){
  3241. agent_id = tmp_target_list->target;
  3242. for (tmp_inter_list = core_inter_head[agent_id]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
  3243. if (tmp_inter_list->type == FAR_INIT_REQ || tmp_inter_list->type == FAR_REQ_MAN) break;
  3244. if (tmp_inter_list == NULL) {
  3245. if (core_inter_head[agent_id] == NULL){
  3246. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  3247. core_inter_tail[agent_id] = core_inter_head[agent_id];
  3248. } else {
  3249. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  3250. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  3251. }
  3252. core_inter_tail[agent_id]->type = AGENT_REQ_CORES_PENDING;
  3253. core_inter_tail[agent_id]->data.reg_arr.num_of_regions = tmp_target_list->num_of_regions;
  3254. core_inter_tail[agent_id]->data.reg_arr.region_arr = (region *) malloc(tmp_target_list->num_of_regions * sizeof(region));
  3255. if (tmp_target_list->target == node_id) printf("Why is this here node_id=%d\n",node_id);
  3256. fprintf(log_file,"Init req target = %d, num_of_regions = %d. Αreas:",tmp_target_list->target,tmp_target_list->num_of_regions);
  3257. for (i=0; i<tmp_target_list->num_of_regions; i++) {
  3258. core_inter_tail[agent_id]->data.reg_arr.region_arr[i] = tmp_target_list->region_arr[i];
  3259. fprintf(log_file," (%d,%d),",tmp_target_list->region_arr[i].C,tmp_target_list->region_arr[i].r);
  3260. }
  3261. fprintf(log_file,"\n");
  3262. fflush(log_file);
  3263. core_inter_tail[agent_id]->next = NULL;
  3264. //kill(pid_num[agent_id], SIG_REQ_CORES);
  3265. if (core_inter_head[agent_id]->next == NULL) {
  3266. //kill(pid_num[agent_id], SIG_REQ_CORES);
  3267. scc_kill(agent_id, SIG_REQ_CORES);
  3268. my_stats.msg_count++;
  3269. my_stats.distance += distance(node_id,agent_id);
  3270. } else if (agent_id == sender_id && core_inter_head[agent_id]->next->type == AGENT_REQ_CORES_PENDING) { //den exei fugei apo to free alla einai wra na stalei
  3271. scc_kill(agent_id, SIG_REQ_CORES);
  3272. //kill(pid_num[agent_id], SIG_REQ_CORES);
  3273. my_stats.msg_count++;
  3274. my_stats.distance += distance(node_id,agent_id);
  3275. } else printf("This init fucker is not NULL interaction=%d interaction2=%d\n",core_inter_head[agent_id]->type,core_inter_head[agent_id]->next->type);
  3276. } else
  3277. printf("I am %d and i did not send local requests to %d because he is my far manager\n",node_id,agent_id);
  3278. }
  3279. else if ((core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING) && selfopt_DDS_replies == selfopt_DDS_idags)
  3280. for (tmp_target_list = selfopt_targets_head; tmp_target_list != NULL; tmp_target_list = tmp_target_list->next){
  3281. agent_id = tmp_target_list->target;
  3282. if (core_inter_head[agent_id] == NULL){
  3283. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  3284. core_inter_tail[agent_id] = core_inter_head[agent_id];
  3285. } else {
  3286. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  3287. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  3288. }
  3289. core_inter_tail[agent_id]->type = SELFOPT_REQ_CORES_PENDING;
  3290. core_inter_tail[agent_id]->data.reg_arr.num_of_regions = tmp_target_list->num_of_regions;
  3291. core_inter_tail[agent_id]->data.reg_arr.region_arr = (region *) malloc(tmp_target_list->num_of_regions * sizeof(region));
  3292. if (tmp_target_list->target == node_id) printf("selfopt Why is this here node_id=%d\n",node_id);
  3293. fprintf(log_file,"Selfopt req target = %d, num_of_regions = %d. Αreas:",tmp_target_list->target,tmp_target_list->num_of_regions);
  3294. for (i=0; i<tmp_target_list->num_of_regions; i++) {
  3295. core_inter_tail[agent_id]->data.reg_arr.region_arr[i] = tmp_target_list->region_arr[i];
  3296. fprintf(log_file," (%d,%d),",tmp_target_list->region_arr[i].C,tmp_target_list->region_arr[i].r);
  3297. }
  3298. fprintf(log_file,"\n");
  3299. fflush(log_file);
  3300. core_inter_tail[agent_id]->next = NULL;
  3301. if (core_inter_head[agent_id]->next == NULL) {
  3302. //kill(pid_num[agent_id], SIG_REQ_CORES);
  3303. scc_kill(agent_id, SIG_REQ_CORES);
  3304. my_stats.msg_count++;
  3305. my_stats.distance += distance(node_id,agent_id);
  3306. } else if (agent_id == sender_id && core_inter_head[agent_id]->next->type == SELFOPT_REQ_CORES_PENDING) {
  3307. //kill(pid_num[agent_id], SIG_REQ_CORES);
  3308. scc_kill(agent_id, SIG_REQ_CORES);
  3309. my_stats.msg_count++;
  3310. my_stats.distance += distance(node_id,agent_id);
  3311. } else printf("This selfopt fucker is not NULL interaction=%d interaction2=%d\n",core_inter_head[agent_id]->type,core_inter_head[agent_id]->next->type);
  3312. }
  3313. tmp_inter_list = core_inter_head[sender_id];
  3314. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  3315. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  3316. else if (core_inter_head[sender_id]->type != AGENT_REQ_CORES_PENDING && core_inter_head[sender_id]->type != SELFOPT_REQ_CORES_PENDING)//far_req_max_man != sender_id &&
  3317. send_next_signal(core_inter_head[sender_id], sender_id);
  3318. free(tmp_inter_list);
  3319. } else if (core_inter_head[sender_id]->type == FAR_INIT_IDAG_REQ_DDS || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS || core_inter_head[sender_id]->type == FAR_REQ_CORES
  3320. || core_inter_head[sender_id]->type == AGENT_REQ_CORES_PENDING || core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS_PENDING
  3321. || core_inter_head[sender_id]->type == IDAG_FIND_IDAGS_PENDING || core_inter_head[sender_id]->type == SELFOPT_IDAG_FIND_IDAGS_PENDING
  3322. || core_inter_head[sender_id]->type == SELFOPT_REQ_CORES_PENDING || core_inter_head[sender_id]->type == FAR_REQ_CORES_PENDING
  3323. || core_inter_head[sender_id]->type == REP_FAR_INIT_REQ) {
  3324. tmp_inter_list = core_inter_head[sender_id];
  3325. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  3326. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  3327. else send_next_signal(core_inter_head[sender_id], sender_id);
  3328. free(tmp_inter_list);
  3329. /*} else if (core_inter_head[sender_id]->type == FAR_REQ_OFFER) {//FAR_REQ_OFFER_SENT
  3330. if (far_man_offers != NULL && far_man_offers->sender == node_id) {
  3331. tmp_cores_list = my_cores;
  3332. while (tmp_cores_list != NULL){
  3333. if (tmp_cores_list->offered_to == sender_id) tmp_cores_list->offered_to = -1;
  3334. tmp_cores_list = tmp_cores_list->next;
  3335. }
  3336. tmp_offer_list = far_man_offers;
  3337. far_man_offers = far_man_offers->next;
  3338. free(tmp_offer_list);
  3339. }
  3340. while (far_man_offers != NULL){
  3341. printf("I am node %d in reject and far offer answer for node %d is 0\n",node_id,far_man_offers->sender);
  3342. *far_man_offers->answer = 0;
  3343. if (core_inter_head[far_man_offers->sender]->type == REP_AGENT_OFFER_PENDING){
  3344. kill(pid_num[far_man_offers->sender],SIG_REP_OFFERS);
  3345. core_inter_head[far_man_offers->sender]->type = REP_AGENT_OFFER_SENT;
  3346. } else printf("I am %d and Apparently lists are poutana ola\n",node_id);
  3347. tmp_offer_list = far_man_offers;
  3348. far_man_offers = far_man_offers->next;
  3349. free(tmp_offer_list);
  3350. }
  3351. far_req_app.A=-1.0;
  3352. far_req_app.var=-1.0;
  3353. far_req_app.num_of_cores=-1;
  3354. far_req_or_sender = -1;
  3355. far_reg.C = -1;
  3356. far_reg.r = -1;
  3357. tmp_inter_list = core_inter_head[sender_id];
  3358. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  3359. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  3360. else send_next_signal(core_inter_head[sender_id], sender_id);
  3361. free(tmp_inter_list);
  3362. */
  3363. } else if (core_inter_head[sender_id]->type == REP_IDAG_FIND_IDAGS) {
  3364. free(core_inter_head[sender_id]->data.idags_in_reg);
  3365. tmp_inter_list = core_inter_head[sender_id];
  3366. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  3367. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  3368. else send_next_signal(core_inter_head[sender_id], sender_id);
  3369. free(tmp_inter_list);
  3370. } else if (core_inter_head[sender_id]->type == REP_IDAG_REQ_DDS) {
  3371. free(core_inter_head[sender_id]->data.agents_in_reg);
  3372. tmp_inter_list = core_inter_head[sender_id];
  3373. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  3374. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  3375. else send_next_signal(core_inter_head[sender_id], sender_id);
  3376. free(tmp_inter_list);
  3377. /*} else if (core_inter_head[sender_id]->type == FAR_REQ_MAN_APPOINT) {
  3378. agent_id = core_inter_head[sender_id]->data.far_req.orig_sender;
  3379. if (core_inter_head[agent_id] == NULL){
  3380. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  3381. core_inter_tail[agent_id] = core_inter_head[agent_id];
  3382. } else {
  3383. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  3384. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  3385. }
  3386. core_inter_tail[agent_id]->type = ABORT_FAR_MAN;
  3387. core_inter_tail[agent_id]->data.far_req_man = sender_id;
  3388. core_inter_tail[agent_id]->next = NULL;
  3389. if (core_inter_head[agent_id]->next == NULL) {
  3390. kill(pid_num[agent_id], SIG_REMOVE_FAR_MAN);
  3391. my_stats.msg_count++;
  3392. my_stats.distance += distance(node_id,agent_id);
  3393. }
  3394. tmp_inter_list = core_inter_head[sender_id];
  3395. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  3396. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  3397. else send_next_signal(core_inter_head[sender_id], sender_id);
  3398. free(tmp_inter_list); */
  3399. } else if (core_inter_head[sender_id]->type == REP_AGENT_REQ_CORES) {
  3400. for (j=0; j<core_inter_head[sender_id]->data.off_arr.num_of_offers; j++)
  3401. if (my_cores != NULL)
  3402. for (i=0; i<core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores; i++)
  3403. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  3404. if (tmp_cores_list->core_id == core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i]) {
  3405. tmp_cores_list->offered_to = -1;
  3406. break;
  3407. }
  3408. free(core_inter_head[sender_id]->data.off_arr.offer_arr);
  3409. tmp_inter_list = core_inter_head[sender_id];
  3410. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  3411. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  3412. else send_next_signal(core_inter_head[sender_id], sender_id);
  3413. free(tmp_inter_list);
  3414. } else if (core_inter_head[sender_id]->type == INIT_APP) {
  3415. printf("I am 0 and %d rejected my init_app req\n",sender_id);
  3416. fprintf(log_file,"I am 0 and %d rejected my init_app req\n",sender_id);
  3417. fflush(log_file);
  3418. if (init_pending_head == NULL){
  3419. init_pending_head = (inter_list *) malloc(sizeof(inter_list));
  3420. init_pending_tail = init_pending_head;
  3421. } else {
  3422. init_pending_tail->next = (inter_list *) malloc(sizeof(inter_list));
  3423. init_pending_tail = init_pending_tail->next;
  3424. }
  3425. init_pending_tail->type = INIT_APP;
  3426. init_pending_tail->data.new_app = core_inter_head[sender_id]->data.new_app;
  3427. init_pending_tail->data.new_app.num_of_cores = sender_id;
  3428. init_pending_tail->next = NULL;
  3429. tmp_inter_list = core_inter_head[sender_id];
  3430. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  3431. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  3432. else send_next_signal(core_inter_head[sender_id], sender_id);
  3433. free(tmp_inter_list);
  3434. } else if (core_inter_head[sender_id]->type == REP_CHK_REM_TIME) {
  3435. printf("I am %d and %d rejected my REP_CHK_REM_TIME with work_time = %d\n",node_id,sender_id,core_inter_head[sender_id]->data.work_time);
  3436. fprintf(log_file,"I am and %d rejected my REP_CHK_REM_TIME with work_time = %d\n",sender_id,core_inter_head[sender_id]->data.work_time);
  3437. fflush(log_file);
  3438. tmp_inter_list = core_inter_head[sender_id];
  3439. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  3440. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  3441. else send_next_signal(core_inter_head[sender_id], sender_id);
  3442. free(tmp_inter_list);
  3443. } else {
  3444. printf("I am %d in sig_reject and i have interaction with sender %d interaction = %d\n",node_id,sender_id,core_inter_head[sender_id]->type);
  3445. fprintf(log_file,"I am in sig_reject and i have interaction with sender interaction = %d\n",core_inter_head[sender_id]->type);
  3446. fflush(log_file);
  3447. }
  3448. cur_time = time(NULL);
  3449. cur_t = localtime(&cur_time);
  3450. fprintf(log_file, "[%d:%d:%d]: I ended sig_REJECT_handler with sender = %d state=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,state);
  3451. fflush(log_file);
  3452. signals_enable();
  3453. }