distrm.c~ 28 KB

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