signal_handlers.c 164 KB

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