my_rtrm.c 26 KB

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