common_node.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. #include "common_node.h"
  2. //extern int *pid_num;
  3. extern int num_idags, node_id ,my_idag;//, fd_log;
  4. extern FILE *log_file;
  5. extern core_states state;
  6. core_states pending_state=NO_PENDING_STATE;
  7. extern inter_list **core_inter_head,**core_inter_tail;
  8. extern app my_app;
  9. extern app init_app;
  10. extern app far_req_app;
  11. extern metrics my_stats;
  12. extern int far_req_or_sender;
  13. extern int *idag_mask, *idag_id_arr;
  14. extern int *Cl_x_max_arr, *Cl_y_max_arr;
  15. extern int DDS_count, my_cores_count;
  16. extern DDS_list *DDS, *DDS_tail;
  17. extern core_list *my_cores, *my_cores_tail;
  18. extern offer_list *init_man_offers, *selfopt_man_offers;
  19. extern offer_list *far_man_offers, *far_man_offers_tail;
  20. extern int far_list_count, far_man_agent_count;
  21. extern int my_agent, time_worked;
  22. extern int debug_global;
  23. extern time_t cur_time;
  24. extern struct tm *cur_t;
  25. extern struct sigevent sev;
  26. extern struct itimerspec its, chk_timer;
  27. extern timer_t timerid;
  28. extern long int selfopt_time_rem;//-1 means it is not set
  29. extern long int upper_work_bound;
  30. extern int time_for_farman;
  31. extern char scen_num[4];
  32. extern int chk_rem_count, chk_rem_num, sum_rem_time;
  33. extern float old_Speedup, my_Speedup;
  34. extern int nodes_ended_cnt;//, app_terminated;
  35. int my_x, my_y, init_areas_num;
  36. int init_DDS_replies, selfopt_DDS_replies, init_DDS_idags, selfopt_DDS_idags, selfopt_interval=200, init_idags_areas_replies, max_cores_count;
  37. target_list *init_targets_head=NULL, *init_targets_tail;
  38. target_list *selfopt_targets_head=NULL, *selfopt_targets_tail;
  39. FILE *app_log_file;
  40. application_states app_state;
  41. my_time_stamp init_app_times[2], my_app_times[2];
  42. extern int *sig_array, *data_array, NUES;
  43. extern RCCE_FLAG flag_signals_enabled,flag_data_written;
  44. void common_node_actions(char scen_num[4]){
  45. offer_list *tmp_offer_list;
  46. int one_core, i, old_cores_cnt;
  47. core_list *tmp_cores_list;
  48. target_list *tmp_target_list;
  49. inter_list *tmp_inter_list, *tmp_inter_prev;
  50. FILE *init_areas_file;//, *selfopt_areas_file;
  51. char init_areas_file_name[64], app_log_file_name[64];//selfopt_areas_file_name[32],
  52. int C,r,init_areas_cnt, init_areas_sent;//area_cnt,selfopt_areas=0,
  53. int time_per_node, time_left, time_to_work, init_core_found=0;
  54. int new_agent, min_dist=-1, tmp_int, j;
  55. //int new_x, new_y;//, selfopt_areas_sent;
  56. int selfopt_r;
  57. DDS_count=0;
  58. my_cores_count=0;
  59. my_stats.msg_count=0;
  60. my_stats.message_size=0;
  61. my_stats.distance=0;
  62. my_stats.app_turnaround=0;
  63. my_stats.comp_effort=0;
  64. my_stats.cores_utilized=0;
  65. my_stats.times_accessed=0;
  66. init_app_times[0].tm_sec = 0;
  67. init_app_times[0].tm_min = 0;
  68. init_app_times[0].tm_hour = 0;
  69. init_app_times[1].tm_sec = 0;
  70. init_app_times[1].tm_min = 0;
  71. init_app_times[1].tm_hour = 0;
  72. my_app_times[0].tm_sec = 0;
  73. my_app_times[0].tm_min = 0;
  74. my_app_times[0].tm_hour = 0;
  75. my_app_times[1].tm_sec = 0;
  76. my_app_times[1].tm_min = 0;
  77. my_app_times[1].tm_hour = 0;
  78. //printf("I Am %d\n",node_id);
  79. DDS=NULL;
  80. my_cores=NULL;
  81. selfopt_r = (int) (1.5 * (X_max / num_idags_x));
  82. install_signal_handlers();
  83. //sig_SEGV_enable();
  84. sev.sigev_notify = SIGEV_SIGNAL;
  85. sev.sigev_signo = SIG_TIMER;
  86. sev.sigev_value.sival_ptr = &timerid;
  87. if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) printf("timer_create error\n");
  88. its.it_value.tv_sec = 0;
  89. its.it_interval.tv_sec = 0;//its.it_value.tv_sec;
  90. its.it_interval.tv_nsec = 0;
  91. log_file = create_log_file(node_id,scen_num);
  92. cur_time = time(NULL);
  93. cur_t = localtime(&cur_time);
  94. fprintf(log_file, "[%d:%d:%d]: Initialisation complete\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  95. fflush(log_file);
  96. RCCE_barrier(&RCCE_COMM_WORLD);
  97. my_x = node_id % X_max;
  98. my_y = node_id / X_max;
  99. state = IDLE_CORE;
  100. RCCE_flag_write(&flag_signals_enabled, RCCE_FLAG_SET, node_id);
  101. my_idag = -1;
  102. int dummy=0;
  103. while (my_idag==-1) {//pause();
  104. for (i=0; i<1000; i++)
  105. for(j=0; j<1000; j++)
  106. dummy++;
  107. scc_signals_check();
  108. }
  109. while (state != TERMINATED)
  110. if (state == IDLE_CORE) {
  111. pending_state = NO_PENDING_STATE;
  112. //pause();
  113. dummy=0;
  114. for (i=0; i<1000; i++)
  115. for(j=0; j<1000; j++)
  116. dummy++;
  117. scc_signals_check();
  118. } else if (state == IDLE_INIT_MAN) {// || state == IDLE_FAR_MAN || state == IDLE_INIT_MAN_SELFOPT_PENDING || state == IDLE_INIT_MAN_WORK_PENDING) {
  119. //pause();
  120. dummy=0;
  121. for (i=0; i<1000; i++)
  122. for(j=0; j<1000; j++)
  123. dummy++;
  124. scc_signals_check();
  125. } else if (state == IDLE_AGENT) {
  126. if (app_state != APP_TERMINATED) {
  127. //pause();//!app_terminated
  128. dummy=0;
  129. for (i=0; i<1000; i++)
  130. for(j=0; j<1000; j++)
  131. dummy++;
  132. scc_signals_check();
  133. } else {
  134. if (timer_gettime(timerid, &chk_timer) == -1) printf("timer_gettime error 2\n");
  135. else selfopt_time_rem = chk_timer.it_value.tv_nsec;
  136. if (selfopt_time_rem > 0) {
  137. its.it_value.tv_nsec = 0;
  138. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error42\n");
  139. }
  140. state = AGENT_ENDING;
  141. }
  142. } else if (state == AGENT_REWIND_FILE) {
  143. printf("I am %d and i have to rewind my selfopt_areas_file for app with id %d\n",node_id,init_app.id);
  144. fprintf(log_file,"I have to rewind my selfopt_areas_file for app with id %d\n",init_app.id);
  145. selfopt_interval = 200;
  146. its.it_value.tv_nsec = selfopt_interval * MS;
  147. selfopt_time_rem = selfopt_interval;
  148. if (timer_settime(timerid, 0, &its, NULL) == -1) perror("timer_settime error823\n");
  149. state = IDLE_AGENT;
  150. } else if (state == IDLE_AGENT_WAITING_OFF) {
  151. //pause();
  152. if (app_state != APP_TERMINATED) {
  153. //pause();//!app_terminated
  154. dummy=0;
  155. for (i=0; i<1000; i++)
  156. for(j=0; j<1000; j++)
  157. dummy++;
  158. scc_signals_check();
  159. } else {
  160. signals_disable();
  161. if (timer_gettime(timerid, &chk_timer) == -1) printf("timer_gettime error 2\n");
  162. else selfopt_time_rem = chk_timer.it_value.tv_nsec;
  163. //den stamataw edw thn diadikasia tou selfopt gia na mhn meinoun oi mexri twra prosfores kai oxi mono anapanthtes
  164. if (selfopt_time_rem == 0) state = AGENT_SELF_CHK_OFFERS;
  165. signals_enable();
  166. }
  167. } else if (state == INIT_MANAGER) {
  168. signals_disable();
  169. printf("Initialising app node_id = %d\n",node_id);
  170. cur_time = time(NULL);
  171. cur_t = localtime(&cur_time);
  172. fprintf(log_file, "[%d:%d:%d]: Initialising app\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  173. fflush(log_file);
  174. strcpy(init_areas_file_name,"/shared/herc/scenaria/");
  175. strcat(init_areas_file_name,scen_num);
  176. strcat(init_areas_file_name,"/");
  177. strcat(init_areas_file_name, itoa(init_app.id));
  178. strcat(init_areas_file_name, ".txt");
  179. //printf("area file path = %s\n",areas_file_name);
  180. if ((init_areas_file = fopen(init_areas_file_name, "r")) == NULL){
  181. printf("Cannot open input file with file path = %s ",init_areas_file_name);
  182. perror("open init_areas_file_name");
  183. }
  184. init_areas_sent = 0;
  185. fscanf(init_areas_file,"%d",&init_areas_cnt);
  186. state = INIT_MANAGER_SEND_OFFERS;
  187. signals_enable();
  188. scc_signals_check();
  189. } else if (state == INIT_MANAGER_SEND_OFFERS) {// || state == INIT_MANAGER_SEND_OFFERS_SELFOPT_PENDING || state == INIT_MANAGER_SEND_OFFERS_WORK_PENDING) {
  190. signals_disable();
  191. init_idags_areas_replies=0;
  192. init_DDS_idags = 0;
  193. init_DDS_replies = 0;
  194. init_areas_num = 1;
  195. if (init_targets_head != NULL)
  196. while (init_targets_head != NULL){
  197. tmp_target_list = init_targets_head;
  198. init_targets_head = init_targets_head->next;
  199. free(tmp_target_list);
  200. }
  201. if (init_man_offers != NULL)
  202. while (init_man_offers != NULL) {
  203. tmp_offer_list = init_man_offers;
  204. init_man_offers = init_man_offers->next;
  205. free(tmp_offer_list);
  206. }
  207. fprintf(log_file, "near init area (%d,%d)\n",node_id,selfopt_r);
  208. fflush(log_file);
  209. if (core_inter_head[my_idag] == NULL){
  210. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  211. core_inter_tail[my_idag] = core_inter_head[my_idag];
  212. } else {
  213. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  214. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  215. }
  216. core_inter_tail[my_idag]->type = IDAG_FIND_IDAGS_PENDING;
  217. core_inter_tail[my_idag]->data.reg.C = node_id;
  218. core_inter_tail[my_idag]->data.reg.r = selfopt_r;
  219. core_inter_tail[my_idag]->next = NULL;
  220. if (core_inter_head[my_idag]->next == NULL) {
  221. //kill(pid_num[my_idag],SIG_IDAG_FIND_IDAGS);
  222. scc_kill(my_idag,SIG_IDAG_FIND_IDAGS);
  223. my_stats.msg_count++;
  224. my_stats.distance += distance(node_id,my_idag);
  225. }
  226. for (i=0; i<init_areas_cnt && i<INIT_FAR_AREAS_NUM; i++) {
  227. fscanf(init_areas_file,"%d",&C);
  228. fscanf(init_areas_file,"%d",&r);
  229. fprintf(log_file, "init area (%d,%d)\n",C,r);
  230. fflush(log_file);
  231. printf("I am %d and i am sending a far offer in region (%d,%d)\n",node_id,C,r);
  232. one_core = C;
  233. if (core_inter_head[one_core] == NULL){
  234. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  235. core_inter_tail[one_core] = core_inter_head[one_core];
  236. } else {
  237. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  238. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  239. }
  240. core_inter_tail[one_core]->type = FAR_INIT_REQ;
  241. core_inter_tail[one_core]->data.reg.C = C;
  242. core_inter_tail[one_core]->data.reg.r = r;
  243. core_inter_tail[one_core]->next = NULL;
  244. if (core_inter_head[one_core]->next == NULL) {
  245. //kill(pid_num[one_core],SIG_INIT_FAR_REQ);
  246. scc_kill(one_core,SIG_INIT_FAR_REQ);
  247. my_stats.msg_count++;
  248. my_stats.distance += distance(node_id,one_core);
  249. }
  250. //init_areas_num++;
  251. }
  252. init_areas_sent += i;
  253. //fclose(areas);
  254. if (selfopt_time_rem != -1){
  255. if (timer_gettime(timerid, &chk_timer) == -1) printf("timer_gettime error\n");
  256. else selfopt_time_rem = chk_timer.it_value.tv_nsec;
  257. if (selfopt_time_rem > 0) {
  258. its.it_value.tv_nsec = 0;//750000000;// * MS;
  259. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error4123\n");
  260. }
  261. }
  262. state = IDLE_INIT_MAN;
  263. signals_enable();
  264. scc_signals_check();
  265. } else if (state == INIT_MAN_CHK_OFFERS) {// || state == INIT_MAN_CHK_OFFERS_SELFOPT_PENDING || state == INIT_MAN_CHK_OFFERS_WORK_PENDING){
  266. signals_disable();
  267. printf("init check alarm went off node_id=%d init_DDS_idags = %d init_DDS_replies = %d state=%d!\n",node_id,init_DDS_idags,init_DDS_replies,state);
  268. cur_time = time(NULL);
  269. cur_t = localtime(&cur_time);
  270. fprintf(log_file, "[%d:%d:%d]: init check alarm went off init_DDS_idags = %d init_DDS_replies = %d state=%d\n",
  271. cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,init_DDS_idags,init_DDS_replies,state);
  272. fflush(log_file);
  273. init_core_found = 0;
  274. if (init_man_offers == NULL) {
  275. printf("I don't have offers\n");
  276. fprintf(log_file, "I don't have init man offers\n");
  277. fflush(log_file);
  278. printf("I am %d and didn't manage to find my core in this round of search for app with id %d\n",node_id,init_app.id);
  279. //state = IDLE_CORE;
  280. if (init_areas_sent == init_areas_cnt){
  281. printf("I am %d and i have to rewind my init_areas_file for app with id %d\n",node_id,init_app.id);
  282. rewind(init_areas_file);
  283. init_areas_sent = 0;
  284. fscanf(init_areas_file,"%d",&init_areas_cnt);
  285. }
  286. state = INIT_MANAGER_SEND_OFFERS;
  287. } else {
  288. tmp_offer_list = init_man_offers;
  289. while (tmp_offer_list != NULL){
  290. fprintf(log_file,"Offer by %d for %d cores with spd_loss %0.4f :",tmp_offer_list->sender,tmp_offer_list->off.num_of_cores,tmp_offer_list->off.spd_loss);
  291. for (i=0; i<tmp_offer_list->off.num_of_cores; i++)
  292. fprintf(log_file," %d,",tmp_offer_list->off.offered_cores[i]);
  293. fprintf(log_file,"\n");
  294. //printf("Offer by %d for %d cores\n",tmp_offer_list->sender,tmp_offer_list->off.num_of_cores);
  295. tmp_offer_list = tmp_offer_list->next;
  296. }
  297. fflush(log_file);
  298. *init_man_offers->answer = 1;
  299. //decide which will be the agent. Will be the one tha minimizes distance between cores
  300. min_dist = -1;
  301. for (i=0; i<init_man_offers->off.num_of_cores; i++){
  302. tmp_int = 0;
  303. for (j=0; j<init_man_offers->off.num_of_cores; j++){
  304. if (i == j) continue;
  305. tmp_int += distance(init_man_offers->off.offered_cores[i],init_man_offers->off.offered_cores[j]);
  306. }
  307. if (min_dist == -1 || tmp_int < min_dist) {
  308. min_dist = tmp_int;
  309. new_agent = init_man_offers->off.offered_cores[i];
  310. }
  311. }
  312. cur_time = time(NULL);
  313. cur_t = localtime(&cur_time);
  314. init_app_times[1].tm_sec = cur_t->tm_sec;
  315. init_app_times[1].tm_min = cur_t->tm_min;
  316. init_app_times[1].tm_hour = cur_t->tm_hour;
  317. printf("New agent is %d\n",new_agent);
  318. cur_time = time(NULL);
  319. cur_t = localtime(&cur_time);
  320. fprintf(log_file, "[%d:%d:%d]: New agent is %d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,new_agent);
  321. fflush(log_file);
  322. if (new_agent != node_id) {
  323. if (core_inter_head[new_agent] == NULL){
  324. core_inter_head[new_agent] = (inter_list *) malloc(sizeof(inter_list));
  325. core_inter_tail[new_agent] = core_inter_head[new_agent];
  326. } else {
  327. printf("Starting this app is not high priority. Should we change that?\n");
  328. core_inter_tail[new_agent]->next = (inter_list *) malloc(sizeof(inter_list));
  329. core_inter_tail[new_agent] = core_inter_tail[new_agent]->next;
  330. }
  331. core_inter_tail[new_agent]->type = INIT_AGENT;
  332. core_inter_tail[new_agent]->data.app_cores = (int *)malloc((init_man_offers->off.num_of_cores+1)*sizeof(int));
  333. core_inter_tail[new_agent]->data.app_cores[0] = init_man_offers->off.num_of_cores;
  334. for(i=1; i<=init_man_offers->off.num_of_cores; i++)
  335. core_inter_tail[new_agent]->data.app_cores[i] = init_man_offers->off.offered_cores[i-1];
  336. core_inter_tail[new_agent]->next = NULL;
  337. init_app.num_of_cores = init_man_offers->off.num_of_cores;
  338. if (core_inter_head[new_agent]->next == NULL) {
  339. //kill(pid_num[new_agent], SIG_INIT_AGENT);
  340. scc_kill(new_agent, SIG_INIT_AGENT);
  341. my_stats.msg_count++;
  342. my_stats.distance += distance(node_id,new_agent);
  343. } else printf("I am init manager and i am doing smth else with init agent interaction=%d\n",core_inter_head[new_agent]->type);
  344. } else {
  345. my_app = init_app;
  346. my_cores_count = init_man_offers->off.num_of_cores;
  347. my_app.num_of_cores = my_cores_count;//+1;
  348. if (my_cores == NULL) {
  349. my_cores = (core_list *) malloc(sizeof(core_list));
  350. my_cores_tail = my_cores;
  351. } else {
  352. printf("My cores still not fucking null!!\n");
  353. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  354. my_cores_tail = my_cores_tail->next;
  355. }
  356. my_cores_tail->core_id = node_id;
  357. my_cores_tail->offered_to = -1;
  358. my_cores_tail->next = NULL;
  359. /*if (core_inter_head[my_idag] == NULL){
  360. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  361. core_inter_tail[my_idag] = core_inter_head[my_idag];
  362. } else {
  363. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  364. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  365. }
  366. core_inter_tail[my_idag]->type = IDAG_ADD_CORES_DDS;
  367. core_inter_tail[my_idag]->data.app_cores = (int *)malloc((my_cores_count+1)*sizeof(int));
  368. core_inter_tail[my_idag]->data.app_cores[0] = my_cores_count;
  369. core_inter_tail[my_idag]->next = NULL;*/
  370. //I want myself to be first in my_cores list
  371. for(i=0; i<init_man_offers->off.num_of_cores; i++) {
  372. one_core = init_man_offers->off.offered_cores[i];
  373. //core_inter_tail[my_idag]->data.app_cores[i+1] = one_core;
  374. if (one_core != node_id){
  375. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  376. my_cores_tail = my_cores_tail->next;
  377. my_cores_tail->core_id = one_core;
  378. my_cores_tail->offered_to = -1;
  379. my_cores_tail->next = NULL;
  380. }
  381. }
  382. my_app_times[0].tm_sec = init_app_times[0].tm_sec;
  383. my_app_times[0].tm_min = init_app_times[0].tm_min;
  384. my_app_times[0].tm_hour = init_app_times[0].tm_hour;
  385. my_app_times[1].tm_sec = init_app_times[1].tm_sec;
  386. my_app_times[1].tm_min = init_app_times[1].tm_min;
  387. my_app_times[1].tm_hour = init_app_times[1].tm_hour;
  388. init_app_times[0].tm_sec = 0;
  389. init_app_times[0].tm_min = 0;
  390. init_app_times[0].tm_hour = 0;
  391. init_app_times[1].tm_sec = 0;
  392. init_app_times[1].tm_min = 0;
  393. init_app_times[1].tm_hour = 0;
  394. /*kill(pid_num[0], SIG_INIT_APP);
  395. my_stats.msg_count++;
  396. my_stats.distance += distance(node_id,0);*/
  397. printf("I am new agent with id %d and app with A=%.2f, var=%.2f and %d my_cores_count = %d cores: \n"
  398. ,node_id,my_app.A,my_app.var,my_app.num_of_cores,my_cores_count);
  399. my_Speedup = Speedup(my_app, my_app.num_of_cores);
  400. /*if (core_inter_head[my_idag]->next == NULL) {
  401. kill(pid_num[my_idag], SIG_ADD_CORES_DDS);
  402. my_stats.msg_count++;
  403. my_stats.distance += distance(node_id,my_idag);
  404. } else printf("I am %d and i didn't call add!! with interaction %d\n",node_id,core_inter_head[my_idag]->type);*/
  405. if (my_agent != -1) {
  406. printf("I am %d and i do this agent switch in common node with pending_state=%d\n",node_id,pending_state);
  407. fprintf(log_file,"I do this agent switch in common node with pending_state=%d\n",pending_state);
  408. if (pending_state == WORKING_NODE) {
  409. if (core_inter_head[my_agent] == NULL){
  410. core_inter_head[my_agent] = (inter_list *) malloc(sizeof(inter_list));
  411. core_inter_tail[my_agent] = core_inter_head[my_agent];
  412. } else {
  413. core_inter_tail[my_agent]->next = (inter_list *) malloc(sizeof(inter_list));
  414. core_inter_tail[my_agent] = core_inter_tail[my_agent]->next;
  415. }
  416. core_inter_tail[my_agent]->type = REP_CHK_REM_TIME;
  417. core_inter_tail[my_agent]->data.work_time = upper_work_bound - time_worked;
  418. core_inter_tail[my_agent]->next = NULL;
  419. }
  420. upper_work_bound = 0;
  421. my_agent = -1;
  422. }
  423. state = AGENT_INIT_STATE;
  424. cur_time = time(NULL);
  425. cur_t = localtime(&cur_time);
  426. 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);
  427. fprintf(log_file, "my cores are:");
  428. for (tmp_cores_list=my_cores; tmp_cores_list!=NULL; tmp_cores_list=tmp_cores_list->next) fprintf(log_file, " %d", tmp_cores_list->core_id);
  429. fprintf(log_file, "\n");
  430. fflush(log_file);
  431. }
  432. tmp_offer_list = init_man_offers->next;
  433. while (tmp_offer_list != NULL){
  434. fflush(log_file);
  435. *tmp_offer_list->answer = 0;
  436. tmp_offer_list = tmp_offer_list->next;
  437. }
  438. while (init_man_offers != NULL){
  439. if (core_inter_head[init_man_offers->sender] != NULL) {
  440. if (core_inter_head[init_man_offers->sender]->type == REP_AGENT_OFFER_PENDING){
  441. core_inter_head[init_man_offers->sender]->type = REP_AGENT_OFFER_SENT;
  442. //kill(pid_num[init_man_offers->sender], SIG_REP_OFFERS);
  443. scc_kill(init_man_offers->sender, SIG_REP_OFFERS);
  444. my_stats.msg_count++;
  445. my_stats.distance += distance(node_id,init_man_offers->sender);
  446. } else if (core_inter_head[init_man_offers->sender]->type == REP_FAR_REQ_OFFER_PENDING){
  447. core_inter_head[init_man_offers->sender]->type = REP_FAR_REQ_OFFER_SENT;
  448. //kill(pid_num[init_man_offers->sender], SIG_REP_OFFERS);
  449. scc_kill(init_man_offers->sender, SIG_REP_OFFERS);
  450. my_stats.msg_count++;
  451. my_stats.distance += distance(node_id,init_man_offers->sender);
  452. } else if (core_inter_head[init_man_offers->sender]->type != REP_FAR_REQ_OFFER_SENT && core_inter_head[init_man_offers->sender]->type != REP_AGENT_OFFER_SENT)
  453. printf("We have aasdfa problem!\n");
  454. } else {
  455. printf("gamietai b = %d",init_man_offers->sender);
  456. fprintf(log_file,"gamietai b = %d",init_man_offers->sender);
  457. fflush(log_file);
  458. }
  459. tmp_offer_list = init_man_offers;
  460. init_man_offers = init_man_offers->next;
  461. free(tmp_offer_list);
  462. }
  463. fclose(init_areas_file);
  464. fprintf(log_file,"selfopt_time_rem = %ld app_state = %d state = %d pending_state = %d\n",selfopt_time_rem,app_state,state,pending_state);
  465. fflush(log_file);
  466. if (selfopt_time_rem != -1 && app_state != APP_TERMINATED){ //a selfopt is pending !app_terminated
  467. selfopt_time_rem -= 500000000;
  468. if (selfopt_time_rem > 0){
  469. its.it_value.tv_nsec = selfopt_time_rem;
  470. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error5\n");
  471. state = IDLE_AGENT;
  472. } else {
  473. selfopt_time_rem = -1;
  474. state = AGENT_SELF_OPT;
  475. }
  476. } else if (state != AGENT_INIT_STATE) {
  477. if (pending_state == IDLE_AGENT) {
  478. printf("I am %d and i entered this realm of whores\n",node_id);
  479. fprintf(log_file,"I finished my init selfopt_interval=%d, selfopt_time_rem=%ld my_cores_count=%d, old_cores_cnt=%d\n"
  480. ,selfopt_interval,selfopt_time_rem,my_cores_count,old_cores_cnt);
  481. fflush(log_file);
  482. fprintf(app_log_file,"I finished my init selfopt_interval=%d, selfopt_time_rem=%ld my_cores_count=%d, old_cores_cnt=%d\n"
  483. ,selfopt_interval,selfopt_time_rem,my_cores_count,old_cores_cnt);
  484. fflush(app_log_file);
  485. if (selfopt_interval > 0) { // && !app_terminated
  486. its.it_value.tv_nsec = selfopt_interval * MS;
  487. selfopt_time_rem = selfopt_interval;
  488. if (old_cores_cnt == 1 || old_cores_cnt == my_cores_count) {
  489. if (my_cores_count == max_cores_count) {
  490. fprintf(app_log_file, "I have maximum cores count. I don't initiate selfopt process in here anyway.\n");
  491. fflush(app_log_file);
  492. selfopt_time_rem = -1;
  493. } else if (time_per_node <= (selfopt_interval / 2) && my_cores_count > 1) {
  494. fprintf(app_log_file, "I have little working time left time_per_node=%d selfopt_interval=%d in here anyway.\n",time_per_node,selfopt_interval);
  495. fflush(app_log_file);
  496. selfopt_time_rem = -1;
  497. } else if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error8\n");
  498. }
  499. } else if (my_cores_count == 1) {
  500. printf("I am %d and i have to rewind my selfopt_areas_file after init for app with id %d\n",node_id,init_app.id);
  501. fprintf(log_file,"I have to rewind my selfopt_areas_file after init for app with id %d\n",init_app.id);
  502. selfopt_interval = 200;
  503. its.it_value.tv_nsec = selfopt_interval * MS;
  504. selfopt_time_rem = selfopt_interval;
  505. if (timer_settime(timerid, 0, &its, NULL) == -1) perror("timer_settime error823\n");
  506. } else selfopt_time_rem = -1;
  507. state = IDLE_AGENT;
  508. pending_state = NO_PENDING_STATE;
  509. } else if (pending_state == AGENT_SELF_OPT || pending_state == WORKING_NODE || pending_state == AGENT_REWIND_FILE) {
  510. state = pending_state;//IDLE_AGENT;
  511. pending_state = NO_PENDING_STATE;
  512. } else state = IDLE_CORE;
  513. } else if (app_state != APP_TERMINATED && new_agent != node_id) state = IDLE_AGENT;//!app_terminated
  514. }
  515. signals_enable();
  516. scc_signals_check();
  517. } else if (state == AGENT_INIT_STATE) {//|| state == AGENT_INIT_STATE_INIT_INTERRUPTED
  518. signals_disable();
  519. selfopt_interval=200;
  520. nodes_ended_cnt = 0;
  521. app_state = RUNNING;
  522. max_cores_count = get_max_cores_count(my_app);
  523. //app_terminated = 0;
  524. //pending_state = NO_PENDING_STATE;
  525. /*if (my_cores_count > 1){
  526. time_to_work = (int) roundf(my_app.workld / my_Speedup);
  527. time_per_node = time_to_work / (my_cores_count-1);
  528. time_left = time_to_work % (my_cores_count-1);
  529. if (time_to_work == 0) time_left++;
  530. }*/
  531. printf("i am init agent %d. speedup = %0.2f time_to_work = %d time_per_node = %d, workld=%0.2f\n",node_id,my_Speedup,time_to_work,time_per_node,my_app.workld);
  532. strcpy(app_log_file_name,"/shared/herc/scenaria/");
  533. strcat(app_log_file_name,scen_num);
  534. strcat(app_log_file_name,"/app_logs/");
  535. strcat(app_log_file_name, itoa(my_app.id));
  536. strcat(app_log_file_name, ".txt");
  537. //printf("area file path self opt = %s\n",areas_file_name);
  538. if ((app_log_file = fopen(app_log_file_name, "w")) == NULL){
  539. printf("Cannot open input file with file path = %s ",app_log_file_name);
  540. perror("open selfopt_areas_file_name");
  541. }
  542. fprintf(app_log_file, "Came into init_agent at [%d:%d:%d]\n",my_app_times[0].tm_hour,my_app_times[0].tm_min,my_app_times[0].tm_sec);
  543. fprintf(app_log_file, "New agent found at [%d:%d:%d]\n",my_app_times[1].tm_hour,my_app_times[1].tm_min,my_app_times[1].tm_sec);
  544. cur_time = time(NULL);
  545. cur_t = localtime(&cur_time);
  546. fprintf(app_log_file, "[%d:%d:%d]: I am agent %d . Init ok!! my_cores_count = %d app_id=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,node_id,my_cores_count,my_app.id);
  547. fprintf(app_log_file, "my cores are:");
  548. for (tmp_cores_list=my_cores; tmp_cores_list!=NULL; tmp_cores_list=tmp_cores_list->next) fprintf(app_log_file, " %d", tmp_cores_list->core_id);
  549. fprintf(app_log_file, "\n");
  550. fprintf(app_log_file,"speedup = %0.2f time_to_work = %d time_per_node = %d, time_left = %d, workld=%0.2f\n",my_Speedup,time_to_work,time_per_node,time_left,my_app.workld);
  551. fflush(app_log_file);
  552. /*tmp_cores_list = my_cores->next;
  553. while (tmp_cores_list != NULL){
  554. one_core = tmp_cores_list->core_id;
  555. if (core_inter_head[one_core] == NULL){
  556. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  557. core_inter_tail[one_core] = core_inter_head[one_core];
  558. } else {
  559. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  560. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  561. }
  562. core_inter_tail[one_core]->type = INIT_WORK_NODE;
  563. core_inter_tail[one_core]->data.work_time = time_per_node;
  564. if (time_left > 0) {
  565. core_inter_tail[one_core]->data.work_time++;
  566. time_left--;
  567. }
  568. core_inter_tail[one_core]->next = NULL;
  569. my_stats.app_turnaround += core_inter_tail[one_core]->data.work_time;
  570. if (core_inter_head[one_core]->next == NULL) {
  571. kill(pid_num[one_core], SIG_APPOINT_WORK);
  572. my_stats.msg_count++;
  573. my_stats.distance += distance(node_id,one_core);
  574. } else printf("I am %d and i am doing smth else with my working node %d in init inter1 = %d inter2 = %d\n",
  575. node_id,one_core,core_inter_head[one_core]->type,core_inter_head[one_core]->next->type);
  576. tmp_cores_list = tmp_cores_list->next;
  577. } //else printf("i have only one fucking core\n");
  578. \if (my_cores_count == max_cores_count) {
  579. fprintf(app_log_file, "I have maximum cores count. I don't initiate selfopt process ini init_agent.\n");
  580. fflush(app_log_file);
  581. selfopt_time_rem = -1;
  582. if (pending_state == IDLE_INIT_MAN || pending_state == INIT_MANAGER || pending_state == INIT_MANAGER_SEND_OFFERS
  583. || pending_state == INIT_MAN_CHK_OFFERS || pending_state == WORKING_NODE_IDLE_INIT) {
  584. state = pending_state;
  585. pending_state = IDLE_AGENT;
  586. } else state = IDLE_AGENT;
  587. } else if (time_per_node <= (selfopt_interval / 2) && my_cores_count > 1) {
  588. fprintf(app_log_file, "I have little working time left time_per_node=%d selfopt_interval=%d in init_agent.\n",time_per_node,selfopt_interval);
  589. fflush(app_log_file);
  590. if (pending_state == IDLE_INIT_MAN || pending_state == INIT_MANAGER || pending_state == INIT_MANAGER_SEND_OFFERS
  591. || pending_state == INIT_MAN_CHK_OFFERS || pending_state == WORKING_NODE_IDLE_INIT) {
  592. state = pending_state;
  593. pending_state = IDLE_AGENT;
  594. } else state = IDLE_AGENT;
  595. selfopt_time_rem = -1;
  596. } else {
  597. if (pending_state == IDLE_INIT_MAN || pending_state == INIT_MANAGER || pending_state == INIT_MANAGER_SEND_OFFERS
  598. || pending_state == INIT_MAN_CHK_OFFERS || pending_state == WORKING_NODE_IDLE_INIT) {
  599. state = pending_state;//IDLE_INIT_MAN;//IDLE_INIT_MAN_SELFOPT_PENDING;
  600. pending_state = AGENT_SELF_OPT;
  601. //printf("I am %d o fountas einai poustara pou zwgrafizei k ton fuckaroun oi gkomenes tou!\n",node_id);
  602. } else state = AGENT_SELF_OPT;
  603. }*/
  604. scc_kill(0, SIG_APP_TERMINATED);
  605. state = IDLE_CORE;
  606. signals_enable();
  607. scc_signals_check();
  608. /*} else if (state == AGENT_SELF_OPT) {
  609. signals_disable();
  610. printf("Initialising self opt node_id = %d\n",node_id);
  611. cur_time = time(NULL);
  612. cur_t = localtime(&cur_time);
  613. fprintf(log_file, "[%d:%d:%d]: Initialising self opt!\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  614. fflush(log_file);
  615. fprintf(app_log_file, "[%d:%d:%d]: Initialising self opt!\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  616. fflush(app_log_file);
  617. pending_state = NO_PENDING_STATE;
  618. if (app_state != APP_TERMINATED) {//!app_terminated
  619. selfopt_DDS_idags = 0;
  620. selfopt_DDS_replies = 0;
  621. //selfopt_areas_sent = 0;
  622. //if (selfopt_man_offers != NULL) printf("selfopt offers list is not NULL\n");
  623. if (selfopt_targets_head != NULL) {
  624. //printf("selfopt targets list is not NULL\n");
  625. while (selfopt_targets_head != NULL){
  626. tmp_target_list = selfopt_targets_head;
  627. selfopt_targets_head = selfopt_targets_head->next;
  628. free(tmp_target_list);
  629. }
  630. }
  631. if (core_inter_head[my_idag] == NULL){
  632. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  633. core_inter_tail[my_idag] = core_inter_head[my_idag];
  634. } else {
  635. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  636. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  637. }
  638. core_inter_tail[my_idag]->type = SELFOPT_IDAG_FIND_IDAGS_PENDING;
  639. core_inter_tail[my_idag]->data.reg.C = node_id;
  640. core_inter_tail[my_idag]->data.reg.r = selfopt_r;
  641. core_inter_tail[my_idag]->next = NULL;
  642. if (core_inter_head[my_idag]->next == NULL) {
  643. kill(pid_num[my_idag],SIG_IDAG_FIND_IDAGS);
  644. my_stats.msg_count++;
  645. my_stats.distance += distance(node_id,my_idag);
  646. }
  647. if (selfopt_interval != 800) selfopt_interval = 2 * selfopt_interval;
  648. else selfopt_interval = -1;
  649. state = IDLE_AGENT_WAITING_OFF;
  650. } else state = AGENT_ENDING;
  651. signals_enable();
  652. scc_signals_check();
  653. } else if (state == AGENT_SELF_CHK_OFFERS) {
  654. signals_disable();
  655. printf("alarm went off for self opt node_id=%d selfopt_DDS_idags = %d selfopt_DDS_replies = %d! app_state=%d\n",node_id,selfopt_DDS_idags,selfopt_DDS_replies,app_state);
  656. cur_time = time(NULL);
  657. cur_t = localtime(&cur_time);
  658. fprintf(log_file, "[%d:%d:%d]: alarm went off for self opt selfopt_DDS_idags = %d selfopt_DDS_replies = %d app_state=%d pending_state = %d\n",
  659. cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,selfopt_DDS_idags,selfopt_DDS_replies,app_state,pending_state);
  660. fflush(log_file);
  661. fprintf(app_log_file, "[%d:%d:%d]: alarm went off for self opt selfopt_DDS_idags = %d selfopt_DDS_replies = %d app_state=%d pending_state = %d\n",
  662. cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,selfopt_DDS_idags,selfopt_DDS_replies,app_state,pending_state);
  663. fflush(app_log_file);
  664. old_cores_cnt = my_cores_count;
  665. if (selfopt_man_offers == NULL) {
  666. printf("I don't have self opt offers app_state=%d\n",app_state);
  667. fprintf(log_file,"I don't have self opt offers app_state=%d\n",app_state);
  668. fflush(log_file);
  669. fprintf(app_log_file,"I don't have self opt offers app_state=%d\n",app_state);
  670. fflush(app_log_file);
  671. //state = IDLE_AGENT;
  672. } else {
  673. tmp_offer_list = selfopt_man_offers;
  674. while (tmp_offer_list != NULL){
  675. //printf("Offer by %d for %d cores\n",tmp_offer_list->sender,tmp_offer_list->off.num_of_cores);
  676. fprintf(log_file,"Offer by %d for %d cores\n",tmp_offer_list->sender,tmp_offer_list->off.num_of_cores);
  677. fflush(log_file);
  678. tmp_offer_list = tmp_offer_list->next;
  679. }
  680. if (app_state != APP_TERMINATED) {
  681. *selfopt_man_offers->answer = 1;
  682. if (core_inter_head[my_idag] == NULL){
  683. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  684. core_inter_tail[my_idag] = core_inter_head[my_idag];
  685. } else {
  686. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  687. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  688. }
  689. core_inter_tail[my_idag]->type = IDAG_ADD_CORES_DDS;
  690. core_inter_tail[my_idag]->data.app_cores = (int *)malloc((selfopt_man_offers->off.num_of_cores+1)*sizeof(int));
  691. core_inter_tail[my_idag]->data.app_cores[0] = selfopt_man_offers->off.num_of_cores;
  692. core_inter_tail[my_idag]->next = NULL;
  693. //
  694. if (app_state != RESIZING) {
  695. old_Speedup = my_Speedup;
  696. chk_rem_num = my_cores_count-1;
  697. fprintf(app_log_file,"Initialising resize with check_rem = %d\n",chk_rem_num);
  698. fflush(app_log_file);
  699. chk_rem_count = 0;
  700. sum_rem_time = 0;
  701. app_state = RESIZING;
  702. my_cores_count += selfopt_man_offers->off.num_of_cores;
  703. my_app.num_of_cores = my_cores_count;
  704. my_Speedup = Speedup(my_app, my_cores_count);
  705. if (old_cores_cnt > 1)
  706. for(tmp_cores_list=my_cores->next; tmp_cores_list!=NULL; tmp_cores_list=tmp_cores_list->next) {//first is myself
  707. one_core = tmp_cores_list->core_id;
  708. if (core_inter_head[one_core] == NULL){
  709. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  710. core_inter_tail[one_core] = core_inter_head[one_core];
  711. } else {
  712. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  713. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  714. }
  715. core_inter_tail[one_core]->type = APPOINT_WORK_NODE_PENDING;//CHK_REM_TIME;
  716. core_inter_tail[one_core]->next = NULL;
  717. if (core_inter_head[one_core]->next == NULL) {
  718. kill(pid_num[one_core], SIG_CHECK_REM_TIME);
  719. my_stats.msg_count++;
  720. my_stats.distance += distance(node_id,one_core);
  721. } else printf("I am %d and i am doing smth else with my working node %d in send SIG_CHECK_REM_TIME interaction = %d\n",node_id,one_core,core_inter_head[one_core]->type);
  722. }
  723. } else {
  724. fprintf(app_log_file,"Allready resizing with chk_rem_num = %d chk_rem_count=%d\n",chk_rem_num,chk_rem_count);
  725. fflush(app_log_file);
  726. my_cores_count += selfopt_man_offers->off.num_of_cores;
  727. my_app.num_of_cores = my_cores_count;
  728. my_Speedup = Speedup(my_app, my_cores_count);
  729. }
  730. for (i=0; i<selfopt_man_offers->off.num_of_cores; i++){
  731. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  732. my_cores_tail = my_cores_tail->next;
  733. my_cores_tail->core_id = selfopt_man_offers->off.offered_cores[i];
  734. my_cores_tail->offered_to = -1;
  735. my_cores_tail->next = NULL;
  736. core_inter_tail[my_idag]->data.app_cores[i+1] = selfopt_man_offers->off.offered_cores[i];
  737. one_core = my_cores_tail->core_id;
  738. //printf("I am %d and i put in appoint work %d\n",node_id,one_core);
  739. if (core_inter_head[one_core] == NULL){
  740. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  741. core_inter_tail[one_core] = core_inter_head[one_core];
  742. } else {
  743. printf("I am %d and I am doing sth with my new core %d interaction is %d\n",node_id,one_core,core_inter_head[one_core]->type);
  744. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  745. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  746. }
  747. core_inter_tail[one_core]->type = INIT_WORK_NODE_PENDING;
  748. core_inter_tail[one_core]->next = NULL;
  749. }
  750. } else *selfopt_man_offers->answer = 0;
  751. tmp_offer_list = selfopt_man_offers->next;
  752. while (tmp_offer_list != NULL){
  753. *tmp_offer_list->answer = 0;
  754. tmp_offer_list = tmp_offer_list->next;
  755. }
  756. if (app_state != APP_TERMINATED) {
  757. //my idag may have offered smth
  758. if (core_inter_head[my_idag]->next == NULL) {
  759. kill(pid_num[my_idag], SIG_ADD_CORES_DDS);
  760. my_stats.msg_count++;
  761. my_stats.distance += distance(node_id,my_idag);
  762. } else printf("I am %d i didn't call add inside selfopt with interaction %d\n",node_id,core_inter_head[my_idag]->type);
  763. if (old_cores_cnt == 1) {
  764. nodes_ended_cnt = 0;
  765. time_to_work = (int) roundf(my_app.workld / my_Speedup);
  766. time_per_node = time_to_work / (my_cores_count-1);
  767. time_left = time_to_work % (my_cores_count-1);
  768. if (time_to_work == 0) time_left++;
  769. fprintf(app_log_file,"in one core speedup = %0.2f time_to_work = %d time_per_node = %d, workld=%0.2f\n",my_Speedup,time_to_work,time_per_node,my_app.workld);
  770. fflush(app_log_file);
  771. tmp_cores_list = my_cores->next;
  772. while (tmp_cores_list != NULL){
  773. //printf("I am inside core listing node=%d\n",node_id);
  774. one_core = tmp_cores_list->core_id;
  775. for (tmp_inter_list = core_inter_head[one_core]; tmp_inter_list != NULL; tmp_inter_list = tmp_inter_list->next)
  776. if (tmp_inter_list->type == INIT_WORK_NODE_PENDING) break;
  777. if (tmp_inter_list != NULL) {
  778. tmp_inter_list->type = INIT_WORK_NODE;
  779. tmp_inter_list->data.work_time = time_per_node;
  780. if (time_left > 0) {
  781. tmp_inter_list->data.work_time++;
  782. time_left--;
  783. }
  784. my_stats.app_turnaround += tmp_inter_list->data.work_time;
  785. } else printf("i am %d kai sto common_node ta hpiame one core = %d\n",node_id,one_core);
  786. if (core_inter_head[one_core]->next == NULL) {
  787. kill(pid_num[one_core], SIG_APPOINT_WORK);
  788. my_stats.msg_count++;
  789. my_stats.distance += distance(node_id,one_core);
  790. } else printf("I am doing smth else with my working node? interaction=%d interaction2=%d\n",core_inter_head[one_core]->type,core_inter_head[one_core]->next->type);
  791. tmp_cores_list = tmp_cores_list->next;
  792. }
  793. app_state = RUNNING;
  794. }
  795. }
  796. while (selfopt_man_offers != NULL) {
  797. if (core_inter_head[selfopt_man_offers->sender]->type == REP_AGENT_OFFER_PENDING){
  798. core_inter_head[selfopt_man_offers->sender]->type = REP_AGENT_OFFER_SENT;
  799. kill(pid_num[selfopt_man_offers->sender], SIG_REP_OFFERS);
  800. my_stats.msg_count++;
  801. my_stats.distance += distance(node_id,selfopt_man_offers->sender);
  802. } else if (core_inter_head[selfopt_man_offers->sender]->type != REP_AGENT_OFFER_SENT) printf("We have zxcvzxc problem!\n");
  803. tmp_offer_list = selfopt_man_offers;
  804. selfopt_man_offers = selfopt_man_offers->next;
  805. free(tmp_offer_list);
  806. }
  807. printf("Self opt seems to be ok cores count = %d\n",my_cores_count);
  808. cur_time = time(NULL);
  809. cur_t = localtime(&cur_time);
  810. fprintf(log_file, "[%d:%d:%d]: Self opt ok. my_cores_count = %d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,my_cores_count);
  811. fprintf(log_file, "my cores are:");
  812. fprintf(app_log_file, "[%d:%d:%d]: Self opt ok. my_cores_count = %d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,my_cores_count);
  813. fprintf(app_log_file, "my cores are:");
  814. printf("I am %d and my cores are:",node_id);
  815. for (tmp_cores_list=my_cores; tmp_cores_list!=NULL; tmp_cores_list=tmp_cores_list->next) {
  816. fprintf(log_file, " %d", tmp_cores_list->core_id);
  817. fprintf(app_log_file, " %d", tmp_cores_list->core_id);
  818. printf(" %d",tmp_cores_list->core_id);
  819. }
  820. printf("\n");
  821. fprintf(log_file, "\n");
  822. fflush(log_file);
  823. fprintf(app_log_file, "\n");
  824. fflush(app_log_file);
  825. }
  826. if (app_state == APP_TERMINATED) state = AGENT_ENDING;//app_terminated
  827. else {
  828. if (pending_state == INIT_MANAGER) {
  829. state = INIT_MANAGER;
  830. pending_state = IDLE_AGENT;
  831. } else {
  832. printf("I am %d in chk_offers and pending_state=%d\n",node_id,pending_state);
  833. if (selfopt_interval > 0) { // && !app_terminated
  834. its.it_value.tv_nsec = selfopt_interval * MS;
  835. selfopt_time_rem = selfopt_interval;
  836. if (old_cores_cnt == 1 || old_cores_cnt == my_cores_count) {
  837. if (my_cores_count == max_cores_count) {
  838. fprintf(app_log_file, "I have maximum cores count. I don't initiate selfopt process in selfopt.\n");
  839. fflush(app_log_file);
  840. selfopt_time_rem = -1;
  841. } else if (time_per_node <= (selfopt_interval / 2) && my_cores_count > 1) {
  842. fprintf(app_log_file, "I have little working time left time_per_node=%d selfopt_interval=%d in selfopt.\n",time_per_node,selfopt_interval);
  843. fflush(app_log_file);
  844. selfopt_time_rem = -1;
  845. } else if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error8\n");
  846. }
  847. } else if (my_cores_count == 1) {
  848. printf("I am %d and i have to rewind my selfopt_areas_file for app with id %d\n",node_id,init_app.id);
  849. fprintf(log_file,"I have to rewind my selfopt_areas_file for app with id %d\n",my_app.id);
  850. fflush(log_file);
  851. fprintf(app_log_file,"I have to rewind my selfopt_areas_file for app with id %d\n",my_app.id);
  852. fflush(app_log_file);
  853. selfopt_interval = 200;
  854. its.it_value.tv_nsec = selfopt_interval * MS;
  855. selfopt_time_rem = selfopt_interval;
  856. if (timer_settime(timerid, 0, &its, NULL) == -1) perror("timer_settime error82\n");
  857. } else selfopt_time_rem = -1;
  858. state = IDLE_AGENT;
  859. }
  860. }
  861. signals_enable();
  862. } else if (state == WORKING_NODE || state == WORKING_NODE_IDLE_INIT){
  863. int j,dummy=0;
  864. signals_disable();
  865. while (time_worked <= upper_work_bound){
  866. signals_enable();
  867. //the following code should be one ms long
  868. for (i=0; i<1000; i++)
  869. for(j=0; j<1000; j++)
  870. dummy++;
  871. signals_disable();
  872. time_worked++;
  873. }
  874. printf("I am node %d with agent = %d and I finished my work upper_work_bound = %ld time_passed=%d\n",node_id,my_agent,upper_work_bound,time_worked);
  875. if (upper_work_bound > 0) {
  876. kill(pid_num[my_agent],SIG_FINISH);
  877. my_stats.msg_count++;
  878. my_stats.distance += distance(node_id,my_agent);
  879. }
  880. if (state == WORKING_NODE) state = IDLE_CORE;
  881. else if (state == WORKING_NODE_IDLE_INIT) state = IDLE_INIT_MAN;
  882. else if (pending_state == WORKING_NODE) pending_state = NO_PENDING_STATE; //just finished on return
  883. signals_enable();
  884. } else if (state == AGENT_ZOMBIE) {
  885. pause();
  886. } else if (state == AGENT_ENDING) {
  887. signals_disable();
  888. cur_time = time(NULL);
  889. cur_t = localtime(&cur_time);
  890. fprintf(log_file, "[%d:%d:%d]: I entered agent_ending\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  891. fflush(log_file);
  892. fprintf(app_log_file, "[%d:%d:%d]: I entered agent_ending\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  893. fflush(app_log_file);
  894. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  895. fprintf(app_log_file,"Core %d is offered to %d\n",tmp_cores_list->core_id,tmp_cores_list->offered_to);
  896. fflush(app_log_file);
  897. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  898. if (tmp_cores_list->offered_to != -1) break;
  899. if (tmp_cores_list != NULL) {
  900. state = AGENT_ZOMBIE;
  901. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next) {
  902. one_core = tmp_cores_list->core_id;
  903. tmp_inter_prev = NULL;
  904. tmp_inter_list = core_inter_head[one_core];
  905. //for (tmp_inter_list = core_inter_head[one_core]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
  906. while (tmp_inter_list != NULL)
  907. if (tmp_inter_list->type == INIT_WORK_NODE || tmp_inter_list->type == APPOINT_WORK_NODE || tmp_inter_list->type == INIT_WORK_NODE_PENDING
  908. || tmp_inter_list->type == APPOINT_WORK_NODE_PENDING || tmp_inter_list->type == REMOVED_NODE_REM_TIME) {
  909. //|| (tmp_inter_list->type == APPOINT_WORK_NODE_PENDING && tmp_inter_prev != NULL)) {
  910. fprintf(log_file, "Removing in zombie one node of %d with inter = %d\n",one_core,tmp_inter_list->type);
  911. fflush(log_file);
  912. if (tmp_inter_prev == NULL) {
  913. core_inter_head[one_core] = core_inter_head[one_core]->next;
  914. free(tmp_inter_list);
  915. tmp_inter_list = core_inter_head[one_core];
  916. } else {
  917. tmp_inter_prev->next = tmp_inter_list->next;
  918. if (tmp_inter_prev->next == NULL) core_inter_tail[one_core] = tmp_inter_prev;
  919. free(tmp_inter_list);
  920. tmp_inter_list = tmp_inter_prev->next;
  921. }
  922. } else {
  923. tmp_inter_prev = tmp_inter_list;
  924. tmp_inter_list = tmp_inter_list->next;
  925. }
  926. }
  927. } else {
  928. if (core_inter_head[my_idag] == NULL){
  929. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  930. core_inter_tail[my_idag] = core_inter_head[my_idag];
  931. } else {
  932. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  933. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  934. }
  935. core_inter_tail[my_idag]->type = REMOVE_APP;
  936. core_inter_tail[my_idag]->data.app_cores = (int *)malloc((my_cores_count+1)*sizeof(int));
  937. core_inter_tail[my_idag]->data.app_cores[0] = my_cores_count;
  938. core_inter_tail[my_idag]->next = NULL;
  939. i=1;
  940. core_inter_tail[my_idag]->data.app_cores[i++] = my_cores->core_id;
  941. tmp_cores_list = my_cores;
  942. my_cores=my_cores->next;
  943. free(tmp_cores_list);
  944. //for(my_cores=my_cores->next; my_cores!=NULL; my_cores = my_cores->next) {
  945. //free(tmp_cores_list);
  946. while (my_cores != NULL) {
  947. tmp_cores_list = my_cores;
  948. one_core = tmp_cores_list->core_id;
  949. core_inter_tail[my_idag]->data.app_cores[i++] = one_core;
  950. tmp_inter_prev = NULL;
  951. tmp_inter_list = core_inter_head[one_core];
  952. //for (tmp_inter_list = core_inter_head[one_core]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
  953. while (tmp_inter_list != NULL)
  954. if (tmp_inter_list->type == INIT_WORK_NODE || tmp_inter_list->type == APPOINT_WORK_NODE || tmp_inter_list->type == INIT_WORK_NODE_PENDING
  955. || tmp_inter_list->type == APPOINT_WORK_NODE_PENDING || tmp_inter_list->type == REMOVED_NODE_REM_TIME) {
  956. fprintf(log_file, "Removing one node of %d with inter = %d\n",one_core,tmp_inter_list->type);
  957. fflush(log_file);
  958. if (tmp_inter_prev == NULL) {
  959. core_inter_head[one_core] = core_inter_head[one_core]->next;
  960. free(tmp_inter_list);
  961. tmp_inter_list = core_inter_head[one_core];
  962. } else {
  963. tmp_inter_prev->next = tmp_inter_list->next;
  964. if (tmp_inter_prev->next == NULL) core_inter_tail[one_core] = tmp_inter_prev;
  965. free(tmp_inter_list);
  966. tmp_inter_list = tmp_inter_prev->next;
  967. }
  968. } else {
  969. tmp_inter_prev = tmp_inter_list;
  970. tmp_inter_list = tmp_inter_list->next;
  971. }
  972. if (core_inter_head[one_core] == NULL) {
  973. kill(pid_num[one_core],SIG_FINISH);
  974. my_stats.msg_count++;
  975. my_stats.distance += distance(node_id,one_core);
  976. } else printf("I am %d and I still still have smth to do with my work node %d before finish which is %d\n",node_id,one_core,core_inter_head[one_core]->type);
  977. my_cores=my_cores->next;
  978. free(tmp_cores_list);
  979. }
  980. if (core_inter_head[my_idag]->next == NULL) {
  981. kill(pid_num[my_idag],SIG_FINISH);
  982. my_stats.msg_count++;
  983. my_stats.distance += distance(node_id,my_idag);
  984. } else printf("I am %d and i am doing smth else with my idag app_remove interaction=%d\n",node_id,core_inter_head[my_idag]->type);
  985. my_app.A = 0.0;
  986. my_app.var = 0.0;
  987. my_app.num_of_cores = -1;
  988. selfopt_time_rem = -1;
  989. //app_terminated = 0;
  990. app_state = NO_APP;
  991. printf("I am %d My app ended app_id = %d pending_state=%d\n",node_id,my_app.id,pending_state);
  992. cur_time = time(NULL);
  993. cur_t = localtime(&cur_time);
  994. fprintf(app_log_file, "[%d:%d:%d]: App ended app_id = %d pending_state=%d",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,my_app.id,pending_state);
  995. //fprintf(app_log_file,"App ended app_id = %d pending_state=%d\n",my_app.id,pending_state);
  996. fclose(app_log_file);
  997. if (pending_state == INIT_MANAGER) {
  998. state = INIT_MANAGER;
  999. pending_state = IDLE_CORE;
  1000. } else if (pending_state == NO_PENDING_STATE) state = IDLE_CORE;
  1001. else {
  1002. printf("I am %d in agent_ending and pending_state = %d\n",node_id,pending_state);
  1003. state = IDLE_CORE;
  1004. }
  1005. }
  1006. signals_enable(); */
  1007. } else {
  1008. printf("Unknown state node_id = %d state = %d\n",node_id,state);
  1009. state = IDLE_CORE;
  1010. }
  1011. cur_time = time(NULL);
  1012. cur_t = localtime(&cur_time);
  1013. fprintf(log_file, "[%d:%d:%d]: I ended well\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  1014. fclose(log_file);
  1015. exit(0);
  1016. }