signal_handlers.c 173 KB

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