signal_handlers.c 174 KB

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