signal_handlers.c 167 KB

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