signal_handlers.c 133 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926
  1. #include "signal_handlers.h"
  2. extern int num_idags, node_id ,my_idag;//, 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. my_stats.msg_count++;
  241. my_stats.distance += distance(node_id,sender_id);
  242. } else {
  243. //kill(info->si_pid, SIG_ACK);
  244. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  245. scc_kill(sender_id, SIG_ACK);
  246. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  247. //scc_kill(sender_id, SIG_ACK);
  248. my_stats.msg_count++;
  249. my_stats.distance += distance(node_id,sender_id);
  250. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), LINE_SIZE * sizeof(int), node_id);
  251. if (error != RCCE_SUCCESS) {
  252. RCCE_error_string(error, error_str, &str_len);
  253. 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);
  254. }
  255. init_app.id = data_array_local[0];
  256. memcpy(&init_app.A,&data_array_local[1],sizeof(int));
  257. memcpy(&init_app.var,&data_array_local[2],sizeof(int));
  258. memcpy(&init_app.workld,&data_array_local[3],sizeof(int));
  259. init_app.num_of_cores = data_array_local[4];
  260. //read(fd_r, &init_app, sizeof(app));
  261. printf("I am node with id %d and my app is A = %f, var = %f, cores = %d\n",node_id,init_app.A,init_app.var,init_app.num_of_cores);
  262. cur_time = time(NULL);
  263. cur_t = localtime(&cur_time);
  264. init_app_times[0].tm_sec = cur_t->tm_sec;
  265. init_app_times[0].tm_min = cur_t->tm_min;
  266. init_app_times[0].tm_hour = cur_t->tm_hour;
  267. printf("I am %d and about to do an init app with my state before change = %d\n",node_id,state);
  268. /*if (state == IDLE_AGENT_WAITING_OFF || state == AGENT_SELF_CHK_OFFERS || state == AGENT_ZOMBIE || state == AGENT_ENDING)
  269. pending_state = INIT_MANAGER;
  270. else if (state == IDLE_AGENT) {
  271. if (timer_gettime(timerid, &chk_timer) == -1) perror("timer_gettime error init\n");
  272. else selfopt_time_rem = chk_timer.it_value.tv_nsec;
  273. its.it_value.tv_nsec = 0;
  274. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error init\n");
  275. pending_state = IDLE_AGENT;
  276. state = INIT_MANAGER;
  277. } else if (state == WORKING_NODE || state == AGENT_SELF_OPT) {
  278. pending_state = state;
  279. state = INIT_MANAGER;
  280. } else state = INIT_MANAGER;*/
  281. state = INIT_MANAGER;
  282. }
  283. cur_time = time(NULL);
  284. cur_t = localtime(&cur_time);
  285. 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);
  286. fflush(log_file);
  287. signals_enable();
  288. }
  289. void sig_TIMER_handler(int signo, siginfo_t *info, void *context)
  290. {
  291. cur_time = time(NULL);
  292. cur_t = localtime(&cur_time);
  293. 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);
  294. fflush(log_file);
  295. if (state == IDLE_INIT_MAN) state = INIT_MAN_CHK_OFFERS;
  296. else if (state == IDLE_FAR_MAN) state = FAR_MAN_CHK_OFFERS;
  297. else if (state == IDLE_AGENT_WAITING_OFF) state = AGENT_SELF_CHK_OFFERS;
  298. else if (state == IDLE_AGENT) state = AGENT_SELF_OPT;
  299. else if (state == IDLE_CHK_APP_FILE) state = CHK_APP_FILE;
  300. else if (state == IDLE_INIT_MAN_SELFOPT_PENDING) state = INIT_MAN_CHK_OFFERS_SELFOPT_PENDING;
  301. else if (state == IDLE_INIT_MAN_WORK_PENDING) state = INIT_MAN_CHK_OFFERS_WORK_PENDING;
  302. else if (state == WORKING_NODE_IDLE_INIT) {
  303. state = INIT_MAN_CHK_OFFERS;
  304. pending_state = WORKING_NODE;
  305. }
  306. else if (state == AGENT_INIT_STATE) {
  307. if (pending_state == IDLE_INIT_MAN) pending_state = INIT_MAN_CHK_OFFERS;
  308. }
  309. else printf("i am %d, timer went off and i don't know what to do. My state is %d\n",node_id,state);
  310. cur_time = time(NULL);
  311. cur_t = localtime(&cur_time);
  312. 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);
  313. fflush(log_file);
  314. }
  315. void sig_INIT_handler(int sender_id)
  316. {
  317. int data_array_local[LINE_SIZE];
  318. int error, str_len;
  319. //RCCE_FLAG_STATUS receiver_status;
  320. char error_str[64];
  321. signals_disable();
  322. //kill(info->si_pid, SIG_ACK);
  323. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  324. scc_kill(sender_id, SIG_ACK);
  325. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  326. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), LINE_SIZE * sizeof(int), node_id);
  327. if (error != RCCE_SUCCESS) {
  328. RCCE_error_string(error, error_str, &str_len);
  329. 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);
  330. } else my_idag = data_array_local[0];
  331. my_stats.msg_count++; //gia to sig_ACK
  332. my_stats.distance += distance(node_id,my_idag);
  333. printf("I am node with id %d my idle agent is %d\n",node_id,my_idag);
  334. cur_time = time(NULL);
  335. cur_t = localtime(&cur_time);
  336. 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());
  337. fflush(log_file);
  338. signals_enable();
  339. }
  340. void sig_ACK_handler(int sender_id)
  341. {
  342. int clear=1, i, j, data_array_local[3 * LINE_SIZE];//, fd_r;//fd_r, ; clear is 1 if node is to be removed
  343. inter_list *tmp_inter_list;
  344. DDS_list *tmp_DDS;//, *tmp_inter_prev=NULL;
  345. offer_list *tmp_offer_list;
  346. int error, str_len;
  347. //RCCE_FLAG_STATUS receiver_status;
  348. char error_str[64];
  349. signals_disable();
  350. if (core_inter_head[sender_id] == NULL){
  351. printf("We were fucked in sig_ACK! sender_id = %d node_id = %d\n",sender_id,node_id);
  352. } else {
  353. cur_time = time(NULL);
  354. cur_t = localtime(&cur_time);
  355. fprintf(log_file, "[%d:%d:%d]: I entered sig_ACK_handler with sender=%d type=%d state=%d\n",
  356. cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,core_inter_head[sender_id]->type,state);
  357. fflush(log_file);
  358. tmp_inter_list = core_inter_head[sender_id];
  359. if (tmp_inter_list->type == INIT_CORE){
  360. //write_res = write(fd_w, &node_id, sizeof(int));
  361. data_array_local[0] = node_id;
  362. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), LINE_SIZE * sizeof(int), sender_id);
  363. if (error != RCCE_SUCCESS) {
  364. RCCE_error_string(error, error_str, &str_len);
  365. printf("I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  366. }
  367. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  368. fprintf(log_file, "node_id=%d\n",node_id);
  369. fflush(log_file);
  370. my_stats.message_size += sizeof(int);
  371. nodes_initialised++;
  372. } else if (tmp_inter_list->type == INIT_APP){
  373. //write_res = write(fd_w, &tmp_inter_list->data.new_app, sizeof(app));
  374. //float tmpf;
  375. data_array_local[0] = tmp_inter_list->data.new_app.id;
  376. memcpy(&data_array_local[1],&tmp_inter_list->data.new_app.A,sizeof(int));
  377. memcpy(&data_array_local[2],&tmp_inter_list->data.new_app.var,sizeof(int));
  378. memcpy(&data_array_local[3],&tmp_inter_list->data.new_app.workld,sizeof(int));
  379. data_array_local[4] = tmp_inter_list->data.new_app.num_of_cores;
  380. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), LINE_SIZE * sizeof(int), sender_id);
  381. if (error != RCCE_SUCCESS) {
  382. RCCE_error_string(error, error_str, &str_len);
  383. printf("I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  384. }
  385. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  386. /*memcpy(&tmpf,&data_array_local[1],sizeof(int));
  387. fprintf(log_file, "A=%0.2f\n",tmpf);
  388. fflush(log_file);
  389. memcpy(&tmpf,&data_array_local[2],sizeof(int));
  390. fprintf(log_file, "var=%0.2f\n",tmpf);
  391. fflush(log_file);
  392. memcpy(&tmpf,&data_array_local[3],sizeof(int));
  393. fprintf(log_file, "workld=%0.2f\n",tmpf);
  394. fflush(log_file);*/
  395. my_stats.message_size += sizeof(app);
  396. } 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
  397. data_array_local[0] = tmp_inter_list->data.reg.C;
  398. data_array_local[1] = tmp_inter_list->data.reg.r;
  399. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), LINE_SIZE * sizeof(int), sender_id);
  400. if (error != RCCE_SUCCESS) {
  401. RCCE_error_string(error, error_str, &str_len);
  402. printf("I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  403. }
  404. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  405. //write_res = write(fd_w, &tmp_inter_list->data.reg, sizeof(region));
  406. fprintf(log_file, "C=%d r=%d\n",tmp_inter_list->data.reg.C,tmp_inter_list->data.reg.r);
  407. fflush(log_file);
  408. if (tmp_inter_list->type == IDAG_FIND_IDAGS_PENDING) tmp_inter_list->type = IDAG_FIND_IDAGS;
  409. else if (tmp_inter_list->type == SELFOPT_IDAG_FIND_IDAGS_PENDING) tmp_inter_list->type = SELFOPT_IDAG_FIND_IDAGS;
  410. my_stats.message_size += sizeof(region);
  411. clear = 0;
  412. } else if (core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING || core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS_PENDING
  413. || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING || core_inter_head[sender_id]->type == DEBUG_IDAG_REQ_DDS){
  414. data_array_local[0] = tmp_inter_list->data.reg.C;
  415. data_array_local[1] = tmp_inter_list->data.reg.r;
  416. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), LINE_SIZE * sizeof(int), sender_id);
  417. if (error != RCCE_SUCCESS) {
  418. RCCE_error_string(error, error_str, &str_len);
  419. printf("I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  420. }
  421. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  422. //write_res = write(fd_w, &tmp_inter_list->data.reg, sizeof(region));
  423. fprintf(log_file, "C=%d r=%d\n",tmp_inter_list->data.reg.C,tmp_inter_list->data.reg.r);
  424. fflush(log_file);
  425. my_stats.message_size += sizeof(region);
  426. if (core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING) core_inter_head[sender_id]->type = IDAG_REQ_DDS;
  427. 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;
  428. else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING) core_inter_head[sender_id]->type = SELFOPT_IDAG_REQ_DDS;
  429. clear = 0;
  430. } else if (tmp_inter_list->type == REP_IDAG_FIND_IDAGS) {//I am the idag
  431. //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
  432. data_array_local[0] = tmp_inter_list->data.idags_in_reg[num_idags_x*num_idags_y];
  433. fprintf(log_file, "num_of_idags=%d\n",tmp_inter_list->data.idags_in_reg[num_idags_x*num_idags_y]);
  434. fflush(log_file);
  435. my_stats.message_size += sizeof(int);
  436. j=1;
  437. for (i=0; i<num_idags_x*num_idags_y; i++)
  438. if (tmp_inter_list->data.idags_in_reg[i]) {
  439. //write_res = write(fd_w, &idag_id_arr[i], sizeof(int)); //matching of increasing number to node_id
  440. data_array_local[j] = idag_id_arr[i];
  441. j++;
  442. fprintf(log_file, "idag=%d\n",idag_id_arr[i]);
  443. fflush(log_file);
  444. my_stats.message_size += sizeof(int);
  445. }
  446. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), LINE_SIZE * sizeof(int), sender_id);
  447. if (error != RCCE_SUCCESS) {
  448. RCCE_error_string(error, error_str, &str_len);
  449. printf("I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  450. }
  451. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  452. } else if (tmp_inter_list->type == REP_IDAG_REQ_DDS) {//I am the idag
  453. if (tmp_inter_list->data.agents_in_reg == NULL){//debugging
  454. fprintf(log_file, "In null rep_idag_dds with sender %d and DDS_count %d\n",sender_id,DDS_count);
  455. fflush(log_file);
  456. //write_res = write(fd_w, &DDS_count, sizeof(int)); //number of idags in region
  457. data_array_local[0] = DDS_count;
  458. data_array_local[1] = 0;
  459. i=2;
  460. tmp_DDS = DDS;
  461. while(tmp_DDS != NULL){
  462. //write_res = write(fd_w, &tmp_DDS->agent_id, sizeof(int));
  463. //write_res = write(fd_w, &tmp_DDS->num_of_cores, sizeof(int));
  464. data_array_local[i++] = tmp_DDS->agent_id;
  465. data_array_local[i++] = tmp_DDS->num_of_cores;
  466. tmp_DDS = tmp_DDS->next;
  467. }
  468. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), 2 * LINE_SIZE * sizeof(int), sender_id);
  469. if (error != RCCE_SUCCESS) {
  470. RCCE_error_string(error, error_str, &str_len);
  471. 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);
  472. fflush(log_file);
  473. }
  474. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  475. } else {
  476. //write_res = write(fd_w, &tmp_inter_list->data.agents_in_reg[0], sizeof(int));
  477. data_array_local[0] = tmp_inter_list->data.agents_in_reg[0];
  478. data_array_local[1] = 0;
  479. fprintf(log_file, "num_of_agents=%d\n",tmp_inter_list->data.agents_in_reg[0]);
  480. fflush(log_file);
  481. my_stats.message_size += sizeof(int);
  482. j=2;
  483. for (i=1; i<=2*tmp_inter_list->data.agents_in_reg[0]; i+=2){
  484. /*write_res = write(fd_w, &tmp_inter_list->data.agents_in_reg[i], sizeof(int));
  485. fprintf(log_file, "agent=%d size=%d write_res=%d\n",tmp_inter_list->data.agents_in_reg[i],sizeof(int),write_res);
  486. fflush(log_file);
  487. write_res = write(fd_w, &tmp_inter_list->data.agents_in_reg[i+1], sizeof(int));//offset may be different!!!!!!
  488. fprintf(log_file, "cores=%d size=%d write_res=%d\n",tmp_inter_list->data.agents_in_reg[i+1],sizeof(int),write_res);
  489. fflush(log_file);*/
  490. 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]);
  491. fflush(log_file);
  492. data_array_local[j++] = tmp_inter_list->data.agents_in_reg[i];
  493. data_array_local[j++] = tmp_inter_list->data.agents_in_reg[i+1];
  494. my_stats.message_size += 2 * sizeof(int);
  495. }
  496. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), 2 * LINE_SIZE * sizeof(int), sender_id);
  497. if (error != RCCE_SUCCESS) {
  498. RCCE_error_string(error, error_str, &str_len);
  499. 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);
  500. fflush(log_file);
  501. }
  502. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  503. }
  504. } else if (tmp_inter_list->type == AGENT_REQ_CORES_PENDING){
  505. //write_res = write(fd_w, &init_app, sizeof(app));
  506. fprintf(log_file, "A=%0.2f\n",init_app.A);
  507. fflush(log_file);
  508. my_stats.message_size += sizeof(app);
  509. data_array_local[0] = init_app.id;
  510. memcpy(&data_array_local[1],&init_app.A,sizeof(int));
  511. memcpy(&data_array_local[2],&init_app.var,sizeof(int));
  512. memcpy(&data_array_local[3],&init_app.workld,sizeof(int));
  513. data_array_local[4] = init_app.num_of_cores;
  514. //write_res = write(fd_w, &tmp_inter_list->data.reg_arr.num_of_regions, sizeof(int));
  515. data_array_local[5] = tmp_inter_list->data.reg_arr.num_of_regions;
  516. fprintf(log_file, "num_of_regions=%d\n",tmp_inter_list->data.reg_arr.num_of_regions);
  517. fflush(log_file);
  518. if (tmp_inter_list->data.reg_arr.num_of_regions > 1) {
  519. fprintf(log_file, "In init ack i have num_of_regions = %d\n",tmp_inter_list->data.reg_arr.num_of_regions);
  520. fflush(log_file);
  521. }
  522. for (i=0; i<tmp_inter_list->data.reg_arr.num_of_regions; i++) {
  523. //write_res = write(fd_w, &tmp_inter_list->data.reg_arr.region_arr[i], sizeof(region));
  524. 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);
  525. fflush(log_file);
  526. data_array_local[6] = tmp_inter_list->data.reg_arr.region_arr[i].C;
  527. data_array_local[7] = tmp_inter_list->data.reg_arr.region_arr[i].r;
  528. my_stats.message_size += sizeof(region);
  529. }
  530. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]),LINE_SIZE * sizeof(int), sender_id);
  531. if (error != RCCE_SUCCESS) {
  532. RCCE_error_string(error, error_str, &str_len);
  533. 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);
  534. fflush(log_file);
  535. }
  536. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  537. tmp_inter_list->type = AGENT_REQ_CORES;
  538. free(tmp_inter_list->data.reg_arr.region_arr);
  539. clear = 0;
  540. } else if (tmp_inter_list->type == SELFOPT_REQ_CORES_PENDING){
  541. //write_res = write(fd_w, &my_app, sizeof(app));
  542. fprintf(log_file, "A=%0.2f num_of_cores = %d\n",my_app.A,my_app.num_of_cores);
  543. fflush(log_file);
  544. my_stats.message_size += sizeof(app);
  545. data_array_local[0] = my_app.id;
  546. memcpy(&data_array_local[1],&my_app.A,sizeof(int));
  547. memcpy(&data_array_local[2],&my_app.var,sizeof(int));
  548. memcpy(&data_array_local[3],&my_app.workld,sizeof(int));
  549. data_array_local[4] = my_app.num_of_cores;
  550. //write_res = write(fd_w, &tmp_inter_list->data.reg_arr.num_of_regions, sizeof(int));
  551. data_array_local[5] = tmp_inter_list->data.reg_arr.num_of_regions;
  552. fprintf(log_file, "num_of_regions=%d\n",tmp_inter_list->data.reg_arr.num_of_regions);
  553. fflush(log_file);
  554. if (tmp_inter_list->data.reg_arr.num_of_regions > 1) {
  555. fprintf(log_file, "In selfopt ack i have num_of_regions = %d\n",tmp_inter_list->data.reg_arr.num_of_regions);
  556. fflush(log_file);
  557. }
  558. for (i=0; i<tmp_inter_list->data.reg_arr.num_of_regions; i++) {
  559. //write_res = write(fd_w, &tmp_inter_list->data.reg_arr.region_arr[i], sizeof(region));
  560. data_array_local[6] = tmp_inter_list->data.reg_arr.region_arr[i].C;
  561. data_array_local[7] = tmp_inter_list->data.reg_arr.region_arr[i].r;
  562. 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);
  563. fflush(log_file);
  564. my_stats.message_size += sizeof(region);
  565. }
  566. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]),LINE_SIZE * sizeof(int), sender_id);
  567. if (error != RCCE_SUCCESS) {
  568. RCCE_error_string(error, error_str, &str_len);
  569. 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);
  570. fflush(log_file);
  571. }
  572. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  573. tmp_inter_list->type = SELFOPT_REQ_CORES;
  574. free(tmp_inter_list->data.reg_arr.region_arr);
  575. clear = 0;
  576. /*} else if (tmp_inter_list->type == FAR_REQ_CORES_PENDING) {//I am the requesting common node
  577. write_res = write(fd_w, &far_req_app, sizeof(app));
  578. fprintf(log_file, "A=%0.2f size=%d write_res=%d\n",far_req_app.A,sizeof(app),write_res);
  579. fflush(log_file);
  580. my_stats.message_size += sizeof(app);
  581. i = 1;
  582. write_res = write(fd_w, &i, sizeof(int));
  583. fprintf(log_file, "num_of_regions=%d size=%d write_res=%d\n",i,sizeof(int),write_res);
  584. fflush(log_file);
  585. my_stats.message_size += sizeof(int);
  586. write_res = write(fd_w, &tmp_inter_list->data.reg, sizeof(region));
  587. 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);
  588. fflush(log_file);
  589. my_stats.message_size += sizeof(region);
  590. tmp_inter_list->type = FAR_REQ_CORES;
  591. clear = 0;*/
  592. } else if (tmp_inter_list->type == REP_AGENT_REQ_CORES) {//I am the agent
  593. //write_res = write(fd_w, &tmp_inter_list->data.off_arr.num_of_offers, sizeof(int));
  594. data_array_local[0] = tmp_inter_list->data.off_arr.num_of_offers;
  595. fprintf(log_file, "num_of_offers=%d\n",tmp_inter_list->data.off_arr.num_of_offers);
  596. fflush(log_file);
  597. my_stats.message_size += sizeof(int);
  598. if (tmp_inter_list->data.off_arr.num_of_offers > 0) {
  599. for (j=0; j<tmp_inter_list->data.off_arr.num_of_offers; j++){
  600. //write_res = write(fd_w, &tmp_inter_list->data.off_arr.offer_arr[j].num_of_cores, sizeof(int));
  601. data_array_local[1] = tmp_inter_list->data.off_arr.offer_arr[j].num_of_cores;
  602. fprintf(log_file, "num_of_cores=%d\n",tmp_inter_list->data.off_arr.offer_arr[j].num_of_cores);
  603. fflush(log_file);
  604. my_stats.message_size += sizeof(int);
  605. //write_res = write(fd_w, &tmp_inter_list->data.off_arr.offer_arr[j].spd_loss, sizeof(float));
  606. memcpy(&data_array_local[2],&tmp_inter_list->data.off_arr.offer_arr[j].spd_loss,sizeof(int));
  607. fprintf(log_file, "spd_loss=%0.2f\n",tmp_inter_list->data.off_arr.offer_arr[j].spd_loss);
  608. fflush(log_file);
  609. my_stats.message_size += sizeof(float);
  610. for (i=0; i<tmp_inter_list->data.off_arr.offer_arr[j].num_of_cores; i++) {
  611. data_array_local[i+LINE_SIZE] = tmp_inter_list->data.off_arr.offer_arr[j].offered_cores[i];
  612. //write_res = write(fd_w, &tmp_inter_list->data.off_arr.offer_arr[j].offered_cores[i], sizeof(int));
  613. fprintf(log_file, "core=%d\n",tmp_inter_list->data.off_arr.offer_arr[j].offered_cores[i]);
  614. fflush(log_file);
  615. my_stats.message_size += sizeof(int);
  616. }
  617. }
  618. /*error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), 2 * LINE_SIZE * sizeof(int), sender_id);
  619. if (error != RCCE_SUCCESS) {
  620. RCCE_error_string(error, error_str, &str_len);
  621. 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);
  622. fflush(log_file);
  623. }
  624. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);*/
  625. core_inter_head[sender_id]->type = AGENT_OFFER_SENT;
  626. clear = 0;
  627. }
  628. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), 2 * LINE_SIZE * sizeof(int), sender_id);
  629. if (error != RCCE_SUCCESS) {
  630. RCCE_error_string(error, error_str, &str_len);
  631. 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);
  632. fflush(log_file);
  633. }
  634. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  635. } else if (tmp_inter_list->type == REP_AGENT_OFFER_SENT) {
  636. fprintf(log_file, "I have to reply %d for %d offers\n",sender_id,tmp_inter_list->data.offer_acc_array[0]);
  637. fflush(log_file);
  638. my_stats.message_size += sizeof(int);
  639. for (i=1; i<=tmp_inter_list->data.offer_acc_array[0]; i++){
  640. //write_res = write(fd_w, &tmp_inter_list->data.offer_acc_array[i], sizeof(int));
  641. data_array_local[i-1] = tmp_inter_list->data.offer_acc_array[i];
  642. fprintf(log_file, "offer_ans=%d\n",tmp_inter_list->data.offer_acc_array[i]);
  643. fflush(log_file);
  644. my_stats.message_size += sizeof(int);
  645. }
  646. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), LINE_SIZE * sizeof(int), sender_id);
  647. if (error != RCCE_SUCCESS) {
  648. RCCE_error_string(error, error_str, &str_len);
  649. 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);
  650. fflush(log_file);
  651. }
  652. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  653. fflush(log_file);
  654. free(tmp_inter_list->data.offer_acc_array);
  655. } else if (tmp_inter_list->type == INIT_AGENT) {
  656. //write_res = write(fd_w, &init_app, sizeof(app));
  657. data_array_local[0] = init_app.id;
  658. memcpy(&data_array_local[1],&init_app.A,sizeof(int));
  659. memcpy(&data_array_local[2],&init_app.var,sizeof(int));
  660. memcpy(&data_array_local[3],&init_app.workld,sizeof(int));
  661. data_array_local[4] = init_app.num_of_cores;
  662. fprintf(log_file, "A=%0.2f\n",init_app.A);
  663. fflush(log_file);
  664. my_stats.message_size += sizeof(app);
  665. for (i=1; i<=init_app.num_of_cores; i++){
  666. //write_res = write(fd_w, &tmp_inter_list->data.app_cores[i], sizeof(int));
  667. data_array_local[LINE_SIZE+i-1] = tmp_inter_list->data.app_cores[i];
  668. fprintf(log_file, "core=%d\n",tmp_inter_list->data.app_cores[i]);
  669. fflush(log_file);
  670. my_stats.message_size += sizeof(int);
  671. }
  672. data_array_local[2*LINE_SIZE] = init_app_times[0].tm_sec;
  673. data_array_local[2*LINE_SIZE+1] = init_app_times[0].tm_min;
  674. data_array_local[2*LINE_SIZE+2] = init_app_times[0].tm_hour;
  675. data_array_local[2*LINE_SIZE+3] = init_app_times[1].tm_sec;
  676. data_array_local[2*LINE_SIZE+4] = init_app_times[1].tm_min;
  677. data_array_local[2*LINE_SIZE+5] = init_app_times[1].tm_hour;
  678. //write_res = write(fd_w, &init_app_times[0], sizeof(my_time_stamp));
  679. 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);
  680. fflush(log_file);
  681. //write_res = write(fd_w, &init_app_times[1], sizeof(my_time_stamp));
  682. 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);
  683. fflush(log_file);
  684. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]),3 * LINE_SIZE * sizeof(int), sender_id);
  685. if (error != RCCE_SUCCESS) {
  686. RCCE_error_string(error, error_str, &str_len);
  687. 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);
  688. fflush(log_file);
  689. }
  690. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  691. init_app.A=-1.0;
  692. init_app.var=-1.0;
  693. init_app.num_of_cores=-1;
  694. init_app_times[0].tm_sec = 0;
  695. init_app_times[0].tm_min = 0;
  696. init_app_times[0].tm_hour = 0;
  697. init_app_times[1].tm_sec = 0;
  698. init_app_times[1].tm_min = 0;
  699. init_app_times[1].tm_hour = 0;
  700. //kill(pid_num[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. fprintf(log_file, "app_cores=%d size=%d write_res=%d\n",tmp_inter_list->data.app_cores[0],sizeof(int),write_res);
  709. fflush(log_file);
  710. my_stats.message_size += sizeof(int);
  711. for (i=1; i<=tmp_inter_list->data.app_cores[0]; i++){
  712. write_res = write(fd_w, &tmp_inter_list->data.app_cores[i], sizeof(int));
  713. fprintf(log_file, "core=%d size=%d write_res=%d\n",tmp_inter_list->data.app_cores[i],sizeof(int),write_res);
  714. fflush(log_file);
  715. my_stats.message_size += sizeof(int);
  716. }
  717. //I am an idag and i have to send to other idags my original sender
  718. if (my_idag == -1) {//idag_id != -1
  719. write_res = write(fd_w, &tmp_inter_list->data.app_cores[i], sizeof(int));
  720. fprintf(log_file, "orig_sender=%d size=%d write_res=%d\n",tmp_inter_list->data.app_cores[i],sizeof(int),write_res);
  721. fflush(log_file);
  722. my_stats.message_size += sizeof(int);
  723. }
  724. } else if (tmp_inter_list->type == REMOVE_APP) {
  725. //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]);
  726. //fflush(log_file);
  727. if (my_idag != -1) {
  728. write_res = write(fd_w, &tmp_inter_list->data.app_cores[0], sizeof(int));
  729. fprintf(log_file, "app_cores=%d size=%d write_res=%d\n",tmp_inter_list->data.app_cores[0],sizeof(int),write_res);
  730. fflush(log_file);
  731. my_stats.message_size += sizeof(int);
  732. for (i=1; i<=tmp_inter_list->data.app_cores[0]; i++){
  733. write_res = write(fd_w, &tmp_inter_list->data.app_cores[i], sizeof(int));
  734. fprintf(log_file, "core=%d size=%d write_res=%d\n",tmp_inter_list->data.app_cores[i],sizeof(int),write_res);
  735. fflush(log_file);
  736. my_stats.message_size += sizeof(int);
  737. }
  738. } else {
  739. //I am an idag and i have to send to other idags my original sender
  740. write_res = write(fd_w, &tmp_inter_list->data.agent_ended, sizeof(int));
  741. fprintf(log_file, "orig_sender=%d size=%d write_res=%d\n",tmp_inter_list->data.agent_ended,sizeof(int),write_res);
  742. fflush(log_file);
  743. my_stats.message_size += sizeof(int);
  744. }
  745. } else if (tmp_inter_list->type == FAR_INIT_REQ) {//I am the requesting common node
  746. write_res = write(fd_w, &init_app, sizeof(app));
  747. fprintf(log_file, "A=%0.2f size=%d write_res=%d\n",init_app.A,sizeof(app),write_res);
  748. fflush(log_file);
  749. my_stats.message_size += sizeof(app);
  750. write_res = write(fd_w, &tmp_inter_list->data.reg, sizeof(region));
  751. 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);
  752. fflush(log_file);
  753. my_stats.message_size += sizeof(region);
  754. clear = 0;
  755. } else if (tmp_inter_list->type == FAR_REQ_MAN_APPOINT) {//I am the requesting common node
  756. //fprintf(log_file, "I am appointing %d for far manager of %d with region (%d,%d)\n",
  757. // 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);
  758. //fflush(log_file);
  759. write_res = write(fd_w, &tmp_inter_list->data.far_req.orig_sender, sizeof(int));
  760. fprintf(log_file, "orig_sender=%d size=%d write_res=%d\n",tmp_inter_list->data.far_req.orig_sender,sizeof(int),write_res);
  761. fflush(log_file);
  762. my_stats.message_size += sizeof(int);
  763. write_res = write(fd_w, &tmp_inter_list->data.far_req.far_app, sizeof(app));
  764. 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);
  765. fflush(log_file);
  766. my_stats.message_size += sizeof(app);
  767. write_res = write(fd_w, &tmp_inter_list->data.far_req.reg, sizeof(region));
  768. 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);
  769. fflush(log_file);
  770. my_stats.message_size += sizeof(region);
  771. 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
  772. fprintf(log_file, "I enter this case\n");
  773. fflush(log_file);
  774. core_inter_head[sender_id] = tmp_inter_list->next;
  775. free(tmp_inter_list);
  776. for (tmp_inter_list = core_inter_head[sender_id]; tmp_inter_list != NULL; tmp_inter_list = tmp_inter_list->next)
  777. fprintf (log_file, "Interaction is %d\n",tmp_inter_list->type);
  778. fflush(log_file);
  779. clear = 0;
  780. }
  781. } else if (tmp_inter_list->type == REP_FAR_INIT_REQ){
  782. write_res = write(fd_w, &tmp_inter_list->data.far_req_man, sizeof(int));
  783. fprintf(log_file, "far manager=%d size=%d write_res=%d\n",tmp_inter_list->data.far_req_man,sizeof(int),write_res);
  784. fflush(log_file);
  785. my_stats.message_size += sizeof(int);
  786. //printf("i am %d in asdfwe and far_req_man is %d\n",node_id,tmp_inter_list->data.far_req_man);
  787. //if (core_inter_head[tmp_inter_list->data.far_req_man] == NULL) printf("kariolares\n");
  788. if (tmp_inter_list->data.far_req_man != node_id && core_inter_head[tmp_inter_list->data.far_req_man] != NULL &&
  789. core_inter_head[tmp_inter_list->data.far_req_man]->type == FAR_REQ_MAN_APPOINT_PENDING) {//next == NULL) {
  790. kill(pid_num[tmp_inter_list->data.far_req_man], SIG_FAR_REQ);
  791. core_inter_head[tmp_inter_list->data.far_req_man]->type = FAR_REQ_MAN_APPOINT;
  792. fprintf(log_file, "I sent it\n");
  793. fflush(log_file);
  794. my_stats.msg_count++;
  795. my_stats.distance += distance(node_id,tmp_inter_list->data.far_req_man);
  796. } else {
  797. fprintf(log_file, "far_man=%d\n",tmp_inter_list->data.far_req_man);
  798. 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);
  799. else fprintf(log_file, "no interaction\n");
  800. fflush(log_file);
  801. }
  802. } else if (tmp_inter_list->type == ABORT_FAR_MAN) {
  803. write_res = write(fd_w, &tmp_inter_list->data.far_req_man, sizeof(int));
  804. fprintf(log_file, "far_req_man=%d size=%d write_res=%d\n",tmp_inter_list->data.far_req_man,sizeof(int),write_res);
  805. fflush(log_file);
  806. my_stats.message_size += sizeof(int);
  807. //printf("i am %d in asdfwe and far_req_man is %d\n",node_id,tmp_inter_list->data.far_req_man);
  808. //if (core_inter_head[tmp_inter_list->data.far_req_man] == NULL) printf("kariolares\n");
  809. } else if (tmp_inter_list->type == FAR_REQ_OFFER) {//I am the agent
  810. //printf("far_list_count = %d\n",far_list_count);
  811. write_res = write(fd_w, &far_list_count, sizeof(int));
  812. fprintf(log_file, "far_list_count=%d size=%d write_res=%d\n",far_list_count,sizeof(int),write_res);
  813. fflush(log_file);
  814. my_stats.message_size += sizeof(int);
  815. if (far_list_count > 0) {
  816. tmp_offer_list = far_man_offers;
  817. while (tmp_offer_list != NULL){
  818. write_res = write(fd_w, &tmp_offer_list->off.num_of_cores, sizeof(int));
  819. fprintf(log_file, "num_of_cores=%d size=%d write_res=%d\n",tmp_offer_list->off.num_of_cores,sizeof(int),write_res);
  820. fflush(log_file);
  821. my_stats.message_size += sizeof(int);
  822. write_res = write(fd_w, &tmp_offer_list->off.spd_loss, sizeof(float));
  823. fprintf(log_file, "spd_loss=%0.2f size=%d write_res=%d\n",tmp_offer_list->off.spd_loss,sizeof(float),write_res);
  824. fflush(log_file);
  825. my_stats.message_size += sizeof(float);
  826. for (i=0; i<tmp_offer_list->off.num_of_cores; i++) {
  827. //printf ("Offering core = %d\n",tmp_inter_list->data.my_offer.offered_cores[i]);
  828. write_res = write(fd_w, &tmp_offer_list->off.offered_cores[i], sizeof(int));
  829. fprintf(log_file, "core=%d size=%d write_res=%d\n",tmp_offer_list->off.offered_cores[i],sizeof(int),write_res);
  830. fflush(log_file);
  831. my_stats.message_size += sizeof(int);
  832. }
  833. tmp_offer_list = tmp_offer_list->next;
  834. }
  835. core_inter_head[sender_id]->type = FAR_REQ_OFFER_SENT;
  836. clear = 0;
  837. } else {
  838. far_req_app.A=-1.0;
  839. far_req_app.var=-1.0;
  840. far_req_app.num_of_cores=-1;
  841. far_req_or_sender = -1;
  842. far_reg.C = -1;
  843. far_reg.r = -1;
  844. }
  845. } else if (tmp_inter_list->type == REP_FAR_REQ_OFFER_SENT) {
  846. fprintf(log_file, "num_of_offers=%d\n",tmp_inter_list->data.offer_acc_array[0]);
  847. fflush(log_file);
  848. for (i=1; i<=tmp_inter_list->data.offer_acc_array[0]; i++) {
  849. write_res = write(fd_w, &tmp_inter_list->data.offer_acc_array[i], sizeof(int));
  850. fprintf(log_file, "offer_ans=%d size=%d write_res=%d\n",tmp_inter_list->data.offer_acc_array[i],sizeof(int),write_res);
  851. fflush(log_file);
  852. my_stats.message_size += sizeof(int);
  853. }
  854. } else if (tmp_inter_list->type == INIT_WORK_NODE) {
  855. if (tmp_inter_list->data.work_time != -1) {
  856. i=1;
  857. write_res = write(fd_w, &i, sizeof(int));
  858. fprintf(log_file, "i=%d size=%d write_res=%d\n",i,sizeof(int),write_res);
  859. fflush(log_file);
  860. write_res = write(fd_w, &node_id, sizeof(int));
  861. fprintf(log_file, "node_id=%d size=%d write_res=%d\n",node_id,sizeof(int),write_res);
  862. fflush(log_file);
  863. write_res = write(fd_w, &tmp_inter_list->data.work_time, sizeof(int));
  864. fprintf(log_file, "work_time=%d size=%d write_res=%d\n",tmp_inter_list->data.work_time,sizeof(int),write_res);
  865. fflush(log_file);
  866. my_stats.message_size += 3 * sizeof(int);
  867. } else {
  868. i=0;
  869. write_res = write(fd_w, &i, sizeof(int));
  870. fprintf(log_file, "i=%d size=%d write_res=%d\n",i,sizeof(int),write_res);
  871. fflush(log_file);
  872. my_stats.message_size += sizeof(int);
  873. }
  874. } else if (tmp_inter_list->type == APPOINT_WORK_NODE) {
  875. if (tmp_inter_list->data.work_time != -1) {
  876. i=1;
  877. write_res = write(fd_w, &i, sizeof(int));
  878. fprintf(log_file, "i=%d size=%d write_res=%d\n",i,sizeof(int),write_res);
  879. fflush(log_file);
  880. write_res = write(fd_w, &tmp_inter_list->data.work_time, sizeof(int));
  881. fprintf(log_file, "work_time=%d size=%d write_res=%d\n",tmp_inter_list->data.work_time,sizeof(int),write_res);
  882. fflush(log_file);
  883. my_stats.message_size += 2 * sizeof(int);
  884. } else {
  885. i=0;
  886. write_res = write(fd_w, &i, sizeof(int));
  887. fprintf(log_file, "i=%d size=%d write_res=%d\n",i,sizeof(int),write_res);
  888. fflush(log_file);
  889. my_stats.message_size += sizeof(int);
  890. }
  891. } else if (tmp_inter_list->type == REP_CHK_REM_TIME) {
  892. write_res = write(fd_w, &tmp_inter_list->data.work_time, sizeof(int));
  893. fprintf(log_file, "work_time=%d size=%d write_res=%d\n",tmp_inter_list->data.work_time,sizeof(int),write_res);
  894. fflush(log_file);
  895. my_stats.message_size += sizeof(int);*/
  896. } else if (tmp_inter_list->type == REP_STATISTICS) {
  897. //write_res = write(fd_w, &tmp_inter_list->data.stats, sizeof(metrics));
  898. data_array_local[0] = tmp_inter_list->data.stats.msg_count;
  899. data_array_local[1] = tmp_inter_list->data.stats.message_size;
  900. data_array_local[2] = tmp_inter_list->data.stats.distance;
  901. data_array_local[3] = tmp_inter_list->data.stats.app_turnaround;
  902. data_array_local[4] = tmp_inter_list->data.stats.comp_effort;
  903. data_array_local[5] = tmp_inter_list->data.stats.cores_utilized;
  904. data_array_local[6] = tmp_inter_list->data.stats.times_accessed;
  905. error = RCCE_put((t_vcharp)(&data_array[0]), (t_vcharp)(&data_array_local[0]), LINE_SIZE * sizeof(int), sender_id);
  906. if (error != RCCE_SUCCESS) {
  907. RCCE_error_string(error, error_str, &str_len);
  908. printf("I am %d and i got an error in put data to %d with descr %s\n",node_id,sender_id,error_str);
  909. }
  910. RCCE_flag_write(&flag_data_written, RCCE_FLAG_SET, sender_id);
  911. state = TERMINATED;
  912. } else printf("We were fucked inside ACK! node_id = %d sender_id = %d\n",node_id,sender_id);
  913. if (clear){
  914. core_inter_head[sender_id] = tmp_inter_list->next;
  915. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  916. else send_next_signal(core_inter_head[sender_id],sender_id);
  917. free(tmp_inter_list);
  918. }
  919. }
  920. cur_time = time(NULL);
  921. cur_t = localtime(&cur_time);
  922. 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);
  923. fflush(log_file);
  924. signals_enable();
  925. }
  926. void sig_IDAG_FIND_IDAGS_handler(int sender_id)
  927. {
  928. int num_of_idags, i, one_idag, error, str_len;//, idags_read=0;
  929. region cur_reg;
  930. inter_list *tmp_inter_list;
  931. int data_array_local[LINE_SIZE];
  932. //RCCE_FLAG_STATUS receiver_status;
  933. char error_str[64];
  934. signals_disable();
  935. cur_time = time(NULL);
  936. cur_t = localtime(&cur_time);
  937. 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);
  938. fflush(log_file);
  939. if (core_inter_head[sender_id] == NULL || core_inter_head[sender_id]->type == REP_IDAG_FIND_IDAGS){ //I am the idag
  940. //kill(info->si_pid, SIG_ACK);
  941. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  942. scc_kill(sender_id, SIG_ACK);
  943. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  944. my_stats.msg_count++;
  945. my_stats.distance += distance(node_id,sender_id);
  946. //read(fd_r, &cur_reg, sizeof(region));
  947. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), LINE_SIZE * sizeof(int), node_id);
  948. if (error != RCCE_SUCCESS) {
  949. RCCE_error_string(error, error_str, &str_len);
  950. 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);
  951. }
  952. cur_reg.C = data_array_local[0];
  953. cur_reg.r = data_array_local[1];
  954. if (core_inter_head[sender_id] == NULL){
  955. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  956. core_inter_tail[sender_id] = core_inter_head[sender_id];
  957. } else {
  958. core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
  959. core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
  960. }
  961. core_inter_tail[sender_id]->type = REP_IDAG_FIND_IDAGS;
  962. core_inter_tail[sender_id]->data.idags_in_reg = (int *) malloc((num_idags_x*num_idags_y+1)*sizeof(int));
  963. core_inter_tail[sender_id]->next = NULL;
  964. get_reg_idags(cur_reg, core_inter_tail[sender_id]->data.idags_in_reg);
  965. if (core_inter_head[sender_id]->next == NULL) {
  966. //kill(info->si_pid, SIG_IDAG_FIND_IDAGS);
  967. scc_kill(sender_id, SIG_IDAG_FIND_IDAGS);
  968. my_stats.msg_count++;
  969. my_stats.distance += distance(node_id,sender_id);
  970. }
  971. } else if (core_inter_head[sender_id]->type == IDAG_FIND_IDAGS && state != IDLE_INIT_MAN && state != WORKING_NODE_IDLE_INIT) {
  972. //IDLE_INIT_MAN_SELFOPT_PENDING && state != IDLE_INIT_MAN_WORK_PENDING) {
  973. 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);
  974. 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);
  975. fflush(log_file);
  976. //kill(info->si_pid, SIG_REJECT);
  977. scc_kill(sender_id, SIG_REJECT);
  978. my_stats.msg_count++;
  979. my_stats.distance += distance(node_id,sender_id);
  980. tmp_inter_list = core_inter_head[sender_id];
  981. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  982. free(tmp_inter_list);
  983. if(core_inter_head[sender_id] == NULL) {
  984. if (core_inter_tail[sender_id] == NULL){
  985. printf("Malaka gamietai to core_inter_tail\n");
  986. core_inter_tail[sender_id] = NULL;
  987. }
  988. } else send_next_signal(core_inter_head[sender_id], sender_id);
  989. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_FIND_IDAGS && state != IDLE_AGENT_WAITING_OFF) {
  990. 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);
  991. 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);
  992. fflush(log_file);
  993. //kill(info->si_pid, SIG_REJECT);
  994. scc_kill(sender_id, SIG_REJECT);
  995. my_stats.msg_count++;
  996. my_stats.distance += distance(node_id,sender_id);
  997. tmp_inter_list = core_inter_head[sender_id];
  998. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  999. free(tmp_inter_list);
  1000. if(core_inter_head[sender_id] == NULL) {
  1001. if (core_inter_tail[sender_id] == NULL){
  1002. printf("Malaka gamietai to core_inter_tail\n");
  1003. core_inter_tail[sender_id] = NULL;
  1004. }
  1005. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1006. } 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
  1007. //kill(info->si_pid, SIG_ACK);
  1008. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  1009. scc_kill(sender_id, SIG_ACK);
  1010. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  1011. my_stats.msg_count++;
  1012. my_stats.distance += distance(node_id,sender_id);
  1013. //read(fd_r, &num_of_idags, sizeof(int));
  1014. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), LINE_SIZE * sizeof(int), node_id);
  1015. if (error != RCCE_SUCCESS) {
  1016. RCCE_error_string(error, error_str, &str_len);
  1017. 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);
  1018. }
  1019. num_of_idags = data_array_local[0];
  1020. fprintf(log_file,"Number of agents in region %d\n",num_of_idags);
  1021. fflush(log_file);
  1022. //if (core_inter_head[sender_id]->type == FAR_INIT_IDAG_FIND_IDAGS) far_req_max_man_count = num_of_idags;
  1023. if (core_inter_head[sender_id]->type == IDAG_FIND_IDAGS) {
  1024. init_idags_areas_replies++;
  1025. init_DDS_idags += num_of_idags;
  1026. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_FIND_IDAGS) selfopt_DDS_idags += num_of_idags;
  1027. for (i=0; i<num_of_idags; i++){ //max 4 idags
  1028. //read(fd_r, &one_idag, sizeof(int));
  1029. one_idag = data_array_local[i];
  1030. fprintf(log_file,"In the region I have idag with id %d\n",one_idag);
  1031. fflush(log_file);
  1032. if (core_inter_head[one_idag] == NULL){
  1033. core_inter_head[one_idag] = (inter_list *) malloc(sizeof(inter_list));
  1034. core_inter_tail[one_idag] = core_inter_head[one_idag];
  1035. } else {
  1036. for (tmp_inter_list = core_inter_head[one_idag]; tmp_inter_list != NULL; tmp_inter_list = tmp_inter_list->next)
  1037. if (tmp_inter_list->type == FAR_REQ_MAN) break;
  1038. if (tmp_inter_list != NULL) {
  1039. fprintf(log_file,"I dismissed node %d in rep_idag_find_idags\n",one_idag);
  1040. fflush(log_file);
  1041. init_DDS_idags--;
  1042. continue;
  1043. }
  1044. core_inter_tail[one_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  1045. core_inter_tail[one_idag] = core_inter_tail[one_idag]->next;
  1046. }
  1047. if (core_inter_head[sender_id]->type == IDAG_FIND_IDAGS) core_inter_tail[one_idag]->type = IDAG_REQ_DDS_PENDING;
  1048. 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;
  1049. else core_inter_tail[one_idag]->type = SELFOPT_IDAG_REQ_DDS_PENDING;
  1050. core_inter_tail[one_idag]->data.reg.C = core_inter_head[sender_id]->data.reg.C;
  1051. core_inter_tail[one_idag]->data.reg.r = core_inter_head[sender_id]->data.reg.r;
  1052. core_inter_tail[one_idag]->next = NULL;
  1053. if (core_inter_head[one_idag]->next == NULL) {
  1054. //kill(pid_num[one_idag], SIG_REQ_DDS); //newly created
  1055. scc_kill(one_idag, SIG_REQ_DDS);
  1056. my_stats.msg_count++;
  1057. my_stats.distance += distance(node_id,one_idag);
  1058. } else {
  1059. 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);
  1060. fflush(log_file);
  1061. }
  1062. }
  1063. tmp_inter_list = core_inter_head[sender_id];
  1064. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1065. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  1066. else send_next_signal(core_inter_head[sender_id], sender_id);
  1067. free(tmp_inter_list);
  1068. } else if (core_inter_head[sender_id]->type == IDAG_FIND_IDAGS_PENDING || core_inter_head[sender_id]->type == SELFOPT_IDAG_FIND_IDAGS_PENDING) {
  1069. printf("I am %d and deadlock was prevented in sig_find_idags\n",node_id);
  1070. tmp_inter_list = core_inter_head[sender_id];
  1071. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1072. free(tmp_inter_list);
  1073. if(core_inter_head[sender_id] == NULL) {
  1074. if (core_inter_tail[sender_id] == NULL){
  1075. printf("Malaka gamietai to core_inter_tail\n");
  1076. core_inter_tail[sender_id] = NULL;
  1077. }
  1078. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1079. } else {
  1080. 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);
  1081. 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);
  1082. fflush(log_file);
  1083. //kill(info->si_pid, SIG_REJECT);
  1084. scc_kill(sender_id, SIG_REJECT);
  1085. my_stats.msg_count++;
  1086. my_stats.distance += distance(node_id,sender_id);
  1087. }
  1088. cur_time = time(NULL);
  1089. cur_t = localtime(&cur_time);
  1090. 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);
  1091. fflush(log_file);
  1092. signals_enable();
  1093. }
  1094. void sig_REQ_DDS_handler(int sender_id)
  1095. {
  1096. int num_of_agents, i, agent_id, num_of_cores, j, agents_read=0;
  1097. region cur_reg;
  1098. core_list *tmp_cores_list;
  1099. inter_list *tmp_inter_list;
  1100. DDS_list *tmp_DDS;
  1101. target_list *tmp_target_list;
  1102. int data_array_local[LINE_SIZE], error, str_len;
  1103. //RCCE_FLAG_STATUS receiver_status;
  1104. char error_str[64];
  1105. signals_disable();
  1106. cur_time = time(NULL);
  1107. cur_t = localtime(&cur_time);
  1108. 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);
  1109. fflush(log_file);
  1110. if (core_inter_head[sender_id] == NULL){ //I am the idag
  1111. //kill(info->si_pid, SIG_ACK);
  1112. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  1113. scc_kill(sender_id, SIG_ACK);
  1114. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  1115. my_stats.msg_count++;
  1116. my_stats.distance += distance(node_id,sender_id);
  1117. //read(fd_r, &cur_reg, sizeof(region));
  1118. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), LINE_SIZE * sizeof(int), node_id);
  1119. if (error != RCCE_SUCCESS) {
  1120. RCCE_error_string(error, error_str, &str_len);
  1121. 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);
  1122. }
  1123. cur_reg.C = data_array_local[0];
  1124. cur_reg.r = data_array_local[1];
  1125. //read(fd_r, &cur_reg, sizeof(region));
  1126. fprintf(log_file,"I am to investigate region C=%d r=%d for %d\n",cur_reg.C,cur_reg.r,sender_id);
  1127. fflush(log_file);
  1128. if (core_inter_head[sender_id] == NULL){
  1129. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  1130. core_inter_tail[sender_id] = core_inter_head[sender_id];
  1131. } else {
  1132. core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
  1133. core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
  1134. }
  1135. core_inter_tail[sender_id]->type = REP_IDAG_REQ_DDS;
  1136. core_inter_tail[sender_id]->next = NULL;
  1137. if (cur_reg.C > -1) {//gia na zhtaei o node 0 dds
  1138. core_inter_tail[sender_id]->data.agents_in_reg = (int *) malloc((2*DDS_count+1)*sizeof(int));
  1139. core_inter_tail[sender_id]->data.agents_in_reg[0] = 0;
  1140. for (i=1; i<=2*DDS_count; i+=2){
  1141. core_inter_tail[sender_id]->data.agents_in_reg[i] = -1;
  1142. core_inter_tail[sender_id]->data.agents_in_reg[i+1] = 0;
  1143. }
  1144. tmp_cores_list = my_cores;
  1145. while (tmp_cores_list != NULL){
  1146. if (distance(tmp_cores_list->core_id, cur_reg.C) <= cur_reg.r){
  1147. if (tmp_cores_list->offered_to == -1) agent_id=node_id;
  1148. else {
  1149. agent_id=tmp_cores_list->offered_to;
  1150. for (tmp_DDS = DDS->next; tmp_DDS!=NULL; tmp_DDS=tmp_DDS->next) if (tmp_DDS->agent_id == agent_id) break;
  1151. if (tmp_DDS == NULL) agent_id = node_id;
  1152. }
  1153. for (i=1; i<=2*DDS_count; i+=2)
  1154. if (core_inter_tail[sender_id]->data.agents_in_reg[i] == agent_id) break;
  1155. else if (core_inter_tail[sender_id]->data.agents_in_reg[i] == -1){
  1156. core_inter_tail[sender_id]->data.agents_in_reg[0]++;
  1157. core_inter_tail[sender_id]->data.agents_in_reg[i] = agent_id;
  1158. break;
  1159. }
  1160. core_inter_tail[sender_id]->data.agents_in_reg[i+1]++;
  1161. }
  1162. tmp_cores_list = tmp_cores_list->next;
  1163. }
  1164. } else core_inter_tail[sender_id]->data.agents_in_reg = NULL;
  1165. //kill(info->si_pid, SIG_REQ_DDS);
  1166. scc_kill(sender_id, SIG_REQ_DDS);
  1167. my_stats.msg_count++;
  1168. my_stats.distance += distance(node_id,sender_id);
  1169. } else if (core_inter_head[sender_id]->type == IDAG_REQ_DDS && state != IDLE_INIT_MAN && state != WORKING_NODE_IDLE_INIT) {
  1170. //IDLE_INIT_MAN_SELFOPT_PENDING && state != IDLE_INIT_MAN_WORK_PENDING) {
  1171. 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);
  1172. 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);
  1173. fflush(log_file);
  1174. //kill(info->si_pid, SIG_REJECT);
  1175. scc_kill(sender_id, SIG_REJECT);
  1176. my_stats.msg_count++;
  1177. my_stats.distance += distance(node_id,sender_id);
  1178. tmp_inter_list = core_inter_head[sender_id];
  1179. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1180. free(tmp_inter_list);
  1181. if(core_inter_head[sender_id] == NULL) {
  1182. if (core_inter_tail[sender_id] == NULL){
  1183. printf("Malaka gamietai to core_inter_tail\n");
  1184. core_inter_tail[sender_id] = NULL;
  1185. }
  1186. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1187. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS && state != IDLE_AGENT_WAITING_OFF) {
  1188. 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);
  1189. 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);
  1190. fflush(log_file);
  1191. //kill(info->si_pid, SIG_REJECT);
  1192. scc_kill(sender_id, SIG_REJECT);
  1193. my_stats.msg_count++;
  1194. my_stats.distance += distance(node_id,sender_id);
  1195. tmp_inter_list = core_inter_head[sender_id];
  1196. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1197. free(tmp_inter_list);
  1198. if(core_inter_head[sender_id] == NULL) {
  1199. if (core_inter_tail[sender_id] == NULL){
  1200. printf("Malaka gamietai to core_inter_tail\n");
  1201. core_inter_tail[sender_id] = NULL;
  1202. }
  1203. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1204. } 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
  1205. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  1206. scc_kill(sender_id, SIG_ACK);
  1207. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  1208. my_stats.msg_count++;
  1209. my_stats.distance += distance(node_id,sender_id);
  1210. //read(fd_r, &num_of_idags, sizeof(int));
  1211. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), LINE_SIZE * sizeof(int), node_id);
  1212. if (error != RCCE_SUCCESS) {
  1213. RCCE_error_string(error, error_str, &str_len);
  1214. 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);
  1215. }
  1216. if (core_inter_head[sender_id]->type == IDAG_REQ_DDS) {
  1217. init_DDS_replies++;
  1218. 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);
  1219. fflush(log_file);
  1220. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS) {
  1221. selfopt_DDS_replies++;
  1222. fprintf(log_file,"selfopt_DDS_idags = %d, selfopt_DDS_replies = %d\n",selfopt_DDS_idags,selfopt_DDS_replies);
  1223. fflush(log_file);
  1224. }
  1225. //read(fd_r, &num_of_agents, sizeof(int));
  1226. num_of_agents = data_array_local[0];
  1227. while (num_of_agents > X_max*Y_max) {
  1228. 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);
  1229. //read(fd_r, &num_of_agents, sizeof(int));
  1230. }
  1231. 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);
  1232. fflush(log_file);
  1233. //for (i=0; i<num_of_agents; i++)
  1234. i = 2; //bypass second element of array to produce cleaner code
  1235. while (agents_read < num_of_agents) {
  1236. //read(fd_r, &agent_id, sizeof(int));
  1237. //read(fd_r, &num_of_cores, sizeof(int));
  1238. if (i > LINE_SIZE - 1) {
  1239. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[LINE_SIZE]), LINE_SIZE * sizeof(int), node_id);
  1240. if (error != RCCE_SUCCESS) {
  1241. RCCE_error_string(error, error_str, &str_len);
  1242. 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);
  1243. }
  1244. i=0;
  1245. }
  1246. agent_id = data_array_local[i++];
  1247. num_of_cores = data_array_local[i++];
  1248. agents_read++;
  1249. fprintf(log_file, "there is an agent with id %d and %d cores\n",agent_id,num_of_cores);
  1250. fflush(log_file);
  1251. if (agent_id == node_id) continue;//((node_id == -1 && agent_id == idag_id) || node_id == agent_id)
  1252. if (core_inter_head[sender_id]->type == IDAG_REQ_DDS) {
  1253. tmp_target_list = init_targets_head;
  1254. while (tmp_target_list != NULL && tmp_target_list->target != agent_id)
  1255. tmp_target_list = tmp_target_list->next;
  1256. if (tmp_target_list == NULL) {
  1257. if (init_targets_head == NULL) {
  1258. init_targets_head = (target_list *) malloc(sizeof(target_list));
  1259. init_targets_tail = init_targets_head;;
  1260. } else {
  1261. init_targets_tail->next = (target_list *) malloc(sizeof(target_list));
  1262. init_targets_tail = init_targets_tail->next;
  1263. }
  1264. init_targets_tail->next = NULL;
  1265. init_targets_tail->target = agent_id;
  1266. init_targets_tail->num_of_regions = 1;
  1267. init_targets_tail->region_arr[0] = core_inter_head[sender_id]->data.reg;
  1268. } else {
  1269. fprintf(log_file, "in here num_of_regions = %d\n",tmp_target_list->num_of_regions);
  1270. fflush(log_file);
  1271. for (j=0; j<tmp_target_list->num_of_regions; j++)
  1272. 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){
  1273. fprintf(log_file, "fucking area allready exists\n");
  1274. fflush(log_file);
  1275. break;
  1276. }
  1277. if (j == tmp_target_list->num_of_regions) {
  1278. tmp_target_list->region_arr[tmp_target_list->num_of_regions++] = core_inter_head[sender_id]->data.reg;
  1279. //fprintf(log_file, "new area added\n");
  1280. //fflush(log_file);
  1281. }
  1282. }
  1283. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS){
  1284. tmp_target_list = selfopt_targets_head;
  1285. while (tmp_target_list != NULL && tmp_target_list->target != agent_id)
  1286. tmp_target_list = tmp_target_list->next;
  1287. if (tmp_target_list == NULL) {
  1288. if (selfopt_targets_head == NULL) {
  1289. selfopt_targets_head = (target_list *) malloc(sizeof(target_list));
  1290. selfopt_targets_tail = selfopt_targets_head;;
  1291. } else {
  1292. selfopt_targets_tail->next = (target_list *) malloc(sizeof(target_list));
  1293. selfopt_targets_tail = selfopt_targets_tail->next;
  1294. }
  1295. selfopt_targets_tail->next = NULL;
  1296. selfopt_targets_tail->target = agent_id;
  1297. selfopt_targets_tail->num_of_regions = 1;
  1298. selfopt_targets_tail->region_arr[0] = core_inter_head[sender_id]->data.reg;
  1299. } else {
  1300. for (j=0; j<tmp_target_list->num_of_regions; j++)
  1301. 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){
  1302. break;
  1303. }
  1304. if (j == tmp_target_list->num_of_regions)
  1305. tmp_target_list->region_arr[tmp_target_list->num_of_regions++] = core_inter_head[sender_id]->data.reg;
  1306. }
  1307. } else if (core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS) {
  1308. if (core_inter_head[agent_id] == NULL){
  1309. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  1310. core_inter_tail[agent_id] = core_inter_head[agent_id];
  1311. } else {
  1312. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  1313. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  1314. }
  1315. //if (core_inter_head[sender_id]->type == IDAG_REQ_DDS) core_inter_tail[agent_id]->type = AGENT_REQ_CORES_PENDING;
  1316. //else if (core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS) core_inter_tail[agent_id]->type = FAR_REQ_CORES;
  1317. //else core_inter_tail[agent_id]->type = SELFOPT_REQ_CORES_PENDING;
  1318. core_inter_tail[agent_id]->type = FAR_REQ_CORES_PENDING;
  1319. core_inter_tail[agent_id]->data.reg = core_inter_head[sender_id]->data.reg;
  1320. core_inter_tail[agent_id]->next = NULL;
  1321. if (core_inter_head[agent_id]->next == NULL) {
  1322. //kill(pid_num[agent_id], SIG_REQ_CORES); //newly created, not an idag
  1323. scc_kill(agent_id, SIG_REQ_CORES);
  1324. my_stats.msg_count++;
  1325. my_stats.distance += distance(node_id,agent_id);
  1326. }
  1327. }
  1328. }
  1329. if (core_inter_head[sender_id]->type == IDAG_REQ_DDS && init_DDS_replies == init_DDS_idags && init_idags_areas_replies == init_areas_num) {
  1330. for (tmp_target_list = init_targets_head; tmp_target_list != NULL; tmp_target_list = tmp_target_list->next){
  1331. agent_id = tmp_target_list->target;
  1332. for (tmp_inter_list = core_inter_head[agent_id]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
  1333. if (tmp_inter_list->type == FAR_INIT_REQ || tmp_inter_list->type == FAR_REQ_MAN) break;
  1334. if (tmp_inter_list == NULL) {
  1335. if (core_inter_head[agent_id] == NULL){
  1336. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  1337. core_inter_tail[agent_id] = core_inter_head[agent_id];
  1338. } else {
  1339. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  1340. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  1341. }
  1342. core_inter_tail[agent_id]->type = AGENT_REQ_CORES_PENDING;
  1343. core_inter_tail[agent_id]->data.reg_arr.num_of_regions = tmp_target_list->num_of_regions;
  1344. core_inter_tail[agent_id]->data.reg_arr.region_arr = (region *) malloc(tmp_target_list->num_of_regions * sizeof(region));
  1345. if (tmp_target_list->target == node_id) printf("Why is this here node_id=%d\n",node_id);
  1346. fprintf(log_file,"Init req target = %d, num_of_regions = %d. Αreas:",tmp_target_list->target,tmp_target_list->num_of_regions);
  1347. for (i=0; i<tmp_target_list->num_of_regions; i++) {
  1348. core_inter_tail[agent_id]->data.reg_arr.region_arr[i] = tmp_target_list->region_arr[i];
  1349. fprintf(log_file," (%d,%d),",tmp_target_list->region_arr[i].C,tmp_target_list->region_arr[i].r);
  1350. }
  1351. fprintf(log_file,"\n");
  1352. fflush(log_file);
  1353. core_inter_tail[agent_id]->next = NULL;
  1354. if (core_inter_head[agent_id]->next == NULL) {
  1355. //kill(pid_num[agent_id], SIG_REQ_CORES);
  1356. scc_kill(agent_id, SIG_REQ_CORES);
  1357. my_stats.msg_count++;
  1358. my_stats.distance += distance(node_id,agent_id);
  1359. } 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
  1360. //kill(pid_num[agent_id], SIG_REQ_CORES);
  1361. scc_kill(agent_id, SIG_REQ_CORES);
  1362. my_stats.msg_count++;
  1363. my_stats.distance += distance(node_id,agent_id);
  1364. } 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);
  1365. } else
  1366. printf("I am %d and i did not send local requests to %d because he is my far manager\n",node_id,agent_id);
  1367. }
  1368. its.it_value.tv_nsec = INIT_NODE_INTERVAL * MS;//750000000;// * MS;
  1369. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error4\n");
  1370. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS && selfopt_DDS_replies == selfopt_DDS_idags) {
  1371. for (tmp_target_list = selfopt_targets_head; tmp_target_list != NULL; tmp_target_list = tmp_target_list->next){
  1372. agent_id = tmp_target_list->target;
  1373. if (core_inter_head[agent_id] == NULL){
  1374. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  1375. core_inter_tail[agent_id] = core_inter_head[agent_id];
  1376. } else {
  1377. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  1378. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  1379. }
  1380. core_inter_tail[agent_id]->type = SELFOPT_REQ_CORES_PENDING;
  1381. core_inter_tail[agent_id]->data.reg_arr.num_of_regions = tmp_target_list->num_of_regions;
  1382. core_inter_tail[agent_id]->data.reg_arr.region_arr = (region *) malloc(tmp_target_list->num_of_regions * sizeof(region));
  1383. if (tmp_target_list->target == node_id) printf("selfopt Why is this here node_id=%d\n",node_id);
  1384. fprintf(log_file,"Selfopt req target = %d, num_of_regions = %d. Αreas:",tmp_target_list->target,tmp_target_list->num_of_regions);
  1385. for (i=0; i<tmp_target_list->num_of_regions; i++) {
  1386. core_inter_tail[agent_id]->data.reg_arr.region_arr[i] = tmp_target_list->region_arr[i];
  1387. fprintf(log_file," (%d,%d),",tmp_target_list->region_arr[i].C,tmp_target_list->region_arr[i].r);
  1388. }
  1389. fprintf(log_file,"\n");
  1390. fflush(log_file);
  1391. core_inter_tail[agent_id]->next = NULL;
  1392. if (core_inter_head[agent_id]->next == NULL) {
  1393. //kill(pid_num[agent_id], SIG_REQ_CORES);
  1394. scc_kill(agent_id, SIG_REQ_CORES);
  1395. my_stats.msg_count++;
  1396. my_stats.distance += distance(node_id,agent_id);
  1397. } else if (agent_id == sender_id && core_inter_head[agent_id]->next->type == SELFOPT_REQ_CORES_PENDING) {
  1398. //kill(pid_num[agent_id], SIG_REQ_CORES);
  1399. scc_kill(agent_id, SIG_REQ_CORES);
  1400. my_stats.msg_count++;
  1401. my_stats.distance += distance(node_id,agent_id);
  1402. } 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);
  1403. }
  1404. its.it_value.tv_nsec = 500 * MS;// 750000000;
  1405. selfopt_time_rem = its.it_value.tv_nsec;
  1406. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error7\n");
  1407. if (state != IDLE_AGENT_WAITING_OFF) {
  1408. printf("I am %d and about to set my alarm for selfopt check and my state before that was %d\n",node_id,state);
  1409. state = IDLE_AGENT_WAITING_OFF;
  1410. }
  1411. }
  1412. tmp_inter_list = core_inter_head[sender_id];
  1413. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1414. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  1415. 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 &&
  1416. send_next_signal(core_inter_head[sender_id], sender_id);
  1417. free(tmp_inter_list);
  1418. } else if (core_inter_head[sender_id]->type == DEBUG_IDAG_REQ_DDS){ //I am the requesting common node
  1419. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  1420. scc_kill(sender_id, SIG_ACK);
  1421. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  1422. //read(fd_r, &num_of_idags, sizeof(int));
  1423. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), LINE_SIZE * sizeof(int), node_id);
  1424. if (error != RCCE_SUCCESS) {
  1425. RCCE_error_string(error, error_str, &str_len);
  1426. 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);
  1427. fflush(log_file);
  1428. }
  1429. //read(fd_r, &num_of_agents, sizeof(int));
  1430. num_of_agents = data_array_local[0];
  1431. printf("\nNumber of agents in region = %d\n",num_of_agents);
  1432. fprintf(log_file,"I come here a\n");
  1433. fflush(log_file);
  1434. //for (i=0; i<num_of_agents; i++)
  1435. i = 2; //bypass second element of array to produce cleaner code
  1436. while (agents_read < num_of_agents) {
  1437. //read(fd_r, &agent_id, sizeof(int));
  1438. //read(fd_r, &num_of_cores, sizeof(int));
  1439. if (i > LINE_SIZE - 1) {
  1440. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[LINE_SIZE]), LINE_SIZE * sizeof(int), node_id);
  1441. if (error != RCCE_SUCCESS) {
  1442. RCCE_error_string(error, error_str, &str_len);
  1443. 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);
  1444. }
  1445. i=0;
  1446. }
  1447. agent_id = data_array_local[i++];
  1448. num_of_cores = data_array_local[i++];
  1449. agents_read++;
  1450. printf("Agent no %d is %d with %d cores\n",i,agent_id,num_of_cores);
  1451. }
  1452. fprintf(log_file,"I come here b\n");
  1453. fflush(log_file);
  1454. idags_replied++;
  1455. tmp_inter_list = core_inter_head[sender_id];
  1456. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1457. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  1458. else send_next_signal(core_inter_head[sender_id], sender_id);
  1459. free(tmp_inter_list);
  1460. } else if (core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING || core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS_PENDING
  1461. || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING){
  1462. printf("I am %d and deadlock was prevented in sig_req_dds\n",node_id);
  1463. tmp_inter_list = core_inter_head[sender_id];
  1464. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1465. free(tmp_inter_list);
  1466. if(core_inter_head[sender_id] == NULL) {
  1467. if (core_inter_tail[sender_id] == NULL){
  1468. printf("Malaka gamietai to core_inter_tail\n");
  1469. core_inter_tail[sender_id] = NULL;
  1470. }
  1471. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1472. } else {
  1473. //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);
  1474. 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);
  1475. 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);
  1476. fflush(log_file);
  1477. //kill(info->si_pid, SIG_REJECT);
  1478. scc_kill(sender_id, SIG_REJECT);
  1479. my_stats.msg_count++;
  1480. my_stats.distance += distance(node_id,sender_id);
  1481. }
  1482. cur_time = time(NULL);
  1483. cur_t = localtime(&cur_time);
  1484. 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);
  1485. fflush(log_file);
  1486. signals_enable();
  1487. }
  1488. void sig_REQ_CORES_handler(int sender_id)
  1489. {
  1490. int i, tmp_int, num_of_offers, j, non_zero_offers=0, cores_util;//num_of_idags, i, one_idag;
  1491. float req_gain;
  1492. region cur_reg;
  1493. app req_app;
  1494. offer one_offer;
  1495. offer_list *tmp_offer_list, *tmp_offer_prev = NULL, *tmp_head, *chosen_node;
  1496. inter_list *tmp_inter_list;
  1497. offer_array off_arr;
  1498. DDS_list *tmp_DDS;
  1499. int data_array_local[2 * LINE_SIZE], error, str_len;
  1500. //RCCE_FLAG_STATUS receiver_status;
  1501. char error_str[64];
  1502. signals_disable();
  1503. cur_time = time(NULL);
  1504. cur_t = localtime(&cur_time);
  1505. 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);
  1506. fflush(log_file);
  1507. if (core_inter_head[sender_id] == NULL && my_cores != NULL) {
  1508. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  1509. scc_kill(sender_id, SIG_ACK);
  1510. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  1511. my_stats.msg_count++;
  1512. my_stats.distance += distance(node_id,sender_id);
  1513. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), LINE_SIZE * sizeof(int), node_id);
  1514. if (error != RCCE_SUCCESS) {
  1515. RCCE_error_string(error, error_str, &str_len);
  1516. 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);
  1517. }
  1518. req_app.id = data_array_local[0];
  1519. memcpy(&req_app.A,&data_array_local[1],sizeof(int));
  1520. memcpy(&req_app.var,&data_array_local[2],sizeof(int));
  1521. memcpy(&req_app.workld,&data_array_local[3],sizeof(int));
  1522. req_app.num_of_cores = data_array_local[4];
  1523. //read(fd_r, &req_app, sizeof(app));
  1524. if (core_inter_head[sender_id] == NULL) {
  1525. core_inter_head[sender_id] = (inter_list *) malloc(sizeof(inter_list));
  1526. core_inter_tail[sender_id] = core_inter_head[sender_id];
  1527. } else {
  1528. core_inter_tail[sender_id]->next = (inter_list *) malloc(sizeof(inter_list));
  1529. core_inter_tail[sender_id] = core_inter_tail[sender_id]->next;
  1530. }
  1531. core_inter_tail[sender_id]->type = REP_AGENT_REQ_CORES;
  1532. off_arr.num_of_offers = data_array_local[5];
  1533. //read(fd_r, &off_arr.num_of_offers, sizeof(int));
  1534. /*while (off_arr.num_of_offers > OLD_INIT_AREAS_NUM) {
  1535. printf("I am %d kai fagame skoupidia apo ton %d kai einai %d\n",node_id,sender_id,off_arr.num_of_offers);
  1536. read(fd_r, &off_arr.num_of_offers, sizeof(int));
  1537. }*/
  1538. if (off_arr.num_of_offers > 1) {
  1539. fprintf(log_file,"I got num of offers greater than %d\n",off_arr.num_of_offers);
  1540. fflush(log_file);
  1541. }
  1542. off_arr.offer_arr = (offer *) malloc(off_arr.num_of_offers * sizeof(offer));
  1543. core_inter_tail[sender_id]->next = NULL;
  1544. for (i=0; i<off_arr.num_of_offers; i++) {
  1545. //read(fd_r, &cur_reg, sizeof(region));
  1546. cur_reg.C = data_array_local[6];
  1547. cur_reg.r = data_array_local[7];
  1548. if (my_idag == -1) {
  1549. off_arr.offer_arr[i].offered_cores = (int *) malloc(my_cores_count*sizeof(int));
  1550. tmp_int = offer_cores(my_cores, req_app, cur_reg, off_arr.offer_arr[i].offered_cores, sender_id);
  1551. off_arr.offer_arr[i].num_of_cores = tmp_int;
  1552. my_stats.comp_effort++;
  1553. if (tmp_int > 0) non_zero_offers++;
  1554. cores_util = 0;
  1555. for (tmp_DDS = DDS->next; tmp_DDS != NULL; tmp_DDS = tmp_DDS->next)
  1556. cores_util += tmp_DDS->num_of_cores;
  1557. if (cores_util == 0) off_arr.offer_arr[i].spd_loss = -2.0;
  1558. else off_arr.offer_arr[i].spd_loss = (float) (-1 * cores_util) / (my_cores_count-1);//0.0;
  1559. } else if (my_cores != NULL && my_cores_count>2) {
  1560. off_arr.offer_arr[i].offered_cores = (int *) malloc(my_cores_count*sizeof(int));
  1561. tmp_int = offer_cores(my_cores, req_app, cur_reg, off_arr.offer_arr[i].offered_cores, sender_id);
  1562. off_arr.offer_arr[i].num_of_cores = tmp_int;
  1563. my_stats.comp_effort++;
  1564. //printf("I am %d in i = %d and i offer %d cores\n",node_id,i,tmp_int);
  1565. off_arr.offer_arr[i].spd_loss = Speedup(my_app, my_cores_count) - Speedup(my_app, my_cores_count-tmp_int);
  1566. req_gain = Speedup(req_app,req_app.num_of_cores+tmp_int) - Speedup(req_app,req_app.num_of_cores);
  1567. if (tmp_int > 0) {
  1568. non_zero_offers++;
  1569. 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);
  1570. fflush(log_file);
  1571. fprintf(app_log_file,"I offered %d cores with spd_loss = %0.2f and %0.2f gain for the req_app\n"
  1572. ,tmp_int,off_arr.offer_arr[i].spd_loss,req_gain);
  1573. fflush(app_log_file);
  1574. }
  1575. } else {
  1576. off_arr.offer_arr[i].num_of_cores = 0;
  1577. off_arr.offer_arr[i].spd_loss = 0.0;
  1578. }
  1579. fprintf(log_file, "I offered %d %d cores: ",sender_id,off_arr.offer_arr[i].num_of_cores);
  1580. for (j=0; j<off_arr.offer_arr[i].num_of_cores; j++)
  1581. fprintf(log_file, "%d, ",off_arr.offer_arr[i].offered_cores[j]);
  1582. fprintf(log_file, "\n");
  1583. fflush(log_file);
  1584. }
  1585. fprintf(log_file,"non_zero_offers = %d\n",non_zero_offers);
  1586. core_inter_tail[sender_id]->data.off_arr.num_of_offers = non_zero_offers;
  1587. if (non_zero_offers > 0) core_inter_tail[sender_id]->data.off_arr.offer_arr = (offer *) malloc(non_zero_offers * sizeof(offer));
  1588. else core_inter_tail[sender_id]->data.off_arr.offer_arr = NULL;
  1589. j = 0;
  1590. for (i=0; i<off_arr.num_of_offers; i++)
  1591. if (off_arr.offer_arr[i].num_of_cores > 0) {
  1592. core_inter_tail[sender_id]->data.off_arr.offer_arr[j].num_of_cores = off_arr.offer_arr[i].num_of_cores;
  1593. core_inter_tail[sender_id]->data.off_arr.offer_arr[j].spd_loss = off_arr.offer_arr[i].spd_loss;
  1594. 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));
  1595. for (tmp_int=0; tmp_int<off_arr.offer_arr[i].num_of_cores; tmp_int++)
  1596. core_inter_tail[sender_id]->data.off_arr.offer_arr[j].offered_cores[tmp_int] = off_arr.offer_arr[i].offered_cores[tmp_int];
  1597. j++;
  1598. }
  1599. if (core_inter_head[sender_id]->next == NULL) {
  1600. //kill(info->si_pid, SIG_REQ_CORES);
  1601. scc_kill(sender_id, SIG_REQ_CORES);
  1602. my_stats.msg_count++;
  1603. my_stats.distance += distance(node_id,sender_id);
  1604. } else printf("Apparently not null interaction=%d\n",core_inter_head[sender_id]->type);
  1605. } else if (core_inter_head[sender_id] == NULL) {
  1606. printf("I am %d and i have to reject req_cores from %d with null interaction\n",node_id,sender_id);
  1607. fprintf(log_file,"i have to reject req_cores from %d. with null interaction\n",sender_id);
  1608. fflush(log_file);
  1609. scc_kill(sender_id, SIG_REJECT);
  1610. my_stats.msg_count++;
  1611. my_stats.distance += distance(node_id,sender_id);
  1612. } else if (core_inter_head[sender_id]->type == AGENT_REQ_CORES && state != IDLE_INIT_MAN && state != WORKING_NODE_IDLE_INIT) {
  1613. //IDLE_INIT_MAN_SELFOPT_PENDING && state != IDLE_INIT_MAN_WORK_PENDING) {
  1614. 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);
  1615. 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);
  1616. fflush(log_file);
  1617. scc_kill(sender_id, SIG_REJECT);
  1618. my_stats.msg_count++;
  1619. my_stats.distance += distance(node_id,sender_id);
  1620. tmp_inter_list = core_inter_head[sender_id];
  1621. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1622. free(tmp_inter_list);
  1623. if(core_inter_head[sender_id] == NULL) {
  1624. if (core_inter_tail[sender_id] == NULL){
  1625. printf("Malaka gamietai to core_inter_tail\n");
  1626. core_inter_tail[sender_id] = NULL;
  1627. }
  1628. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1629. } else if (core_inter_head[sender_id]->type == SELFOPT_REQ_CORES && state != IDLE_AGENT_WAITING_OFF) {
  1630. 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);
  1631. 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);
  1632. fflush(log_file);
  1633. scc_kill(sender_id, SIG_REJECT);
  1634. my_stats.msg_count++;
  1635. my_stats.distance += distance(node_id,sender_id);
  1636. tmp_inter_list = core_inter_head[sender_id];
  1637. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1638. free(tmp_inter_list);
  1639. if(core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  1640. else send_next_signal(core_inter_head[sender_id], sender_id);
  1641. } else if (core_inter_head[sender_id]->type == FAR_REQ_CORES && (state == IDLE_IDAG || (node_id == 0 && time_for_farman == -1))) {
  1642. 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);
  1643. 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);
  1644. fflush(log_file);
  1645. scc_kill(sender_id, SIG_REJECT);
  1646. my_stats.msg_count++;
  1647. my_stats.distance += distance(node_id,sender_id);
  1648. tmp_inter_list = core_inter_head[sender_id];
  1649. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1650. free(tmp_inter_list);
  1651. if(core_inter_head[sender_id] == NULL) {
  1652. if (core_inter_tail[sender_id] == NULL){
  1653. printf("Malaka gamietai to core_inter_tail\n");
  1654. core_inter_tail[sender_id] = NULL;
  1655. }
  1656. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1657. } 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){
  1658. //I am the requesting common node
  1659. //kill(info->si_pid, SIG_ACK);
  1660. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  1661. scc_kill(sender_id, SIG_ACK);
  1662. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  1663. my_stats.msg_count++;
  1664. my_stats.distance += distance(node_id,sender_id);
  1665. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), 2 * LINE_SIZE * sizeof(int), node_id);
  1666. if (error != RCCE_SUCCESS) {
  1667. RCCE_error_string(error, error_str, &str_len);
  1668. 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);
  1669. }
  1670. if (core_inter_head[sender_id]->type == AGENT_REQ_CORES || core_inter_head[sender_id]->type == SELFOPT_REQ_CORES) {//den erxontai ta far edw
  1671. //read(fd_r, &num_of_offers, sizeof(int));
  1672. num_of_offers = data_array_local[0];
  1673. fprintf(log_file, "num_of_offers = %d\n",num_of_offers);
  1674. fflush(log_file);
  1675. if (off_arr.num_of_offers > 1) {
  1676. fprintf(log_file,"I got offered offers greater than 1 = %d\n",off_arr.num_of_offers);
  1677. fflush(log_file);
  1678. }
  1679. if (num_of_offers > 0) {
  1680. core_inter_head[sender_id]->data.offer_acc_array = (int *) malloc((num_of_offers+1)*sizeof(int));
  1681. core_inter_head[sender_id]->data.offer_acc_array[0] = num_of_offers;
  1682. //core_inter_head[sender_id]->data.offer_accepted = -1;
  1683. //one_offer.offered_cores = NULL;
  1684. for (j=1; j<=num_of_offers; j++){
  1685. //read(fd_r, &one_offer.num_of_cores, sizeof(int));
  1686. //read(fd_r, &one_offer.spd_loss, sizeof(float));
  1687. one_offer.num_of_cores = data_array_local[1];
  1688. memcpy(&one_offer.spd_loss,&data_array_local[2],sizeof(int));
  1689. if (core_inter_head[sender_id]->type == AGENT_REQ_CORES && init_man_offers == NULL) {
  1690. init_man_offers = (offer_list *) malloc(sizeof(offer_list));
  1691. chosen_node = init_man_offers;
  1692. init_man_offers->next = NULL;
  1693. } else if (core_inter_head[sender_id]->type == SELFOPT_REQ_CORES && selfopt_man_offers == NULL){
  1694. selfopt_man_offers = (offer_list *) malloc(sizeof(offer_list));
  1695. chosen_node = selfopt_man_offers;
  1696. selfopt_man_offers->next = NULL;
  1697. } else {
  1698. tmp_offer_prev = NULL;
  1699. if (core_inter_head[sender_id]->type == AGENT_REQ_CORES) {
  1700. tmp_offer_list = init_man_offers;
  1701. if (one_offer.spd_loss < 0.0) {
  1702. 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){
  1703. if (tmp_offer_list->off.num_of_cores > one_offer.num_of_cores) {
  1704. tmp_offer_prev = tmp_offer_list;
  1705. tmp_offer_list = tmp_offer_list->next;
  1706. } else if (tmp_offer_list->off.spd_loss < one_offer.spd_loss) {
  1707. tmp_offer_prev = tmp_offer_list;
  1708. tmp_offer_list = tmp_offer_list->next;
  1709. } else break;
  1710. }
  1711. /*while (tmp_offer_list != NULL && tmp_offer_list->off.spd_loss < one_offer.spd_loss && tmp_offer_list->off.spd_loss < 0.0){
  1712. tmp_offer_prev = tmp_offer_list;
  1713. tmp_offer_list = tmp_offer_list->next;
  1714. }*/
  1715. } else {
  1716. 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)){
  1717. tmp_offer_prev = tmp_offer_list;
  1718. tmp_offer_list = tmp_offer_list->next;
  1719. }
  1720. }
  1721. } else {
  1722. tmp_offer_list = selfopt_man_offers;
  1723. while (tmp_offer_list != NULL && tmp_offer_list->off.num_of_cores >= one_offer.num_of_cores){
  1724. tmp_offer_prev = tmp_offer_list;
  1725. tmp_offer_list = tmp_offer_list->next;
  1726. }
  1727. }
  1728. if (tmp_offer_list == NULL) { //prepei na mpei teleutaio
  1729. tmp_offer_prev->next = (offer_list *) malloc(sizeof(offer_list));
  1730. tmp_offer_list = tmp_offer_prev->next;
  1731. tmp_offer_list->next = NULL;
  1732. chosen_node = tmp_offer_list;
  1733. } else if (tmp_offer_prev == NULL) { //prepei na mpei prwto
  1734. if (core_inter_head[sender_id]->type == AGENT_REQ_CORES) {
  1735. init_man_offers = (offer_list *) malloc(sizeof(offer_list));
  1736. tmp_head = init_man_offers;
  1737. } else {
  1738. selfopt_man_offers = (offer_list *) malloc(sizeof(offer_list));
  1739. tmp_head = selfopt_man_offers;
  1740. }
  1741. chosen_node = tmp_head;
  1742. tmp_head->next = tmp_offer_list;
  1743. } else {
  1744. tmp_offer_prev->next = (offer_list *) malloc(sizeof(offer_list));
  1745. tmp_offer_prev = tmp_offer_prev->next;
  1746. chosen_node = tmp_offer_prev;
  1747. tmp_offer_prev->next = tmp_offer_list;
  1748. }
  1749. }
  1750. chosen_node->off.num_of_cores = one_offer.num_of_cores;
  1751. chosen_node->off.spd_loss = one_offer.spd_loss;
  1752. chosen_node->off.offered_cores = (int *) malloc(one_offer.num_of_cores*sizeof(int));
  1753. for (i=0; i<one_offer.num_of_cores; i++)
  1754. //read(fd_r, &chosen_node->off.offered_cores[i], sizeof(int));
  1755. chosen_node->off.offered_cores[i] = data_array_local[i+LINE_SIZE];
  1756. /*printf("asdasNode %d is offering %d cores: ",sender_id,chosen_node->off.num_of_cores);
  1757. for (i=0; i<chosen_node->off.num_of_cores; i++)
  1758. printf(" %d,",chosen_node->off.offered_cores[i]);
  1759. printf("\n");*/
  1760. chosen_node->sender = sender_id;
  1761. core_inter_head[sender_id]->data.offer_acc_array[j] = -1;
  1762. chosen_node->answer = &core_inter_head[sender_id]->data.offer_acc_array[j];
  1763. cur_time = time(NULL);
  1764. cur_t = localtime(&cur_time);
  1765. fprintf(log_file, "[%d:%d:%d]: One node successfully added in list type=%d sender_id=%d\n",
  1766. cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,core_inter_head[sender_id]->type,sender_id);
  1767. fflush(log_file);
  1768. }
  1769. core_inter_head[sender_id]->type = REP_AGENT_OFFER_PENDING;
  1770. } else { //no answer is required
  1771. tmp_inter_list = core_inter_head[sender_id];
  1772. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1773. free(tmp_inter_list);
  1774. if(core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  1775. else send_next_signal(core_inter_head[sender_id], sender_id);
  1776. }
  1777. } else {
  1778. fprintf(log_file, "I somehow got into a far request\n");
  1779. fflush(log_file);
  1780. /*read(fd_r, &num_of_offers, sizeof(int));
  1781. fprintf(log_file, "num_of_offers is %d\n",num_of_offers);
  1782. fflush(log_file);
  1783. if (num_of_offers > 0) {
  1784. core_inter_head[sender_id]->data.offer_acc_array = (int *) malloc((num_of_offers+1)*sizeof(int));
  1785. core_inter_head[sender_id]->data.offer_acc_array[0] = num_of_offers;
  1786. core_inter_head[sender_id]->type = REP_AGENT_OFFER_PENDING;
  1787. read(fd_r, &one_offer.num_of_cores, sizeof(int));
  1788. read(fd_r, &one_offer.spd_loss, sizeof(float));
  1789. one_offer.offered_cores = (int *) malloc(one_offer.num_of_cores*sizeof(int));
  1790. for (i=0; i<one_offer.num_of_cores; i++)
  1791. read(fd_r, &one_offer.offered_cores[i], sizeof(int));
  1792. fprintf(log_file, "%d is offering %d cores with speedup loss %f\n",sender_id,one_offer.num_of_cores,one_offer.spd_loss);
  1793. fflush(log_file);
  1794. //core_inter_head[sender_id]->data.offer_accepted = -1;
  1795. core_inter_head[sender_id]->data.offer_acc_array[1] = -1;
  1796. if (far_man_offers == NULL){
  1797. far_man_offers = (offer_list *) malloc(sizeof(offer_list));
  1798. far_man_offers_tail = far_man_offers;
  1799. } else {
  1800. far_man_offers_tail->next = (offer_list *) malloc(sizeof(offer_list));
  1801. far_man_offers_tail = far_man_offers_tail->next;
  1802. }
  1803. far_man_offers_tail->off = one_offer;
  1804. far_man_offers_tail->sender = sender_id;
  1805. far_man_offers_tail->answer = &core_inter_head[sender_id]->data.offer_acc_array[1];//offer_accepted;
  1806. far_man_offers_tail->next = NULL;
  1807. far_list_count++;
  1808. cur_time = time(NULL);
  1809. cur_t = localtime(&cur_time);
  1810. fprintf(log_file, "[%d:%d:%d]: One node successfully added in far_man_offers list %d\n",
  1811. cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,core_inter_head[sender_id]->data.offer_acc_array[0]);
  1812. fflush(log_file);
  1813. } else { //no answer is required
  1814. tmp_inter_list = core_inter_head[sender_id];
  1815. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1816. free(tmp_inter_list);
  1817. if(core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  1818. else send_next_signal(core_inter_head[sender_id], sender_id);
  1819. }*/
  1820. }
  1821. } else if (core_inter_head[sender_id]->type == AGENT_REQ_CORES_PENDING || core_inter_head[sender_id]->type == SELFOPT_REQ_CORES_PENDING
  1822. || core_inter_head[sender_id]->type == FAR_REQ_CORES_PENDING){
  1823. printf("I am %d and deadlock was prevented in req_cores by %d\n",node_id,sender_id);
  1824. tmp_inter_list = core_inter_head[sender_id];
  1825. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  1826. free(tmp_inter_list);
  1827. if(core_inter_head[sender_id] == NULL) {
  1828. if (core_inter_tail[sender_id] == NULL){
  1829. printf("Malaka gamietai to core_inter_tail\n");
  1830. core_inter_tail[sender_id] = NULL;
  1831. }
  1832. } else send_next_signal(core_inter_head[sender_id], sender_id);
  1833. } else {
  1834. 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);
  1835. 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);
  1836. fflush(log_file);
  1837. scc_kill(sender_id, SIG_REJECT);
  1838. my_stats.msg_count++;
  1839. my_stats.distance += distance(node_id,sender_id);
  1840. }
  1841. cur_time = time(NULL);
  1842. cur_t = localtime(&cur_time);
  1843. 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);
  1844. fflush(log_file);
  1845. signals_enable();
  1846. }
  1847. void sig_REP_OFFERS_handler(int sender_id)
  1848. {
  1849. int offer_ans, i, one_core, j, old_cores_cnt;//num_of_idags, i, one_idag;
  1850. core_list *tmp_cores, *tmp_cores_prev, *tmp_cores_list;
  1851. inter_list *tmp_inter_list, *tmp_inter_prev;
  1852. offer_list *tmp_offer_list;
  1853. int data_array_local[2 * LINE_SIZE], error, str_len;
  1854. //RCCE_FLAG_STATUS receiver_status;
  1855. char error_str[64];
  1856. signals_disable();
  1857. cur_time = time(NULL);
  1858. cur_t = localtime(&cur_time);
  1859. 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);
  1860. fflush(log_file);
  1861. if (core_inter_head[sender_id] == NULL) printf("fail!\n");
  1862. else if (core_inter_head[sender_id]->type == AGENT_OFFER_SENT) {
  1863. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  1864. scc_kill(sender_id, SIG_ACK);
  1865. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  1866. my_stats.msg_count++;
  1867. my_stats.distance += distance(node_id,sender_id);
  1868. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), 2 * LINE_SIZE * sizeof(int), node_id);
  1869. if (error != RCCE_SUCCESS) {
  1870. RCCE_error_string(error, error_str, &str_len);
  1871. 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);
  1872. }
  1873. fprintf(log_file, "Num of offers is = %d\n",core_inter_head[sender_id]->data.off_arr.num_of_offers);
  1874. fflush(log_file);
  1875. //even if i am in a far req offer, my answer will be the first
  1876. for (j=0; j<core_inter_head[sender_id]->data.off_arr.num_of_offers; j++) {
  1877. //read(fd_r, &offer_ans, sizeof(int));
  1878. offer_ans = data_array_local[0];
  1879. fprintf(log_file, "offer_ans = %d\n",offer_ans);
  1880. fflush(log_file);
  1881. while (offer_ans != 0 && offer_ans != 1){
  1882. printf("I am %d and i am getting bizarre answer = %d from %d\n",node_id,offer_ans,sender_id);
  1883. //read(fd_r, &offer_ans, sizeof(int));
  1884. }
  1885. if (offer_ans == 0 && my_cores != NULL) { // && my_cores != NULL
  1886. for (i=0; i<core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores; i++)
  1887. for (tmp_cores = my_cores->next; tmp_cores != NULL; tmp_cores = tmp_cores->next)
  1888. if (tmp_cores->core_id == core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i]) {
  1889. fprintf(log_file,"core id = %d offered_to = %d\n",tmp_cores->core_id,tmp_cores->offered_to);
  1890. fflush(log_file);
  1891. if (tmp_cores->offered_to == sender_id) tmp_cores->offered_to = -1;
  1892. break;
  1893. }
  1894. if (app_state == APP_TERMINATED) {//app_terminated
  1895. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  1896. if (tmp_cores_list->offered_to != -1) break;
  1897. //if (tmp_cores_list != NULL && state ) state = AGENT_ZOMBIE;
  1898. //else state = AGENT_ENDING;
  1899. if (tmp_cores_list == NULL && state == AGENT_ZOMBIE) state = AGENT_ENDING;
  1900. }
  1901. /*tmp_cores = my_cores;
  1902. while (tmp_cores != NULL){
  1903. if (tmp_cores->offered_to == sender_id) tmp_cores->offered_to = -1;
  1904. tmp_cores = tmp_cores->next;
  1905. }*/
  1906. } 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.
  1907. if (my_idag == -1){ //I am an idag
  1908. DDS->num_of_cores -= core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores;
  1909. } else {//I am common node
  1910. /*first i must get the remaining time from my cores, before i change my core list*/
  1911. if (app_state != APP_TERMINATED) { //!app_terminated
  1912. old_Speedup = my_Speedup;
  1913. old_cores_cnt = my_cores_count;
  1914. my_cores_count -= core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores;
  1915. my_app.num_of_cores = my_cores_count;
  1916. my_Speedup = Speedup(my_app, my_cores_count);
  1917. fprintf(log_file,"Initialising removal new_cores_count = %d app_state = %d\n",my_cores_count,app_state);
  1918. fflush(log_file);
  1919. fprintf(app_log_file,"Initialising removal new_cores_count = %d app_state = %d\n",my_cores_count,app_state);
  1920. fflush(app_log_file);
  1921. if (app_state == RUNNING) {
  1922. chk_rem_num = old_cores_cnt-1;
  1923. chk_rem_count = 0;
  1924. sum_rem_time = 0;
  1925. app_state = RESIZING;
  1926. for(tmp_cores_list=my_cores->next; tmp_cores_list!=NULL; tmp_cores_list=tmp_cores_list->next) {//first is myself
  1927. one_core = tmp_cores_list->core_id;
  1928. if (core_inter_head[one_core] == NULL){
  1929. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  1930. core_inter_tail[one_core] = core_inter_head[one_core];
  1931. } else {
  1932. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  1933. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  1934. }
  1935. core_inter_tail[one_core]->type = APPOINT_WORK_NODE_PENDING;//CHK_REM_TIME;
  1936. core_inter_tail[one_core]->next = NULL;
  1937. if (core_inter_head[one_core]->next == NULL) {
  1938. //kill(pid_num[one_core], SIG_CHECK_REM_TIME);
  1939. scc_kill(one_core, SIG_CHECK_REM_TIME);
  1940. my_stats.msg_count++;
  1941. my_stats.distance += distance(node_id,one_core);
  1942. } 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",
  1943. node_id,one_core,core_inter_head[one_core]->type);
  1944. }
  1945. }
  1946. } else {
  1947. my_cores_count -= core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores;
  1948. my_app.num_of_cores = my_cores_count;
  1949. fprintf(log_file,"Initialising removal app finished new_cores_count = %d\n",my_cores_count);
  1950. fflush(log_file);
  1951. fprintf(app_log_file,"Initialising removal app finished new_cores_count = %d\n",my_cores_count);
  1952. fflush(app_log_file);
  1953. }
  1954. if (core_inter_head[my_idag] == NULL){
  1955. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  1956. core_inter_tail[my_idag] = core_inter_head[my_idag];
  1957. } else {
  1958. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  1959. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  1960. }
  1961. core_inter_tail[my_idag]->type = IDAG_REM_CORES_DDS;
  1962. 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));
  1963. core_inter_tail[my_idag]->data.app_cores[0] = core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores;
  1964. for(i=1; i<=core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores; i++) {
  1965. one_core = core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i-1];
  1966. if (app_state != APP_TERMINATED) {
  1967. tmp_inter_prev = core_inter_head[one_core];
  1968. tmp_inter_list = core_inter_head[one_core]->next;
  1969. } else {
  1970. tmp_inter_prev = NULL;
  1971. tmp_inter_list = core_inter_head[one_core];
  1972. }
  1973. while (tmp_inter_list != NULL)
  1974. if (tmp_inter_list->type == INIT_WORK_NODE || tmp_inter_list->type == APPOINT_WORK_NODE || tmp_inter_list->type == INIT_WORK_NODE_PENDING
  1975. || tmp_inter_list->type == APPOINT_WORK_NODE_PENDING) {
  1976. //|| (tmp_inter_list->type == APPOINT_WORK_NODE_PENDING && tmp_inter_prev != NULL)) {
  1977. fprintf(log_file, "Removing in rem offers one node of %d with inter = %d\n",one_core,tmp_inter_list->type);
  1978. fflush(log_file);
  1979. if (tmp_inter_prev == NULL) {
  1980. core_inter_head[one_core] = core_inter_head[one_core]->next;
  1981. free(tmp_inter_list);
  1982. tmp_inter_list = core_inter_head[one_core];
  1983. } else {
  1984. tmp_inter_prev->next = tmp_inter_list->next;
  1985. if (tmp_inter_prev->next == NULL) core_inter_tail[one_core] = tmp_inter_prev;
  1986. free(tmp_inter_list);
  1987. tmp_inter_list = tmp_inter_prev->next;
  1988. }
  1989. } else {
  1990. tmp_inter_prev = tmp_inter_list;
  1991. tmp_inter_list = tmp_inter_list->next;
  1992. }
  1993. if (app_state != APP_TERMINATED) {
  1994. if (core_inter_head[one_core] == NULL) {
  1995. fprintf(log_file,"No interaction with %d. Theoritically impossible\n",one_core);
  1996. fflush(log_file);
  1997. } else if (core_inter_head[one_core]->type == INIT_WORK_NODE_PENDING) {
  1998. fprintf(log_file,"I offered my new core %d. I will clear the interaction\n",one_core);
  1999. fflush(log_file);
  2000. tmp_inter_list = core_inter_head[one_core];
  2001. core_inter_head[one_core] = core_inter_head[one_core]->next;
  2002. if (core_inter_head[one_core] == NULL) core_inter_tail[one_core] = NULL;
  2003. else send_next_signal(core_inter_head[one_core], one_core);
  2004. free(tmp_inter_list);
  2005. } else if (core_inter_head[one_core]->type == INIT_WORK_NODE || core_inter_head[one_core]->type == APPOINT_WORK_NODE) {
  2006. fprintf(log_file,"Invalidating %d. Interaction is %d\n",one_core,core_inter_head[one_core]->type);
  2007. fflush(log_file);
  2008. core_inter_head[one_core]->data.work_time = -1;
  2009. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  2010. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  2011. core_inter_tail[one_core]->type = REMOVED_NODE_REM_TIME;//APPOINT_WORK_NODE_PENDING;//CHK_REM_TIME;
  2012. core_inter_tail[one_core]->next = NULL;
  2013. } else if (core_inter_head[one_core]->type == APPOINT_WORK_NODE_PENDING) {
  2014. fprintf(log_file,"Everything ok %d.\n",one_core);
  2015. fflush(log_file);
  2016. core_inter_head[one_core]->type = REMOVED_NODE_REM_TIME;
  2017. } else {
  2018. fprintf(log_file,"Another interaction with %d. Interaction is %d\n",one_core,core_inter_head[one_core]->type);
  2019. fflush(log_file);
  2020. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  2021. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  2022. core_inter_tail[one_core]->type = REMOVED_NODE_REM_TIME;//APPOINT_WORK_NODE_PENDING;//CHK_REM_TIME;
  2023. core_inter_tail[one_core]->next = NULL;
  2024. }
  2025. }
  2026. tmp_cores = my_cores;
  2027. tmp_cores_prev = NULL;
  2028. while (tmp_cores != NULL && tmp_cores->core_id != core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i-1]){
  2029. tmp_cores_prev = tmp_cores;
  2030. tmp_cores = tmp_cores->next;
  2031. }
  2032. if (tmp_cores == NULL) printf("strangely offered core is not in my_cores list\n");
  2033. else {
  2034. if (tmp_cores_prev == NULL) {
  2035. printf("i fucking offered my agent core!!!\n");
  2036. my_cores = my_cores->next;
  2037. } else if (tmp_cores == my_cores_tail){
  2038. my_cores_tail = tmp_cores_prev;
  2039. my_cores_tail->next = NULL;
  2040. } else tmp_cores_prev->next = tmp_cores->next;
  2041. free(tmp_cores);
  2042. }
  2043. core_inter_tail[my_idag]->data.app_cores[i] = core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i-1];
  2044. }
  2045. core_inter_tail[my_idag]->next = NULL;
  2046. if (core_inter_head[my_idag]->next == NULL) {
  2047. //kill(pid_num[my_idag], SIG_REM_CORES_DDS);
  2048. scc_kill(my_idag, SIG_REM_CORES_DDS);
  2049. my_stats.msg_count++;
  2050. my_stats.distance += distance(node_id,my_idag);
  2051. } else printf("I did not send rem signal!\n");
  2052. cur_time = time(NULL);
  2053. cur_t = localtime(&cur_time);
  2054. 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);
  2055. fprintf(app_log_file, "my cores are:");
  2056. for (tmp_cores_list=my_cores; tmp_cores_list!=NULL; tmp_cores_list=tmp_cores_list->next) {
  2057. //fprintf(log_file, " %d", tmp_cores_list->core_id);
  2058. fprintf(app_log_file, " %d", tmp_cores_list->core_id);
  2059. //printf(" %d",tmp_cores_list->core_id);
  2060. }
  2061. fprintf(app_log_file, "\n");
  2062. fflush(app_log_file);
  2063. if (app_state == APP_TERMINATED) {//app_terminated
  2064. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  2065. if (tmp_cores_list->offered_to != -1) break;
  2066. if (tmp_cores_list == NULL && state == AGENT_ZOMBIE) state = AGENT_ENDING;
  2067. }
  2068. }
  2069. } 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);
  2070. }
  2071. /*} else if (core_inter_head[sender_id]->type == FAR_REQ_OFFER_SENT) {
  2072. sem_getvalue(&node_sem[node_id],&i);
  2073. fprintf(log_file, "In 8 Trying to acquire semaphore. Sem value = %d\n",i);
  2074. fflush(log_file);
  2075. kill(info->si_pid, SIG_ACK);
  2076. sem_wait(&node_sem[node_id]);
  2077. sem_getvalue(&node_sem[node_id],&i);
  2078. fprintf(log_file, "Semaphore acquired successfully. Sem value = %d\n",i);
  2079. fflush(log_file);
  2080. my_stats.msg_count++;
  2081. my_stats.distance += distance(node_id,sender_id);
  2082. //printf("open9 idag_id=%d node_id=%d fifo_name=%s\n",idag_id,node_id,fifo_name);
  2083. fd_r = open(fifo_name, O_RDONLY);
  2084. if (fd_r == -1) {
  2085. perror("opening reading pipe");
  2086. signals_enable();
  2087. return;
  2088. } else {
  2089. fprintf(log_file, "I went through open\n");
  2090. fflush(log_file);
  2091. }
  2092. if (far_man_offers->sender == node_id) {
  2093. read(fd_r, &offer_ans, sizeof(int));
  2094. printf("I am node %d and my offer answer is %d interaction=%d\n",node_id,offer_ans,core_inter_head[sender_id]->type);
  2095. if (offer_ans == 0) {
  2096. tmp_cores = my_cores;
  2097. while (tmp_cores != NULL){
  2098. if (tmp_cores->offered_to == sender_id) tmp_cores->offered_to = -1;
  2099. tmp_cores = tmp_cores->next;
  2100. }
  2101. } 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.
  2102. if (DDS->agent_id != node_id) printf("Fuck i am not first in my DDS list!!\n");
  2103. DDS->num_of_cores -= core_inter_head[sender_id]->data.my_offer.num_of_cores;
  2104. } else printf("I am %d Answer different than 0 or 1 in far from %d!!\n",node_id,sender_id);
  2105. //if (core_inter_head[sender_id]->type == FAR_REQ_OFFER_SENT) {//FAR_REQ_OFFER
  2106. tmp_offer_list = far_man_offers;
  2107. far_man_offers = far_man_offers->next;
  2108. free(tmp_offer_list);
  2109. }
  2110. while (far_man_offers != NULL){
  2111. //printf("kai edw ftamw\n");
  2112. read(fd_r, &offer_ans, sizeof(int));
  2113. printf("I am node %d and far offer answer for node %d is %d\n",node_id,far_man_offers->sender,offer_ans);
  2114. if (offer_ans == 0 || offer_ans == 1){
  2115. *far_man_offers->answer = offer_ans;
  2116. if (core_inter_head[far_man_offers->sender]->type == REP_AGENT_OFFER_PENDING){
  2117. kill(pid_num[far_man_offers->sender],SIG_REP_OFFERS);
  2118. core_inter_head[far_man_offers->sender]->type = REP_AGENT_OFFER_SENT;
  2119. my_stats.msg_count++;
  2120. my_stats.distance += distance(node_id,far_man_offers->sender);
  2121. } else printf("Apparently lists are poutana ola\n");
  2122. } else printf("Far Answer different than 0 or 1!!\n");
  2123. tmp_offer_list = far_man_offers;
  2124. far_man_offers = far_man_offers->next;
  2125. free(tmp_offer_list);
  2126. }
  2127. sem_getvalue(&node_sem_out[sender_id],&i);
  2128. fprintf(log_file, "sender_id = %d 1Sem value = %d\n",sender_id,i);
  2129. fflush(log_file);
  2130. if (sem_post(&node_sem_out[sender_id]) == -1){
  2131. printf("i am %d ta hpiame pali sender_id = %d\n",node_id,sender_id);
  2132. perror("sem_post");
  2133. }
  2134. sem_getvalue(&node_sem_out[sender_id],&i);
  2135. fprintf(log_file, "Sem value = %d\n",i);
  2136. fflush(log_file);
  2137. //sem_post(&node_sem[node_id]);
  2138. //sem_post(&node_sem[sender_id]);
  2139. far_req_app.A=-1.0;
  2140. far_req_app.var=-1.0;
  2141. far_req_app.num_of_cores=-1;
  2142. far_req_or_sender = -1;
  2143. far_reg.C = -1;
  2144. far_reg.r = -1; */
  2145. } else printf("I am %d and fail 2\n",node_id);
  2146. tmp_inter_list = core_inter_head[sender_id];
  2147. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  2148. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  2149. else send_next_signal(core_inter_head[sender_id], sender_id);
  2150. free(tmp_inter_list);
  2151. cur_time = time(NULL);
  2152. cur_t = localtime(&cur_time);
  2153. 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);
  2154. fflush(log_file);
  2155. signals_enable();
  2156. }
  2157. void sig_INIT_AGENT_handler(int sender_id)
  2158. {
  2159. int i, tmp;
  2160. char *fifo_name;
  2161. core_list *tmp_core;
  2162. int data_array_local[3 * LINE_SIZE], error, str_len;
  2163. //RCCE_FLAG_STATUS receiver_status;
  2164. char error_str[64];
  2165. signals_disable();
  2166. cur_time = time(NULL);
  2167. cur_t = localtime(&cur_time);
  2168. 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);
  2169. fflush(log_file);
  2170. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  2171. scc_kill(sender_id, SIG_ACK);
  2172. RCCE_wait_until(flag_data_written, RCCE_FLAG_SET);
  2173. my_stats.msg_count++;
  2174. my_stats.distance += distance(node_id,sender_id);
  2175. error = RCCE_get((t_vcharp)(&data_array_local[0]), (t_vcharp)(&data_array[0]), 3 * LINE_SIZE * sizeof(int), node_id);
  2176. if (error != RCCE_SUCCESS) {
  2177. RCCE_error_string(error, error_str, &str_len);
  2178. 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);
  2179. }
  2180. if (my_app.num_of_cores != -1) { //Very very very important!!!
  2181. printf ("I am already managing an app!!! Fail!!!!\n");
  2182. while (my_cores != NULL){
  2183. tmp_core = my_cores;
  2184. my_cores = my_cores->next;
  2185. free(tmp_core);
  2186. }
  2187. my_cores_tail = NULL;
  2188. my_cores_count = 0;
  2189. }
  2190. //read(fd_r, &my_app, sizeof(app));
  2191. my_app.id = data_array_local[0];
  2192. memcpy(&my_app.A,&data_array_local[1],sizeof(int));
  2193. memcpy(&my_app.var,&data_array_local[2],sizeof(int));
  2194. memcpy(&my_app.workld,&data_array_local[3],sizeof(int));
  2195. my_app.num_of_cores = data_array_local[4];
  2196. my_cores_count = my_app.num_of_cores;//+1;
  2197. if (my_cores == NULL) {
  2198. my_cores = (core_list *) malloc(sizeof(core_list));
  2199. my_cores_tail = my_cores;
  2200. } else {
  2201. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  2202. my_cores_tail = my_cores_tail->next;
  2203. }
  2204. my_cores_tail->core_id = node_id;
  2205. my_cores_tail->offered_to = -1;
  2206. my_cores_tail->next = NULL;
  2207. //I want myself to be first in my_cores list
  2208. for (i=0; i<my_app.num_of_cores; i++){
  2209. //read(fd_r, &tmp, sizeof(int));
  2210. tmp = data_array_local[i+LINE_SIZE];
  2211. if (tmp != node_id){
  2212. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  2213. my_cores_tail = my_cores_tail->next;
  2214. //read(fd_r, &my_cores_tail->core_id, sizeof(int));
  2215. my_cores_tail->core_id = tmp;
  2216. my_cores_tail->offered_to = -1;
  2217. my_cores_tail->next = NULL;
  2218. }
  2219. }
  2220. //read(fd_r, &my_app_times[0], sizeof(my_time_stamp));
  2221. //read(fd_r, &my_app_times[1], sizeof(my_time_stamp));
  2222. my_app_times[0].tm_sec = data_array_local[2*LINE_SIZE];
  2223. my_app_times[0].tm_min = data_array_local[2*LINE_SIZE+1];
  2224. my_app_times[0].tm_hour = data_array_local[2*LINE_SIZE+2];
  2225. my_app_times[1].tm_sec = data_array_local[2*LINE_SIZE+3];
  2226. my_app_times[1].tm_min = data_array_local[2*LINE_SIZE+4];
  2227. my_app_times[1].tm_hour = data_array_local[2*LINE_SIZE+5];
  2228. printf("I am new agent with id %d and app with A=%.2f, var=%.2f and %d my_cores_count = %d cores: \n"
  2229. ,node_id,my_app.A,my_app.var,my_app.num_of_cores,my_cores_count);
  2230. my_Speedup = Speedup(my_app, my_app.num_of_cores);
  2231. /*if (core_inter_head[my_idag] == NULL){
  2232. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  2233. core_inter_tail[my_idag] = core_inter_head[my_idag];
  2234. } else {
  2235. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  2236. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  2237. }
  2238. core_inter_tail[my_idag]->type = IDAG_ADD_CORES_DDS;
  2239. core_inter_tail[my_idag]->data.app_cores = (int *)malloc((my_cores_count+1)*sizeof(int));
  2240. core_inter_tail[my_idag]->data.app_cores[0] = my_cores_count;
  2241. tmp_core = my_cores;
  2242. i=1;
  2243. while (tmp_core != NULL){
  2244. core_inter_tail[my_idag]->data.app_cores[i] = tmp_core->core_id;
  2245. tmp_core = tmp_core->next;
  2246. i++;
  2247. }
  2248. core_inter_tail[my_idag]->next = NULL;
  2249. if (core_inter_head[my_idag]->next == NULL) {
  2250. kill(pid_num[my_idag], SIG_ADD_CORES_DDS);
  2251. my_stats.msg_count++;
  2252. my_stats.distance += distance(node_id,my_idag);
  2253. } else printf("I am %d and i didn't call add!! with interaction %d\n",node_id,core_inter_head[my_idag]->type);
  2254. */
  2255. if (my_agent != -1) {
  2256. printf("I am %d and i do this agent switch\n",node_id);
  2257. if (state == WORKING_NODE || state == WORKING_NODE_IDLE_INIT) {
  2258. if (core_inter_head[my_agent] == NULL){
  2259. core_inter_head[my_agent] = (inter_list *) malloc(sizeof(inter_list));
  2260. core_inter_tail[my_agent] = core_inter_head[my_agent];
  2261. } else {
  2262. core_inter_tail[my_agent]->next = (inter_list *) malloc(sizeof(inter_list));
  2263. core_inter_tail[my_agent] = core_inter_tail[my_agent]->next;
  2264. }
  2265. core_inter_tail[my_agent]->type = REP_CHK_REM_TIME;
  2266. core_inter_tail[my_agent]->data.work_time = upper_work_bound - time_worked;
  2267. core_inter_tail[my_agent]->next = NULL;
  2268. }
  2269. upper_work_bound = 0;
  2270. my_agent = -1;
  2271. }
  2272. 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);
  2273. cur_time = time(NULL);
  2274. cur_t = localtime(&cur_time);
  2275. 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);
  2276. fprintf(log_file, "my cores are:");
  2277. for (tmp_core=my_cores; tmp_core!=NULL; tmp_core=tmp_core->next) fprintf(log_file, " %d", tmp_core->core_id);
  2278. fprintf(log_file, "\n");
  2279. printf("I am %d and about to do an selfopt agent with my state before change = %d\n",node_id,state);
  2280. if (state == IDLE_INIT_MAN || state == INIT_MANAGER || state == INIT_MANAGER_SEND_OFFERS || state == INIT_MAN_CHK_OFFERS || state == WORKING_NODE_IDLE_INIT) {
  2281. fprintf(log_file,"I have pending init\n");
  2282. pending_state = state;
  2283. state = AGENT_INIT_STATE;
  2284. } else state = AGENT_INIT_STATE;
  2285. cur_time = time(NULL);
  2286. cur_t = localtime(&cur_time);
  2287. 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);
  2288. fflush(log_file);
  2289. signals_enable();
  2290. }
  2291. void sig_REJECT_handler(int sender_id)
  2292. {
  2293. int agent_id, i, j;
  2294. inter_list *tmp_inter_list;
  2295. core_list *tmp_cores_list;
  2296. target_list *tmp_target_list;
  2297. offer_list *tmp_offer_list;
  2298. signals_disable();
  2299. cur_time = time(NULL);
  2300. cur_t = localtime(&cur_time);
  2301. 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);
  2302. fflush(log_file);
  2303. if (core_inter_head[sender_id] != NULL){
  2304. 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);
  2305. fflush(log_file);
  2306. }
  2307. if (core_inter_head[sender_id] == NULL){ //edw tha mpei otan exw allaksei agent
  2308. printf("I am %d in sig_reject and i have null interaction with sender %d\n",node_id,sender_id);
  2309. } 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
  2310. || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING || core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING) {
  2311. if (core_inter_head[sender_id]->type == IDAG_REQ_DDS || core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING) {
  2312. init_DDS_replies++;
  2313. fprintf(log_file, "One init_req_dds has been rejected by %d!\n",sender_id);
  2314. fflush(log_file);
  2315. if (init_areas_num == 1) {
  2316. fprintf(log_file, "My only init area was rejected. my state is %d my pending_state = %d\n",state,pending_state);
  2317. fflush(log_file);
  2318. state = INIT_MANAGER_SEND_OFFERS;
  2319. }
  2320. } else if (core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS || core_inter_head[sender_id]->type == SELFOPT_IDAG_REQ_DDS_PENDING) {
  2321. selfopt_DDS_replies++;
  2322. fprintf(log_file, "One selfopt_req_dds has been rejected by %d!\n",sender_id);
  2323. fflush(log_file);
  2324. }
  2325. if ((core_inter_head[sender_id]->type == IDAG_REQ_DDS || core_inter_head[sender_id]->type == IDAG_REQ_DDS_PENDING)
  2326. && init_DDS_replies == init_DDS_idags && init_idags_areas_replies == init_areas_num)
  2327. for (tmp_target_list = init_targets_head; tmp_target_list != NULL; tmp_target_list = tmp_target_list->next){
  2328. agent_id = tmp_target_list->target;
  2329. for (tmp_inter_list = core_inter_head[agent_id]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
  2330. if (tmp_inter_list->type == FAR_INIT_REQ || tmp_inter_list->type == FAR_REQ_MAN) break;
  2331. if (tmp_inter_list == NULL) {
  2332. if (core_inter_head[agent_id] == NULL){
  2333. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  2334. core_inter_tail[agent_id] = core_inter_head[agent_id];
  2335. } else {
  2336. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  2337. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  2338. }
  2339. core_inter_tail[agent_id]->type = AGENT_REQ_CORES_PENDING;
  2340. core_inter_tail[agent_id]->data.reg_arr.num_of_regions = tmp_target_list->num_of_regions;
  2341. core_inter_tail[agent_id]->data.reg_arr.region_arr = (region *) malloc(tmp_target_list->num_of_regions * sizeof(region));
  2342. if (tmp_target_list->target == node_id) printf("Why is this here node_id=%d\n",node_id);
  2343. fprintf(log_file,"Init req target = %d, num_of_regions = %d. Αreas:",tmp_target_list->target,tmp_target_list->num_of_regions);
  2344. for (i=0; i<tmp_target_list->num_of_regions; i++) {
  2345. core_inter_tail[agent_id]->data.reg_arr.region_arr[i] = tmp_target_list->region_arr[i];
  2346. fprintf(log_file," (%d,%d),",tmp_target_list->region_arr[i].C,tmp_target_list->region_arr[i].r);
  2347. }
  2348. fprintf(log_file,"\n");
  2349. fflush(log_file);
  2350. core_inter_tail[agent_id]->next = NULL;
  2351. //kill(pid_num[agent_id], SIG_REQ_CORES);
  2352. if (core_inter_head[agent_id]->next == NULL) {
  2353. //kill(pid_num[agent_id], SIG_REQ_CORES);
  2354. scc_kill(agent_id, SIG_REQ_CORES);
  2355. my_stats.msg_count++;
  2356. my_stats.distance += distance(node_id,agent_id);
  2357. } 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
  2358. scc_kill(agent_id, SIG_REQ_CORES);
  2359. //kill(pid_num[agent_id], SIG_REQ_CORES);
  2360. my_stats.msg_count++;
  2361. my_stats.distance += distance(node_id,agent_id);
  2362. } 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);
  2363. } else
  2364. printf("I am %d and i did not send local requests to %d because he is my far manager\n",node_id,agent_id);
  2365. }
  2366. 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)
  2367. for (tmp_target_list = selfopt_targets_head; tmp_target_list != NULL; tmp_target_list = tmp_target_list->next){
  2368. agent_id = tmp_target_list->target;
  2369. if (core_inter_head[agent_id] == NULL){
  2370. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  2371. core_inter_tail[agent_id] = core_inter_head[agent_id];
  2372. } else {
  2373. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  2374. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  2375. }
  2376. core_inter_tail[agent_id]->type = SELFOPT_REQ_CORES_PENDING;
  2377. core_inter_tail[agent_id]->data.reg_arr.num_of_regions = tmp_target_list->num_of_regions;
  2378. core_inter_tail[agent_id]->data.reg_arr.region_arr = (region *) malloc(tmp_target_list->num_of_regions * sizeof(region));
  2379. if (tmp_target_list->target == node_id) printf("selfopt Why is this here node_id=%d\n",node_id);
  2380. fprintf(log_file,"Selfopt req target = %d, num_of_regions = %d. Αreas:",tmp_target_list->target,tmp_target_list->num_of_regions);
  2381. for (i=0; i<tmp_target_list->num_of_regions; i++) {
  2382. core_inter_tail[agent_id]->data.reg_arr.region_arr[i] = tmp_target_list->region_arr[i];
  2383. fprintf(log_file," (%d,%d),",tmp_target_list->region_arr[i].C,tmp_target_list->region_arr[i].r);
  2384. }
  2385. fprintf(log_file,"\n");
  2386. fflush(log_file);
  2387. core_inter_tail[agent_id]->next = NULL;
  2388. if (core_inter_head[agent_id]->next == NULL) {
  2389. //kill(pid_num[agent_id], SIG_REQ_CORES);
  2390. scc_kill(agent_id, SIG_REQ_CORES);
  2391. my_stats.msg_count++;
  2392. my_stats.distance += distance(node_id,agent_id);
  2393. } else if (agent_id == sender_id && core_inter_head[agent_id]->next->type == SELFOPT_REQ_CORES_PENDING) {
  2394. //kill(pid_num[agent_id], SIG_REQ_CORES);
  2395. scc_kill(agent_id, SIG_REQ_CORES);
  2396. my_stats.msg_count++;
  2397. my_stats.distance += distance(node_id,agent_id);
  2398. } 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);
  2399. }
  2400. tmp_inter_list = core_inter_head[sender_id];
  2401. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  2402. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  2403. 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 &&
  2404. send_next_signal(core_inter_head[sender_id], sender_id);
  2405. free(tmp_inter_list);
  2406. } 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
  2407. || core_inter_head[sender_id]->type == AGENT_REQ_CORES_PENDING || core_inter_head[sender_id]->type == FAR_REQ_IDAG_REQ_DDS_PENDING
  2408. || core_inter_head[sender_id]->type == IDAG_FIND_IDAGS_PENDING || core_inter_head[sender_id]->type == SELFOPT_IDAG_FIND_IDAGS_PENDING
  2409. || core_inter_head[sender_id]->type == SELFOPT_REQ_CORES_PENDING || core_inter_head[sender_id]->type == FAR_REQ_CORES_PENDING
  2410. || core_inter_head[sender_id]->type == REP_FAR_INIT_REQ) {
  2411. tmp_inter_list = core_inter_head[sender_id];
  2412. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  2413. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  2414. else send_next_signal(core_inter_head[sender_id], sender_id);
  2415. free(tmp_inter_list);
  2416. /*} else if (core_inter_head[sender_id]->type == FAR_REQ_OFFER) {//FAR_REQ_OFFER_SENT
  2417. if (far_man_offers != NULL && far_man_offers->sender == node_id) {
  2418. tmp_cores_list = my_cores;
  2419. while (tmp_cores_list != NULL){
  2420. if (tmp_cores_list->offered_to == sender_id) tmp_cores_list->offered_to = -1;
  2421. tmp_cores_list = tmp_cores_list->next;
  2422. }
  2423. tmp_offer_list = far_man_offers;
  2424. far_man_offers = far_man_offers->next;
  2425. free(tmp_offer_list);
  2426. }
  2427. while (far_man_offers != NULL){
  2428. printf("I am node %d in reject and far offer answer for node %d is 0\n",node_id,far_man_offers->sender);
  2429. *far_man_offers->answer = 0;
  2430. if (core_inter_head[far_man_offers->sender]->type == REP_AGENT_OFFER_PENDING){
  2431. kill(pid_num[far_man_offers->sender],SIG_REP_OFFERS);
  2432. core_inter_head[far_man_offers->sender]->type = REP_AGENT_OFFER_SENT;
  2433. } else printf("I am %d and Apparently lists are poutana ola\n",node_id);
  2434. tmp_offer_list = far_man_offers;
  2435. far_man_offers = far_man_offers->next;
  2436. free(tmp_offer_list);
  2437. }
  2438. far_req_app.A=-1.0;
  2439. far_req_app.var=-1.0;
  2440. far_req_app.num_of_cores=-1;
  2441. far_req_or_sender = -1;
  2442. far_reg.C = -1;
  2443. far_reg.r = -1;
  2444. tmp_inter_list = core_inter_head[sender_id];
  2445. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  2446. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  2447. else send_next_signal(core_inter_head[sender_id], sender_id);
  2448. free(tmp_inter_list);
  2449. */
  2450. } else if (core_inter_head[sender_id]->type == REP_IDAG_FIND_IDAGS) {
  2451. free(core_inter_head[sender_id]->data.idags_in_reg);
  2452. tmp_inter_list = core_inter_head[sender_id];
  2453. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  2454. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  2455. else send_next_signal(core_inter_head[sender_id], sender_id);
  2456. free(tmp_inter_list);
  2457. } else if (core_inter_head[sender_id]->type == REP_IDAG_REQ_DDS) {
  2458. free(core_inter_head[sender_id]->data.agents_in_reg);
  2459. tmp_inter_list = core_inter_head[sender_id];
  2460. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  2461. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  2462. else send_next_signal(core_inter_head[sender_id], sender_id);
  2463. free(tmp_inter_list);
  2464. /*} else if (core_inter_head[sender_id]->type == FAR_REQ_MAN_APPOINT) {
  2465. agent_id = core_inter_head[sender_id]->data.far_req.orig_sender;
  2466. if (core_inter_head[agent_id] == NULL){
  2467. core_inter_head[agent_id] = (inter_list *) malloc(sizeof(inter_list));
  2468. core_inter_tail[agent_id] = core_inter_head[agent_id];
  2469. } else {
  2470. core_inter_tail[agent_id]->next = (inter_list *) malloc(sizeof(inter_list));
  2471. core_inter_tail[agent_id] = core_inter_tail[agent_id]->next;
  2472. }
  2473. core_inter_tail[agent_id]->type = ABORT_FAR_MAN;
  2474. core_inter_tail[agent_id]->data.far_req_man = sender_id;
  2475. core_inter_tail[agent_id]->next = NULL;
  2476. if (core_inter_head[agent_id]->next == NULL) {
  2477. kill(pid_num[agent_id], SIG_REMOVE_FAR_MAN);
  2478. my_stats.msg_count++;
  2479. my_stats.distance += distance(node_id,agent_id);
  2480. }
  2481. tmp_inter_list = core_inter_head[sender_id];
  2482. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  2483. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  2484. else send_next_signal(core_inter_head[sender_id], sender_id);
  2485. free(tmp_inter_list); */
  2486. } else if (core_inter_head[sender_id]->type == REP_AGENT_REQ_CORES) {
  2487. for (j=0; j<core_inter_head[sender_id]->data.off_arr.num_of_offers; j++)
  2488. if (my_cores != NULL)
  2489. for (i=0; i<core_inter_head[sender_id]->data.off_arr.offer_arr[j].num_of_cores; i++)
  2490. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  2491. if (tmp_cores_list->core_id == core_inter_head[sender_id]->data.off_arr.offer_arr[j].offered_cores[i]) {
  2492. tmp_cores_list->offered_to = -1;
  2493. break;
  2494. }
  2495. free(core_inter_head[sender_id]->data.off_arr.offer_arr);
  2496. tmp_inter_list = core_inter_head[sender_id];
  2497. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  2498. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  2499. else send_next_signal(core_inter_head[sender_id], sender_id);
  2500. free(tmp_inter_list);
  2501. } else if (core_inter_head[sender_id]->type == INIT_APP) {
  2502. printf("I am 0 and %d rejected my init_app req\n",sender_id);
  2503. fprintf(log_file,"I am 0 and %d rejected my init_app req\n",sender_id);
  2504. fflush(log_file);
  2505. if (init_pending_head == NULL){
  2506. init_pending_head = (inter_list *) malloc(sizeof(inter_list));
  2507. init_pending_tail = init_pending_head;
  2508. } else {
  2509. init_pending_tail->next = (inter_list *) malloc(sizeof(inter_list));
  2510. init_pending_tail = init_pending_tail->next;
  2511. }
  2512. init_pending_tail->type = INIT_APP;
  2513. init_pending_tail->data.new_app = core_inter_head[sender_id]->data.new_app;
  2514. init_pending_tail->data.new_app.num_of_cores = sender_id;
  2515. init_pending_tail->next = NULL;
  2516. tmp_inter_list = core_inter_head[sender_id];
  2517. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  2518. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  2519. else send_next_signal(core_inter_head[sender_id], sender_id);
  2520. free(tmp_inter_list);
  2521. } else if (core_inter_head[sender_id]->type == REP_CHK_REM_TIME) {
  2522. 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);
  2523. 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);
  2524. fflush(log_file);
  2525. tmp_inter_list = core_inter_head[sender_id];
  2526. core_inter_head[sender_id] = core_inter_head[sender_id]->next;
  2527. if (core_inter_head[sender_id] == NULL) core_inter_tail[sender_id] = NULL;
  2528. else send_next_signal(core_inter_head[sender_id], sender_id);
  2529. free(tmp_inter_list);
  2530. } else {
  2531. 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);
  2532. fprintf(log_file,"I am in sig_reject and i have interaction with sender interaction = %d\n",core_inter_head[sender_id]->type);
  2533. fflush(log_file);
  2534. }
  2535. cur_time = time(NULL);
  2536. cur_t = localtime(&cur_time);
  2537. 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);
  2538. fflush(log_file);
  2539. signals_enable();
  2540. }