offer_cores_backup.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. int offer_cores(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id) {
  2. int Of_cores_num=0, min_dist=0, cur_dist=0;
  3. float gain_total=0.1,base_receiver=0.0,base_giver=0.0,gain_receiver=0.0,loss_giver=0.0,share_giver=0.0,new_gain=0.0;
  4. int Cores_receiver = req_app.num_of_cores, Cores_giver = my_app.num_of_cores;
  5. core_list *tmp, *GreedyChoice;
  6. int offered_cnt=0, counted_cores=0;
  7. for (tmp=cores; tmp!=NULL; tmp=tmp->next) {
  8. if (distance(req_reg.C, tmp->core_id) <= req_reg.r) share_giver++;
  9. counted_cores++;
  10. if (tmp->offered_to != -1) offered_cnt++;
  11. fprintf(log_file,"Core %d is offered to %d\n",tmp->core_id,tmp->offered_to);
  12. }
  13. fflush(log_file);
  14. if (offered_cnt == (counted_cores-2) && my_idag != -1) {
  15. fprintf(log_file,"I did not give up my only not offered core\n");
  16. fflush(log_file);
  17. return 0;
  18. }
  19. share_giver = share_giver / (float) region_count(req_reg);
  20. if (my_idag == -1) {
  21. while (gain_total > 0.0) {
  22. gain_total = 0.0;
  23. GreedyChoice = NULL;//-1;
  24. min_dist = -1;
  25. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  26. base_giver = 0;
  27. tmp = cores->next;//very important!!! that way i avoid giving up my agent core
  28. while (tmp != NULL) {
  29. cur_dist = distance(req_reg.C, tmp->core_id);
  30. if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
  31. //Of_cores_num == 0 to be the first offered core
  32. //Cores_receiver == 0 to avoid providing the core to an non-initial core search
  33. if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 0) {
  34. if (Cores_receiver == 0 && Of_cores_num == 0) gain_receiver = 1000; //0 sto init_app
  35. else gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  36. loss_giver = 0;
  37. new_gain = gain_receiver - loss_giver;
  38. gain_total = new_gain;
  39. GreedyChoice = tmp;//->core_id;
  40. break;
  41. #ifdef LOW_VOLTAGE_ISLANDS_4
  42. } else if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 1) {
  43. if (Cores_receiver == 0 && Of_cores_num == 0) gain_receiver = 1000; //0 sto init_app
  44. else gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  45. loss_giver = 0;
  46. new_gain = gain_receiver - loss_giver;
  47. gain_total = new_gain;
  48. GreedyChoice = tmp;//->core_id;
  49. break;
  50. #endif
  51. } else if (low_voltage_core[tmp->core_id] == 0) {
  52. //if (Cores_receiver == 0 && Of_cores_num == 1 && req_app.var < 1.0) gain_receiver = 1000000;
  53. if (Cores_receiver == 0 && Of_cores_num == 0) gain_receiver = 1000; //0 sto init_app
  54. else gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  55. loss_giver = 0;
  56. new_gain = gain_receiver - loss_giver;
  57. if (new_gain > gain_total){
  58. gain_total = new_gain;
  59. min_dist = cur_dist;
  60. GreedyChoice = tmp;//->core_id;
  61. } else if (new_gain == gain_total && cur_dist < min_dist) {
  62. //printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
  63. // node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
  64. min_dist = cur_dist;
  65. GreedyChoice = tmp;
  66. }
  67. }
  68. }
  69. tmp = tmp->next;
  70. }
  71. if (gain_total > 0.0) {
  72. Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  73. GreedyChoice->offered_to = req_id;
  74. }
  75. }
  76. }
  77. #ifndef GREEDY_MANAGER
  78. else {
  79. while (gain_total > 0.0) {
  80. gain_total = 0.0;
  81. GreedyChoice = NULL;//-1;
  82. min_dist = -1;
  83. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  84. base_giver = Speedup(my_app, Cores_giver - Of_cores_num);
  85. tmp = cores->next->next;//very important!!! that way i avoid giving up my only working core
  86. while (tmp != NULL) {
  87. if (core_inter_head[tmp->core_id] != NULL &&
  88. (core_inter_head[tmp->core_id]->type == INIT_WORK_NODE_PENDING || core_inter_head[tmp->core_id]->type == INIT_WORK_NODE)) {
  89. fprintf(log_file,"Core %d is about to start work type = %d\n",tmp->core_id,core_inter_head[tmp->core_id]->type);
  90. fflush(log_file);
  91. tmp = tmp->next;
  92. } else {
  93. cur_dist = distance(req_reg.C, tmp->core_id);
  94. if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
  95. //if (Cores_receiver == 0 && Of_cores_num == 1 && req_app.var < 1.0) gain_receiver = 1000000;
  96. if (Cores_receiver == 0 && Of_cores_num == 0) gain_receiver = 1000; //0 sto init_app
  97. else gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  98. loss_giver = base_giver - Speedup(my_app, Cores_giver - Of_cores_num - 1);
  99. new_gain = gain_receiver - loss_giver;
  100. if (new_gain > gain_total){
  101. gain_total = new_gain;
  102. min_dist = cur_dist;
  103. GreedyChoice = tmp;//->core_id;
  104. } else if (new_gain == gain_total && cur_dist < min_dist) {
  105. //printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
  106. // node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
  107. min_dist = cur_dist;
  108. GreedyChoice = tmp;
  109. }
  110. }
  111. tmp = tmp->next;
  112. }
  113. }
  114. if (gain_total > 0.0) {
  115. Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  116. GreedyChoice->offered_to = req_id;
  117. }
  118. }
  119. }
  120. #endif
  121. return Of_cores_num;
  122. }
  123. int offer_cores_fft(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id) {
  124. int Of_cores_num=0, min_dist=0, cur_dist=0;
  125. float gain_total=0.1,base_receiver=0.0,base_giver=0.0,gain_receiver=0.0,loss_giver=0.0,share_giver=0.0,new_gain=0.0;
  126. int Cores_receiver = req_app.num_of_cores, Cores_giver = my_app.num_of_cores;
  127. core_list *tmp, *GreedyChoice;
  128. int offered_cnt=0, counted_cores=0;
  129. for (tmp=cores; tmp!=NULL; tmp=tmp->next) {
  130. if (distance(req_reg.C, tmp->core_id) <= req_reg.r) share_giver++;
  131. counted_cores++;
  132. if (tmp->offered_to != -1) offered_cnt++;
  133. fprintf(log_file,"Core %d is offered to %d\n",tmp->core_id,tmp->offered_to);
  134. }
  135. fflush(log_file);
  136. if (offered_cnt == (counted_cores-2) && my_idag != -1) {
  137. fprintf(log_file,"I did not give up my only not offered core\n");
  138. fflush(log_file);
  139. return 0;
  140. }
  141. share_giver = share_giver / (float) region_count(req_reg);
  142. if (my_idag == -1) {
  143. while (gain_total > 0.0) {
  144. gain_total = 0.0;
  145. GreedyChoice = NULL;//-1;
  146. min_dist = -1;
  147. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  148. base_giver = 0;
  149. tmp = cores->next;//very important!!! that way i avoid giving up my agent core
  150. while (tmp != NULL) {
  151. cur_dist = distance(req_reg.C, tmp->core_id);
  152. if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
  153. //Of_cores_num == 0 to be the first offered core
  154. //Cores_receiver == 0 to avoid providing the core to an non-initial core search
  155. if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 0) {
  156. if (Cores_receiver == 0 && Of_cores_num == 0) gain_receiver = 1000; //0 sto init_app
  157. else gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); /* +1 stands for the possibly offered core */
  158. loss_giver = 0;
  159. new_gain = gain_receiver - loss_giver;
  160. gain_total = new_gain;
  161. GreedyChoice = tmp;//->core_id;
  162. break;
  163. #ifdef LOW_VOLTAGE_ISLANDS_4
  164. } else if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 1) {
  165. if (Cores_receiver == 0 && Of_cores_num == 0) gain_receiver = 1000; //0 sto init_app
  166. else gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); /* +1 stands for the possibly offered core */
  167. loss_giver = 0;
  168. new_gain = gain_receiver - loss_giver;
  169. gain_total = new_gain;
  170. GreedyChoice = tmp;//->core_id;
  171. break;
  172. #endif
  173. } else if (low_voltage_core[tmp->core_id] == 0) {
  174. //if (Cores_receiver == 0 && Of_cores_num == 1 && req_app.var < 1.0) gain_receiver = 1000000;
  175. if (Cores_receiver == 0 && Of_cores_num == 0) gain_receiver = 1000; //0 sto init_app
  176. else gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); /* +1 stands for the possibly offered core */
  177. loss_giver = 0;
  178. new_gain = gain_receiver - loss_giver;
  179. if (new_gain > gain_total){
  180. gain_total = new_gain;
  181. min_dist = cur_dist;
  182. GreedyChoice = tmp;//->core_id;
  183. } else if (new_gain == gain_total && cur_dist < min_dist) {
  184. //printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
  185. // node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
  186. min_dist = cur_dist;
  187. GreedyChoice = tmp;
  188. }
  189. }
  190. }
  191. tmp = tmp->next;
  192. }
  193. if (gain_total > 0.0) {
  194. Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  195. GreedyChoice->offered_to = req_id;
  196. }
  197. }
  198. /* FFT app requires only power of 2 exec cores plus its manager
  199. * I do not include higher than 5 because it will create no speedup
  200. */
  201. if ((Cores_receiver + Of_cores_num) == 4) {
  202. for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
  203. if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
  204. fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
  205. tmp->offered_to = -1;
  206. Of_cores_num--;
  207. break;
  208. }
  209. }
  210. } else if (Of_cores_num > 4) {
  211. }
  212. }
  213. #ifndef GREEDY_MANAGER
  214. else {
  215. while (gain_total > 0.0) {
  216. gain_total = 0.0;
  217. GreedyChoice = NULL;//-1;
  218. min_dist = -1;
  219. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  220. base_giver = Speedup(my_app, Cores_giver - Of_cores_num);
  221. tmp = cores->next->next;//very important!!! that way i avoid giving up my only working core
  222. while (tmp != NULL) {
  223. if (core_inter_head[tmp->core_id] != NULL &&
  224. (core_inter_head[tmp->core_id]->type == INIT_WORK_NODE_PENDING || core_inter_head[tmp->core_id]->type == INIT_WORK_NODE)) {
  225. fprintf(log_file,"Core %d is about to start work type = %d\n",tmp->core_id,core_inter_head[tmp->core_id]->type);
  226. fflush(log_file);
  227. tmp = tmp->next;
  228. } else {
  229. cur_dist = distance(req_reg.C, tmp->core_id);
  230. if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
  231. //if (Cores_receiver == 0 && Of_cores_num == 1 && req_app.var < 1.0) gain_receiver = 1000000;
  232. if (Cores_receiver == 0 && Of_cores_num == 0) gain_receiver = 1000; //0 sto init_app
  233. else gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  234. loss_giver = base_giver - Speedup(my_app, Cores_giver - Of_cores_num - 1);
  235. new_gain = gain_receiver - loss_giver;
  236. if (new_gain > gain_total){
  237. gain_total = new_gain;
  238. min_dist = cur_dist;
  239. GreedyChoice = tmp;//->core_id;
  240. } else if (new_gain == gain_total && cur_dist < min_dist) {
  241. //printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
  242. // node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
  243. min_dist = cur_dist;
  244. GreedyChoice = tmp;
  245. }
  246. }
  247. tmp = tmp->next;
  248. }
  249. }
  250. if (gain_total > 0.0) {
  251. Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  252. GreedyChoice->offered_to = req_id;
  253. }
  254. }
  255. }
  256. #endif
  257. if (my_idag == -1) {
  258. /* FFT app requires only power of 2 exec cores plus its manager */
  259. if (executed_app == FFT) {
  260. if (Of_cores_num == 3) {
  261. for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
  262. if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
  263. fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
  264. tmp->offered_to = -1;
  265. Of_cores_num--;
  266. break;
  267. }
  268. }
  269. } else if (Of_cores_num > 4) {
  270. }
  271. }
  272. return Of_cores_num;
  273. }