common_node.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  1. #include "common_node.h"
  2. //extern int *pid_num;
  3. extern int num_idags, node_id ,my_idag;//, fd_log;
  4. extern FILE *log_file;
  5. extern core_states state;
  6. core_states pending_state=NO_PENDING_STATE;
  7. extern inter_list **core_inter_head,**core_inter_tail;
  8. extern app my_app;
  9. extern app init_app;
  10. extern app far_req_app;
  11. extern metrics my_stats;
  12. extern int far_req_or_sender;
  13. extern int *idag_mask, *idag_id_arr;
  14. extern int *Cl_x_max_arr, *Cl_y_max_arr;
  15. extern int DDS_count, my_cores_count;
  16. extern DDS_list *DDS, *DDS_tail;
  17. extern core_list *my_cores, *my_cores_tail;
  18. extern offer_list *init_man_offers, *selfopt_man_offers;
  19. extern offer_list *far_man_offers, *far_man_offers_tail;
  20. extern int far_list_count, far_man_agent_count;
  21. extern int my_agent, time_worked;
  22. extern int debug_global;
  23. extern time_t cur_time;
  24. extern struct tm *cur_t;
  25. extern struct sigevent sev;
  26. extern struct itimerspec its, chk_timer;
  27. extern timer_t timerid;
  28. extern long int selfopt_time_rem;//-1 means it is not set
  29. extern long int upper_work_bound;
  30. extern int time_for_farman;
  31. extern char scen_num[4];
  32. extern int chk_rem_count, chk_rem_num, sum_rem_time;
  33. extern float old_Speedup, my_Speedup;
  34. extern int nodes_ended_cnt;//, app_terminated;
  35. int my_x, my_y;
  36. int init_DDS_replies, selfopt_DDS_replies, init_DDS_idags, selfopt_DDS_idags, selfopt_interval=200, init_idags_areas_replies, init_near_areas_num;
  37. target_list *init_targets_head=NULL, *init_targets_tail;
  38. target_list *selfopt_targets_head=NULL, *selfopt_targets_tail;
  39. FILE *app_log_file;
  40. application_states app_state;
  41. my_time_stamp init_app_times[2], my_app_times[2];
  42. extern int *sig_array, *data_array, NUES;
  43. extern RCCE_FLAG flag_signals_enabled,flag_data_written;
  44. void common_node_actions(char scen_num[4]){
  45. offer_list *tmp_offer_list;
  46. int one_core, i, old_cores_cnt;
  47. core_list *tmp_cores_list;
  48. target_list *tmp_target_list;
  49. inter_list *tmp_inter_list, *tmp_inter_prev;
  50. FILE *init_areas_file, *selfopt_areas_file;
  51. char init_areas_file_name[64], selfopt_areas_file_name[64], app_log_file_name[64];
  52. int area_cnt,C,r,selfopt_areas=0, init_areas_cnt, init_areas_sent;
  53. int time_per_node, time_left, time_to_work, init_core_found=0;
  54. int new_agent, min_dist=-1, tmp_int, j;
  55. int new_x, new_y, selfopt_areas_sent;
  56. DDS_count=0;
  57. my_cores_count=0;
  58. my_stats.msg_count=0;
  59. my_stats.message_size=0;
  60. my_stats.distance=0;
  61. my_stats.app_turnaround=0;
  62. my_stats.comp_effort=0;
  63. my_stats.cores_utilized=0;
  64. my_stats.times_accessed=0;
  65. init_app_times[0].tm_sec = 0;
  66. init_app_times[0].tm_min = 0;
  67. init_app_times[0].tm_hour = 0;
  68. init_app_times[1].tm_sec = 0;
  69. init_app_times[1].tm_min = 0;
  70. init_app_times[1].tm_hour = 0;
  71. my_app_times[0].tm_sec = 0;
  72. my_app_times[0].tm_min = 0;
  73. my_app_times[0].tm_hour = 0;
  74. my_app_times[1].tm_sec = 0;
  75. my_app_times[1].tm_min = 0;
  76. my_app_times[1].tm_hour = 0;
  77. DDS=NULL;
  78. my_cores=NULL;
  79. install_signal_handlers();
  80. //sig_SEGV_enable();
  81. sev.sigev_notify = SIGEV_SIGNAL;
  82. sev.sigev_signo = SIG_TIMER;
  83. sev.sigev_value.sival_ptr = &timerid;
  84. if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) printf("timer_create error\n");
  85. its.it_value.tv_sec = 0;
  86. its.it_interval.tv_sec = 0;//its.it_value.tv_sec;
  87. its.it_interval.tv_nsec = 0;
  88. log_file = create_log_file(node_id,scen_num);
  89. cur_time = time(NULL);
  90. cur_t = localtime(&cur_time);
  91. fprintf(log_file, "[%d:%d:%d]: Initialisation complete\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  92. fflush(log_file);
  93. RCCE_barrier(&RCCE_COMM_WORLD);
  94. my_x = node_id % X_max;
  95. my_y = node_id / X_max;
  96. state = IDLE_CORE;
  97. RCCE_flag_write(&flag_signals_enabled, RCCE_FLAG_SET, node_id);
  98. my_idag = -1;
  99. int dummy=0;
  100. while (my_idag==-1) {//pause();
  101. for (i=0; i<1000; i++)
  102. for(j=0; j<1000; j++)
  103. dummy++;
  104. scc_signals_check();
  105. }
  106. while (state != TERMINATED)
  107. if (state == IDLE_CORE) {
  108. pending_state = NO_PENDING_STATE;
  109. dummy=0;
  110. for (i=0; i<100; i++)
  111. for(j=0; j<100; j++)
  112. dummy++;
  113. scc_signals_check();
  114. } else if (state == IDLE_INIT_MAN) {
  115. dummy=0;
  116. for (i=0; i<100; i++)
  117. for(j=0; j<100; j++)
  118. dummy++;
  119. scc_signals_check();
  120. } else if (state == IDLE_AGENT) {
  121. if (app_state != APP_TERMINATED) {
  122. dummy=0;
  123. for (i=0; i<100; i++)
  124. for(j=0; j<100; j++)
  125. dummy++;
  126. scc_signals_check();
  127. } else {
  128. if (timer_gettime(timerid, &chk_timer) == -1) printf("timer_gettime error 2\n");
  129. else selfopt_time_rem = chk_timer.it_value.tv_nsec;
  130. if (selfopt_time_rem > 0) {
  131. its.it_value.tv_nsec = 0;
  132. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error42\n");
  133. }
  134. state = AGENT_ENDING;
  135. }
  136. } else if (state == AGENT_REWIND_FILE) {
  137. //printf("I am %d and i have to rewind my selfopt_areas_file for app with id %d\n",node_id,init_app.id);
  138. fprintf(log_file,"I have to rewind my selfopt_areas_file for app with id %d\n",init_app.id);
  139. rewind(selfopt_areas_file);
  140. fscanf(selfopt_areas_file,"%d",&area_cnt);
  141. for (i=0; i<area_cnt; i++){
  142. fscanf(selfopt_areas_file,"%d",&C);
  143. fscanf(selfopt_areas_file,"%d",&r);
  144. }
  145. fscanf(selfopt_areas_file,"%d",&area_cnt);
  146. selfopt_interval = 200;
  147. its.it_value.tv_nsec = selfopt_interval * MS;
  148. selfopt_time_rem = selfopt_interval;
  149. if (timer_settime(timerid, 0, &its, NULL) == -1) perror("timer_settime error823\n");
  150. state = IDLE_AGENT;
  151. } else if (state == IDLE_AGENT_WAITING_OFF) {
  152. if (app_state != APP_TERMINATED) {
  153. dummy=0;
  154. for (i=0; i<100; i++)
  155. for(j=0; j<100; j++)
  156. dummy++;
  157. scc_signals_check();
  158. } else {
  159. signals_disable();
  160. if (timer_gettime(timerid, &chk_timer) == -1) printf("timer_gettime error 2\n");
  161. else selfopt_time_rem = chk_timer.it_value.tv_nsec;
  162. //den stamataw edw thn diadikasia tou selfopt gia na mhn meinoun oi mexri twra prosfores kai oxi mono anapanthtes
  163. if (selfopt_time_rem == 0) state = AGENT_SELF_CHK_OFFERS;
  164. signals_enable();
  165. }
  166. } else if (state == INIT_MANAGER) {
  167. signals_disable();
  168. //printf("Initialising app node_id = %d\n",node_id);
  169. cur_time = time(NULL);
  170. cur_t = localtime(&cur_time);
  171. fprintf(log_file, "[%d:%d:%d]: Initialising app\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  172. fflush(log_file);
  173. strcpy(init_areas_file_name,"/shared/herc/distrm/scenaria/");
  174. strcat(init_areas_file_name,scen_num);
  175. strcat(init_areas_file_name,"/");
  176. strcat(init_areas_file_name, itoa(init_app.id));
  177. strcat(init_areas_file_name, ".txt");
  178. //printf("area file path = %s\n",areas_file_name);
  179. if ((init_areas_file = fopen(init_areas_file_name, "r")) == NULL){
  180. printf("Cannot open input file with file path = %s ",init_areas_file_name);
  181. perror("open init_areas_file_name");
  182. }
  183. init_areas_sent = 0;
  184. fscanf(init_areas_file,"%d",&init_areas_cnt);
  185. state = INIT_MANAGER_SEND_OFFERS;
  186. signals_enable();
  187. } else if (state == INIT_MANAGER_SEND_OFFERS) {// || state == INIT_MANAGER_SEND_OFFERS_SELFOPT_PENDING || state == INIT_MANAGER_SEND_OFFERS_WORK_PENDING) {
  188. signals_disable();
  189. init_idags_areas_replies=0;
  190. init_DDS_idags = 0;
  191. init_DDS_replies = 0;
  192. init_near_areas_num = 0;
  193. if (init_targets_head != NULL)
  194. while (init_targets_head != NULL){
  195. tmp_target_list = init_targets_head;
  196. init_targets_head = init_targets_head->next;
  197. free(tmp_target_list);
  198. }
  199. if (init_man_offers != NULL)
  200. while (init_man_offers != NULL){
  201. tmp_offer_list = init_man_offers;
  202. init_man_offers = init_man_offers->next;
  203. free(tmp_offer_list);
  204. }
  205. for (i=0; i<INIT_AREAS_NUM; i++) {
  206. fscanf(init_areas_file,"%d",&C);
  207. fscanf(init_areas_file,"%d",&r);
  208. fprintf(log_file, "init area (%d,%d)\n",C,r);
  209. fflush(log_file);
  210. if (distance(node_id,C) < LOCALITY_THRESH){
  211. if (core_inter_head[my_idag] == NULL){
  212. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  213. core_inter_tail[my_idag] = core_inter_head[my_idag];
  214. } else {
  215. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  216. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  217. }
  218. core_inter_tail[my_idag]->type = IDAG_FIND_IDAGS_PENDING;
  219. core_inter_tail[my_idag]->data.reg.C = C;
  220. core_inter_tail[my_idag]->data.reg.r = r;
  221. core_inter_tail[my_idag]->next = NULL;
  222. if (core_inter_head[my_idag]->next == NULL) {
  223. //scc_kill(my_idag,SIG_IDAG_FIND_IDAGS);
  224. sig_ACK_sender(my_idag,SIG_IDAG_FIND_IDAGS);
  225. my_stats.msg_count++;
  226. my_stats.distance += distance(node_id,my_idag);
  227. }
  228. init_near_areas_num++;
  229. //else printf("kariolares1 interaction=%d\n",core_inter_head[my_idag]->type);
  230. } else {
  231. fprintf(log_file,"I am sending a far offer in region (%d,%d)\n",C,r);
  232. fflush(log_file);
  233. /*one_core = C;
  234. if (core_inter_head[one_core] == NULL){
  235. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  236. core_inter_tail[one_core] = core_inter_head[one_core];
  237. } else {
  238. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  239. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  240. }
  241. core_inter_tail[one_core]->type = FAR_INIT_REQ;
  242. core_inter_tail[one_core]->data.reg.C = C;
  243. core_inter_tail[one_core]->data.reg.r = r;
  244. core_inter_tail[one_core]->next = NULL;
  245. if (core_inter_head[one_core]->next == NULL) {
  246. kill(pid_num[one_core],SIG_INIT_FAR_REQ);
  247. my_stats.msg_count++;
  248. my_stats.distance += distance(node_id,one_core);
  249. }*/
  250. }
  251. }
  252. fprintf(log_file, "init_near_areas_num = %d\n",init_near_areas_num);
  253. fflush(log_file);
  254. init_areas_sent += INIT_AREAS_NUM;
  255. //fclose(areas);
  256. if (selfopt_time_rem != -1){
  257. if (timer_gettime(timerid, &chk_timer) == -1) printf("timer_gettime error\n");
  258. else selfopt_time_rem = chk_timer.it_value.tv_nsec;
  259. if (selfopt_time_rem > 0) {
  260. its.it_value.tv_nsec = 0;//750000000;// * MS;
  261. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error4123\n");
  262. }
  263. }
  264. state = IDLE_INIT_MAN;
  265. signals_enable();
  266. scc_signals_check();
  267. } else if (state == INIT_MAN_CHK_OFFERS) {// || state == INIT_MAN_CHK_OFFERS_SELFOPT_PENDING || state == INIT_MAN_CHK_OFFERS_WORK_PENDING){
  268. signals_disable();
  269. //printf("init check alarm went off node_id=%d init_DDS_idags = %d init_DDS_replies = %d state=%d!\n",node_id,init_DDS_idags,init_DDS_replies,state);
  270. cur_time = time(NULL);
  271. cur_t = localtime(&cur_time);
  272. fprintf(log_file, "[%d:%d:%d]: init check alarm went off init_DDS_idags = %d init_DDS_replies = %d state=%d\n",
  273. cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,init_DDS_idags,init_DDS_replies,state);
  274. fflush(log_file);
  275. init_core_found = 0;
  276. if (init_man_offers == NULL) {
  277. //printf("I don't have offers\n");
  278. fprintf(log_file, "I don't have init man offers\n");
  279. fflush(log_file);
  280. if (init_areas_sent == init_areas_cnt){
  281. fprintf(log_file,"I have to rewind my init_areas_file for app with id %d\n",init_app.id);
  282. fflush(log_file);
  283. rewind(init_areas_file);
  284. init_areas_sent = 0;
  285. fscanf(init_areas_file,"%d",&init_areas_cnt);
  286. }
  287. state = INIT_MANAGER_SEND_OFFERS;
  288. } else {
  289. tmp_offer_list = init_man_offers;
  290. while (tmp_offer_list != NULL){
  291. fprintf(log_file,"Offer by %d for %d cores:",tmp_offer_list->sender,tmp_offer_list->off.num_of_cores);
  292. for (i=0; i<tmp_offer_list->off.num_of_cores; i++)
  293. fprintf(log_file," %d,",tmp_offer_list->off.offered_cores[i]);
  294. fprintf(log_file,"\n");
  295. //printf("Offer by %d for %d cores\n",tmp_offer_list->sender,tmp_offer_list->off.num_of_cores);
  296. tmp_offer_list = tmp_offer_list->next;
  297. }
  298. fflush(log_file);
  299. *init_man_offers->answer = 1;
  300. //decide which will be the agent. Will be the one tha minimizes distance between cores
  301. min_dist = -1;
  302. for (i=0; i<init_man_offers->off.num_of_cores; i++){
  303. tmp_int = 0;
  304. for (j=0; j<init_man_offers->off.num_of_cores; j++){
  305. if (i == j) continue;
  306. tmp_int += distance(init_man_offers->off.offered_cores[i],init_man_offers->off.offered_cores[j]);
  307. }
  308. if (min_dist == -1 || tmp_int < min_dist) {
  309. min_dist = tmp_int;
  310. new_agent = init_man_offers->off.offered_cores[i];
  311. }
  312. }
  313. //printf("New agent is %d\n",new_agent);
  314. cur_time = time(NULL);
  315. cur_t = localtime(&cur_time);
  316. init_app_times[1].tm_sec = cur_t->tm_sec;
  317. init_app_times[1].tm_min = cur_t->tm_min;
  318. init_app_times[1].tm_hour = cur_t->tm_hour;
  319. fprintf(log_file, "[%d:%d:%d]: New agent is %d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,new_agent);
  320. fflush(log_file);
  321. if (new_agent != node_id) {
  322. if (core_inter_head[new_agent] == NULL){
  323. core_inter_head[new_agent] = (inter_list *) malloc(sizeof(inter_list));
  324. core_inter_tail[new_agent] = core_inter_head[new_agent];
  325. } else {
  326. printf("Starting this app is not high priority. Should we change that?\n");
  327. core_inter_tail[new_agent]->next = (inter_list *) malloc(sizeof(inter_list));
  328. core_inter_tail[new_agent] = core_inter_tail[new_agent]->next;
  329. }
  330. core_inter_tail[new_agent]->type = INIT_AGENT;
  331. core_inter_tail[new_agent]->data.app_cores = (int *)malloc((init_man_offers->off.num_of_cores+1)*sizeof(int));
  332. core_inter_tail[new_agent]->data.app_cores[0] = init_man_offers->off.num_of_cores;
  333. for(i=1; i<=init_man_offers->off.num_of_cores; i++)
  334. core_inter_tail[new_agent]->data.app_cores[i] = init_man_offers->off.offered_cores[i-1];
  335. core_inter_tail[new_agent]->next = NULL;
  336. init_app.num_of_cores = init_man_offers->off.num_of_cores;
  337. if (core_inter_head[new_agent]->next == NULL) {
  338. //scc_kill(new_agent, SIG_INIT_AGENT);
  339. sig_ACK_sender(new_agent, SIG_INIT_AGENT);
  340. my_stats.msg_count++;
  341. my_stats.distance += distance(node_id,new_agent);
  342. } else {
  343. fprintf(log_file,"I am init manager and i am doing smth else with init agent interaction=%d\n",core_inter_head[new_agent]->type);
  344. fflush(log_file);
  345. }
  346. } else {
  347. my_app = init_app;
  348. my_cores_count = init_man_offers->off.num_of_cores;
  349. my_app.num_of_cores = my_cores_count;//+1;
  350. if (my_cores == NULL) {
  351. my_cores = (core_list *) malloc(sizeof(core_list));
  352. my_cores_tail = my_cores;
  353. } else {
  354. printf("My cores still not fucking null!!\n");
  355. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  356. my_cores_tail = my_cores_tail->next;
  357. }
  358. my_cores_tail->core_id = node_id;
  359. my_cores_tail->offered_to = -1;
  360. my_cores_tail->next = NULL;
  361. if (core_inter_head[my_idag] == NULL){
  362. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  363. core_inter_tail[my_idag] = core_inter_head[my_idag];
  364. } else {
  365. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  366. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  367. }
  368. core_inter_tail[my_idag]->type = IDAG_ADD_CORES_DDS;
  369. core_inter_tail[my_idag]->data.app_cores = (int *)malloc((my_cores_count+1)*sizeof(int));
  370. core_inter_tail[my_idag]->data.app_cores[0] = my_cores_count;
  371. core_inter_tail[my_idag]->next = NULL;
  372. //I want myself to be first in my_cores list
  373. for(i=0; i<init_man_offers->off.num_of_cores; i++) {
  374. one_core = init_man_offers->off.offered_cores[i];
  375. core_inter_tail[my_idag]->data.app_cores[i+1] = one_core;
  376. if (one_core != node_id){
  377. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  378. my_cores_tail = my_cores_tail->next;
  379. my_cores_tail->core_id = one_core;
  380. my_cores_tail->offered_to = -1;
  381. my_cores_tail->next = NULL;
  382. }
  383. }
  384. my_app_times[0].tm_sec = init_app_times[0].tm_sec;
  385. my_app_times[0].tm_min = init_app_times[0].tm_min;
  386. my_app_times[0].tm_hour = init_app_times[0].tm_hour;
  387. my_app_times[1].tm_sec = init_app_times[1].tm_sec;
  388. my_app_times[1].tm_min = init_app_times[1].tm_min;
  389. my_app_times[1].tm_hour = init_app_times[1].tm_hour;
  390. init_app_times[0].tm_sec = 0;
  391. init_app_times[0].tm_min = 0;
  392. init_app_times[0].tm_hour = 0;
  393. init_app_times[1].tm_sec = 0;
  394. init_app_times[1].tm_min = 0;
  395. init_app_times[1].tm_hour = 0;
  396. //kill(pid_num[0], SIG_INIT_APP);
  397. scc_kill(0, SIG_INIT_APP);
  398. my_stats.msg_count++;
  399. my_stats.distance += distance(node_id,0);
  400. //printf("I am new agent with id %d and app with A=%.2f, var=%.2f and %d my_cores_count = %d cores: \n"
  401. // ,node_id,my_app.A,my_app.var,my_app.num_of_cores,my_cores_count);
  402. my_Speedup = Speedup(my_app, my_app.num_of_cores);
  403. if (core_inter_head[my_idag]->next == NULL) {
  404. //scc_kill(my_idag, SIG_ADD_CORES_DDS);
  405. sig_ACK_sender(my_idag, SIG_ADD_CORES_DDS);
  406. my_stats.msg_count++;
  407. my_stats.distance += distance(node_id,my_idag);
  408. } else {
  409. fprintf(log_file,"I didn't call add!! with interaction %d\n",core_inter_head[my_idag]->type);
  410. fflush(log_file);
  411. }
  412. if (my_agent != -1) {
  413. //printf("I am %d and i do this agent switch in common node with pending_state=%d\n",node_id,pending_state);
  414. fprintf(log_file,"I do this agent switch in common node with pending_state=%d\n",pending_state);
  415. if (pending_state == WORKING_NODE) {
  416. if (core_inter_head[my_agent] == NULL){
  417. core_inter_head[my_agent] = (inter_list *) malloc(sizeof(inter_list));
  418. core_inter_tail[my_agent] = core_inter_head[my_agent];
  419. } else {
  420. core_inter_tail[my_agent]->next = (inter_list *) malloc(sizeof(inter_list));
  421. core_inter_tail[my_agent] = core_inter_tail[my_agent]->next;
  422. }
  423. core_inter_tail[my_agent]->type = REP_CHK_REM_TIME;
  424. core_inter_tail[my_agent]->data.work_time = upper_work_bound - time_worked;
  425. core_inter_tail[my_agent]->next = NULL;
  426. }
  427. upper_work_bound = 0;
  428. my_agent = -1;
  429. }
  430. state = AGENT_INIT_STATE;
  431. cur_time = time(NULL);
  432. cur_t = localtime(&cur_time);
  433. fprintf(log_file, "[%d:%d:%d]: Init ok!! my_cores_count = %d app_id=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,my_cores_count,my_app.id);
  434. fprintf(log_file, "my cores are:");
  435. for (tmp_cores_list=my_cores; tmp_cores_list!=NULL; tmp_cores_list=tmp_cores_list->next) fprintf(log_file, " %d", tmp_cores_list->core_id);
  436. fprintf(log_file, "\n");
  437. fflush(log_file);
  438. }
  439. tmp_offer_list = init_man_offers->next;
  440. while (tmp_offer_list != NULL){
  441. *tmp_offer_list->answer = 0;
  442. tmp_offer_list = tmp_offer_list->next;
  443. }
  444. while (init_man_offers != NULL){
  445. if (core_inter_head[init_man_offers->sender] != NULL) {
  446. if (core_inter_head[init_man_offers->sender]->type == REP_AGENT_OFFER_PENDING){
  447. core_inter_head[init_man_offers->sender]->type = REP_AGENT_OFFER_SENT;
  448. //kill(pid_num[init_man_offers->sender], SIG_REP_OFFERS);
  449. //scc_kill(init_man_offers->sender, SIG_REP_OFFERS);
  450. sig_ACK_sender(init_man_offers->sender, SIG_REP_OFFERS);
  451. my_stats.msg_count++;
  452. my_stats.distance += distance(node_id,init_man_offers->sender);
  453. } else if (core_inter_head[init_man_offers->sender]->type == REP_FAR_REQ_OFFER_PENDING){
  454. core_inter_head[init_man_offers->sender]->type = REP_FAR_REQ_OFFER_SENT;
  455. //kill(pid_num[init_man_offers->sender], SIG_REP_OFFERS);
  456. //scc_kill(init_man_offers->sender, SIG_REP_OFFERS);
  457. sig_ACK_sender(init_man_offers->sender, SIG_REP_OFFERS);
  458. my_stats.msg_count++;
  459. my_stats.distance += distance(node_id,init_man_offers->sender);
  460. } else if (core_inter_head[init_man_offers->sender]->type != REP_FAR_REQ_OFFER_SENT && core_inter_head[init_man_offers->sender]->type != REP_AGENT_OFFER_SENT)
  461. printf("We have aasdfa problem!\n");
  462. } else {
  463. printf("gamietai b = %d",init_man_offers->sender);
  464. fprintf(log_file,"gamietai b = %d",init_man_offers->sender);
  465. fflush(log_file);
  466. }
  467. tmp_offer_list = init_man_offers;
  468. init_man_offers = init_man_offers->next;
  469. free(tmp_offer_list);
  470. }
  471. fclose(init_areas_file);
  472. fprintf(log_file,"selfopt_time_rem = %ld app_state = %d state = %d pending_state = %d\n",selfopt_time_rem,app_state,state,pending_state);
  473. fflush(log_file);
  474. if (selfopt_time_rem != -1 && app_state != APP_TERMINATED){ //a selfopt is pending !app_terminated
  475. selfopt_time_rem -= 500000000;
  476. if (selfopt_time_rem > 0){
  477. its.it_value.tv_nsec = selfopt_time_rem;
  478. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error5\n");
  479. state = IDLE_AGENT;
  480. } else {
  481. selfopt_time_rem = -1;
  482. state = AGENT_SELF_OPT;
  483. }
  484. } else if (state != AGENT_INIT_STATE) {
  485. if (pending_state == IDLE_AGENT) {
  486. printf("I am %d and i entered this realm of whores\n",node_id);
  487. fprintf(log_file,"I finished my init selfopt_interval=%d, selfopt_time_rem=%ld my_cores_count=%d, old_cores_cnt=%d\n"
  488. ,selfopt_interval,selfopt_time_rem,my_cores_count,old_cores_cnt);
  489. fflush(log_file);
  490. fprintf(app_log_file,"I finished my init selfopt_interval=%d, selfopt_time_rem=%ld my_cores_count=%d, old_cores_cnt=%d\n"
  491. ,selfopt_interval,selfopt_time_rem,my_cores_count,old_cores_cnt);
  492. fflush(app_log_file);
  493. if (selfopt_interval > 0) { // && !app_terminated
  494. its.it_value.tv_nsec = selfopt_interval * MS;
  495. selfopt_time_rem = selfopt_interval;
  496. if (old_cores_cnt == 1 || old_cores_cnt == my_cores_count)
  497. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error8\n");
  498. } else if (my_cores_count == 1) {
  499. //printf("I am %d and i have to rewind my selfopt_areas_file after init for app with id %d\n",node_id,init_app.id);
  500. fprintf(log_file,"I have to rewind my selfopt_areas_file after init for app with id %d\n",init_app.id);
  501. rewind(selfopt_areas_file);
  502. fscanf(selfopt_areas_file,"%d",&area_cnt);
  503. for (i=0; i<area_cnt; i++){
  504. fscanf(selfopt_areas_file,"%d",&C);
  505. fscanf(selfopt_areas_file,"%d",&r);
  506. }
  507. fscanf(selfopt_areas_file,"%d",&area_cnt);
  508. selfopt_interval = 200;
  509. its.it_value.tv_nsec = selfopt_interval * MS;
  510. selfopt_time_rem = selfopt_interval;
  511. if (timer_settime(timerid, 0, &its, NULL) == -1) perror("timer_settime error823\n");
  512. } else selfopt_time_rem = -1;
  513. state = IDLE_AGENT;
  514. pending_state = NO_PENDING_STATE;
  515. } else if (pending_state == AGENT_SELF_OPT || pending_state == WORKING_NODE || pending_state == AGENT_REWIND_FILE) {
  516. state = pending_state;//IDLE_AGENT;
  517. pending_state = NO_PENDING_STATE;
  518. } else state = IDLE_CORE;
  519. } else if (app_state != APP_TERMINATED && new_agent != node_id) state = IDLE_AGENT;//!app_terminated
  520. signals_enable();
  521. }
  522. scc_signals_check();
  523. } else if (state == AGENT_INIT_STATE) {//|| state == AGENT_INIT_STATE_INIT_INTERRUPTED
  524. signals_disable();
  525. selfopt_interval=200;
  526. nodes_ended_cnt = 0;
  527. app_state = RUNNING;
  528. if (my_cores_count > 1){
  529. time_to_work = (int) roundf(my_app.workld / my_Speedup);
  530. time_per_node = time_to_work / (my_cores_count-1);
  531. time_left = time_to_work % (my_cores_count-1);
  532. if (time_to_work == 0) time_left++;
  533. }
  534. //printf("i am init agent %d. speedup = %0.2f time_to_work = %d time_per_node = %d, workld=%0.2f\n",node_id,my_Speedup,time_to_work,time_per_node,my_app.workld);
  535. strcpy(app_log_file_name,"/shared/herc/distrm/scenaria/");
  536. strcat(app_log_file_name,scen_num);
  537. strcat(app_log_file_name,"/app_logs/");
  538. strcat(app_log_file_name, itoa(my_app.id));
  539. strcat(app_log_file_name, ".txt");
  540. //printf("area file path self opt = %s\n",areas_file_name);
  541. if ((app_log_file = fopen(app_log_file_name, "w")) == NULL){
  542. printf("Cannot open input file with file path = %s ",app_log_file_name);
  543. perror("open selfopt_areas_file_name");
  544. }
  545. fprintf(app_log_file, "Came into init_agent at [%d:%d:%d]\n",my_app_times[0].tm_hour,my_app_times[0].tm_min,my_app_times[0].tm_sec);
  546. fprintf(app_log_file, "New agent found at [%d:%d:%d]\n",my_app_times[1].tm_hour,my_app_times[1].tm_min,my_app_times[1].tm_sec);
  547. cur_time = time(NULL);
  548. cur_t = localtime(&cur_time);
  549. fprintf(app_log_file, "[%d:%d:%d]: I am agent %d . Init ok!! my_cores_count = %d app_id=%d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,node_id,my_cores_count,my_app.id);
  550. fprintf(app_log_file, "my cores are:");
  551. for (tmp_cores_list=my_cores; tmp_cores_list!=NULL; tmp_cores_list=tmp_cores_list->next) fprintf(app_log_file, " %d", tmp_cores_list->core_id);
  552. fprintf(app_log_file, "\n");
  553. fprintf(app_log_file,"speedup = %0.2f time_to_work = %d time_per_node = %d, time_left = %d, workld=%0.2f\n",my_Speedup,time_to_work,time_per_node,time_left,my_app.workld);
  554. fflush(app_log_file);
  555. tmp_cores_list = my_cores->next;
  556. while (tmp_cores_list != NULL) {
  557. one_core = tmp_cores_list->core_id;
  558. if (core_inter_head[one_core] == NULL){
  559. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  560. core_inter_tail[one_core] = core_inter_head[one_core];
  561. } else {
  562. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  563. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  564. }
  565. core_inter_tail[one_core]->type = INIT_WORK_NODE;
  566. core_inter_tail[one_core]->data.work_time = time_per_node;
  567. if (time_left > 0) {
  568. core_inter_tail[one_core]->data.work_time++;
  569. time_left--;
  570. }
  571. core_inter_tail[one_core]->next = NULL;
  572. my_stats.app_turnaround += core_inter_tail[one_core]->data.work_time;
  573. if (core_inter_head[one_core]->next == NULL) {
  574. //scc_kill(one_core, SIG_APPOINT_WORK);
  575. sig_ACK_sender(one_core, SIG_APPOINT_WORK);
  576. my_stats.msg_count++;
  577. my_stats.distance += distance(node_id,one_core);
  578. } else {
  579. fprintf(log_file,"I am doing smth else with my working node %d in init inter1 = %d inter2 = %d\n",
  580. one_core,core_inter_head[one_core]->type,core_inter_head[one_core]->next->type);
  581. fflush(log_file);
  582. }
  583. tmp_cores_list = tmp_cores_list->next;
  584. } //else printf("i have only one fucking core\n");
  585. strcpy(selfopt_areas_file_name,"/shared/herc/distrm/scenaria/");
  586. strcat(selfopt_areas_file_name,scen_num);
  587. strcat(selfopt_areas_file_name,"/");
  588. strcat(selfopt_areas_file_name, itoa(my_app.id));
  589. strcat(selfopt_areas_file_name, ".txt");
  590. //printf("area file path self opt = %s\n",areas_file_name);
  591. if ((selfopt_areas_file = fopen(selfopt_areas_file_name, "r")) == NULL){
  592. printf("Cannot open input file with file path = %s ",selfopt_areas_file_name);
  593. perror("open selfopt_areas_file_name");
  594. }
  595. /*get rid of init areas*/
  596. fscanf(selfopt_areas_file,"%d",&area_cnt);
  597. for (i=0; i<area_cnt; i++){
  598. fscanf(selfopt_areas_file,"%d",&C);
  599. fscanf(selfopt_areas_file,"%d",&r);
  600. }
  601. if (fscanf(selfopt_areas_file,"%d",&selfopt_areas) != EOF) {
  602. //if (pending_state == IDLE_INIT_MAN) {//state == AGENT_INIT_STATE_INIT_INTERRUPTED
  603. if (pending_state == IDLE_INIT_MAN || pending_state == INIT_MANAGER || pending_state == INIT_MANAGER_SEND_OFFERS
  604. || pending_state == INIT_MAN_CHK_OFFERS || pending_state == WORKING_NODE_IDLE_INIT) {
  605. state = pending_state;//IDLE_INIT_MAN;//IDLE_INIT_MAN_SELFOPT_PENDING;
  606. pending_state = AGENT_SELF_OPT;
  607. } else state = AGENT_SELF_OPT;
  608. /*for (i=0; i<area_cnt; i++){
  609. fscanf(areas,"%d",&C);
  610. fscanf(areas,"%d",&r);
  611. }*/
  612. } else state = IDLE_AGENT;
  613. signals_enable();
  614. scc_signals_check();
  615. } else if (state == AGENT_SELF_OPT){
  616. signals_disable();
  617. //printf("Initialising self opt node_id = %d\n",node_id);
  618. cur_time = time(NULL);
  619. cur_t = localtime(&cur_time);
  620. fprintf(log_file, "[%d:%d:%d]: Initialising self opt!\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  621. fflush(log_file);
  622. pending_state = NO_PENDING_STATE;
  623. if (app_state != APP_TERMINATED) {//!app_terminated
  624. selfopt_DDS_idags = 0;
  625. selfopt_DDS_replies = 0;
  626. selfopt_areas_sent = 0;
  627. if (selfopt_man_offers != NULL) printf("selfopt offers list is not NULL\n");
  628. if (selfopt_targets_head != NULL) {
  629. //printf("selfopt targets list is not NULL\n");
  630. while (selfopt_targets_head != NULL){
  631. tmp_target_list = selfopt_targets_head;
  632. selfopt_targets_head = selfopt_targets_head->next;
  633. free(tmp_target_list);
  634. }
  635. }
  636. for (i=0; i<selfopt_areas; i++){
  637. //fscanf(areas,"%d",&C);
  638. fscanf(selfopt_areas_file,"%d",&new_x);
  639. fscanf(selfopt_areas_file,"%d",&new_y);
  640. fscanf(selfopt_areas_file,"%d",&r);
  641. new_x += my_x;
  642. if (new_x < 0 || new_x >= X_max) continue;
  643. new_y += my_y;
  644. if (new_y < 0 || new_y >= Y_max) continue;
  645. C = new_y * Y_max + new_x;
  646. //if (C < 0) printf("piaston new_x = %d new_y = %d my_x = %d my_y = %d\n",new_x,new_y,my_x,my_y);
  647. fprintf(log_file,"Selfopt in region (%d,%d)\n",C,r);
  648. fflush(log_file);
  649. if (core_inter_head[my_idag] == NULL){
  650. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  651. core_inter_tail[my_idag] = core_inter_head[my_idag];
  652. } else {
  653. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  654. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  655. }
  656. core_inter_tail[my_idag]->type = SELFOPT_IDAG_FIND_IDAGS_PENDING;
  657. core_inter_tail[my_idag]->data.reg.C = C;
  658. core_inter_tail[my_idag]->data.reg.r = r;
  659. core_inter_tail[my_idag]->next = NULL;
  660. if (core_inter_head[my_idag]->next == NULL) {
  661. //scc_kill(my_idag,SIG_IDAG_FIND_IDAGS);
  662. sig_ACK_sender(my_idag,SIG_IDAG_FIND_IDAGS);
  663. my_stats.msg_count++;
  664. my_stats.distance += distance(node_id,my_idag);
  665. }
  666. //else printf("SIG_IDAG_FIND_IDAGS not sent yet interact=%d interaction2 = %d\n",core_inter_head[my_idag]->type,core_inter_head[my_idag]->next->type);
  667. if (++selfopt_areas_sent == SELFOPT_AREAS_NUM) break;
  668. }
  669. for (; i<selfopt_areas; i++) {
  670. fscanf(selfopt_areas_file,"%d",&new_x);
  671. fscanf(selfopt_areas_file,"%d",&new_y);
  672. fscanf(selfopt_areas_file,"%d",&r);
  673. }
  674. if (fscanf(selfopt_areas_file,"%d",&area_cnt) != EOF) selfopt_interval = 2 * selfopt_interval;
  675. else selfopt_interval = -1;
  676. /*its.it_value.tv_nsec = 500 * MS;// 750000000;
  677. selfopt_time_rem = its.it_value.tv_nsec;
  678. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error7\n");
  679. */
  680. state = IDLE_AGENT_WAITING_OFF;
  681. } else state = AGENT_ENDING;
  682. signals_enable();
  683. scc_signals_check();
  684. } else if (state == AGENT_SELF_CHK_OFFERS){
  685. signals_disable();
  686. //printf("alarm went off for self opt node_id=%d selfopt_DDS_idags = %d selfopt_DDS_replies = %d! app_state=%d\n",node_id,selfopt_DDS_idags,selfopt_DDS_replies,app_state);
  687. cur_time = time(NULL);
  688. cur_t = localtime(&cur_time);
  689. fprintf(log_file, "[%d:%d:%d]: alarm went off for self opt selfopt_DDS_idags = %d selfopt_DDS_replies = %d app_state=%d pending_state = %d\n",
  690. cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,selfopt_DDS_idags,selfopt_DDS_replies,app_state,pending_state);
  691. fflush(log_file);
  692. fprintf(app_log_file, "[%d:%d:%d]: alarm went off for self opt selfopt_DDS_idags = %d selfopt_DDS_replies = %d app_state=%d pending_state = %d\n",
  693. cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,selfopt_DDS_idags,selfopt_DDS_replies,app_state,pending_state);
  694. fflush(app_log_file);
  695. old_cores_cnt = my_cores_count;
  696. if (selfopt_man_offers == NULL) {
  697. //printf("I don't have self opt offers app_state=%d\n",app_state);
  698. fprintf(log_file,"I don't have self opt offers app_state=%d\n",app_state);
  699. fflush(log_file);
  700. state = IDLE_AGENT;
  701. } else {
  702. tmp_offer_list = selfopt_man_offers;
  703. while (tmp_offer_list != NULL){
  704. //printf("Offer by %d for %d cores\n",tmp_offer_list->sender,tmp_offer_list->off.num_of_cores);
  705. fprintf(log_file,"Offer by %d for %d cores\n",tmp_offer_list->sender,tmp_offer_list->off.num_of_cores);
  706. fflush(log_file);
  707. tmp_offer_list = tmp_offer_list->next;
  708. }
  709. if (app_state != APP_TERMINATED) {//!app_terminated
  710. *selfopt_man_offers->answer = 1;
  711. if (core_inter_head[my_idag] == NULL){
  712. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  713. core_inter_tail[my_idag] = core_inter_head[my_idag];
  714. } else {
  715. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  716. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  717. }
  718. core_inter_tail[my_idag]->type = IDAG_ADD_CORES_DDS;
  719. core_inter_tail[my_idag]->data.app_cores = (int *)malloc((selfopt_man_offers->off.num_of_cores+1)*sizeof(int));
  720. core_inter_tail[my_idag]->data.app_cores[0] = selfopt_man_offers->off.num_of_cores;
  721. core_inter_tail[my_idag]->next = NULL;
  722. if (app_state != RESIZING) {
  723. old_Speedup = my_Speedup;
  724. chk_rem_num = my_cores_count-1;
  725. fprintf(app_log_file,"Initialising resize with check_rem = %d\n",chk_rem_num);
  726. fflush(app_log_file);
  727. chk_rem_count = 0;
  728. sum_rem_time = 0;
  729. app_state = RESIZING;
  730. my_cores_count += selfopt_man_offers->off.num_of_cores;
  731. my_app.num_of_cores = my_cores_count;
  732. my_Speedup = Speedup(my_app, my_cores_count);
  733. if (old_cores_cnt > 1)
  734. for(tmp_cores_list=my_cores->next; tmp_cores_list!=NULL; tmp_cores_list=tmp_cores_list->next) {//first is myself
  735. one_core = tmp_cores_list->core_id;
  736. if (core_inter_head[one_core] == NULL){
  737. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  738. core_inter_tail[one_core] = core_inter_head[one_core];
  739. } else {
  740. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  741. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  742. }
  743. core_inter_tail[one_core]->type = APPOINT_WORK_NODE_PENDING;//CHK_REM_TIME;
  744. core_inter_tail[one_core]->next = NULL;
  745. if (core_inter_head[one_core]->next == NULL) {
  746. //kill(pid_num[one_core], SIG_CHECK_REM_TIME);
  747. scc_kill(one_core, SIG_CHECK_REM_TIME);
  748. my_stats.msg_count++;
  749. my_stats.distance += distance(node_id,one_core);
  750. } else printf("I am %d and i am doing smth else with my working node %d in send SIG_CHECK_REM_TIME interaction = %d\n",node_id,one_core,core_inter_head[one_core]->type);
  751. }
  752. } else {
  753. fprintf(app_log_file,"Allready resizing with chk_rem_num = %d chk_rem_count=%d\n",chk_rem_num,chk_rem_count);
  754. fflush(app_log_file);
  755. my_cores_count += selfopt_man_offers->off.num_of_cores;
  756. my_app.num_of_cores = my_cores_count;
  757. my_Speedup = Speedup(my_app, my_cores_count);
  758. }
  759. for (i=0; i<selfopt_man_offers->off.num_of_cores; i++){
  760. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  761. my_cores_tail = my_cores_tail->next;
  762. my_cores_tail->core_id = selfopt_man_offers->off.offered_cores[i];
  763. my_cores_tail->offered_to = -1;
  764. my_cores_tail->next = NULL;
  765. core_inter_tail[my_idag]->data.app_cores[i+1] = selfopt_man_offers->off.offered_cores[i];
  766. one_core = my_cores_tail->core_id;
  767. //printf("I am %d and i put in appoint work %d\n",node_id,one_core);
  768. if (core_inter_head[one_core] == NULL){
  769. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  770. core_inter_tail[one_core] = core_inter_head[one_core];
  771. } else {
  772. fprintf(log_file,"I am doing sth with my new core %d interaction is %d\n",one_core,core_inter_head[one_core]->type);
  773. fflush(log_file);
  774. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  775. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  776. }
  777. core_inter_tail[one_core]->type = INIT_WORK_NODE_PENDING;
  778. core_inter_tail[one_core]->next = NULL;
  779. }
  780. } else *selfopt_man_offers->answer = 0;
  781. tmp_offer_list = selfopt_man_offers->next;
  782. while (tmp_offer_list != NULL){
  783. *tmp_offer_list->answer = 0;
  784. tmp_offer_list = tmp_offer_list->next;
  785. }
  786. if (app_state != APP_TERMINATED) {//!app_terminated
  787. if (core_inter_head[my_idag]->next == NULL) {
  788. //kill(pid_num[my_idag], SIG_ADD_CORES_DDS);
  789. //scc_kill(my_idag, SIG_ADD_CORES_DDS);
  790. sig_ACK_sender(my_idag, SIG_ADD_CORES_DDS);
  791. my_stats.msg_count++;
  792. my_stats.distance += distance(node_id,my_idag);
  793. } else {
  794. fprintf(log_file,"I didn't call add inside selfopt with interaction %d\n",core_inter_head[my_idag]->type);
  795. fflush(log_file);
  796. }
  797. if (old_cores_cnt == 1) {
  798. nodes_ended_cnt = 0;
  799. time_to_work = (int) roundf(my_app.workld / my_Speedup);
  800. time_per_node = time_to_work / (my_cores_count-1);
  801. time_left = time_to_work % (my_cores_count-1);
  802. if (time_to_work == 0) time_left++;
  803. fprintf(app_log_file,"in one core speedup = %0.2f time_to_work = %d time_per_node = %d, workld=%0.2f\n",my_Speedup,time_to_work,time_per_node,my_app.workld);
  804. fflush(app_log_file);
  805. tmp_cores_list = my_cores->next;
  806. while (tmp_cores_list != NULL){
  807. //printf("I am inside core listing node=%d\n",node_id);
  808. one_core = tmp_cores_list->core_id;
  809. for (tmp_inter_list = core_inter_head[one_core]; tmp_inter_list != NULL; tmp_inter_list = tmp_inter_list->next)
  810. if (tmp_inter_list->type == INIT_WORK_NODE_PENDING) break;
  811. if (tmp_inter_list != NULL) {
  812. tmp_inter_list->type = INIT_WORK_NODE;
  813. tmp_inter_list->data.work_time = time_per_node;
  814. if (time_left > 0) {
  815. tmp_inter_list->data.work_time++;
  816. time_left--;
  817. }
  818. my_stats.app_turnaround += tmp_inter_list->data.work_time;
  819. } else printf("i am %d kai sto common_node ta hpiame one core = %d\n",node_id,one_core);
  820. if (core_inter_head[one_core]->next == NULL) {
  821. //kill(pid_num[one_core], SIG_APPOINT_WORK);
  822. //scc_kill(one_core, SIG_APPOINT_WORK);
  823. sig_ACK_sender(one_core, SIG_APPOINT_WORK);
  824. my_stats.msg_count++;
  825. my_stats.distance += distance(node_id,one_core);
  826. } else {
  827. fprintf(log_file,"I am doing smth else with my working node? interaction=%d interaction2=%d\n",
  828. core_inter_head[one_core]->type,core_inter_head[one_core]->next->type);
  829. fflush(log_file);
  830. }
  831. tmp_cores_list = tmp_cores_list->next;
  832. }
  833. app_state = RUNNING;
  834. }
  835. }
  836. while (selfopt_man_offers != NULL) {
  837. if (core_inter_head[selfopt_man_offers->sender]->type == REP_AGENT_OFFER_PENDING){
  838. core_inter_head[selfopt_man_offers->sender]->type = REP_AGENT_OFFER_SENT;
  839. //kill(pid_num[selfopt_man_offers->sender], SIG_REP_OFFERS);
  840. //scc_kill(selfopt_man_offers->sender, SIG_REP_OFFERS);
  841. sig_ACK_sender(selfopt_man_offers->sender, SIG_REP_OFFERS);
  842. my_stats.msg_count++;
  843. my_stats.distance += distance(node_id,selfopt_man_offers->sender);
  844. } else if (core_inter_head[selfopt_man_offers->sender]->type != REP_AGENT_OFFER_SENT) printf("We have zxcvzxc problem!\n");
  845. tmp_offer_list = selfopt_man_offers;
  846. selfopt_man_offers = selfopt_man_offers->next;
  847. free(tmp_offer_list);
  848. }
  849. //printf("Self opt seems to be ok cores count = %d\n",my_cores_count);
  850. cur_time = time(NULL);
  851. cur_t = localtime(&cur_time);
  852. fprintf(log_file, "[%d:%d:%d]: Self opt ok. my_cores_count = %d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,my_cores_count);
  853. fprintf(log_file, "my cores are:");
  854. fprintf(app_log_file, "[%d:%d:%d]: Self opt ok. my_cores_count = %d\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,my_cores_count);
  855. fprintf(app_log_file, "my cores are:");
  856. //printf("I am %d and my cores are:",node_id);
  857. for (tmp_cores_list=my_cores; tmp_cores_list!=NULL; tmp_cores_list=tmp_cores_list->next) {
  858. fprintf(log_file, " %d", tmp_cores_list->core_id);
  859. fprintf(app_log_file, " %d", tmp_cores_list->core_id);
  860. //printf(" %d",tmp_cores_list->core_id);
  861. }
  862. fprintf(log_file, "\n");
  863. fflush(log_file);
  864. fprintf(app_log_file, "\n");
  865. fflush(app_log_file);
  866. }
  867. if (app_state == APP_TERMINATED) state = AGENT_ENDING;//app_terminated
  868. else {
  869. if (pending_state == INIT_MANAGER) {
  870. state = INIT_MANAGER;
  871. pending_state = IDLE_AGENT;
  872. } else {
  873. printf("I am %d in chk_offers and pending_state=%d\n",node_id,pending_state);
  874. if (selfopt_interval > 0) { // && !app_terminated
  875. its.it_value.tv_nsec = selfopt_interval * MS;
  876. selfopt_time_rem = selfopt_interval;
  877. if (old_cores_cnt == 1 || old_cores_cnt == my_cores_count)
  878. if (timer_settime(timerid, 0, &its, NULL) == -1) printf("timer_settime error8\n");
  879. } else if (my_cores_count == 1) {
  880. //printf("I am %d and i have to rewind my selfopt_areas_file for app with id %d\n",node_id,init_app.id);
  881. fprintf(log_file,"I have to rewind my selfopt_areas_file for app with id %d\n",init_app.id);
  882. rewind(selfopt_areas_file);
  883. fscanf(selfopt_areas_file,"%d",&area_cnt);
  884. for (i=0; i<area_cnt; i++){
  885. fscanf(selfopt_areas_file,"%d",&C);
  886. fscanf(selfopt_areas_file,"%d",&r);
  887. }
  888. fscanf(selfopt_areas_file,"%d",&area_cnt);
  889. selfopt_interval = 200;
  890. its.it_value.tv_nsec = selfopt_interval * MS;
  891. selfopt_time_rem = selfopt_interval;
  892. if (timer_settime(timerid, 0, &its, NULL) == -1) perror("timer_settime error82\n");
  893. } else selfopt_time_rem = -1;
  894. state = IDLE_AGENT;
  895. }
  896. }
  897. signals_enable();
  898. scc_signals_check();
  899. } else if (state == WORKING_NODE || state == WORKING_NODE_IDLE_INIT) {
  900. //int j,dummy=0;
  901. signals_disable();
  902. while (time_worked <= upper_work_bound){
  903. signals_enable();
  904. //the following code should be one ms long
  905. for (i=0; i<1000; i++)
  906. for(j=0; j<1000; j++)
  907. dummy++;
  908. signals_disable();
  909. time_worked++;
  910. }
  911. printf("I am node %d with agent = %d and I finished my work upper_work_bound = %ld time_passed=%d\n",node_id,my_agent,upper_work_bound,time_worked);
  912. if (upper_work_bound > 0) {
  913. //kill(pid_num[my_agent],SIG_FINISH);
  914. scc_kill(my_agent,SIG_FINISH);
  915. my_stats.msg_count++;
  916. my_stats.distance += distance(node_id,my_agent);
  917. }
  918. if (state == WORKING_NODE) state = IDLE_CORE;
  919. else if (state == WORKING_NODE_IDLE_INIT) state = IDLE_INIT_MAN;
  920. else if (pending_state == WORKING_NODE) pending_state = NO_PENDING_STATE; //just finished on return
  921. signals_enable();
  922. scc_signals_check();
  923. } else if (state == AGENT_ZOMBIE) {
  924. dummy=0;
  925. for (i=0; i<100; i++)
  926. for(j=0; j<100; j++)
  927. dummy++;
  928. scc_signals_check();
  929. } else if (state == AGENT_ENDING) {
  930. signals_disable();
  931. cur_time = time(NULL);
  932. cur_t = localtime(&cur_time);
  933. fprintf(log_file, "[%d:%d:%d]: I entered agent_ending\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  934. fflush(log_file);
  935. fprintf(app_log_file, "[%d:%d:%d]: I entered agent_ending\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  936. fflush(app_log_file);
  937. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  938. fprintf(app_log_file,"Core %d is offered to %d\n",tmp_cores_list->core_id,tmp_cores_list->offered_to);
  939. fflush(app_log_file);
  940. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next)
  941. if (tmp_cores_list->offered_to != -1) break;
  942. if (tmp_cores_list != NULL) {
  943. state = AGENT_ZOMBIE;
  944. for (tmp_cores_list = my_cores->next; tmp_cores_list != NULL; tmp_cores_list = tmp_cores_list->next) {
  945. one_core = tmp_cores_list->core_id;
  946. tmp_inter_prev = NULL;
  947. tmp_inter_list = core_inter_head[one_core];
  948. //for (tmp_inter_list = core_inter_head[one_core]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
  949. while (tmp_inter_list != NULL)
  950. if (tmp_inter_list->type == INIT_WORK_NODE || tmp_inter_list->type == APPOINT_WORK_NODE || tmp_inter_list->type == INIT_WORK_NODE_PENDING
  951. || tmp_inter_list->type == APPOINT_WORK_NODE_PENDING || tmp_inter_list->type == REMOVED_NODE_REM_TIME) {
  952. //|| (tmp_inter_list->type == APPOINT_WORK_NODE_PENDING && tmp_inter_prev != NULL)) {
  953. fprintf(log_file, "Removing in zombie one node of %d with inter = %d\n",one_core,tmp_inter_list->type);
  954. fflush(log_file);
  955. if (tmp_inter_prev == NULL) {
  956. core_inter_head[one_core] = core_inter_head[one_core]->next;
  957. free(tmp_inter_list);
  958. tmp_inter_list = core_inter_head[one_core];
  959. } else {
  960. tmp_inter_prev->next = tmp_inter_list->next;
  961. if (tmp_inter_prev->next == NULL) core_inter_tail[one_core] = tmp_inter_prev;
  962. free(tmp_inter_list);
  963. tmp_inter_list = tmp_inter_prev->next;
  964. }
  965. } else {
  966. tmp_inter_prev = tmp_inter_list;
  967. tmp_inter_list = tmp_inter_list->next;
  968. }
  969. }
  970. } else {
  971. if (core_inter_head[my_idag] == NULL){
  972. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  973. core_inter_tail[my_idag] = core_inter_head[my_idag];
  974. } else {
  975. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  976. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  977. }
  978. core_inter_tail[my_idag]->type = REMOVE_APP;
  979. core_inter_tail[my_idag]->data.app_cores = (int *)malloc((my_cores_count+1)*sizeof(int));
  980. core_inter_tail[my_idag]->data.app_cores[0] = my_cores_count;
  981. core_inter_tail[my_idag]->next = NULL;
  982. i=1;
  983. core_inter_tail[my_idag]->data.app_cores[i++] = my_cores->core_id;
  984. tmp_cores_list = my_cores;
  985. my_cores=my_cores->next;
  986. free(tmp_cores_list);
  987. //for(my_cores=my_cores->next; my_cores!=NULL; my_cores = my_cores->next) {
  988. //free(tmp_cores_list);
  989. while (my_cores != NULL) {
  990. tmp_cores_list = my_cores;
  991. one_core = tmp_cores_list->core_id;
  992. core_inter_tail[my_idag]->data.app_cores[i++] = one_core;
  993. tmp_inter_prev = NULL;
  994. tmp_inter_list = core_inter_head[one_core];
  995. //for (tmp_inter_list = core_inter_head[one_core]; tmp_inter_list != NULL; tmp_inter_list=tmp_inter_list->next)
  996. while (tmp_inter_list != NULL)
  997. if (tmp_inter_list->type == INIT_WORK_NODE || tmp_inter_list->type == APPOINT_WORK_NODE || tmp_inter_list->type == INIT_WORK_NODE_PENDING
  998. || tmp_inter_list->type == APPOINT_WORK_NODE_PENDING || tmp_inter_list->type == REMOVED_NODE_REM_TIME) {
  999. fprintf(log_file, "Removing one node of %d with inter = %d\n",one_core,tmp_inter_list->type);
  1000. fflush(log_file);
  1001. if (tmp_inter_prev == NULL) {
  1002. core_inter_head[one_core] = core_inter_head[one_core]->next;
  1003. free(tmp_inter_list);
  1004. tmp_inter_list = core_inter_head[one_core];
  1005. } else {
  1006. tmp_inter_prev->next = tmp_inter_list->next;
  1007. if (tmp_inter_prev->next == NULL) core_inter_tail[one_core] = tmp_inter_prev;
  1008. free(tmp_inter_list);
  1009. tmp_inter_list = tmp_inter_prev->next;
  1010. }
  1011. } else {
  1012. tmp_inter_prev = tmp_inter_list;
  1013. tmp_inter_list = tmp_inter_list->next;
  1014. }
  1015. if (core_inter_head[one_core] == NULL) {
  1016. //kill(pid_num[one_core],SIG_FINISH);
  1017. scc_kill(one_core,SIG_FINISH);
  1018. my_stats.msg_count++;
  1019. my_stats.distance += distance(node_id,one_core);
  1020. } else {
  1021. fprintf(log_file,"I still still have smth to do with my work node %d before finish which is %d\n",one_core,core_inter_head[one_core]->type);
  1022. fflush(log_file);
  1023. }
  1024. my_cores=my_cores->next;
  1025. free(tmp_cores_list);
  1026. }
  1027. if (core_inter_head[my_idag]->next == NULL) {
  1028. //kill(pid_num[my_idag],SIG_FINISH);
  1029. //scc_kill(my_idag,SIG_FINISH);
  1030. sig_ACK_sender(my_idag,SIG_FINISH);
  1031. my_stats.msg_count++;
  1032. my_stats.distance += distance(node_id,my_idag);
  1033. } else {
  1034. fprintf(log_file,"I am doing smth else with my idag app_remove interaction=%d\n",core_inter_head[my_idag]->type);
  1035. fflush(log_file);
  1036. }
  1037. my_app.A = 0.0;
  1038. my_app.var = 0.0;
  1039. my_app.num_of_cores = -1;
  1040. selfopt_time_rem = -1;
  1041. //app_terminated = 0;
  1042. app_state = NO_APP;
  1043. //printf("I am %d My app ended app_id = %d pending_state=%d\n",node_id,my_app.id,pending_state);
  1044. cur_time = time(NULL);
  1045. cur_t = localtime(&cur_time);
  1046. fprintf(app_log_file, "[%d:%d:%d]: App ended app_id = %d pending_state=%d",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,my_app.id,pending_state);
  1047. //fprintf(app_log_file,"App ended app_id = %d pending_state=%d\n",my_app.id,pending_state);
  1048. fclose(app_log_file);
  1049. if (pending_state == INIT_MANAGER) {
  1050. state = INIT_MANAGER;
  1051. pending_state = IDLE_CORE;
  1052. } else if (pending_state == NO_PENDING_STATE) state = IDLE_CORE;
  1053. else {
  1054. //printf("I am %d in agent_ending and pending_state = %d\n",node_id,pending_state);
  1055. state = IDLE_CORE;
  1056. }
  1057. }
  1058. if (my_idag != 0) {
  1059. if (core_inter_head[0] == NULL) scc_kill(0, SIG_APP_TERMINATED);
  1060. else {
  1061. core_inter_tail[0]->next = (inter_list *) malloc(sizeof(inter_list));
  1062. core_inter_tail[0] = core_inter_tail[0]->next;
  1063. core_inter_tail[0]->type = NOTIFY_APP_TERMINATION;
  1064. core_inter_tail[0]->next = NULL;
  1065. }
  1066. }
  1067. signals_enable();
  1068. scc_signals_check();
  1069. } else {
  1070. printf("Unknown state node_id = %d state = %d\n",node_id,state);
  1071. state = IDLE_CORE;
  1072. }
  1073. cur_time = time(NULL);
  1074. cur_t = localtime(&cur_time);
  1075. fprintf(log_file, "[%d:%d:%d]: I ended well\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  1076. fclose(log_file);
  1077. exit(0);
  1078. }