source_common.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012 Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <string.h>
  17. #include <pthread.h>
  18. #include <starpu.h>
  19. #include <core/task.h>
  20. #include <core/sched_policy.h>
  21. #include <drivers/driver_common/driver_common.h>
  22. #include <datawizard/coherency.h>
  23. #include <datawizard/interfaces/data_interface.h>
  24. #include <drivers/mp_common/mp_common.h>
  25. /* Finalize the execution of a task by a worker*/
  26. static int _starpu_src_common_finalize_job (struct _starpu_job *j, struct _starpu_worker *worker)
  27. {
  28. uint32_t mask = 0;
  29. int profiling = starpu_profiling_status_get();
  30. struct timespec codelet_end;
  31. _starpu_driver_end_job(worker, j, worker->perf_arch, &codelet_end, 0,
  32. profiling);
  33. int count = worker->current_rank;
  34. /* If it's a combined worker, we check if it's the last one of his combined */
  35. if(j->task_size > 1)
  36. {
  37. struct _starpu_combined_worker * cb_worker = _starpu_get_combined_worker_struct(worker->combined_workerid);
  38. pthread_mutex_lock(&cb_worker->count_mutex);
  39. count = cb_worker->count--;
  40. if(count == 0)
  41. cb_worker->count = cb_worker->worker_size - 1;
  42. pthread_mutex_unlock(&cb_worker->count_mutex);
  43. }
  44. /* Finalize the execution */
  45. if(count == 0)
  46. {
  47. _starpu_driver_update_job_feedback(j, worker, worker->perf_arch,
  48. &j->cl_start, &codelet_end,
  49. profiling);
  50. _starpu_push_task_output (j, mask);
  51. _starpu_handle_job_termination(j);
  52. }
  53. return 0;
  54. }
  55. /* Complete the execution of the job */
  56. static int _starpu_src_common_process_completed_job(struct _starpu_worker_set *workerset, void * arg, int arg_size)
  57. {
  58. int coreid;
  59. STARPU_ASSERT(sizeof(coreid) == arg_size);
  60. coreid = *(int *) arg;
  61. struct _starpu_worker *worker = &workerset->workers[coreid];
  62. struct _starpu_job *j = _starpu_get_job_associated_to_task(worker->current_task);
  63. struct _starpu_worker * old_worker = _starpu_get_local_worker_key();
  64. _starpu_set_local_worker_key(worker);
  65. _starpu_src_common_finalize_job (j, worker);
  66. _starpu_set_local_worker_key(old_worker);
  67. worker->current_task = NULL;
  68. return 0;
  69. }
  70. /* Tell the scheduler when the execution has begun */
  71. static void _starpu_src_common_pre_exec(void * arg, int arg_size)
  72. {
  73. int cb_workerid, i;
  74. STARPU_ASSERT(sizeof(cb_workerid) == arg_size);
  75. cb_workerid = *(int *) arg;
  76. struct _starpu_combined_worker *combined_worker = _starpu_get_combined_worker_struct(cb_workerid);
  77. for(i=0; i < combined_worker->worker_size; i++)
  78. {
  79. struct _starpu_worker * worker = _starpu_get_worker_struct(combined_worker->combined_workerid[i]);
  80. _starpu_set_local_worker_key(worker);
  81. _starpu_sched_pre_exec_hook(worker->current_task);
  82. }
  83. }
  84. /* recv a message and handle asynchronous message
  85. * return 0 if the message has not been handle (it's certainly mean that it's a synchronous message)
  86. * return 1 if the message has been handle
  87. */
  88. static int _starpu_src_common_handle_async(const struct _starpu_mp_node *node STARPU_ATTRIBUTE_UNUSED,
  89. void * arg, int arg_size,
  90. enum _starpu_mp_command answer)
  91. {
  92. struct _starpu_worker_set * worker_set=NULL;
  93. switch(answer)
  94. {
  95. case STARPU_EXECUTION_COMPLETED:
  96. worker_set = _starpu_get_worker_struct(starpu_worker_get_id())->set;
  97. _starpu_src_common_process_completed_job(worker_set, arg, arg_size);
  98. break;
  99. case STARPU_PRE_EXECUTION:
  100. _starpu_src_common_pre_exec(arg,arg_size);
  101. break;
  102. default:
  103. return 0;
  104. break;
  105. }
  106. return 1;
  107. }
  108. /* Handle all message which have been stored in the message_queue */
  109. static void _starpu_src_common_handle_stored_async(struct _starpu_mp_node *node)
  110. {
  111. STARPU_PTHREAD_MUTEX_LOCK(&node->message_queue_mutex);
  112. /* while the list is not empty */
  113. while(!mp_message_list_empty(node->message_queue))
  114. {
  115. /* We pop a message and handle it */
  116. struct mp_message * message = mp_message_list_pop_back(node->message_queue);
  117. _starpu_src_common_handle_async(node, message->buffer,
  118. message->size, message->type);
  119. mp_message_delete(message);
  120. }
  121. STARPU_PTHREAD_MUTEX_UNLOCK(&node->message_queue_mutex);
  122. }
  123. /* Store a message if is asynchronous
  124. * return 1 if the message has been stored
  125. * return 0 if the message is unknown or synchrone */
  126. int _starpu_src_common_store_message(struct _starpu_mp_node *node,
  127. void * arg, int arg_size, enum _starpu_mp_command answer)
  128. {
  129. struct mp_message * message = NULL;
  130. switch(answer)
  131. {
  132. case STARPU_EXECUTION_COMPLETED:
  133. case STARPU_PRE_EXECUTION:
  134. message = mp_message_new();
  135. message->type = answer;
  136. memcpy(message->buffer, arg, arg_size);
  137. message->size = arg_size;
  138. STARPU_PTHREAD_MUTEX_LOCK(&node->message_queue_mutex);
  139. mp_message_list_push_front(node->message_queue,message);
  140. STARPU_PTHREAD_MUTEX_UNLOCK(&node->message_queue_mutex);
  141. return 1;
  142. break;
  143. default:
  144. return 0;
  145. break;
  146. }
  147. }
  148. /* Store all asynchronous messages and return when a synchronous message is received */
  149. static enum _starpu_mp_command _starpu_src_common_wait_command_sync(struct _starpu_mp_node *node,
  150. void ** arg, int* arg_size)
  151. {
  152. enum _starpu_mp_command answer;
  153. int is_sync = 0;
  154. while(!is_sync)
  155. {
  156. answer = _starpu_mp_common_recv_command(node, arg, arg_size);
  157. if(!_starpu_src_common_store_message(node,*arg,*arg_size,answer))
  158. is_sync=1;
  159. }
  160. return answer;
  161. }
  162. /* Handle a asynchrone message and return a error if a synchronous message is received */
  163. static void _starpu_src_common_recv_async(struct _starpu_mp_node * node)
  164. {
  165. enum _starpu_mp_command answer;
  166. void *arg;
  167. int arg_size;
  168. answer = _starpu_mp_common_recv_command(node, &arg, &arg_size);
  169. if(!_starpu_src_common_handle_async(node,arg,arg_size,answer))
  170. {
  171. printf("incorrect commande: unknown command or sync command");
  172. STARPU_ASSERT(0);
  173. }
  174. }
  175. /* Handle all asynchrone message while a completed execution message from a specific worker has been receive */
  176. enum _starpu_mp_command _starpu_src_common_wait_completed_execution(struct _starpu_mp_node *node, int devid, void **arg, int * arg_size)
  177. {
  178. enum _starpu_mp_command answer;
  179. int completed = 0;
  180. /*While the waited completed execution message has not been receive*/
  181. while(!completed)
  182. {
  183. answer = _starpu_mp_common_recv_command (node, arg, arg_size);
  184. if(answer == STARPU_EXECUTION_COMPLETED)
  185. {
  186. int coreid;
  187. STARPU_ASSERT(sizeof(coreid) == *arg_size);
  188. coreid = *(int *) *arg;
  189. if(devid == coreid)
  190. completed = 1;
  191. else
  192. if(!_starpu_src_common_store_message(node, *arg, *arg_size, answer))
  193. /* We receive a unknown or asynchronous message */
  194. STARPU_ASSERT(0);
  195. }
  196. else
  197. {
  198. if(!_starpu_src_common_store_message(node, *arg, *arg_size, answer))
  199. /* We receive a unknown or asynchronous message */
  200. STARPU_ASSERT(0);
  201. }
  202. }
  203. return answer;
  204. }
  205. /* Send a request to the sink NODE for the number of cores on it. */
  206. int _starpu_src_common_sink_nbcores (const struct _starpu_mp_node *node, int *buf)
  207. {
  208. enum _starpu_mp_command answer;
  209. void *arg;
  210. int arg_size = sizeof (int);
  211. _starpu_mp_common_send_command (node, STARPU_SINK_NBCORES, NULL, 0);
  212. answer = _starpu_mp_common_recv_command (node, &arg, &arg_size);
  213. STARPU_ASSERT (answer == STARPU_ANSWER_SINK_NBCORES && arg_size == sizeof (int));
  214. memcpy (buf, arg, arg_size);
  215. return 0;
  216. }
  217. /* Send a request to the sink linked to NODE for the pointer to the
  218. * function defined by FUNC_NAME.
  219. * In case of success, it returns 0 and FUNC_PTR contains the pointer ;
  220. * else it returns -ESPIPE if the function was not found.
  221. */
  222. int _starpu_src_common_lookup(struct _starpu_mp_node *node,
  223. void (**func_ptr)(void), const char *func_name)
  224. {
  225. enum _starpu_mp_command answer;
  226. void *arg;
  227. int arg_size;
  228. /* strlen ignore the terminating '\0' */
  229. arg_size = (strlen(func_name) + 1) * sizeof(char);
  230. //_STARPU_DEBUG("Looking up %s\n", func_name);
  231. _starpu_mp_common_send_command(node, STARPU_LOOKUP, (void *) func_name,
  232. arg_size);
  233. answer = _starpu_src_common_wait_command_sync(node, (void **) &arg,
  234. &arg_size);
  235. if (answer == STARPU_ERROR_LOOKUP)
  236. {
  237. _STARPU_DISP("Error looking up symbol %s\n", func_name);
  238. return -ESPIPE;
  239. }
  240. /* We have to be sure the device answered the right question and the
  241. * answer has the right size */
  242. STARPU_ASSERT(answer == STARPU_ANSWER_LOOKUP);
  243. STARPU_ASSERT(arg_size == sizeof(*func_ptr));
  244. memcpy(func_ptr, arg, arg_size);
  245. //_STARPU_DEBUG("got %p\n", *func_ptr);
  246. return 0;
  247. }
  248. /* Send a message to the sink to execute a kernel.
  249. * The message sent has the form below :
  250. * [Function pointer on sink, number of interfaces, interfaces
  251. * (union _starpu_interface), cl_arg]
  252. */
  253. /* Launch the execution of the function KERNEL points to on the sink linked
  254. * to NODE. Returns 0 in case of success, -EINVAL if kernel is an invalid
  255. * pointer.
  256. * Data interfaces in task are send to the sink.
  257. */
  258. int _starpu_src_common_execute_kernel(struct _starpu_mp_node *node,
  259. void (*kernel)(void), unsigned coreid,
  260. enum starpu_codelet_type type,
  261. int is_parallel_task, int cb_workerid,
  262. starpu_data_handle_t *handles,
  263. void **interfaces,
  264. unsigned nb_interfaces,
  265. void *cl_arg, size_t cl_arg_size)
  266. {
  267. void *buffer, *buffer_ptr, *arg =NULL;
  268. int buffer_size = 0, arg_size =0;
  269. unsigned i;
  270. buffer_size = sizeof(kernel) + sizeof(coreid) + sizeof(type)
  271. + sizeof(nb_interfaces) + nb_interfaces * sizeof(union _starpu_interface) + sizeof(is_parallel_task);
  272. /*if the task is paralle*/
  273. if(is_parallel_task)
  274. {
  275. buffer_size += sizeof(cb_workerid);
  276. }
  277. /* If the user didn't give any cl_arg, there is no need to send it */
  278. if (cl_arg)
  279. {
  280. STARPU_ASSERT(cl_arg_size);
  281. buffer_size += cl_arg_size;
  282. }
  283. /* We give to send_command a buffer we just allocated, which contains
  284. * a pointer to the function (sink-side), core on which execute this
  285. * function (sink-side), number of interfaces we send,
  286. * an array of generic (union) interfaces and the value of cl_arg */
  287. buffer_ptr = buffer = (void *) malloc(buffer_size);
  288. *(void(**)(void)) buffer = kernel;
  289. buffer_ptr += sizeof(kernel);
  290. *(enum starpu_codelet_type *) buffer_ptr = type;
  291. buffer_ptr += sizeof(type);
  292. *(int *) buffer_ptr = is_parallel_task;
  293. buffer_ptr += sizeof(is_parallel_task);
  294. if(is_parallel_task)
  295. {
  296. *(int *) buffer_ptr = cb_workerid ;
  297. buffer_ptr += sizeof(cb_workerid);
  298. }
  299. *(unsigned *) buffer_ptr = coreid;
  300. buffer_ptr += sizeof(coreid);
  301. *(unsigned *) buffer_ptr = nb_interfaces;
  302. buffer_ptr += sizeof(nb_interfaces);
  303. /* Message-passing execution is a particular case as the codelet is
  304. * executed on a sink with a different memory, whereas a codelet is
  305. * executed on the host part for the other accelerators.
  306. * Thus we need to send a copy of each interface on the MP device */
  307. for (i = 0; i < nb_interfaces; i++)
  308. {
  309. starpu_data_handle_t handle = handles[i];
  310. memcpy (buffer_ptr, interfaces[i],
  311. handle->ops->interface_size);
  312. /* The sink side has no mean to get the type of each
  313. * interface, we use a union to make it generic and permit the
  314. * sink to go through the array */
  315. buffer_ptr += sizeof(union _starpu_interface);
  316. }
  317. if (cl_arg)
  318. memcpy(buffer_ptr, cl_arg, cl_arg_size);
  319. _starpu_mp_common_send_command(node, STARPU_EXECUTE, buffer, buffer_size);
  320. enum _starpu_mp_command answer = _starpu_src_common_wait_command_sync(node, &arg, &arg_size);
  321. if (answer == STARPU_ERROR_EXECUTE)
  322. return -EINVAL;
  323. STARPU_ASSERT(answer == STARPU_EXECUTION_SUBMITTED);
  324. free(buffer);
  325. return 0;
  326. }
  327. /* Get the information and call the function to send to the sink a message to execute the task*/
  328. static int _starpu_src_common_execute(struct _starpu_job *j,
  329. struct _starpu_worker *worker,
  330. struct _starpu_mp_node * node)
  331. {
  332. int ret;
  333. uint32_t mask = 0;
  334. STARPU_ASSERT(j);
  335. struct starpu_task *task = j->task;
  336. int profiling = starpu_profiling_status_get();
  337. STARPU_ASSERT(task);
  338. if (worker->current_rank == 0)
  339. {
  340. ret = _starpu_fetch_task_input(j, mask);
  341. if (ret != 0)
  342. {
  343. /* there was not enough memory, so the input of
  344. * the codelet cannot be fetched ... put the
  345. * codelet back, and try it later */
  346. return -EAGAIN;
  347. }
  348. }
  349. void (*kernel)(void) = node->get_kernel_from_job(node,j);
  350. _starpu_driver_start_job(worker, j, &j->cl_start, 0, profiling);
  351. //_STARPU_DEBUG("\nworkerid:%d, rank:%d, type:%d, cb_workerid:%d, task_size:%d\n\n",worker->devid,worker->current_rank,task->cl->type,j->combined_workerid,j->task_size);
  352. _starpu_src_common_execute_kernel(node, kernel, worker->devid, task->cl->type,
  353. (j->task_size > 1),
  354. j->combined_workerid, task->handles,
  355. task->interfaces, task->cl->nbuffers,
  356. task->cl_arg, task->cl_arg_size);
  357. return 0;
  358. }
  359. /* Send a request to the sink linked to the MP_NODE to allocate SIZE bytes on
  360. * the sink.
  361. * In case of success, it returns 0 and *ADDR contains the address of the
  362. * allocated area ;
  363. * else it returns 1 if the allocation fail.
  364. */
  365. int _starpu_src_common_allocate(struct _starpu_mp_node *mp_node,
  366. void **addr, size_t size)
  367. {
  368. enum _starpu_mp_command answer;
  369. void *arg;
  370. int arg_size;
  371. _starpu_mp_common_send_command(mp_node, STARPU_ALLOCATE, &size,
  372. sizeof(size));
  373. answer = _starpu_src_common_wait_command_sync(mp_node, &arg, &arg_size);
  374. if (answer == STARPU_ERROR_ALLOCATE)
  375. return 1;
  376. STARPU_ASSERT(answer == STARPU_ANSWER_ALLOCATE &&
  377. arg_size == sizeof(*addr));
  378. memcpy(addr, arg, arg_size);
  379. return 0;
  380. }
  381. /* Send a request to the sink linked to the MP_NODE to deallocate the memory
  382. * area pointed by ADDR.
  383. */
  384. void _starpu_src_common_free(const struct _starpu_mp_node *mp_node,
  385. void *addr)
  386. {
  387. _starpu_mp_common_send_command(mp_node, STARPU_FREE, &addr, sizeof(addr));
  388. }
  389. /* Send SIZE bytes pointed by SRC to DST on the sink linked to the MP_NODE.
  390. */
  391. int _starpu_src_common_copy_host_to_sink(const struct _starpu_mp_node *mp_node,
  392. void *src, void *dst, size_t size)
  393. {
  394. struct _starpu_mp_transfer_command cmd = {size, dst};
  395. _starpu_mp_common_send_command(mp_node, STARPU_RECV_FROM_HOST, &cmd, sizeof(cmd));
  396. mp_node->dt_send(mp_node, src, size);
  397. return 0;
  398. }
  399. /* Receive SIZE bytes pointed by SRC on the sink linked to the MP_NODE and store them in DST.
  400. */
  401. int _starpu_src_common_copy_sink_to_host(const struct _starpu_mp_node *mp_node,
  402. void *src, void *dst, size_t size)
  403. {
  404. struct _starpu_mp_transfer_command cmd = {size, src};
  405. _starpu_mp_common_send_command(mp_node, STARPU_SEND_TO_HOST, &cmd, sizeof(cmd));
  406. mp_node->dt_recv(mp_node, dst, size);
  407. return 0;
  408. }
  409. /* Tell the sink linked to SRC_NODE to send SIZE bytes of data pointed by SRC
  410. * to the sink linked to DST_NODE. The latter store them in DST.
  411. */
  412. int _starpu_src_common_copy_sink_to_sink(const struct _starpu_mp_node *src_node,
  413. const struct _starpu_mp_node *dst_node, void *src, void *dst, size_t size)
  414. {
  415. enum _starpu_mp_command answer;
  416. void *arg;
  417. int arg_size;
  418. struct _starpu_mp_transfer_command_to_device cmd = {dst_node->peer_id, size, src};
  419. /* Tell source to send data to dest. */
  420. _starpu_mp_common_send_command(src_node, STARPU_SEND_TO_SINK, &cmd, sizeof(cmd));
  421. cmd.devid = src_node->peer_id;
  422. cmd.size = size;
  423. cmd.addr = dst;
  424. /* Tell dest to receive data from source. */
  425. _starpu_mp_common_send_command(dst_node, STARPU_RECV_FROM_SINK, &cmd, sizeof(cmd));
  426. /* Wait for answer from dest to know wether transfer is finished. */
  427. answer = _starpu_mp_common_recv_command(dst_node, &arg, &arg_size);
  428. STARPU_ASSERT(answer == STARPU_TRANSFER_COMPLETE);
  429. return 0;
  430. }
  431. /* 5 functions to determine the executable to run on the device (MIC, SCC,
  432. * MPI).
  433. */
  434. static void _starpu_src_common_cat_3(char *final, const char *first,
  435. const char *second, const char *third)
  436. {
  437. strcpy(final, first);
  438. strcat(final, second);
  439. strcat(final, third);
  440. }
  441. static void _starpu_src_common_cat_2(char *final, const char *first, const char *second)
  442. {
  443. _starpu_src_common_cat_3(final, first, second, "");
  444. }
  445. static void _starpu_src_common_dir_cat(char *final, const char *dir, const char *file)
  446. {
  447. if (file[0] == '/')
  448. ++file;
  449. size_t size = strlen(dir);
  450. if (dir[size - 1] == '/')
  451. _starpu_src_common_cat_2(final, dir, file);
  452. else
  453. _starpu_src_common_cat_3(final, dir, "/", file);
  454. }
  455. static int _starpu_src_common_test_suffixes(char *located_file_name, const char *base, const char **suffixes)
  456. {
  457. unsigned int i;
  458. for (i = 0; suffixes[i] != NULL; ++i)
  459. {
  460. _starpu_src_common_cat_2(located_file_name, base, suffixes[i]);
  461. if (access(located_file_name, R_OK) == 0)
  462. return 0;
  463. }
  464. return 1;
  465. }
  466. int _starpu_src_common_locate_file(char *located_file_name,
  467. const char *env_file_name, const char *env_mic_path,
  468. const char *config_file_name, const char *actual_file_name,
  469. const char **suffixes)
  470. {
  471. if (env_file_name != NULL)
  472. {
  473. if (access(env_file_name, R_OK) == 0)
  474. {
  475. strcpy(located_file_name, env_file_name);
  476. return 0;
  477. }
  478. else if(env_mic_path != NULL)
  479. {
  480. _starpu_src_common_dir_cat(located_file_name, env_mic_path, env_file_name);
  481. return access(located_file_name, R_OK);
  482. }
  483. }
  484. else if (config_file_name != NULL)
  485. {
  486. if (access(config_file_name, R_OK) == 0)
  487. {
  488. strcpy(located_file_name, config_file_name);
  489. return 0;
  490. }
  491. else if (env_mic_path != NULL)
  492. {
  493. _starpu_src_common_dir_cat(located_file_name, env_mic_path, config_file_name);
  494. return access(located_file_name, R_OK);
  495. }
  496. }
  497. else if (actual_file_name != NULL)
  498. {
  499. if (_starpu_src_common_test_suffixes(located_file_name, actual_file_name, suffixes) == 0)
  500. return 0;
  501. if (env_mic_path != NULL)
  502. {
  503. char actual_cpy[1024];
  504. strcpy(actual_cpy, actual_file_name);
  505. char *last = strrchr(actual_cpy, '/');
  506. while (last != NULL)
  507. {
  508. char tmp[1024];
  509. _starpu_src_common_dir_cat(tmp, env_mic_path, last);
  510. if (access(tmp, R_OK) == 0)
  511. {
  512. strcpy(located_file_name, tmp);
  513. return 0;
  514. }
  515. if (_starpu_src_common_test_suffixes(located_file_name, tmp, suffixes) == 0)
  516. return 0;
  517. *last = '\0';
  518. char *last_tmp = strrchr(actual_cpy, '/');
  519. *last = '/';
  520. last = last_tmp;
  521. }
  522. }
  523. }
  524. return 1;
  525. }
  526. /* Send workers to the sink node
  527. */
  528. static void _starpu_src_common_send_workers(struct _starpu_mp_node * node, int baseworkerid, int nworkers)
  529. {
  530. struct _starpu_machine_config *config = _starpu_get_machine_config();
  531. int worker_size = sizeof(struct _starpu_worker)*nworkers;
  532. int combined_worker_size = STARPU_NMAX_COMBINEDWORKERS*sizeof(struct _starpu_combined_worker);
  533. int msg[5];
  534. msg[0] = nworkers;
  535. msg[1] = worker_size;
  536. msg[2] = combined_worker_size;
  537. msg[3] = baseworkerid;
  538. msg[4] = starpu_worker_get_count();
  539. /* tell the sink node that we will send him all workers */
  540. _starpu_mp_common_send_command(node, STARPU_SYNC_WORKERS,
  541. &msg, sizeof(msg));
  542. /* Send all worker to the sink node */
  543. node->dt_send(node,&config->workers[baseworkerid],worker_size);
  544. /* Send all combined workers to the sink node */
  545. node->dt_send(node, &config->combined_workers,combined_worker_size);
  546. }
  547. /* Function looping on the source node */
  548. void _starpu_src_common_worker(struct _starpu_worker_set * worker_set,
  549. unsigned baseworkerid,
  550. struct _starpu_mp_node * mp_node)
  551. {
  552. unsigned memnode = worker_set->workers[0].memory_node;
  553. struct starpu_task **tasks = malloc(sizeof(struct starpu_task *)*worker_set->nworkers);
  554. _starpu_src_common_send_workers(mp_node, baseworkerid, worker_set->nworkers);
  555. /*main loop*/
  556. while (_starpu_machine_is_running())
  557. {
  558. int res;
  559. struct _starpu_job * j;
  560. _STARPU_TRACE_START_PROGRESS(memnode);
  561. _starpu_datawizard_progress(memnode, 1);
  562. _STARPU_TRACE_END_PROGRESS(memnode);
  563. /* Handle message which have been store */
  564. _starpu_src_common_handle_stored_async(mp_node);
  565. /* poll the device for completed jobs.*/
  566. while(mp_node->mp_recv_is_ready(mp_node))
  567. _starpu_src_common_recv_async(mp_node);
  568. /* get task for each worker*/
  569. res = _starpu_get_multi_worker_task(worker_set->workers, tasks, worker_set->nworkers);
  570. /*if at least one worker have pop a task*/
  571. if(res != 0)
  572. {
  573. unsigned i;
  574. for(i=0; i<worker_set->nworkers; i++)
  575. {
  576. if(tasks[i] != NULL)
  577. {
  578. j = _starpu_get_job_associated_to_task(tasks[i]);
  579. _starpu_set_local_worker_key(&worker_set->workers[i]);
  580. res = _starpu_src_common_execute(j, &worker_set->workers[i], mp_node);
  581. switch (res)
  582. {
  583. case 0:
  584. /* The task task has been launched with no error */
  585. break;
  586. case -EAGAIN:
  587. _STARPU_DISP("ouch, Xeon Phi could not actually run task %p, putting it back...\n", tasks[i]);
  588. _starpu_push_task_to_workers(tasks[i]);
  589. STARPU_ABORT();
  590. continue;
  591. break;
  592. default:
  593. STARPU_ASSERT(0);
  594. }
  595. }
  596. }
  597. }
  598. }
  599. free(tasks);
  600. _starpu_handle_all_pending_node_data_requests(memnode);
  601. /* In case there remains some memory that was automatically
  602. * allocated by StarPU, we release it now. Note that data
  603. * coherency is not maintained anymore at that point ! */
  604. _starpu_free_all_automatically_allocated_buffers(memnode);
  605. }