signal_handlers.c 176 KB

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