resource_negotiation.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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. #else
  18. return offer_cores_original(cores, req_app, req_reg, Offered_cores, req_id);
  19. #endif
  20. }
  21. /* Speedup is calculated correclty inside Speedup function */
  22. int offer_cores_original(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id) {
  23. int Of_cores_num=0, min_dist=0, cur_dist=0;
  24. 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;
  25. int Cores_receiver = req_app.num_of_cores, Cores_giver = my_app.num_of_cores;
  26. core_list *tmp, *GreedyChoice;
  27. int offered_cnt=0, counted_cores=0;
  28. for (tmp=cores; tmp!=NULL; tmp=tmp->next) {
  29. if (distance(req_reg.C, tmp->core_id) <= req_reg.r) share_giver++;
  30. counted_cores++;
  31. if (tmp->offered_to != -1) offered_cnt++;
  32. fprintf(log_file,"Core %d is offered to %d\n",tmp->core_id,tmp->offered_to);
  33. }
  34. fprintf(log_file,"Proceeding\n");
  35. fflush(log_file);
  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. fflush(log_file);
  39. return 0;
  40. }
  41. share_giver = share_giver / (float) region_count(req_reg);
  42. if (my_idag == -1) {
  43. while (gain_total > 0.0) {
  44. gain_total = 0.0;
  45. GreedyChoice = NULL;//-1;
  46. min_dist = -1;
  47. base_giver = 0;
  48. tmp = cores->next;//very important!!! that way i avoid giving up my agent core
  49. while (tmp != NULL) {
  50. cur_dist = distance(req_reg.C, tmp->core_id);
  51. if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
  52. //Of_cores_num == 0 to be the first offered core
  53. //Cores_receiver == 0 to avoid providing the core to an non-initial core search
  54. if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 0) {
  55. if ((Cores_receiver + Of_cores_num) == 0) {
  56. gain_receiver = 1000; //0 sto init_app
  57. } else if ((Cores_receiver + Of_cores_num) == 1) {
  58. gain_receiver = 100; //no worker cores
  59. } else { /* (Cores_receiver + Of_cores_num) > 1 */
  60. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  61. //gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  62. gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  63. }
  64. loss_giver = 0;
  65. new_gain = gain_receiver - loss_giver;
  66. gain_total = new_gain;
  67. GreedyChoice = tmp;//->core_id;
  68. break;
  69. #ifdef LOW_VOLTAGE_ISLANDS_4
  70. } else if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 1) {
  71. if (Cores_receiver == 0 && Of_cores_num == 0) gain_receiver = 1000; //0 sto init_app
  72. else gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  73. loss_giver = 0;
  74. new_gain = gain_receiver - loss_giver;
  75. gain_total = new_gain;
  76. GreedyChoice = tmp;//->core_id;
  77. break;
  78. #endif
  79. } else if (low_voltage_core[tmp->core_id] == 0) {
  80. if ((Cores_receiver + Of_cores_num) == 0) {
  81. gain_receiver = 1000; //0 sto init_app
  82. } else if ((Cores_receiver + Of_cores_num) == 1) {
  83. gain_receiver = 100; //no worker cores
  84. } else { /* (Cores_receiver + Of_cores_num) > 1 */
  85. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  86. gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  87. }
  88. loss_giver = 0;
  89. new_gain = gain_receiver - loss_giver;
  90. if (new_gain > gain_total){
  91. gain_total = new_gain;
  92. min_dist = cur_dist;
  93. GreedyChoice = tmp;//->core_id;
  94. } else if (new_gain == gain_total && cur_dist < min_dist) {
  95. //printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
  96. // node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
  97. min_dist = cur_dist;
  98. GreedyChoice = tmp;
  99. }
  100. }
  101. }
  102. tmp = tmp->next;
  103. }
  104. if (gain_total > 0.0) {
  105. Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  106. GreedyChoice->offered_to = req_id;
  107. }
  108. }
  109. }
  110. #ifndef GREEDY_MANAGER
  111. else {
  112. while (gain_total > 0.0) {
  113. gain_total = 0.0;
  114. GreedyChoice = NULL;//-1;
  115. min_dist = -1;
  116. base_giver = Speedup(my_app, Cores_giver - Of_cores_num);
  117. tmp = cores->next->next;//very important!!! that way i avoid giving up my only working core
  118. while (tmp != NULL) {
  119. if (core_inter_head[tmp->core_id] != NULL &&
  120. (core_inter_head[tmp->core_id]->type == INIT_WORK_NODE_PENDING || core_inter_head[tmp->core_id]->type == INIT_WORK_NODE)) {
  121. fprintf(log_file,"Core %d is about to start work type = %d\n",tmp->core_id,core_inter_head[tmp->core_id]->type);
  122. fflush(log_file);
  123. tmp = tmp->next;
  124. } else {
  125. cur_dist = distance(req_reg.C, tmp->core_id);
  126. if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
  127. if ((Cores_receiver + Of_cores_num) == 0) {
  128. gain_receiver = 1000; //0 sto init_app
  129. } else if ((Cores_receiver + Of_cores_num) == 1) {
  130. gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here
  131. } else { /* (Cores_receiver + Of_cores_num) > 1 */
  132. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  133. gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); /* + 1 is ommited due to workload convention */
  134. }
  135. loss_giver = base_giver - Speedup(my_app, Cores_giver - (Of_cores_num + 1));
  136. new_gain = gain_receiver - loss_giver;
  137. if (new_gain > gain_total){
  138. gain_total = new_gain;
  139. min_dist = cur_dist;
  140. GreedyChoice = tmp;//->core_id;
  141. } else if (new_gain == gain_total && cur_dist < min_dist) {
  142. //printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
  143. // node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
  144. min_dist = cur_dist;
  145. GreedyChoice = tmp;
  146. }
  147. }
  148. tmp = tmp->next;
  149. }
  150. }
  151. if (gain_total > 0.0) {
  152. Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  153. GreedyChoice->offered_to = req_id;
  154. }
  155. }
  156. }
  157. #endif
  158. fprintf(log_file,"I will offer %d cores\n",Of_cores_num);
  159. fflush(log_file);
  160. return Of_cores_num;
  161. }
  162. /* Speedup is calculated correclty inside Speedup function */
  163. int offer_cores_updated(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id) {
  164. int Of_cores_num=0, min_dist=0, cur_dist=0, offered_cnt=0, counted_cores=0;
  165. 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;
  166. int Cores_receiver = req_app.num_of_cores, Cores_giver = my_app.num_of_cores;
  167. core_list *tmp, *GreedyChoice;
  168. if (my_idag != -1) {
  169. for (tmp=cores; tmp!=NULL; tmp=tmp->next) {
  170. counted_cores++;
  171. if (tmp->offered_to != -1) offered_cnt++;
  172. fprintf(log_file,"Core %d is offered to %d\n",tmp->core_id,tmp->offered_to);
  173. }
  174. if (offered_cnt == (counted_cores-2)) {
  175. fprintf(log_file,"I did not give up my only not offered core\n");
  176. fflush(log_file);
  177. return 0;
  178. }
  179. }
  180. if (my_idag == -1) {
  181. while (gain_total > 0.0) {
  182. gain_total = 0.0;
  183. GreedyChoice = NULL;//-1;
  184. min_dist = -1;
  185. base_giver = 0;
  186. tmp = cores->next;//very important!!! that way i avoid giving up my agent core
  187. while (tmp != NULL) {
  188. cur_dist = distance(req_reg.C, tmp->core_id);
  189. if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
  190. //Of_cores_num == 0 to be the first offered core
  191. //Cores_receiver == 0 to avoid providing the core to an non-initial core search
  192. if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 0) {
  193. if ((Cores_receiver + Of_cores_num) == 0) {
  194. gain_receiver = 1000; //0 sto init_app
  195. } else if ((Cores_receiver + Of_cores_num) == 1) {
  196. gain_receiver = 100; //no worker cores
  197. } else { /* (Cores_receiver + Of_cores_num) > 1 */
  198. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  199. gain_receiver = Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver;
  200. }
  201. loss_giver = 0;
  202. new_gain = gain_receiver - loss_giver;
  203. gain_total = new_gain;
  204. GreedyChoice = tmp;//->core_id;
  205. break;
  206. } else if (low_voltage_core[tmp->core_id] == 0) {
  207. if ((Cores_receiver + Of_cores_num) == 0) {
  208. gain_receiver = 1000; //0 sto init_app
  209. } else if ((Cores_receiver + Of_cores_num) == 1) {
  210. gain_receiver = 100; //no worker cores
  211. } else { /* (Cores_receiver + Of_cores_num) > 1 */
  212. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  213. gain_receiver = Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver;
  214. }
  215. loss_giver = 0;
  216. new_gain = gain_receiver - loss_giver;
  217. if (new_gain > gain_total){
  218. gain_total = new_gain;
  219. min_dist = cur_dist;
  220. GreedyChoice = tmp;
  221. } else if (new_gain == gain_total && cur_dist < min_dist) {
  222. min_dist = cur_dist;
  223. GreedyChoice = tmp;
  224. }
  225. }
  226. }
  227. tmp = tmp->next;
  228. }
  229. if (gain_total > 0.0) {
  230. Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  231. GreedyChoice->offered_to = req_id;
  232. }
  233. }
  234. }
  235. #ifndef GREEDY_MANAGER
  236. else {
  237. while (gain_total > 0.0) {
  238. gain_total = 0.0;
  239. GreedyChoice = NULL;//-1;
  240. min_dist = -1;
  241. base_giver = Speedup(my_app, Cores_giver - Of_cores_num);
  242. tmp = cores->next->next;//very important!!! that way i avoid giving up my only working core
  243. while (tmp != NULL) {
  244. if (core_inter_head[tmp->core_id] != NULL &&
  245. (core_inter_head[tmp->core_id]->type == INIT_WORK_NODE_PENDING || core_inter_head[tmp->core_id]->type == INIT_WORK_NODE)) {
  246. fprintf(log_file,"Core %d is about to start work type = %d\n",tmp->core_id,core_inter_head[tmp->core_id]->type);
  247. fflush(log_file);
  248. tmp = tmp->next;
  249. } else {
  250. cur_dist = distance(req_reg.C, tmp->core_id);
  251. if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
  252. if ((Cores_receiver + Of_cores_num) == 0) {
  253. gain_receiver = 1000; //0 sto init_app
  254. } else if ((Cores_receiver + Of_cores_num) == 1) {
  255. gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here
  256. } else { /* (Cores_receiver + Of_cores_num) > 1 */
  257. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  258. gain_receiver = Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver; /* + 1 is ommited due to workload convention */
  259. }
  260. loss_giver = base_giver - Speedup(my_app, Cores_giver - (Of_cores_num + 1));
  261. new_gain = gain_receiver - loss_giver;
  262. if (new_gain > gain_total){
  263. gain_total = new_gain;
  264. min_dist = cur_dist;
  265. GreedyChoice = tmp;
  266. } else if (new_gain == gain_total && cur_dist < min_dist) {
  267. min_dist = cur_dist;
  268. GreedyChoice = tmp;
  269. }
  270. }
  271. tmp = tmp->next;
  272. }
  273. }
  274. if (gain_total > 0.0) {
  275. if (get_times(my_app, Cores_giver - (Of_cores_num + 1)) > get_times(req_app, Cores_receiver + Of_cores_num + 1)) {
  276. Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  277. GreedyChoice->offered_to = req_id;
  278. fprintf(log_file,"Accepted bargain with giver_times %d receiver_times %d\n",get_times(my_app, Cores_giver - (Of_cores_num + 1)),get_times(req_app, Cores_receiver + Of_cores_num + 1));
  279. fflush(log_file);
  280. } else {
  281. gain_total = 0.0;
  282. fprintf(log_file,"Abandoning because I will finish earlier!\n");
  283. fflush(log_file);
  284. }
  285. }
  286. }
  287. }
  288. #endif
  289. fprintf(log_file,"I will offer %d cores\n",Of_cores_num);
  290. fflush(log_file);
  291. return Of_cores_num;
  292. }
  293. // int offer_cores_fft(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id) {
  294. // int Of_cores_num=0, min_dist=0, cur_dist=0;
  295. // 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;
  296. // int Cores_receiver = req_app.num_of_cores, Workers_giver = my_app.num_of_cores-1;
  297. // core_list *tmp, *GreedyChoice;
  298. // int offered_cnt=0, counted_cores=0;
  299. //
  300. // for (tmp=cores; tmp!=NULL; tmp=tmp->next) {
  301. // if (distance(req_reg.C, tmp->core_id) <= req_reg.r) share_giver++;
  302. // counted_cores++;
  303. // if (tmp->offered_to != -1) offered_cnt++;
  304. // fprintf(log_file,"Core %d is offered to %d\n",tmp->core_id,tmp->offered_to);
  305. // }
  306. // fflush(log_file);
  307. //
  308. // if (offered_cnt == (counted_cores-2) && my_idag != -1) {
  309. // fprintf(log_file,"I did not give up my only not offered core\n");
  310. // fflush(log_file);
  311. // return 0;
  312. // }
  313. // share_giver = share_giver / (float) region_count(req_reg);
  314. //
  315. // if (my_idag == -1) {
  316. // while (gain_total > 0.0) {
  317. // gain_total = 0.0;
  318. // GreedyChoice = NULL;//-1;
  319. // min_dist = -1;
  320. // base_giver = 0;
  321. // tmp = cores->next;//very important!!! that way i avoid giving up my agent core
  322. //
  323. // while (tmp != NULL) {
  324. // cur_dist = distance(req_reg.C, tmp->core_id);
  325. // if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
  326. // //Of_cores_num == 0 to be the first offered core
  327. // //Cores_receiver == 0 to avoid providing the core to an non-initial core search
  328. // if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 0) {
  329. // if ((Cores_receiver + Of_cores_num) == 0) {
  330. // gain_receiver = 1000; //0 sto init_app
  331. // } else if ((Cores_receiver + Of_cores_num) == 1) {
  332. // gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here
  333. // } else { /* (Cores_receiver + Of_cores_num) > 1 */
  334. // base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num - 1);
  335. // gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num) - base_receiver); /* + 1 is ommited due to workload convention */
  336. // }
  337. //
  338. // loss_giver = 0;
  339. // new_gain = gain_receiver - loss_giver;
  340. // gain_total = new_gain;
  341. // GreedyChoice = tmp;//->core_id;
  342. // break;
  343. // #ifdef LOW_VOLTAGE_ISLANDS_4
  344. // } else if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 1) {
  345. // if (Cores_receiver == 0 && Of_cores_num == 0) gain_receiver = 1000; //0 sto init_app
  346. // else gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); /* +1 stands for the possibly offered core */
  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. // #endif
  354. // } else if (low_voltage_core[tmp->core_id] == 0) {
  355. // if ((Cores_receiver + Of_cores_num) == 0) {
  356. // gain_receiver = 1000; //0 sto init_app
  357. // } else if ((Cores_receiver + Of_cores_num) == 1) {
  358. // gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here
  359. // } else { /* (Cores_receiver + Of_cores_num) > 1 */
  360. // base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num - 1);
  361. // gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num) - base_receiver); /* + 1 is ommited due to workload convention */
  362. // }
  363. //
  364. // loss_giver = 0;
  365. // new_gain = gain_receiver - loss_giver;
  366. // if (new_gain > gain_total){
  367. // gain_total = new_gain;
  368. // min_dist = cur_dist;
  369. // GreedyChoice = tmp;//->core_id;
  370. // } else if (new_gain == gain_total && cur_dist < min_dist) {
  371. // //printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
  372. // // node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
  373. // min_dist = cur_dist;
  374. // GreedyChoice = tmp;
  375. // }
  376. // }
  377. // }
  378. //
  379. // tmp = tmp->next;
  380. // }
  381. //
  382. // if (gain_total > 0.0) {
  383. // Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  384. // GreedyChoice->offered_to = req_id;
  385. // }
  386. // }
  387. //
  388. // /* FFT app requires only power of 2 exec cores plus its manager
  389. // * I do not include higher than 5 because it will create no speedup
  390. // */
  391. // if ((Cores_receiver + Of_cores_num) == 4) {
  392. // for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
  393. // if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
  394. // fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
  395. // tmp->offered_to = -1;
  396. // Of_cores_num--;
  397. // break;
  398. // }
  399. // }
  400. // }
  401. // /*
  402. // else if (Of_cores_num > 4) {
  403. //
  404. // }
  405. // */
  406. // }
  407. // #ifndef GREEDY_MANAGER
  408. // else {
  409. //
  410. // if (((Workers_giver == 4) && (Cores_receiver < 3)) || ((Workers_giver == 2) && (Cores_receiver < 2))) {
  411. //
  412. // while (gain_total > 0.0) {
  413. // gain_total = 0.0;
  414. // GreedyChoice = NULL;//-1;
  415. // min_dist = -1;
  416. // base_giver = Speedup(my_app, Workers_giver - Of_cores_num);
  417. //
  418. // tmp = cores->next->next;//very important!!! that way i avoid giving up my only working core
  419. //
  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. // fflush(log_file);
  425. // tmp = tmp->next;
  426. // } else {
  427. // cur_dist = distance(req_reg.C, tmp->core_id);
  428. // if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
  429. // if ((Cores_receiver + Of_cores_num) == 0) {
  430. // gain_receiver = 1000; //0 sto init_app
  431. // } else if ((Cores_receiver + Of_cores_num) == 1) {
  432. // gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here
  433. // } else { /* (Cores_receiver + Of_cores_num) > 1 */
  434. // base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num - 1);
  435. // gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num) - base_receiver); /* + 1 is ommited due to workload convention */
  436. // }
  437. //
  438. // loss_giver = base_giver - Speedup(my_app, Workers_giver - Of_cores_num - 1);
  439. //
  440. // new_gain = gain_receiver - loss_giver;
  441. // if (new_gain > gain_total){
  442. // gain_total = new_gain;
  443. // min_dist = cur_dist;
  444. // GreedyChoice = tmp;//->core_id;
  445. // } else if (new_gain == gain_total && cur_dist < min_dist) {
  446. // //printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
  447. // // node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
  448. // min_dist = cur_dist;
  449. // GreedyChoice = tmp;
  450. // }
  451. // }
  452. //
  453. // tmp = tmp->next;
  454. // }
  455. // }
  456. //
  457. // if (gain_total > 0.0) {
  458. // Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  459. // GreedyChoice->offered_to = req_id;
  460. // }
  461. // }
  462. //
  463. // if ((Cores_receiver + Of_cores_num) == 4) {
  464. // for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
  465. // if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
  466. // fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
  467. // tmp->offered_to = -1;
  468. // Of_cores_num--;
  469. // break;
  470. // }
  471. // }
  472. // }
  473. //
  474. // }
  475. // }
  476. // #endif
  477. //
  478. // /* FFT app requires only power of 2 exec cores plus its manager */
  479. // /*
  480. // if (my_idag == -1) {
  481. //
  482. // if (executed_app == FFT) {
  483. // if (Of_cores_num == 3) {
  484. // for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
  485. // if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
  486. // fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
  487. // tmp->offered_to = -1;
  488. // Of_cores_num--;
  489. // break;
  490. // }
  491. // }
  492. // } else if (Of_cores_num > 4) {
  493. //
  494. // }
  495. // }
  496. // */
  497. // return Of_cores_num;
  498. // }
  499. int offer_cores_fft_original(core_list *cores, app req_app, region req_reg, int *Offered_cores, int req_id) {
  500. int Of_cores_num=0, min_dist=0, cur_dist=0;
  501. 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;
  502. int Cores_receiver = req_app.num_of_cores, Workers_giver = my_app.num_of_cores-1;
  503. core_list *tmp, *GreedyChoice;
  504. int offered_cnt=0, counted_cores=0;
  505. for (tmp=cores; tmp!=NULL; tmp=tmp->next) {
  506. if (distance(req_reg.C, tmp->core_id) <= req_reg.r) share_giver++;
  507. counted_cores++;
  508. if (tmp->offered_to != -1) offered_cnt++;
  509. fprintf(log_file,"Core %d is offered to %d\n",tmp->core_id,tmp->offered_to);
  510. }
  511. fflush(log_file);
  512. if (offered_cnt == (counted_cores-2) && my_idag != -1) {
  513. fprintf(log_file,"I did not give up my only not offered core\n");
  514. fflush(log_file);
  515. return 0;
  516. }
  517. if (Cores_receiver == 2) {
  518. fprintf(log_file,"Receiver already has two cores\n");
  519. fflush(log_file);
  520. return 0;
  521. }
  522. share_giver = share_giver / (float) region_count(req_reg);
  523. if (my_idag == -1) {
  524. while ((gain_total > 0.0) && ((Cores_receiver + Of_cores_num) <= 1)) {
  525. gain_total = 0.0;
  526. GreedyChoice = NULL;//-1;
  527. min_dist = -1;
  528. base_giver = 0;
  529. tmp = cores->next;//very important!!! that way i avoid giving up my agent core
  530. while (tmp != NULL) {
  531. cur_dist = distance(req_reg.C, tmp->core_id);
  532. if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
  533. //Of_cores_num == 0 to be the first offered core
  534. //Cores_receiver == 0 to avoid providing the core to an non-initial core search
  535. if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 0) {
  536. if ((Cores_receiver + Of_cores_num) == 0) {
  537. gain_receiver = 1000; //0 sto init_app
  538. } else if ((Cores_receiver + Of_cores_num) == 1) {
  539. gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here
  540. } else { /* (Cores_receiver + Of_cores_num) > 1 */
  541. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  542. //gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  543. gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  544. }
  545. loss_giver = 0;
  546. new_gain = gain_receiver - loss_giver;
  547. gain_total = new_gain;
  548. GreedyChoice = tmp;//->core_id;
  549. break;
  550. #ifdef LOW_VOLTAGE_ISLANDS_4
  551. } else if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 1) {
  552. if (Cores_receiver == 0 && Of_cores_num == 0) gain_receiver = 1000; //0 sto init_app
  553. else gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); /* +1 stands for the possibly offered core */
  554. loss_giver = 0;
  555. new_gain = gain_receiver - loss_giver;
  556. gain_total = new_gain;
  557. GreedyChoice = tmp;//->core_id;
  558. break;
  559. #endif
  560. } else if (low_voltage_core[tmp->core_id] == 0) {
  561. if ((Cores_receiver + Of_cores_num) == 0) {
  562. gain_receiver = 1000; //0 sto init_app
  563. } else if ((Cores_receiver + Of_cores_num) == 1) {
  564. gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here
  565. } else { /* (Cores_receiver + Of_cores_num) > 1 */
  566. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  567. gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  568. }
  569. loss_giver = 0;
  570. new_gain = gain_receiver - loss_giver;
  571. if (new_gain > gain_total){
  572. gain_total = new_gain;
  573. min_dist = cur_dist;
  574. GreedyChoice = tmp;//->core_id;
  575. } else if (new_gain == gain_total && cur_dist < min_dist) {
  576. //printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
  577. // node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
  578. min_dist = cur_dist;
  579. GreedyChoice = tmp;
  580. }
  581. }
  582. }
  583. tmp = tmp->next;
  584. }
  585. if (gain_total > 0.0) {
  586. Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  587. GreedyChoice->offered_to = req_id;
  588. }
  589. }
  590. /* FFT app requires only power of 2 exec cores plus its manager
  591. * I do not include higher than 5 because it will create no speedup
  592. */
  593. if ((Cores_receiver + Of_cores_num) == 4) {
  594. for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
  595. if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
  596. fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
  597. tmp->offered_to = -1;
  598. Of_cores_num--;
  599. break;
  600. }
  601. }
  602. }
  603. /*
  604. else if (Of_cores_num > 4) {
  605. }
  606. */
  607. }
  608. #ifndef GREEDY_MANAGER
  609. else {
  610. if (((Workers_giver == 4) && (Cores_receiver < 3)) || ((Workers_giver == 2) && (Cores_receiver < 2))) {
  611. while (gain_total > 0.0) {
  612. gain_total = 0.0;
  613. GreedyChoice = NULL;//-1;
  614. min_dist = -1;
  615. base_giver = Speedup(my_app, Workers_giver - Of_cores_num);
  616. tmp = cores->next->next;//very important!!! that way i avoid giving up my only working core
  617. while (tmp != NULL) {
  618. if (core_inter_head[tmp->core_id] != NULL &&
  619. (core_inter_head[tmp->core_id]->type == INIT_WORK_NODE_PENDING || core_inter_head[tmp->core_id]->type == INIT_WORK_NODE)) {
  620. fprintf(log_file,"Core %d is about to start work type = %d\n",tmp->core_id,core_inter_head[tmp->core_id]->type);
  621. fflush(log_file);
  622. tmp = tmp->next;
  623. } else {
  624. cur_dist = distance(req_reg.C, tmp->core_id);
  625. if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
  626. if ((Cores_receiver + Of_cores_num) == 0) {
  627. gain_receiver = 1000; //0 sto init_app
  628. } else if ((Cores_receiver + Of_cores_num) == 1) {
  629. gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here
  630. } else { /* (Cores_receiver + Of_cores_num) > 1 */
  631. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  632. gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); /* + 1 is ommited due to workload convention */
  633. }
  634. loss_giver = base_giver - Speedup(my_app, Workers_giver - (Of_cores_num + 1));
  635. new_gain = gain_receiver - loss_giver;
  636. if (new_gain > gain_total){
  637. gain_total = new_gain;
  638. min_dist = cur_dist;
  639. GreedyChoice = tmp;//->core_id;
  640. } else if (new_gain == gain_total && cur_dist < min_dist) {
  641. //printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
  642. // node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
  643. min_dist = cur_dist;
  644. GreedyChoice = tmp;
  645. }
  646. }
  647. tmp = tmp->next;
  648. }
  649. }
  650. if (gain_total > 0.0) {
  651. Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  652. GreedyChoice->offered_to = req_id;
  653. }
  654. }
  655. if ((Cores_receiver + Of_cores_num) == 4) {
  656. for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
  657. if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
  658. fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
  659. tmp->offered_to = -1;
  660. Of_cores_num--;
  661. break;
  662. }
  663. }
  664. }
  665. }
  666. }
  667. #endif
  668. /* FFT app requires only power of 2 exec cores plus its manager */
  669. /*
  670. if (my_idag == -1) {
  671. if (executed_app == FFT) {
  672. if (Of_cores_num == 3) {
  673. for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
  674. if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
  675. fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
  676. tmp->offered_to = -1;
  677. Of_cores_num--;
  678. break;
  679. }
  680. }
  681. } else if (Of_cores_num > 4) {
  682. }
  683. }
  684. */
  685. return Of_cores_num;
  686. }
  687. 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 */
  688. int Of_cores_num=0, min_dist=0, cur_dist=0;
  689. 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;
  690. int Cores_receiver = req_app.num_of_cores, Workers_giver = my_app.num_of_cores-1;
  691. core_list *tmp, *GreedyChoice;
  692. int offered_cnt=0, counted_cores=0;
  693. for (tmp=cores; tmp!=NULL; tmp=tmp->next) {
  694. if (distance(req_reg.C, tmp->core_id) <= req_reg.r) share_giver++;
  695. counted_cores++;
  696. if (tmp->offered_to != -1) offered_cnt++;
  697. fprintf(log_file,"Core %d is offered to %d\n",tmp->core_id,tmp->offered_to);
  698. }
  699. fflush(log_file);
  700. if (offered_cnt == (counted_cores-2) && my_idag != -1) {
  701. fprintf(log_file,"I did not give up my only not offered core\n");
  702. fflush(log_file);
  703. return 0;
  704. }
  705. share_giver = share_giver / (float) region_count(req_reg);
  706. if (my_idag == -1) {
  707. while ((gain_total > 0.0) && ((Cores_receiver + Of_cores_num) <= 2)) {
  708. gain_total = 0.0;
  709. GreedyChoice = NULL;//-1;
  710. min_dist = -1;
  711. base_giver = 0;
  712. tmp = cores->next;//very important!!! that way i avoid giving up my agent core
  713. while (tmp != NULL) {
  714. cur_dist = distance(req_reg.C, tmp->core_id);
  715. if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
  716. //Of_cores_num == 0 to be the first offered core
  717. //Cores_receiver == 0 to avoid providing the core to an non-initial core search
  718. if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 0) {
  719. if ((Cores_receiver + Of_cores_num) == 0) {
  720. gain_receiver = 1000; //0 sto init_app
  721. } else if ((Cores_receiver + Of_cores_num) == 1) {
  722. gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here
  723. } else { /* (Cores_receiver + Of_cores_num) > 1 */
  724. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  725. //gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  726. gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  727. }
  728. loss_giver = 0;
  729. new_gain = gain_receiver - loss_giver;
  730. gain_total = new_gain;
  731. GreedyChoice = tmp;//->core_id;
  732. break;
  733. #ifdef LOW_VOLTAGE_ISLANDS_4
  734. } else if (low_voltage_core[tmp->core_id] && Of_cores_num == 0 && Cores_receiver == 1) {
  735. if (Cores_receiver == 0 && Of_cores_num == 0) gain_receiver = 1000; //0 sto init_app
  736. else gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); /* +1 stands for the possibly offered core */
  737. loss_giver = 0;
  738. new_gain = gain_receiver - loss_giver;
  739. gain_total = new_gain;
  740. GreedyChoice = tmp;//->core_id;
  741. break;
  742. #endif
  743. } else if (low_voltage_core[tmp->core_id] == 0) {
  744. if ((Cores_receiver + Of_cores_num) == 0) {
  745. gain_receiver = 1000; //0 sto init_app
  746. } else if ((Cores_receiver + Of_cores_num) == 1) {
  747. gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here
  748. } else { /* (Cores_receiver + Of_cores_num) > 1 */
  749. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  750. gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver);
  751. }
  752. loss_giver = 0;
  753. new_gain = gain_receiver - loss_giver;
  754. if (new_gain > gain_total){
  755. gain_total = new_gain;
  756. min_dist = cur_dist;
  757. GreedyChoice = tmp;//->core_id;
  758. } else if (new_gain == gain_total && cur_dist < min_dist) {
  759. //printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
  760. // node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
  761. min_dist = cur_dist;
  762. GreedyChoice = tmp;
  763. }
  764. }
  765. }
  766. tmp = tmp->next;
  767. }
  768. if (gain_total > 0.0) {
  769. Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  770. GreedyChoice->offered_to = req_id;
  771. }
  772. }
  773. /* FFT app requires only power of 2 exec cores plus its manager
  774. * I do not include higher than 5 because it will create no speedup
  775. */
  776. if ((Cores_receiver + Of_cores_num) == 4) {
  777. for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
  778. if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
  779. fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
  780. tmp->offered_to = -1;
  781. Of_cores_num--;
  782. break;
  783. }
  784. }
  785. }
  786. /*
  787. else if (Of_cores_num > 4) {
  788. }
  789. */
  790. }
  791. #ifndef GREEDY_MANAGER
  792. else {
  793. if (((Workers_giver == 4) && (Cores_receiver < 3)) || ((Workers_giver == 2) && (Cores_receiver < 2))) {
  794. while (gain_total > 0.0) {
  795. gain_total = 0.0;
  796. GreedyChoice = NULL;//-1;
  797. min_dist = -1;
  798. base_giver = Speedup(my_app, Workers_giver - Of_cores_num);
  799. tmp = cores->next->next;//very important!!! that way i avoid giving up my only working core
  800. while (tmp != NULL) {
  801. if (core_inter_head[tmp->core_id] != NULL &&
  802. (core_inter_head[tmp->core_id]->type == INIT_WORK_NODE_PENDING || core_inter_head[tmp->core_id]->type == INIT_WORK_NODE)) {
  803. fprintf(log_file,"Core %d is about to start work type = %d\n",tmp->core_id,core_inter_head[tmp->core_id]->type);
  804. fflush(log_file);
  805. tmp = tmp->next;
  806. } else {
  807. cur_dist = distance(req_reg.C, tmp->core_id);
  808. if (tmp->offered_to == -1 && cur_dist <= req_reg.r) {
  809. if ((Cores_receiver + Of_cores_num) == 0) {
  810. gain_receiver = 1000; //0 sto init_app
  811. } else if ((Cores_receiver + Of_cores_num) == 1) {
  812. gain_receiver = 100; //no worker cores -- in case I have only one worker core then I should not be here
  813. } else { /* (Cores_receiver + Of_cores_num) > 1 */
  814. base_receiver = Speedup(req_app, Cores_receiver + Of_cores_num);
  815. gain_receiver = share_giver * (Speedup(req_app, Cores_receiver + Of_cores_num + 1) - base_receiver); /* + 1 is ommited due to workload convention */
  816. }
  817. loss_giver = base_giver - Speedup(my_app, Workers_giver - (Of_cores_num + 1));
  818. new_gain = gain_receiver - loss_giver;
  819. if (new_gain > gain_total){
  820. gain_total = new_gain;
  821. min_dist = cur_dist;
  822. GreedyChoice = tmp;//->core_id;
  823. } else if (new_gain == gain_total && cur_dist < min_dist) {
  824. //printf("I am %d and i change offer to %d with cores %d->%d with distances %d->%d\n",
  825. // node_id,req_id,GreedyChoice->core_id,tmp->core_id,min_dist,cur_dist);
  826. min_dist = cur_dist;
  827. GreedyChoice = tmp;
  828. }
  829. }
  830. tmp = tmp->next;
  831. }
  832. }
  833. if (gain_total > 0.0) {
  834. Offered_cores[Of_cores_num++] = GreedyChoice->core_id;
  835. GreedyChoice->offered_to = req_id;
  836. }
  837. }
  838. if ((Cores_receiver + Of_cores_num) == 4) {
  839. for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
  840. if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
  841. fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
  842. tmp->offered_to = -1;
  843. Of_cores_num--;
  844. break;
  845. }
  846. }
  847. }
  848. }
  849. }
  850. #endif
  851. /* FFT app requires only power of 2 exec cores plus its manager */
  852. /*
  853. if (my_idag == -1) {
  854. if (executed_app == FFT) {
  855. if (Of_cores_num == 3) {
  856. for (tmp = my_cores->next; tmp!=NULL; tmp=tmp->next) {
  857. if (tmp->core_id == Offered_cores[Of_cores_num-1]) {
  858. fprintf(log_file,"Abandoning offered core %d because FFT needs 2 cores\n",tmp->core_id);
  859. tmp->offered_to = -1;
  860. Of_cores_num--;
  861. break;
  862. }
  863. }
  864. } else if (Of_cores_num > 4) {
  865. }
  866. }
  867. */
  868. return Of_cores_num;
  869. }