my_rtrm.c 26 KB

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