signal_handlers.c 169 KB

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