signal_handlers.c 169 KB

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