signal_handlers.c~ 173 KB

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