distrm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. #include "distrm.h"
  2. #include "libfunctions.h"
  3. #include "noc_functions.h"
  4. #include "sig_aux.h"
  5. #include "idle_agent.h"
  6. #include "common_node.h"
  7. #include "signal_handlers.h"
  8. #include "scc_signals.h"
  9. //int *pid_num;
  10. int num_idags, node_id=-1,my_idag=-1;//, fd_log;
  11. FILE *log_file;
  12. core_states state;
  13. inter_list **core_inter_head,**core_inter_tail, *init_pending_head=NULL, *init_pending_tail;
  14. /*app my_app = {.A=-1, .var=-1, .num_of_cores=-1};
  15. app init_app = {.A=-1, .var=-1, .num_of_cores=-1};
  16. app far_req_app = {.A=-1, .var=-1, .num_of_cores=-1};
  17. metrics my_stats = {.msg_count=0, .message_size=0, .distance=0, .app_turnaround=0, .comp_effort=0, .cores_utilized=0, .times_accessed=0};
  18. metrics total_stats = {.msg_count=0, .message_size=0, .distance=0, .app_turnaround=0, .comp_effort=0, .cores_utilized=0, .times_accessed=0};*/
  19. app my_app, init_app;
  20. metrics my_stats, total_stats;
  21. int far_req_or_sender=-1;//far_req_max_man_cores=-1, far_req_max_man=-1, far_req_max_man_count=0,
  22. int *idag_mask, *idag_id_arr;
  23. int *Cl_x_max_arr, *Cl_y_max_arr;
  24. int DDS_count=0, my_cores_count=0;
  25. DDS_list *DDS=NULL, *DDS_tail;
  26. core_list *my_cores=NULL, *my_cores_tail;
  27. offer_list *init_man_offers=NULL, *selfopt_man_offers=NULL;
  28. offer_list *far_man_offers=NULL, *far_man_offers_tail=NULL;
  29. int far_list_count=0, far_man_agent_count=0;
  30. int my_agent = -1, time_worked=0;
  31. int debug_global=0;
  32. time_t cur_time;
  33. struct tm *cur_t;
  34. struct sigevent sev;
  35. struct itimerspec its, chk_timer;
  36. timer_t timerid;
  37. long int selfopt_time_rem=-1;//-1 means it is not set
  38. long int upper_work_bound=-1;
  39. int time_for_farman = -1;
  40. char scen_num[4];
  41. int chk_rem_count=0, chk_rem_num=0, sum_rem_time=0;
  42. float old_Speedup, my_Speedup;
  43. int nodes_ended_cnt=0, app_terminated=0;
  44. int nodes_initialised=0, stats_replied=0, num_apps_terminated=0,num_apps=0, idags_replied=0;
  45. int *sig_array, *data_array, NUES;
  46. RCCE_FLAG flag_signals_enabled,flag_data_written;
  47. float Speedup(app cur_app, int num_of_cores){
  48. float res=0;
  49. if (num_of_cores == 0) return 0;
  50. if (cur_app.var < 1.0)
  51. if (num_of_cores == 1) res = 1;//000000;//0;//cur_app.A;
  52. else if (num_of_cores > 1 && num_of_cores <= cur_app.A)
  53. res = (num_of_cores*cur_app.A) / (cur_app.A + (cur_app.var / (2.0*(num_of_cores-1))));
  54. else if (num_of_cores >= cur_app.A && num_of_cores <= 2.0*cur_app.A - 1)
  55. res = (num_of_cores*cur_app.A) / (cur_app.var*(cur_app.A -0.5) + num_of_cores*(1.0 - 0.5*cur_app.var));
  56. else res = cur_app.A;
  57. else
  58. if (num_of_cores >= 1 && num_of_cores <= cur_app.A + cur_app.A*cur_app.var - cur_app.var)
  59. res = (num_of_cores*cur_app.A*(cur_app.var + 1)) / (cur_app.A + cur_app.var*(num_of_cores-1 + cur_app.var));
  60. else res = cur_app.A;
  61. return res;
  62. }
  63. int offer_cores(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id){
  64. int Of_cores_num=0;
  65. float gain_total=0.1,base_receiver,base_giver,gain_receiver,loss_giver,share_giver;
  66. int Cores_receiver = req_app.num_of_cores, Cores_giver = my_app.num_of_cores;
  67. core_list *tmp, *GreedyChoice;
  68. //printf("node_id=%d Lookinelse if (head->type == IDAG_REM_CORES_DDS)
  69. tmp = cores;
  70. while (tmp != NULL){
  71. if (distance(req_reg.C, tmp->core_id) <= req_reg.r) share_giver++;
  72. tmp = tmp->next;
  73. }
  74. share_giver = share_giver / (float) region_count(req_reg);
  75. while (gain_total > 0.0){
  76. gain_total = 0.0;
  77. GreedyChoice = NULL;//-1;
  78. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  79. if (my_idag == -1) base_giver = 0;
  80. else base_giver = Speedup(my_app, Cores_giver - Of_cores_num);
  81. tmp = cores->next;//very important!!! that way i avoid giving up my agent core
  82. while (tmp != NULL){
  83. if (tmp->offered_to == -1 && distance(req_reg.C, tmp->core_id) <= req_reg.r){
  84. if (Cores_receiver == 0 && Of_cores_num == 1 && req_app.var < 1.0) gain_receiver = 1000000;
  85. else gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  86. if (my_idag == -1) loss_giver = 0;
  87. else loss_giver = base_giver - Speedup(my_app, Cores_giver - Of_cores_num - 1);
  88. if (gain_receiver - loss_giver > gain_total){
  89. gain_total = gain_receiver - loss_giver;
  90. GreedyChoice = tmp;//->core_id;
  91. //tmp->offered_to = req_id;
  92. }
  93. }
  94. tmp = tmp->next;
  95. }
  96. if (gain_total > 0.0) {
  97. Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  98. GreedyChoice->offered_to = req_id;//tmp
  99. }
  100. }
  101. return Of_cores_num;
  102. }
  103. void send_next_signal(inter_list *head, int node_num){
  104. inter_list *tmp_inter_list=NULL;
  105. //printf("I am in send next signal with node num = %d\n",node_num);
  106. if (head->type == IDAG_FIND_IDAGS || head->type == SELFOPT_IDAG_FIND_IDAGS || head->type == REP_IDAG_FIND_IDAGS
  107. || head->type == SELFOPT_IDAG_FIND_IDAGS_PENDING || head->type == IDAG_FIND_IDAGS_PENDING)
  108. //if (head->type == SELFOPT_IDAG_FIND_IDAGS) printf("kariolares!!\n");
  109. scc_kill(node_num, SIG_IDAG_FIND_IDAGS);
  110. else if (head->type == IDAG_REQ_DDS || head->type == FAR_REQ_IDAG_REQ_DDS || head->type == SELFOPT_IDAG_REQ_DDS
  111. || head->type == SELFOPT_IDAG_REQ_DDS_PENDING || head->type == FAR_REQ_IDAG_REQ_DDS_PENDING || head->type == IDAG_REQ_DDS_PENDING)
  112. scc_kill(node_num, SIG_REQ_DDS);
  113. else if (head->type == AGENT_REQ_CORES || head->type == FAR_REQ_CORES || head->type == SELFOPT_REQ_CORES
  114. || head->type == AGENT_REQ_CORES_PENDING || head->type == SELFOPT_REQ_CORES_PENDING || head->type == FAR_REQ_CORES_PENDING)
  115. scc_kill(node_num, SIG_REQ_CORES);
  116. else if (head->type == FAR_REQ_MAN_APPOINT || head->type == FAR_REQ_OFFER)
  117. scc_kill(node_num, SIG_FAR_REQ);
  118. else if (head->type == IDAG_ADD_CORES_DDS)
  119. scc_kill(node_num, SIG_ADD_CORES_DDS);
  120. else if (head->type == IDAG_REM_CORES_DDS)
  121. scc_kill(node_num, SIG_REM_CORES_DDS);
  122. else if (head->type == INIT_WORK_NODE || head->type == APPOINT_WORK_NODE)
  123. scc_kill(node_num, SIG_APPOINT_WORK);
  124. else if (head->type == REMOVE_APP)
  125. scc_kill(node_num, SIG_FINISH);
  126. else if (head->type == INIT_APP)
  127. scc_kill(node_num, SIG_INIT_APP);
  128. else if (head->type == FAR_INIT_REQ)
  129. scc_kill(node_num, SIG_INIT_FAR_REQ);
  130. else if (head->type == REP_AGENT_REQ_CORES)
  131. scc_kill(node_num, SIG_REQ_CORES);
  132. else if (head->type == INIT_AGENT)
  133. scc_kill(node_num, SIG_INIT_AGENT);
  134. else if (head->type == ABORT_FAR_MAN)
  135. scc_kill(node_num, SIG_REMOVE_FAR_MAN);
  136. else if (head->type == REP_CHK_REM_TIME || head->type == APPOINT_WORK_NODE_PENDING || head->type == REMOVED_NODE_REM_TIME) {//|| head->type == INIT_WORK_NODE_PENDING
  137. scc_kill(node_num, SIG_CHECK_REM_TIME);
  138. fprintf(log_file,"I sent in send next SIG_CHECK_REM_TIME to node %d with interaction = %d\n",node_num,head->type);
  139. fflush(log_file);
  140. } else if (head->type == NOTIFY_APP_TERMINATION) {
  141. scc_kill(0, SIG_APP_TERMINATED);
  142. tmp_inter_list = core_inter_head[0];
  143. core_inter_head[0] = core_inter_head[0]->next;
  144. if (core_inter_head[0] == NULL) core_inter_tail[0] = NULL;
  145. else send_next_signal(core_inter_head[0],0);
  146. free(tmp_inter_list);
  147. } else if (head->type != FAR_REQ_MAN) {
  148. printf("undefined state in send_next %d node_num=%d\n",head->type,node_num);
  149. fprintf(log_file,"undefined state in send_next %d node_num=%d\n",head->type,node_num);
  150. fflush(log_file);
  151. }
  152. my_stats.msg_count++;
  153. my_stats.distance += distance(node_id,node_num);
  154. }
  155. int RCCE_APP(int argc, char *argv[]){
  156. int Cl_x_max, Cl_y_max;//, num_idags_x, num_idags_y,i;
  157. int num_of_bytes,segment_id;
  158. int i,j,k;
  159. int ans=0;
  160. int one_idag, one_core;
  161. DDS_list *tmp_DDS;
  162. pid_t p;
  163. core_list *tmp_cores_list;
  164. inter_list *tmp_inter_list;
  165. float avg_cluster_util;
  166. RCCE_init(&argc, &argv);
  167. node_id = RCCE_ue();
  168. NUES = RCCE_num_ues();
  169. RCCE_flag_alloc(&flag_signals_enabled);
  170. RCCE_flag_alloc(&flag_data_written);
  171. RCCE_flag_write(&flag_signals_enabled, RCCE_FLAG_UNSET, node_id);
  172. RCCE_flag_write(&flag_data_written, RCCE_FLAG_UNSET, node_id);
  173. sig_array = (int *) RCCE_malloc(NUES * LINE_SIZE * sizeof(int));//NUES * NUES
  174. //data_array = (int *) RCCE_malloc(3 * LINE_SIZE * sizeof(int));
  175. num_idags = num_idags_x * num_idags_y;
  176. strcpy(scen_num,argv[1]);
  177. data_array = (int *) RCCE_malloc(4 * 36 * LINE_SIZE * sizeof(int));
  178. core_inter_head = (inter_list **) malloc(X_max*Y_max*sizeof(inter_list *));
  179. core_inter_tail = (inter_list **) malloc(X_max*Y_max*sizeof(inter_list *));
  180. for (i=0; i<X_max*Y_max; i++){
  181. core_inter_head[i] = NULL;
  182. core_inter_tail[i] = NULL;
  183. }
  184. my_stats.msg_count=0;
  185. my_stats.message_size=0;
  186. my_stats.distance=0;
  187. my_stats.app_turnaround=0;
  188. my_stats.comp_effort=0;
  189. my_stats.cores_utilized=0;
  190. my_stats.times_accessed=0;
  191. total_stats.msg_count=0;
  192. total_stats.message_size=0;
  193. total_stats.distance=0;
  194. total_stats.app_turnaround=0;
  195. total_stats.comp_effort=0;
  196. total_stats.cores_utilized=0;
  197. total_stats.times_accessed=0;
  198. my_app.A=-1;
  199. my_app.var=-1;
  200. my_app.num_of_cores=-1;
  201. init_app.A=-1;
  202. init_app.var=-1;
  203. init_app.num_of_cores=-1;
  204. if (node_id == 3) {
  205. idle_agent_actions(1, scen_num);
  206. } else if (node_id == 18) {
  207. idle_agent_actions(2, scen_num);
  208. } else if (node_id == 21) {
  209. idle_agent_actions(3, scen_num);
  210. } else if (node_id != 0) {
  211. common_node_actions(scen_num);
  212. } else {
  213. i = get_cluster_info(0, &Cl_x_max, &Cl_y_max);
  214. idag_id_arr = (int *) malloc(num_idags*sizeof(int));
  215. Cl_x_max_arr = (int *) malloc(num_idags*sizeof(int));
  216. Cl_y_max_arr = (int *) malloc(num_idags*sizeof(int));
  217. idag_mask = (int *) malloc(X_max*Y_max*sizeof(int));
  218. for (i=0; i<num_idags; i++){
  219. idag_id_arr[i] = get_cluster_info(i, &Cl_x_max_arr[i], &Cl_y_max_arr[i]);
  220. for (j=idag_id_arr[i]; j<idag_id_arr[i] + Cl_y_max_arr[i]*X_max; j+=X_max)
  221. for (k=0; k<Cl_x_max_arr[i]; k++)
  222. idag_mask[j+k] = idag_id_arr[i];
  223. }
  224. //printf("node_id = %d, Cl_x_max = %d, Cl_y_max = %d, pid = %d\n",node_id,Cl_x_max,Cl_y_max,getpid());
  225. log_file = create_log_file(node_id, scen_num);
  226. cur_time = time(NULL);
  227. cur_t = localtime(&cur_time);
  228. fprintf(log_file, "[%d:%d:%d]: I start initialising node_id=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,node_id);
  229. fflush(log_file);
  230. install_signal_handlers();
  231. //sig_SEGV_enable();
  232. sev.sigev_notify = SIGEV_SIGNAL;
  233. sev.sigev_signo = SIG_TIMER;
  234. sev.sigev_value.sival_ptr = &timerid;
  235. if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) printf("timer_create error\n");
  236. for (j=node_id; j<node_id+Cl_x_max*X_max; j+=X_max)
  237. for (k=0; k<Cl_x_max; k++) {
  238. if (my_cores == NULL) {
  239. my_cores = (core_list *) malloc(sizeof(core_list));
  240. my_cores_tail = my_cores;
  241. } else {
  242. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  243. my_cores_tail = my_cores_tail->next;
  244. }
  245. my_cores_count++;
  246. my_cores_tail->core_id = j+k;
  247. my_cores_tail->offered_to = -1;
  248. my_cores_tail->next = NULL;
  249. if ((j+k) == node_id) {
  250. DDS = (DDS_list *) malloc(sizeof(DDS_list));
  251. DDS->agent_id = j+k;
  252. DDS->num_of_cores = Cl_x_max*Cl_y_max;
  253. DDS->next = NULL;
  254. DDS_tail = DDS;
  255. DDS_count++;
  256. //pid_num[j+k] = getpid();
  257. }
  258. }
  259. RCCE_barrier(&RCCE_COMM_WORLD);
  260. //sleep(1);
  261. for (j=node_id; j<node_id+Cl_x_max*X_max; j+=X_max)
  262. for (k=0; k<Cl_x_max; k++)
  263. if ((j+k) != node_id) {
  264. signals_disable();
  265. one_core = j+k;
  266. if (core_inter_head[one_core] == NULL){
  267. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  268. core_inter_tail[one_core] = core_inter_head[one_core];
  269. } else {
  270. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  271. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  272. }
  273. core_inter_tail[one_core]->type = INIT_CORE;
  274. core_inter_tail[one_core]->next = NULL;
  275. //kill(pid_num[one_core], SIG_INIT);
  276. //scc_kill(one_core, SIG_INIT);
  277. sig_ACK_sender(one_core, SIG_INIT);
  278. //my_stats.msg_count++;
  279. //my_stats.distance += distance(node_id,one_core);
  280. signals_enable();
  281. }
  282. //while (nodes_initialised != my_cores_count-1) pause();
  283. //sleep(1);
  284. //printf("End of initialisation\n");
  285. int dummy=0;
  286. while (nodes_initialised != my_cores_count-1) {//pause(); my_cores_count
  287. for (i=0; i<1000; i++)
  288. for(j=0; j<1000; j++)
  289. dummy++;
  290. scc_signals_check();
  291. }
  292. printf("End of initialisation\n");
  293. FILE *app_input;
  294. char app_input_file_name[64];
  295. int app_cnt=0,time_passed=-1,time_next,init_core;
  296. offer_list *tmp_offer_list;
  297. strcpy(app_input_file_name,"/shared/herc/distrm/scenaria/");
  298. strcat(app_input_file_name, argv[1]);
  299. strcat(app_input_file_name, "/app_input.txt");
  300. //printf("file path = %s\n",app_input_file_name);
  301. if ((app_input = fopen(app_input_file_name, "r")) == NULL){
  302. printf("Cannot open input file with file path = %s ",app_input_file_name);
  303. perror("open app_input");
  304. }
  305. fscanf(app_input,"%d",&time_next);
  306. state = IDLE_CHK_APP_FILE;
  307. its.it_interval.tv_sec = 0;
  308. its.it_interval.tv_nsec = 0;
  309. its.it_value.tv_sec = 0;
  310. its.it_value.tv_nsec = 10 * MS;
  311. if (timer_settime(timerid, 0, &its, NULL) == -1) perror("timer_settime error9");
  312. while (state != IDAG_ENDING)
  313. if (state == IDLE_IDAG || state == IDLE_FAR_MAN || state == IDLE_CHK_APP_FILE) {
  314. //pause();
  315. dummy=0;
  316. for (i=0; i<100; i++)
  317. for(j=0; j<100; j++)
  318. dummy++;
  319. scc_signals_check();
  320. } else if (state == CHK_APP_FILE) {
  321. signals_disable();
  322. time_passed++;
  323. if (time_for_farman > 0) time_for_farman -= 10;
  324. if (time_next == time_passed) {
  325. fscanf(app_input,"%d",&init_core);
  326. num_apps++;
  327. for (tmp_inter_list = core_inter_head[init_core]; tmp_inter_list != NULL; tmp_inter_list = tmp_inter_list->next)
  328. if (tmp_inter_list->type == INIT_APP) break;
  329. if (tmp_inter_list == NULL) {
  330. if (core_inter_head[init_core] == NULL){
  331. core_inter_head[init_core] = (inter_list *) malloc(sizeof(inter_list));
  332. core_inter_tail[init_core] = core_inter_head[init_core];
  333. } else {
  334. core_inter_tail[init_core]->next = (inter_list *) malloc(sizeof(inter_list));
  335. core_inter_tail[init_core] = core_inter_tail[init_core]->next;
  336. }
  337. core_inter_tail[init_core]->type = INIT_APP;
  338. fscanf(app_input,"%f",&core_inter_tail[init_core]->data.new_app.workld);
  339. fscanf(app_input,"%f",&core_inter_tail[init_core]->data.new_app.A);
  340. fscanf(app_input,"%f",&core_inter_tail[init_core]->data.new_app.var);
  341. core_inter_tail[init_core]->data.new_app.id = app_cnt++;
  342. core_inter_tail[init_core]->data.new_app.num_of_cores = 0;
  343. core_inter_tail[init_core]->next = NULL;
  344. printf("time = %d, id = %d, workld = %0.2f \n",time_passed,core_inter_tail[init_core]->data.new_app.id,core_inter_tail[init_core]->data.new_app.workld);
  345. cur_time = time(NULL);
  346. cur_t = localtime(&cur_time);
  347. fprintf(log_file, "[%d:%d:%d]: Initialising app_id=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,core_inter_tail[init_core]->data.new_app.id);
  348. fflush(log_file);
  349. //printf("A = %0.2f, var = %0.2f\n",core_inter_tail[init_core]->data.new_app.A,core_inter_tail[init_core]->data.new_app.var);
  350. if (core_inter_head[init_core]->next == NULL) {
  351. //kill(pid_num[init_core],SIG_INIT_APP);
  352. sig_ACK_sender(init_core,SIG_INIT_APP);
  353. //scc_kill(init_core,SIG_INIT_APP);
  354. my_stats.msg_count++;
  355. my_stats.distance += distance(node_id,init_core);
  356. }
  357. } else {
  358. if (init_pending_head == NULL){
  359. init_pending_head = (inter_list *) malloc(sizeof(inter_list));
  360. init_pending_tail = init_pending_head;
  361. } else {
  362. init_pending_tail->next = (inter_list *) malloc(sizeof(inter_list));
  363. init_pending_tail = init_pending_tail->next;
  364. }
  365. init_pending_tail->type = INIT_APP;
  366. fscanf(app_input,"%f",&init_pending_tail->data.new_app.workld);
  367. fscanf(app_input,"%f",&init_pending_tail->data.new_app.A);
  368. fscanf(app_input,"%f",&init_pending_tail->data.new_app.var);
  369. init_pending_tail->data.new_app.id = app_cnt++;
  370. //apparently i want num_of_cores to be 0. But i will temporarilly use it as an indicator of init_core so as not
  371. //change inter_list type or introduse an a new data union structure
  372. init_pending_tail->data.new_app.num_of_cores = init_core;
  373. init_pending_tail->next = NULL;
  374. }
  375. if (fscanf(app_input,"%d",&time_next) == EOF) {
  376. if (time_for_farman == 0 || time_for_farman == -5) state = FAR_MAN_CHK_OFFERS;
  377. else if (time_for_farman > 0){
  378. its.it_value.tv_nsec = time_for_farman * 10 * MS;
  379. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error10\n");
  380. state = USER_INPUT;
  381. } else state = USER_INPUT;
  382. time_passed = -1;
  383. } else {
  384. if (time_for_farman == 0 || time_for_farman == -5) state = FAR_MAN_CHK_OFFERS;
  385. else {
  386. its.it_value.tv_nsec = 10 * MS;
  387. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error11\n");
  388. state = IDLE_CHK_APP_FILE;
  389. }
  390. }
  391. } else {
  392. state = IDLE_CHK_APP_FILE;
  393. its.it_value.tv_nsec = 10 * MS;
  394. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error92\n");
  395. }
  396. signals_enable();
  397. } else if (state == USER_INPUT) {
  398. while (num_apps_terminated != num_apps) {//pause(); my_cores_count
  399. dummy=0;
  400. for (i=0; i<100; i++)
  401. for(j=0; j<100; j++)
  402. dummy++;
  403. scc_signals_check();
  404. }
  405. for (one_idag=0; one_idag <10; one_idag++) {
  406. dummy=0;
  407. for (i=0; i<1000; i++)
  408. for(j=0; j<1000; j++)
  409. dummy++;
  410. scc_signals_check();
  411. }
  412. for (j=0; j<num_idags; j++) {
  413. one_idag = idag_id_arr[j];
  414. if (one_idag != 0){
  415. if (core_inter_head[one_idag] == NULL){
  416. core_inter_head[one_idag] = (inter_list *) malloc(sizeof(inter_list));
  417. core_inter_tail[one_idag] = core_inter_head[one_idag];
  418. } else {
  419. core_inter_tail[one_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  420. core_inter_tail[one_idag] = core_inter_tail[one_idag]->next;
  421. }
  422. core_inter_tail[one_idag]->type = DEBUG_IDAG_REQ_DDS;
  423. core_inter_tail[one_idag]->data.reg.C = -1;
  424. core_inter_tail[one_idag]->data.reg.r = 0;
  425. core_inter_tail[one_idag]->next = NULL;
  426. if (core_inter_head[one_idag]->next == NULL) sig_ACK_sender(one_idag, SIG_REQ_DDS);
  427. //scc_kill(one_idag, SIG_REQ_DDS);//kill(pid_num[one_idag], SIG_REQ_DDS);
  428. else printf("what the fuck? interaction is %d\n",core_inter_head[one_idag]->type);
  429. } else {
  430. printf("Number of agents in region = %d\n",DDS_count);
  431. tmp_DDS = DDS;
  432. i=0;
  433. while (tmp_DDS != NULL){
  434. printf("Agent no %d is %d with %d cores\n",i,tmp_DDS->agent_id,tmp_DDS->num_of_cores);
  435. tmp_DDS = tmp_DDS->next;
  436. i++;
  437. }
  438. }
  439. }
  440. while (idags_replied < num_idags - 1) {
  441. dummy=0;
  442. for (i=0; i<1000; i++)
  443. for(j=0; j<1000; j++)
  444. dummy++;
  445. scc_signals_check();
  446. }
  447. fprintf(log_file,"killing\n");
  448. fflush(log_file);
  449. for (i=1; i<num_idags; i++) {
  450. printf("i am killing %d\n",idag_id_arr[i]);
  451. one_core = idag_id_arr[i];
  452. if (core_inter_head[one_core] == NULL){
  453. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  454. core_inter_tail[one_core] = core_inter_head[one_core];
  455. } else {
  456. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  457. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  458. fprintf(log_file,"I am still doing smth with idag %d interaction = %d\n",one_core,core_inter_head[one_core]->type);
  459. fflush(log_file);
  460. }
  461. core_inter_tail[one_core]->type = TERMINATION_STATS;
  462. core_inter_tail[one_core]->next = NULL;
  463. scc_kill(one_core, SIG_TERMINATE);
  464. //kill(pid_num[idag_id_arr[i]], SIG_TERMINATE);
  465. //my_stats.msg_count++;
  466. //my_stats.distance += distance(node_id,idag_id_arr[i]);
  467. }
  468. tmp_cores_list = my_cores;
  469. my_cores = my_cores->next;
  470. free(tmp_cores_list);
  471. for (; my_cores != NULL; my_cores = my_cores->next){
  472. tmp_cores_list = my_cores;
  473. one_core = my_cores->core_id;
  474. if (core_inter_head[one_core] == NULL){
  475. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  476. core_inter_tail[one_core] = core_inter_head[one_core];
  477. } else {
  478. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  479. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  480. fprintf(log_file,"I am still doing smth with my node %d interaction = %d\n",one_core,core_inter_head[one_core]->type);
  481. fflush(log_file);
  482. }
  483. core_inter_tail[one_core]->type = TERMINATION_STATS;
  484. core_inter_tail[one_core]->next = NULL;
  485. //kill(pid_num[one_core], SIG_TERMINATE);
  486. scc_kill(one_core, SIG_TERMINATE);
  487. my_stats.msg_count++;
  488. my_stats.distance += distance(node_id,one_core);
  489. free(tmp_cores_list);
  490. }
  491. state = IDAG_ENDING;
  492. } else {
  493. printf("Uknown state node_id = %d state = %d\n",node_id,state);
  494. state = IDLE_IDAG;
  495. }
  496. while (state == IDAG_ENDING) {
  497. //pause();
  498. dummy=0;
  499. for (i=0; i<1000; i++)
  500. for(j=0; j<1000; j++)
  501. dummy++;
  502. scc_signals_check();
  503. if (stats_replied == my_cores_count+num_idags-2) state = TERMINATED;
  504. }
  505. //for (i=0; i<Cl_x_max*Cl_y_max-1; i++) wait(NULL); //wait for children
  506. //for (i=0; i<num_idags-1; i++) wait(NULL);//wait for the other idags
  507. total_stats.msg_count += my_stats.msg_count;
  508. total_stats.message_size += my_stats.message_size;
  509. total_stats.distance += my_stats.distance;
  510. total_stats.app_turnaround += my_stats.app_turnaround;
  511. total_stats.comp_effort += my_stats.comp_effort;
  512. total_stats.cores_utilized += my_stats.cores_utilized;
  513. total_stats.times_accessed += my_stats.times_accessed;
  514. avg_cluster_util = (float) my_stats.cores_utilized / (my_stats.times_accessed * (my_cores_count-1));
  515. printf("I am %d with cores_utilized = %d times_accessed = %d my_cores_count = %d and avg_cluster_util = %0.2f\n",
  516. node_id,my_stats.cores_utilized,my_stats.times_accessed,my_cores_count,avg_cluster_util);
  517. fprintf(log_file,"cores_utilized = %d times_accessed = %d my_cores_count = %d and avg_cluster_util = %0.2f\n",
  518. my_stats.cores_utilized,my_stats.times_accessed,my_cores_count,avg_cluster_util);
  519. fflush(log_file);
  520. printf("Total stats are:\n");
  521. printf("Total message count = %d\n",total_stats.msg_count);
  522. printf("Total message size = %d\n",total_stats.message_size);
  523. printf("Total distance = %d\n",total_stats.distance);
  524. printf("Total app turnaround time = %d\n",total_stats.app_turnaround);
  525. printf("Total computational effort = %d\n",total_stats.comp_effort);
  526. printf("Total cores_utilized = %d\n",total_stats.cores_utilized);
  527. printf("Total times_accessed = %d\n",total_stats.times_accessed);
  528. free(idag_mask);
  529. free(idag_id_arr);
  530. free(Cl_x_max_arr);
  531. free(Cl_y_max_arr);
  532. for (i=0; i<X_max*Y_max; i++){
  533. free(core_inter_head[i]);
  534. free(core_inter_tail[i]);
  535. }
  536. free(core_inter_head);
  537. free(core_inter_tail);
  538. cur_time = time(NULL);
  539. cur_t = localtime(&cur_time);
  540. fprintf(log_file, "[%d:%d:%d]: I ended well\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  541. fclose(log_file);
  542. }
  543. return 0;
  544. }