resource_negotiation.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. #include "resource_negotiation.h"
  2. #include "noc_functions.h"
  3. #include "apps.h"
  4. extern app my_app;
  5. int offer_cores_original(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id);
  6. int offer_cores_updated(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id);
  7. int offer_cores_fft_original(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id);
  8. int offer_cores(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id) {
  9. #ifdef RESOURCE_ALGO_ORIG
  10. if (executed_app != FFT) {
  11. return offer_cores_original(cores, req_app, req_reg, Offered_cores, req_id);
  12. } else {
  13. return offer_cores_fft_original(cores, req_app, req_reg, Offered_cores, req_id);
  14. }
  15. #elif RESOURCE_ALGO_UPDATED
  16. return offer_cores_updated(cores, req_app, req_reg, Offered_cores, req_id);
  17. #elif RESOURCE_ALGO_UPDATED_GENEROUS
  18. return offer_cores_updated(cores, req_app, req_reg, Offered_cores, req_id);
  19. #else
  20. return offer_cores_original(cores, req_app, req_reg, Offered_cores, req_id);
  21. #endif
  22. }
  23. /* Speedup is calculated correclty inside Speedup function */
  24. int offer_cores_original(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id) {
  25. int Of_cores_num=0, min_dist=0, cur_dist=0;
  26. 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;
  27. int Cores_receiver = req_app.num_of_cores, Cores_giver = my_app.num_of_cores;
  28. core_list *tmp, *GreedyChoice;
  29. int offered_cnt=0, counted_cores=0;
  30. for (tmp=cores; tmp!=NULL; tmp=tmp->next) {
  31. if (distance(req_reg.C, tmp->core_id) <= req_reg.r) share_giver++;
  32. counted_cores++;
  33. if (tmp->offered_to != -1) offered_cnt++;
  34. fprintf(log_file,"Core %d is offered to %d\n",tmp->core_id,tmp->offered_to);
  35. }
  36. if (offered_cnt == (counted_cores-2) && my_idag != -1) {
  37. fprintf(log_file,"I did not give up my only not offered core\n");
  38. return 0;
  39. }
  40. share_giver = share_giver / (float) region_count(req_reg);
  41. if (my_idag == -1) {
  42. while (gain_total > 0.0) {
  43. gain_total = 0.0;
  44. GreedyChoice = NULL;//-1;
  45. min_dist = -1;
  46. base_giver = 0;
  47. tmp = cores->next;//very important!!! that way i avoid giving up my agent core
  48. while (tmp != NULL) {
  49. cur_dist = distance(req_reg.C, tmp->core_id);
  50. if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
  51. //Of_cores_num == 0 to be the first offered core
  52. //Cores_receiver == 0 to avoid providing the core to an non-initial core search
  53. if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 0) {
  54. if ((Cores_receiver + Of_cores_num) == 0) {
  55. gain_receiver = 1000; //0 sto init_app
  56. } else if ((Cores_receiver + Of_cores_num) == 1) {
  57. gain_receiver = 100; //no worker cores
  58. } else { /* (Cores_receiver + Of_cores_num) > 1 */
  59. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  60. //gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  61. gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  62. }
  63. loss_giver = 0;
  64. new_gain = gain_receiver - loss_giver;
  65. gain_total = new_gain;
  66. GreedyChoice = tmp;//->core_id;
  67. break;
  68. #ifdef LOW_VOLTAGE_ISLANDS_4
  69. } else if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 1) {
  70. if (Cores_receiver == 0 && Of_cores_num == 0) gain_receiver = 1000; //0 sto init_app
  71. else gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  72. loss_giver = 0;
  73. new_gain = gain_receiver - loss_giver;
  74. gain_total = new_gain;
  75. GreedyChoice = tmp;//->core_id;
  76. break;
  77. #endif
  78. } else if (low_voltage_core[tmp->core_id] == 0) {
  79. if ((Cores_receiver + Of_cores_num) == 0) {
  80. gain_receiver = 1000; //0 sto init_app
  81. } else if ((Cores_receiver + Of_cores_num) == 1) {
  82. gain_receiver = 100; //no worker cores
  83. } else { /* (Cores_receiver + Of_cores_num) > 1 */
  84. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  85. gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  86. }
  87. loss_giver = 0;
  88. new_gain = gain_receiver - loss_giver;
  89. if (new_gain > gain_total){
  90. gain_total = new_gain;
  91. min_dist = cur_dist;
  92. GreedyChoice = tmp;//->core_id;
  93. } else if (new_gain == gain_total && cur_dist < min_dist) {
  94. //printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
  95. // node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
  96. min_dist = cur_dist;
  97. GreedyChoice = tmp;
  98. }
  99. }
  100. }
  101. tmp = tmp->next;
  102. }
  103. if (gain_total > 0.0) {
  104. Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  105. GreedyChoice->offered_to = req_id;
  106. }
  107. }
  108. }
  109. #ifndef GREEDY_MANAGER
  110. else {
  111. while (gain_total > 0.0) {
  112. gain_total = 0.0;
  113. GreedyChoice = NULL;//-1;
  114. min_dist = -1;
  115. base_giver = Speedup(my_app, Cores_giver - Of_cores_num);
  116. tmp = cores->next->next;//very important!!! that way i avoid giving up my only working core
  117. while (tmp != NULL) {
  118. if (core_inter_head[tmp->core_id] != NULL &&
  119. (core_inter_head[tmp->core_id]->type == INIT_WORK_NODE_PENDING || core_inter_head[tmp->core_id]->type == INIT_WORK_NODE)) {
  120. fprintf(log_file,"Core %d is about to start work type = %d\n",tmp->core_id,core_inter_head[tmp->core_id]->type);
  121. tmp = tmp->next;
  122. } else {
  123. cur_dist = distance(req_reg.C, tmp->core_id);
  124. if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
  125. if ((Cores_receiver + Of_cores_num) == 0) {
  126. gain_receiver = 1000; //0 sto init_app
  127. } else if ((Cores_receiver + Of_cores_num) == 1) {
  128. gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here
  129. } else { /* (Cores_receiver + Of_cores_num) > 1 */
  130. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  131. gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); /* + 1 is ommited due to workload convention */
  132. }
  133. loss_giver = base_giver - Speedup(my_app, Cores_giver - (Of_cores_num + 1));
  134. new_gain = gain_receiver - loss_giver;
  135. if (new_gain > gain_total){
  136. gain_total = new_gain;
  137. min_dist = cur_dist;
  138. GreedyChoice = tmp;//->core_id;
  139. } else if (new_gain == gain_total && cur_dist < min_dist) {
  140. //printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
  141. // node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
  142. min_dist = cur_dist;
  143. GreedyChoice = tmp;
  144. }
  145. }
  146. tmp = tmp->next;
  147. }
  148. }
  149. if (gain_total > 0.0) {
  150. Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  151. GreedyChoice->offered_to = req_id;
  152. }
  153. }
  154. }
  155. #endif
  156. fprintf(log_file,"I will offer %d cores\n",Of_cores_num);
  157. return Of_cores_num;
  158. }
  159. /* Speedup is calculated correclty inside Speedup function */
  160. int offer_cores_updated(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id) {
  161. int Of_cores_num=0, min_dist=0, cur_dist=0, offered_cnt=0, counted_cores=0;
  162. float gain_total=0.1, base_receiver=0.0, base_giver=0.0, gain_receiver=0.0, loss_giver=0.0, new_gain=0.0;
  163. int Cores_receiver = req_app.num_of_cores, Cores_giver = my_app.num_of_cores;
  164. core_list *tmp, *GreedyChoice;
  165. if (my_idag != -1) {
  166. for (tmp=cores; tmp!=NULL; tmp=tmp->next) {
  167. counted_cores++;
  168. if (tmp->offered_to != -1) offered_cnt++;
  169. fprintf(log_file,"Core %d is offered to %d\n",tmp->core_id,tmp->offered_to);
  170. }
  171. if (offered_cnt == (counted_cores-2)) {
  172. fprintf(log_file,"I did not give up my only not offered core\n");
  173. return 0;
  174. }
  175. }
  176. if (my_idag == -1) {
  177. while (gain_total > 0.0) {
  178. gain_total = 0.0;
  179. GreedyChoice = NULL;//-1;
  180. min_dist = -1;
  181. base_giver = 0;
  182. tmp = cores->next;//very important!!! that way i avoid giving up my agent core
  183. while (tmp != NULL) {
  184. cur_dist = distance(req_reg.C, tmp->core_id);
  185. if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
  186. //Of_cores_num == 0 to be the first offered core
  187. //Cores_receiver == 0 to avoid providing the core to an non-initial core search
  188. if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 0) {
  189. if ((Cores_receiver + Of_cores_num) == 0) {
  190. gain_receiver = 1000; //0 sto init_app
  191. } else if ((Cores_receiver + Of_cores_num) == 1) {
  192. gain_receiver = 100; //no worker cores
  193. } else { /* (Cores_receiver + Of_cores_num) > 1 */
  194. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  195. gain_receiver = Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver;
  196. }
  197. loss_giver = 0;
  198. new_gain = gain_receiver - loss_giver;
  199. gain_total = new_gain;
  200. GreedyChoice = tmp;//->core_id;
  201. break;
  202. } else if (low_voltage_core[tmp->core_id] == 0) {
  203. if ((Cores_receiver + Of_cores_num) == 0) {
  204. gain_receiver = 1000; //0 sto init_app
  205. } else if ((Cores_receiver + Of_cores_num) == 1) {
  206. gain_receiver = 100; //no worker cores
  207. } else { /* (Cores_receiver + Of_cores_num) > 1 */
  208. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  209. gain_receiver = Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver;
  210. }
  211. loss_giver = 0;
  212. new_gain = gain_receiver - loss_giver;
  213. if (new_gain > gain_total){
  214. gain_total = new_gain;
  215. min_dist = cur_dist;
  216. GreedyChoice = tmp;
  217. } else if (new_gain == gain_total && cur_dist < min_dist) {
  218. min_dist = cur_dist;
  219. GreedyChoice = tmp;
  220. }
  221. }
  222. }
  223. tmp = tmp->next;
  224. }
  225. if (gain_total > 0.0) {
  226. Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  227. GreedyChoice->offered_to = req_id;
  228. }
  229. }
  230. }
  231. #ifndef GREEDY_MANAGER
  232. else {
  233. while (gain_total > 0.0) {
  234. gain_total = 0.0;
  235. GreedyChoice = NULL;//-1;
  236. min_dist = -1;
  237. base_giver = Speedup(my_app, Cores_giver - Of_cores_num);
  238. tmp = cores->next->next;//very important!!! that way i avoid giving up my only working core
  239. while (tmp != NULL) {
  240. if (core_inter_head[tmp->core_id] != NULL &&
  241. (core_inter_head[tmp->core_id]->type == INIT_WORK_NODE_PENDING || core_inter_head[tmp->core_id]->type == INIT_WORK_NODE)) {
  242. fprintf(log_file,"Core %d is about to start work type = %d\n",tmp->core_id,core_inter_head[tmp->core_id]->type);
  243. tmp = tmp->next;
  244. } else {
  245. cur_dist = distance(req_reg.C, tmp->core_id);
  246. if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
  247. if ((Cores_receiver + Of_cores_num) == 0) {
  248. gain_receiver = 1000; //0 sto init_app
  249. } else if ((Cores_receiver + Of_cores_num) == 1) {
  250. gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here
  251. } else { /* (Cores_receiver + Of_cores_num) > 1 */
  252. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  253. gain_receiver = Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver; /* + 1 is ommited due to workload convention */
  254. }
  255. loss_giver = base_giver - Speedup(my_app, Cores_giver - (Of_cores_num + 1));
  256. new_gain = gain_receiver - loss_giver;
  257. if (new_gain > gain_total){
  258. gain_total = new_gain;
  259. min_dist = cur_dist;
  260. GreedyChoice = tmp;
  261. } else if (new_gain == gain_total && cur_dist < min_dist) {
  262. min_dist = cur_dist;
  263. GreedyChoice = tmp;
  264. }
  265. }
  266. tmp = tmp->next;
  267. }
  268. }
  269. if (gain_total > 0.0) {
  270. #ifdef RESOURCE_ALGO_UPDATED_GENEROUS
  271. if ((Cores_receiver + Of_cores_num) > 1) {
  272. if (get_times(my_app, Cores_giver - (Of_cores_num + 1)) > get_times(req_app, Cores_receiver + Of_cores_num + 1)) {
  273. Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  274. GreedyChoice->offered_to = req_id;
  275. fprintf(log_file,"Accepted bargain with giver_times %d receiver_times %d, Cores_giver %d, Cores_receiver %d, Of_cores_num %d\n",
  276. get_times(my_app, Cores_giver - (Of_cores_num + 1)), get_times(req_app, Cores_receiver + Of_cores_num + 1), Cores_giver, Cores_receiver, Of_cores_num);
  277. } else {
  278. gain_total = 0.0;
  279. fprintf(log_file,"Refused bargain with giver_times %d receiver_times %d, Cores_giver %d, Cores_receiver %d, Of_cores_num %d\n",
  280. get_times(my_app, Cores_giver - (Of_cores_num + 1)), get_times(req_app, Cores_receiver + Of_cores_num + 1), Cores_giver, Cores_receiver, Of_cores_num);
  281. }
  282. } else {
  283. Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  284. GreedyChoice->offered_to = req_id;
  285. }
  286. #else
  287. if (get_times(my_app, Cores_giver - (Of_cores_num + 1)) > get_times(req_app, Cores_receiver + Of_cores_num + 1)) {
  288. Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  289. GreedyChoice->offered_to = req_id;
  290. fprintf(log_file,"Accepted bargain with giver_times %d receiver_times %d, Cores_giver %d, Cores_receiver %d, Of_cores_num %d\n",
  291. get_times(my_app, Cores_giver - (Of_cores_num + 1)), get_times(req_app, Cores_receiver + Of_cores_num + 1), Cores_giver, Cores_receiver, Of_cores_num);
  292. } else {
  293. gain_total = 0.0;
  294. fprintf(log_file,"Refused bargain with giver_times %d receiver_times %d, Cores_giver %d, Cores_receiver %d, Of_cores_num %d\n",
  295. get_times(my_app, Cores_giver - (Of_cores_num + 1)), get_times(req_app, Cores_receiver + Of_cores_num + 1), Cores_giver, Cores_receiver, Of_cores_num);
  296. }
  297. #endif
  298. }
  299. }
  300. }
  301. #endif
  302. fprintf(log_file,"I will offer %d cores\n",Of_cores_num);
  303. return Of_cores_num;
  304. }
  305. int offer_cores_fft_original(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id) {
  306. int Of_cores_num=0, min_dist=0, cur_dist=0;
  307. 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;
  308. int Cores_receiver = req_app.num_of_cores, Workers_giver = my_app.num_of_cores-1;
  309. core_list *tmp, *GreedyChoice;
  310. int offered_cnt=0, counted_cores=0;
  311. for (tmp=cores; tmp!=NULL; tmp=tmp->next) {
  312. if (distance(req_reg.C, tmp->core_id) <= req_reg.r) share_giver++;
  313. counted_cores++;
  314. if (tmp->offered_to != -1) offered_cnt++;
  315. fprintf(log_file,"Core %d is offered to %d\n",tmp->core_id,tmp->offered_to);
  316. }
  317. if (offered_cnt == (counted_cores-2) && my_idag != -1) {
  318. fprintf(log_file,"I did not give up my only not offered core\n");
  319. return 0;
  320. }
  321. if (Cores_receiver == 2) {
  322. fprintf(log_file,"Receiver already has two cores\n");
  323. return 0;
  324. }
  325. share_giver = share_giver / (float) region_count(req_reg);
  326. if (my_idag == -1) {
  327. while ((gain_total > 0.0) && ((Cores_receiver + Of_cores_num) <= 1)) {
  328. gain_total = 0.0;
  329. GreedyChoice = NULL;//-1;
  330. min_dist = -1;
  331. base_giver = 0;
  332. tmp = cores->next;//very important!!! that way i avoid giving up my agent core
  333. while (tmp != NULL) {
  334. cur_dist = distance(req_reg.C, tmp->core_id);
  335. if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
  336. //Of_cores_num == 0 to be the first offered core
  337. //Cores_receiver == 0 to avoid providing the core to an non-initial core search
  338. if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 0) {
  339. if ((Cores_receiver + Of_cores_num) == 0) {
  340. gain_receiver = 1000; //0 sto init_app
  341. } else if ((Cores_receiver + Of_cores_num) == 1) {
  342. gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here
  343. } else { /* (Cores_receiver + Of_cores_num) > 1 */
  344. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  345. //gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  346. gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  347. }
  348. loss_giver = 0;
  349. new_gain = gain_receiver - loss_giver;
  350. gain_total = new_gain;
  351. GreedyChoice = tmp;//->core_id;
  352. break;
  353. #ifdef LOW_VOLTAGE_ISLANDS_4
  354. } else if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 1) {
  355. if (Cores_receiver == 0 && Of_cores_num == 0) gain_receiver = 1000; //0 sto init_app
  356. else gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); /* +1 stands for the possibly offered core */
  357. loss_giver = 0;
  358. new_gain = gain_receiver - loss_giver;
  359. gain_total = new_gain;
  360. GreedyChoice = tmp;//->core_id;
  361. break;
  362. #endif
  363. } else if (low_voltage_core[tmp->core_id] == 0) {
  364. if ((Cores_receiver + Of_cores_num) == 0) {
  365. gain_receiver = 1000; //0 sto init_app
  366. } else if ((Cores_receiver + Of_cores_num) == 1) {
  367. gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here
  368. } else { /* (Cores_receiver + Of_cores_num) > 1 */
  369. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  370. gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  371. }
  372. loss_giver = 0;
  373. new_gain = gain_receiver - loss_giver;
  374. if (new_gain > gain_total){
  375. gain_total = new_gain;
  376. min_dist = cur_dist;
  377. GreedyChoice = tmp;//->core_id;
  378. } else if (new_gain == gain_total && cur_dist < min_dist) {
  379. //printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
  380. // node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
  381. min_dist = cur_dist;
  382. GreedyChoice = tmp;
  383. }
  384. }
  385. }
  386. tmp = tmp->next;
  387. }
  388. if (gain_total > 0.0) {
  389. Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  390. GreedyChoice->offered_to = req_id;
  391. }
  392. }
  393. /* FFT app requires only power of 2 exec cores plus its manager
  394. * I do not include higher than 5 because it will create no speedup
  395. */
  396. if ((Cores_receiver + Of_cores_num) == 4) {
  397. for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
  398. if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
  399. fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
  400. tmp->offered_to = -1;
  401. Of_cores_num--;
  402. break;
  403. }
  404. }
  405. }
  406. /*
  407. else if (Of_cores_num > 4) {
  408. }
  409. */
  410. }
  411. #ifndef GREEDY_MANAGER
  412. else {
  413. if (((Workers_giver == 4) && (Cores_receiver < 3)) || ((Workers_giver == 2) && (Cores_receiver < 2))) {
  414. while (gain_total > 0.0) {
  415. gain_total = 0.0;
  416. GreedyChoice = NULL;//-1;
  417. min_dist = -1;
  418. base_giver = Speedup(my_app, Workers_giver - Of_cores_num);
  419. tmp = cores->next->next;//very important!!! that way i avoid giving up my only working core
  420. while (tmp != NULL) {
  421. if (core_inter_head[tmp->core_id] != NULL &&
  422. (core_inter_head[tmp->core_id]->type == INIT_WORK_NODE_PENDING || core_inter_head[tmp->core_id]->type == INIT_WORK_NODE)) {
  423. fprintf(log_file,"Core %d is about to start work type = %d\n",tmp->core_id,core_inter_head[tmp->core_id]->type);
  424. tmp = tmp->next;
  425. } else {
  426. cur_dist = distance(req_reg.C, tmp->core_id);
  427. if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
  428. if ((Cores_receiver + Of_cores_num) == 0) {
  429. gain_receiver = 1000; //0 sto init_app
  430. } else if ((Cores_receiver + Of_cores_num) == 1) {
  431. gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here
  432. } else { /* (Cores_receiver + Of_cores_num) > 1 */
  433. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  434. gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); /* + 1 is ommited due to workload convention */
  435. }
  436. loss_giver = base_giver - Speedup(my_app, Workers_giver - (Of_cores_num + 1));
  437. new_gain = gain_receiver - loss_giver;
  438. if (new_gain > gain_total){
  439. gain_total = new_gain;
  440. min_dist = cur_dist;
  441. GreedyChoice = tmp;//->core_id;
  442. } else if (new_gain == gain_total && cur_dist < min_dist) {
  443. //printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
  444. // node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
  445. min_dist = cur_dist;
  446. GreedyChoice = tmp;
  447. }
  448. }
  449. tmp = tmp->next;
  450. }
  451. }
  452. if (gain_total > 0.0) {
  453. Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  454. GreedyChoice->offered_to = req_id;
  455. }
  456. }
  457. if ((Cores_receiver + Of_cores_num) == 4) {
  458. for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
  459. if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
  460. fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
  461. tmp->offered_to = -1;
  462. Of_cores_num--;
  463. break;
  464. }
  465. }
  466. }
  467. }
  468. }
  469. #endif
  470. /* FFT app requires only power of 2 exec cores plus its manager */
  471. /*
  472. if (my_idag == -1) {
  473. if (executed_app == FFT) {
  474. if (Of_cores_num == 3) {
  475. for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
  476. if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
  477. fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
  478. tmp->offered_to = -1;
  479. Of_cores_num--;
  480. break;
  481. }
  482. }
  483. } else if (Of_cores_num > 4) {
  484. }
  485. }
  486. */
  487. return Of_cores_num;
  488. }
  489. int offer_cores_fft_updated(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id) { /* 26.5.2017 - I am not sure if it is correct. Has not been used to measure F_min_max0 */
  490. int Of_cores_num=0, min_dist=0, cur_dist=0;
  491. 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;
  492. int Cores_receiver = req_app.num_of_cores, Workers_giver = my_app.num_of_cores-1;
  493. core_list *tmp, *GreedyChoice;
  494. int offered_cnt=0, counted_cores=0;
  495. for (tmp=cores; tmp!=NULL; tmp=tmp->next) {
  496. if (distance(req_reg.C, tmp->core_id) <= req_reg.r) share_giver++;
  497. counted_cores++;
  498. if (tmp->offered_to != -1) offered_cnt++;
  499. fprintf(log_file,"Core %d is offered to %d\n",tmp->core_id,tmp->offered_to);
  500. }
  501. if (offered_cnt == (counted_cores-2) && my_idag != -1) {
  502. fprintf(log_file,"I did not give up my only not offered core\n");
  503. return 0;
  504. }
  505. share_giver = share_giver / (float) region_count(req_reg);
  506. if (my_idag == -1) {
  507. while ((gain_total > 0.0) && ((Cores_receiver + Of_cores_num) <= 2)) {
  508. gain_total = 0.0;
  509. GreedyChoice = NULL;//-1;
  510. min_dist = -1;
  511. base_giver = 0;
  512. tmp = cores->next;//very important!!! that way i avoid giving up my agent core
  513. while (tmp != NULL) {
  514. cur_dist = distance(req_reg.C, tmp->core_id);
  515. if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
  516. //Of_cores_num == 0 to be the first offered core
  517. //Cores_receiver == 0 to avoid providing the core to an non-initial core search
  518. if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 0) {
  519. if ((Cores_receiver + Of_cores_num) == 0) {
  520. gain_receiver = 1000; //0 sto init_app
  521. } else if ((Cores_receiver + Of_cores_num) == 1) {
  522. gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here
  523. } else { /* (Cores_receiver + Of_cores_num) > 1 */
  524. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  525. //gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  526. gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  527. }
  528. loss_giver = 0;
  529. new_gain = gain_receiver - loss_giver;
  530. gain_total = new_gain;
  531. GreedyChoice = tmp;//->core_id;
  532. break;
  533. #ifdef LOW_VOLTAGE_ISLANDS_4
  534. } else if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 1) {
  535. if (Cores_receiver == 0 && Of_cores_num == 0) gain_receiver = 1000; //0 sto init_app
  536. else gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); /* +1 stands for the possibly offered core */
  537. loss_giver = 0;
  538. new_gain = gain_receiver - loss_giver;
  539. gain_total = new_gain;
  540. GreedyChoice = tmp;//->core_id;
  541. break;
  542. #endif
  543. } else if (low_voltage_core[tmp->core_id] == 0) {
  544. if ((Cores_receiver + Of_cores_num) == 0) {
  545. gain_receiver = 1000; //0 sto init_app
  546. } else if ((Cores_receiver + Of_cores_num) == 1) {
  547. gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here
  548. } else { /* (Cores_receiver + Of_cores_num) > 1 */
  549. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  550. gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  551. }
  552. loss_giver = 0;
  553. new_gain = gain_receiver - loss_giver;
  554. if (new_gain > gain_total){
  555. gain_total = new_gain;
  556. min_dist = cur_dist;
  557. GreedyChoice = tmp;//->core_id;
  558. } else if (new_gain == gain_total && cur_dist < min_dist) {
  559. //printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
  560. // node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
  561. min_dist = cur_dist;
  562. GreedyChoice = tmp;
  563. }
  564. }
  565. }
  566. tmp = tmp->next;
  567. }
  568. if (gain_total > 0.0) {
  569. Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  570. GreedyChoice->offered_to = req_id;
  571. }
  572. }
  573. /* FFT app requires only power of 2 exec cores plus its manager
  574. * I do not include higher than 5 because it will create no speedup
  575. */
  576. if ((Cores_receiver + Of_cores_num) == 4) {
  577. for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
  578. if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
  579. fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
  580. tmp->offered_to = -1;
  581. Of_cores_num--;
  582. break;
  583. }
  584. }
  585. }
  586. /*
  587. else if (Of_cores_num > 4) {
  588. }
  589. */
  590. }
  591. #ifndef GREEDY_MANAGER
  592. else {
  593. if (((Workers_giver == 4) && (Cores_receiver < 3)) || ((Workers_giver == 2) && (Cores_receiver < 2))) {
  594. while (gain_total > 0.0) {
  595. gain_total = 0.0;
  596. GreedyChoice = NULL;//-1;
  597. min_dist = -1;
  598. base_giver = Speedup(my_app, Workers_giver - Of_cores_num);
  599. tmp = cores->next->next;//very important!!! that way i avoid giving up my only working core
  600. while (tmp != NULL) {
  601. if (core_inter_head[tmp->core_id] != NULL &&
  602. (core_inter_head[tmp->core_id]->type == INIT_WORK_NODE_PENDING || core_inter_head[tmp->core_id]->type == INIT_WORK_NODE)) {
  603. fprintf(log_file,"Core %d is about to start work type = %d\n",tmp->core_id,core_inter_head[tmp->core_id]->type);
  604. tmp = tmp->next;
  605. } else {
  606. cur_dist = distance(req_reg.C, tmp->core_id);
  607. if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
  608. if ((Cores_receiver + Of_cores_num) == 0) {
  609. gain_receiver = 1000; //0 sto init_app
  610. } else if ((Cores_receiver + Of_cores_num) == 1) {
  611. gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here
  612. } else { /* (Cores_receiver + Of_cores_num) > 1 */
  613. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  614. gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); /* + 1 is ommited due to workload convention */
  615. }
  616. loss_giver = base_giver - Speedup(my_app, Workers_giver - (Of_cores_num + 1));
  617. new_gain = gain_receiver - loss_giver;
  618. if (new_gain > gain_total){
  619. gain_total = new_gain;
  620. min_dist = cur_dist;
  621. GreedyChoice = tmp;//->core_id;
  622. } else if (new_gain == gain_total && cur_dist < min_dist) {
  623. //printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
  624. // node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
  625. min_dist = cur_dist;
  626. GreedyChoice = tmp;
  627. }
  628. }
  629. tmp = tmp->next;
  630. }
  631. }
  632. if (gain_total > 0.0) {
  633. Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  634. GreedyChoice->offered_to = req_id;
  635. }
  636. }
  637. if ((Cores_receiver + Of_cores_num) == 4) {
  638. for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
  639. if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
  640. fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
  641. tmp->offered_to = -1;
  642. Of_cores_num--;
  643. break;
  644. }
  645. }
  646. }
  647. }
  648. }
  649. #endif
  650. /* FFT app requires only power of 2 exec cores plus its manager */
  651. /*
  652. if (my_idag == -1) {
  653. if (executed_app == FFT) {
  654. if (Of_cores_num == 3) {
  655. for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
  656. if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
  657. fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
  658. tmp->offered_to = -1;
  659. Of_cores_num--;
  660. break;
  661. }
  662. }
  663. } else if (Of_cores_num > 4) {
  664. }
  665. }
  666. */
  667. return Of_cores_num;
  668. }