source_common.c 21 KB

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