distrm.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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. core_inter_head = (inter_list **) malloc(X_max*Y_max*sizeof(inter_list *));
  178. core_inter_tail = (inter_list **) malloc(X_max*Y_max*sizeof(inter_list *));
  179. for (i=0; i<X_max*Y_max; i++){
  180. core_inter_head[i] = NULL;
  181. core_inter_tail[i] = NULL;
  182. }
  183. my_stats.msg_count=0;
  184. my_stats.message_size=0;
  185. my_stats.distance=0;
  186. my_stats.app_turnaround=0;
  187. my_stats.comp_effort=0;
  188. my_stats.cores_utilized=0;
  189. my_stats.times_accessed=0;
  190. total_stats.msg_count=0;
  191. total_stats.message_size=0;
  192. total_stats.distance=0;
  193. total_stats.app_turnaround=0;
  194. total_stats.comp_effort=0;
  195. total_stats.cores_utilized=0;
  196. total_stats.times_accessed=0;
  197. my_app.A=-1;
  198. my_app.var=-1;
  199. my_app.num_of_cores=-1;
  200. init_app.A=-1;
  201. init_app.var=-1;
  202. init_app.num_of_cores=-1;
  203. if (node_id == 3) {
  204. idle_agent_actions(1, scen_num);
  205. } else if (node_id == 18) {
  206. idle_agent_actions(2, scen_num);
  207. } else if (node_id == 21) {
  208. idle_agent_actions(3, scen_num);
  209. } else if (node_id != 0) {
  210. common_node_actions(scen_num);
  211. } else {
  212. i = get_cluster_info(0, &Cl_x_max, &Cl_y_max);
  213. idag_id_arr = (int *) malloc(num_idags*sizeof(int));
  214. Cl_x_max_arr = (int *) malloc(num_idags*sizeof(int));
  215. Cl_y_max_arr = (int *) malloc(num_idags*sizeof(int));
  216. idag_mask = (int *) malloc(X_max*Y_max*sizeof(int));
  217. for (i=0; i<num_idags; i++){
  218. idag_id_arr[i] = get_cluster_info(i, &Cl_x_max_arr[i], &Cl_y_max_arr[i]);
  219. for (j=idag_id_arr[i]; j<idag_id_arr[i] + Cl_y_max_arr[i]*X_max; j+=X_max)
  220. for (k=0; k<Cl_x_max_arr[i]; k++)
  221. idag_mask[j+k] = idag_id_arr[i];
  222. }
  223. //printf("node_id = %d, Cl_x_max = %d, Cl_y_max = %d, pid = %d\n",node_id,Cl_x_max,Cl_y_max,getpid());
  224. log_file = create_log_file(node_id, scen_num);
  225. cur_time = time(NULL);
  226. cur_t = localtime(&cur_time);
  227. 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);
  228. fflush(log_file);
  229. install_signal_handlers();
  230. //sig_SEGV_enable();
  231. sev.sigev_notify = SIGEV_SIGNAL;
  232. sev.sigev_signo = SIG_TIMER;
  233. sev.sigev_value.sival_ptr = &timerid;
  234. if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) printf("timer_create error\n");
  235. for (j=node_id; j<node_id+Cl_x_max*X_max; j+=X_max)
  236. for (k=0; k<Cl_x_max; k++) {
  237. if (my_cores == NULL) {
  238. my_cores = (core_list *) malloc(sizeof(core_list));
  239. my_cores_tail = my_cores;
  240. } else {
  241. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  242. my_cores_tail = my_cores_tail->next;
  243. }
  244. my_cores_count++;
  245. my_cores_tail->core_id = j+k;
  246. my_cores_tail->offered_to = -1;
  247. my_cores_tail->next = NULL;
  248. if ((j+k) == node_id) {
  249. DDS = (DDS_list *) malloc(sizeof(DDS_list));
  250. DDS->agent_id = j+k;
  251. DDS->num_of_cores = Cl_x_max*Cl_y_max;
  252. DDS->next = NULL;
  253. DDS_tail = DDS;
  254. DDS_count++;
  255. //pid_num[j+k] = getpid();
  256. }
  257. }
  258. RCCE_barrier(&RCCE_COMM_WORLD);
  259. //sleep(1);
  260. for (j=node_id; j<node_id+Cl_x_max*X_max; j+=X_max)
  261. for (k=0; k<Cl_x_max; k++)
  262. if ((j+k) != node_id) {
  263. signals_disable();
  264. one_core = j+k;
  265. if (core_inter_head[one_core] == NULL){
  266. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  267. core_inter_tail[one_core] = core_inter_head[one_core];
  268. } else {
  269. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  270. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  271. }
  272. core_inter_tail[one_core]->type = INIT_CORE;
  273. core_inter_tail[one_core]->next = NULL;
  274. //kill(pid_num[one_core], SIG_INIT);
  275. scc_kill(one_core, SIG_INIT);
  276. //my_stats.msg_count++;
  277. //my_stats.distance += distance(node_id,one_core);
  278. signals_enable();
  279. }
  280. //while (nodes_initialised != my_cores_count-1) pause();
  281. //sleep(1);
  282. //printf("End of initialisation\n");
  283. int dummy=0;
  284. while (nodes_initialised != my_cores_count-1) {//pause(); my_cores_count
  285. for (i=0; i<1000; i++)
  286. for(j=0; j<1000; j++)
  287. dummy++;
  288. scc_signals_check();
  289. }
  290. printf("End of initialisation\n");
  291. FILE *app_input;
  292. char app_input_file_name[64];
  293. int app_cnt=0,time_passed=-1,time_next,init_core;
  294. offer_list *tmp_offer_list;
  295. strcpy(app_input_file_name,"/shared/herc/distrm/scenaria/");
  296. strcat(app_input_file_name, argv[1]);
  297. strcat(app_input_file_name, "/app_input.txt");
  298. //printf("file path = %s\n",app_input_file_name);
  299. if ((app_input = fopen(app_input_file_name, "r")) == NULL){
  300. printf("Cannot open input file with file path = %s ",app_input_file_name);
  301. perror("open app_input");
  302. }
  303. fscanf(app_input,"%d",&time_next);
  304. state = IDLE_CHK_APP_FILE;
  305. its.it_interval.tv_sec = 0;
  306. its.it_interval.tv_nsec = 0;
  307. its.it_value.tv_sec = 0;
  308. its.it_value.tv_nsec = 10 * MS;
  309. if (timer_settime(timerid, 0, &its, NULL) == -1) perror("timer_settime error9");
  310. while (state != IDAG_ENDING)
  311. if (state == IDLE_IDAG || state == IDLE_FAR_MAN || state == IDLE_CHK_APP_FILE) {
  312. //pause();
  313. dummy=0;
  314. for (i=0; i<100; i++)
  315. for(j=0; j<100; j++)
  316. dummy++;
  317. scc_signals_check();
  318. } else if (state == CHK_APP_FILE) {
  319. signals_disable();
  320. time_passed++;
  321. if (time_for_farman > 0) time_for_farman -= 10;
  322. if (time_next == time_passed) {
  323. fscanf(app_input,"%d",&init_core);
  324. num_apps++;
  325. for (tmp_inter_list = core_inter_head[init_core]; tmp_inter_list != NULL; tmp_inter_list = tmp_inter_list->next)
  326. if (tmp_inter_list->type == INIT_APP) break;
  327. if (tmp_inter_list == NULL) {
  328. if (core_inter_head[init_core] == NULL){
  329. core_inter_head[init_core] = (inter_list *) malloc(sizeof(inter_list));
  330. core_inter_tail[init_core] = core_inter_head[init_core];
  331. } else {
  332. core_inter_tail[init_core]->next = (inter_list *) malloc(sizeof(inter_list));
  333. core_inter_tail[init_core] = core_inter_tail[init_core]->next;
  334. }
  335. core_inter_tail[init_core]->type = INIT_APP;
  336. fscanf(app_input,"%f",&core_inter_tail[init_core]->data.new_app.workld);
  337. fscanf(app_input,"%f",&core_inter_tail[init_core]->data.new_app.A);
  338. fscanf(app_input,"%f",&core_inter_tail[init_core]->data.new_app.var);
  339. core_inter_tail[init_core]->data.new_app.id = app_cnt++;
  340. core_inter_tail[init_core]->data.new_app.num_of_cores = 0;
  341. core_inter_tail[init_core]->next = NULL;
  342. 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);
  343. cur_time = time(NULL);
  344. cur_t = localtime(&cur_time);
  345. 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);
  346. fflush(log_file);
  347. //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);
  348. if (core_inter_head[init_core]->next == NULL) {
  349. //kill(pid_num[init_core],SIG_INIT_APP);
  350. scc_kill(init_core,SIG_INIT_APP);
  351. my_stats.msg_count++;
  352. my_stats.distance += distance(node_id,init_core);
  353. }
  354. } else {
  355. if (init_pending_head == NULL){
  356. init_pending_head = (inter_list *) malloc(sizeof(inter_list));
  357. init_pending_tail = init_pending_head;
  358. } else {
  359. init_pending_tail->next = (inter_list *) malloc(sizeof(inter_list));
  360. init_pending_tail = init_pending_tail->next;
  361. }
  362. init_pending_tail->type = INIT_APP;
  363. fscanf(app_input,"%f",&init_pending_tail->data.new_app.workld);
  364. fscanf(app_input,"%f",&init_pending_tail->data.new_app.A);
  365. fscanf(app_input,"%f",&init_pending_tail->data.new_app.var);
  366. init_pending_tail->data.new_app.id = app_cnt++;
  367. //apparently i want num_of_cores to be 0. But i will temporarilly use it as an indicator of init_core so as not
  368. //change inter_list type or introduse an a new data union structure
  369. init_pending_tail->data.new_app.num_of_cores = init_core;
  370. init_pending_tail->next = NULL;
  371. }
  372. if (fscanf(app_input,"%d",&time_next) == EOF) {
  373. if (time_for_farman == 0 || time_for_farman == -5) state = FAR_MAN_CHK_OFFERS;
  374. else if (time_for_farman > 0){
  375. its.it_value.tv_nsec = time_for_farman * 10 * MS;
  376. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error10\n");
  377. state = USER_INPUT;
  378. } else state = USER_INPUT;
  379. time_passed = -1;
  380. } else {
  381. if (time_for_farman == 0 || time_for_farman == -5) state = FAR_MAN_CHK_OFFERS;
  382. else {
  383. its.it_value.tv_nsec = 10 * MS;
  384. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error11\n");
  385. state = IDLE_CHK_APP_FILE;
  386. }
  387. }
  388. } else {
  389. state = IDLE_CHK_APP_FILE;
  390. its.it_value.tv_nsec = 10 * MS;
  391. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error92\n");
  392. }
  393. signals_enable();
  394. /*} else if (state == FAR_MAN_CHK_OFFERS){
  395. signals_disable();
  396. printf("far check alarm went off in idag %d far_req_or_sender = %d!\n",node_id,far_req_or_sender);
  397. cur_time = time(NULL);
  398. cur_t = localtime(&cur_time);
  399. 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);
  400. fflush(log_file);
  401. tmp_offer_list = far_man_offers;
  402. while (tmp_offer_list != NULL){
  403. printf("zxcA Offer by %d for %d cores\n",tmp_offer_list->sender,tmp_offer_list->off.num_of_cores);
  404. //tmp_offer_list->answer = &core_inter_head[sender_id]->data.offer_accepted; must be a serious bug
  405. //tmp_offer_list->answer = &core_inter_head[tmp_offer_list->sender]->data.offer_accepted;
  406. tmp_offer_list = tmp_offer_list->next;
  407. }
  408. if (core_inter_head[far_req_or_sender] == NULL){
  409. core_inter_head[far_req_or_sender] = (inter_list *) malloc(sizeof(inter_list));
  410. core_inter_tail[far_req_or_sender] = core_inter_head[far_req_or_sender];
  411. } else {
  412. core_inter_tail[far_req_or_sender]->next = (inter_list *) malloc(sizeof(inter_list));
  413. core_inter_tail[far_req_or_sender] = core_inter_tail[far_req_or_sender]->next;
  414. }
  415. core_inter_tail[far_req_or_sender]->type = FAR_REQ_OFFER;
  416. core_inter_tail[far_req_or_sender]->data.my_offer = far_man_offers->off;
  417. core_inter_tail[far_req_or_sender]->next = NULL;
  418. //kill(pid_num[far_req_or_sender],SIG_FAR_REQ);
  419. if (core_inter_head[far_req_or_sender]->next == NULL) {
  420. kill(pid_num[far_req_or_sender],SIG_FAR_REQ);
  421. my_stats.msg_count++;
  422. my_stats.distance += distance(node_id,far_req_or_sender);
  423. } 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);
  424. //if (selfopt_time_rem != -1) printf("selfopt timer in idag??\n");
  425. time_for_farman = -1;
  426. if (time_passed == -1) state = USER_INPUT;
  427. else {
  428. state = IDLE_CHK_APP_FILE;
  429. its.it_value.tv_nsec = 10 * MS;
  430. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error112\n");
  431. state = IDLE_CHK_APP_FILE;
  432. }
  433. signals_enable();*/
  434. } else if (state == USER_INPUT) {
  435. /*while (ans != 1){
  436. printf("give next command: 1 to term 2 to start new_app 3 to req DDS: \n");
  437. scanf("%d",&ans);
  438. if (ans == 1) {
  439. printf("killing\n");
  440. for (i=1; i<num_idags; i++) {
  441. printf("i am killing %d\n",idag_id_arr[i]);
  442. one_core = idag_id_arr[i];
  443. if (core_inter_head[one_core] == NULL){
  444. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  445. core_inter_tail[one_core] = core_inter_head[one_core];
  446. } else {
  447. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  448. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  449. 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);
  450. }
  451. core_inter_tail[one_core]->type = TERMINATION_STATS;
  452. core_inter_tail[one_core]->next = NULL;
  453. kill(pid_num[idag_id_arr[i]], SIG_TERMINATE);
  454. my_stats.msg_count++;
  455. my_stats.distance += distance(node_id,idag_id_arr[i]);
  456. }
  457. tmp_cores_list = my_cores;
  458. my_cores = my_cores->next;
  459. free(tmp_cores_list);
  460. for (; my_cores != NULL; my_cores = my_cores->next){
  461. tmp_cores_list = my_cores;
  462. one_core = my_cores->core_id;
  463. if (core_inter_head[one_core] == NULL){
  464. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  465. core_inter_tail[one_core] = core_inter_head[one_core];
  466. } else {
  467. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  468. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  469. 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);
  470. }
  471. core_inter_tail[one_core]->type = TERMINATION_STATS;
  472. core_inter_tail[one_core]->next = NULL;
  473. kill(pid_num[one_core], SIG_TERMINATE);
  474. my_stats.msg_count++;
  475. my_stats.distance += distance(node_id,one_core);
  476. free(tmp_cores_list);
  477. }
  478. state = IDAG_ENDING;
  479. //else if (node_id == 0 && stats_replied == my_cores_count-1) state = TERMINATED;
  480. } else if (ans == 2) {
  481. printf("Give me idag to request DDS: ");
  482. scanf("%d",&one_idag);
  483. printf("Req dds from %d\n",one_idag);
  484. if (one_idag != 0){
  485. if (core_inter_head[one_idag] == NULL){
  486. core_inter_head[one_idag] = (inter_list *) malloc(sizeof(inter_list));
  487. core_inter_tail[one_idag] = core_inter_head[one_idag];
  488. } else {
  489. core_inter_tail[one_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  490. core_inter_tail[one_idag] = core_inter_tail[one_idag]->next;
  491. }
  492. core_inter_tail[one_idag]->type = DEBUG_IDAG_REQ_DDS;
  493. core_inter_tail[one_idag]->data.reg.C = -1;
  494. core_inter_tail[one_idag]->data.reg.r = 0;
  495. core_inter_tail[one_idag]->next = NULL;
  496. if (core_inter_head[one_idag]->next == NULL) kill(pid_num[one_idag], SIG_REQ_DDS);
  497. else printf("what the fuck? interaction is %d\n",core_inter_head[one_idag]->type);
  498. } else {
  499. printf("Number of agents in region = %d\n",DDS_count);
  500. tmp_DDS = DDS;
  501. i=0;
  502. while (tmp_DDS != NULL){
  503. printf("Agent no %d is %d with %d cores\n",i,tmp_DDS->agent_id,tmp_DDS->num_of_cores);
  504. tmp_DDS = tmp_DDS->next;
  505. i++;
  506. }
  507. }
  508. } else printf("fucking else %d\n",ans);
  509. } */
  510. while (num_apps_terminated != num_apps) {//pause(); my_cores_count
  511. dummy=0;
  512. for (i=0; i<100; i++)
  513. for(j=0; j<100; j++)
  514. dummy++;
  515. scc_signals_check();
  516. }
  517. for (one_idag=0; one_idag <10; one_idag++) {
  518. dummy=0;
  519. for (i=0; i<1000; i++)
  520. for(j=0; j<1000; j++)
  521. dummy++;
  522. scc_signals_check();
  523. }
  524. for (j=0; j<num_idags; j++) {
  525. one_idag = idag_id_arr[j];
  526. if (one_idag != 0){
  527. if (core_inter_head[one_idag] == NULL){
  528. core_inter_head[one_idag] = (inter_list *) malloc(sizeof(inter_list));
  529. core_inter_tail[one_idag] = core_inter_head[one_idag];
  530. } else {
  531. core_inter_tail[one_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  532. core_inter_tail[one_idag] = core_inter_tail[one_idag]->next;
  533. }
  534. core_inter_tail[one_idag]->type = DEBUG_IDAG_REQ_DDS;
  535. core_inter_tail[one_idag]->data.reg.C = -1;
  536. core_inter_tail[one_idag]->data.reg.r = 0;
  537. core_inter_tail[one_idag]->next = NULL;
  538. if (core_inter_head[one_idag]->next == NULL) scc_kill(one_idag, SIG_REQ_DDS);//kill(pid_num[one_idag], SIG_REQ_DDS);
  539. else printf("what the fuck? interaction is %d\n",core_inter_head[one_idag]->type);
  540. } else {
  541. printf("Number of agents in region = %d\n",DDS_count);
  542. tmp_DDS = DDS;
  543. i=0;
  544. while (tmp_DDS != NULL){
  545. printf("Agent no %d is %d with %d cores\n",i,tmp_DDS->agent_id,tmp_DDS->num_of_cores);
  546. tmp_DDS = tmp_DDS->next;
  547. i++;
  548. }
  549. }
  550. }
  551. while (idags_replied < num_idags - 1) {
  552. dummy=0;
  553. for (i=0; i<1000; i++)
  554. for(j=0; j<1000; j++)
  555. dummy++;
  556. scc_signals_check();
  557. }
  558. fprintf(log_file,"killing\n");
  559. fflush(log_file);
  560. for (i=1; i<num_idags; i++) {
  561. printf("i am killing %d\n",idag_id_arr[i]);
  562. one_core = idag_id_arr[i];
  563. if (core_inter_head[one_core] == NULL){
  564. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  565. core_inter_tail[one_core] = core_inter_head[one_core];
  566. } else {
  567. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  568. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  569. fprintf(log_file,"I am still doing smth with idag %d interaction = %d\n",one_core,core_inter_head[one_core]->type);
  570. fflush(log_file);
  571. }
  572. core_inter_tail[one_core]->type = TERMINATION_STATS;
  573. core_inter_tail[one_core]->next = NULL;
  574. scc_kill(one_core, SIG_TERMINATE);
  575. //kill(pid_num[idag_id_arr[i]], SIG_TERMINATE);
  576. //my_stats.msg_count++;
  577. //my_stats.distance += distance(node_id,idag_id_arr[i]);
  578. }
  579. tmp_cores_list = my_cores;
  580. my_cores = my_cores->next;
  581. free(tmp_cores_list);
  582. for (; my_cores != NULL; my_cores = my_cores->next){
  583. tmp_cores_list = my_cores;
  584. one_core = my_cores->core_id;
  585. if (core_inter_head[one_core] == NULL){
  586. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  587. core_inter_tail[one_core] = core_inter_head[one_core];
  588. } else {
  589. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  590. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  591. fprintf(log_file,"I am still doing smth with my node %d interaction = %d\n",one_core,core_inter_head[one_core]->type);
  592. fflush(log_file);
  593. }
  594. core_inter_tail[one_core]->type = TERMINATION_STATS;
  595. core_inter_tail[one_core]->next = NULL;
  596. //kill(pid_num[one_core], SIG_TERMINATE);
  597. scc_kill(one_core, SIG_TERMINATE);
  598. my_stats.msg_count++;
  599. my_stats.distance += distance(node_id,one_core);
  600. free(tmp_cores_list);
  601. }
  602. state = IDAG_ENDING;
  603. } else {
  604. printf("Uknown state node_id = %d state = %d\n",node_id,state);
  605. state = IDLE_IDAG;
  606. }
  607. while (state == IDAG_ENDING) {
  608. //pause();
  609. dummy=0;
  610. for (i=0; i<1000; i++)
  611. for(j=0; j<1000; j++)
  612. dummy++;
  613. scc_signals_check();
  614. if (stats_replied == my_cores_count+num_idags-2) state = TERMINATED;
  615. }
  616. //for (i=0; i<Cl_x_max*Cl_y_max-1; i++) wait(NULL); //wait for children
  617. //for (i=0; i<num_idags-1; i++) wait(NULL);//wait for the other idags
  618. total_stats.msg_count += my_stats.msg_count;
  619. total_stats.message_size += my_stats.message_size;
  620. total_stats.distance += my_stats.distance;
  621. total_stats.app_turnaround += my_stats.app_turnaround;
  622. total_stats.comp_effort += my_stats.comp_effort;
  623. total_stats.cores_utilized += my_stats.cores_utilized;
  624. total_stats.times_accessed += my_stats.times_accessed;
  625. avg_cluster_util = (float) my_stats.cores_utilized / (my_stats.times_accessed * (my_cores_count-1));
  626. printf("I am %d with cores_utilized = %d times_accessed = %d my_cores_count = %d and avg_cluster_util = %0.2f\n",
  627. node_id,my_stats.cores_utilized,my_stats.times_accessed,my_cores_count,avg_cluster_util);
  628. fprintf(log_file,"cores_utilized = %d times_accessed = %d my_cores_count = %d and avg_cluster_util = %0.2f\n",
  629. my_stats.cores_utilized,my_stats.times_accessed,my_cores_count,avg_cluster_util);
  630. fflush(log_file);
  631. printf("Total stats are:\n");
  632. printf("Total message count = %d\n",total_stats.msg_count);
  633. printf("Total message size = %d\n",total_stats.message_size);
  634. printf("Total distance = %d\n",total_stats.distance);
  635. printf("Total app turnaround time = %d\n",total_stats.app_turnaround);
  636. printf("Total computational effort = %d\n",total_stats.comp_effort);
  637. printf("Total cores_utilized = %d\n",total_stats.cores_utilized);
  638. printf("Total times_accessed = %d\n",total_stats.times_accessed);
  639. free(idag_mask);
  640. free(idag_id_arr);
  641. free(Cl_x_max_arr);
  642. free(Cl_y_max_arr);
  643. for (i=0; i<X_max*Y_max; i++){
  644. free(core_inter_head[i]);
  645. free(core_inter_tail[i]);
  646. }
  647. free(core_inter_head);
  648. free(core_inter_tail);
  649. RCCE_flag_free(&flag_signals_enabled);
  650. RCCE_flag_free(&flag_data_written);
  651. RCCE_free((t_vcharp) sig_array);
  652. RCCE_free((t_vcharp) data_array);
  653. cur_time = time(NULL);
  654. cur_t = localtime(&cur_time);
  655. fprintf(log_file, "[%d:%d:%d]: I ended well\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  656. fclose(log_file);
  657. }
  658. return 0;
  659. }