driver_scc_source.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 <starpu.h>
  17. #include <starpu_profiling.h>
  18. #include <core/sched_policy.h>
  19. #include <core/task.h>
  20. #include <common/uthash.h>
  21. #include <RCCE.h>
  22. #include <drivers/driver_common/driver_common.h>
  23. #include <drivers/mp_common/source_common.h>
  24. #include <drivers/scc/driver_scc_common.h>
  25. #include <drivers/scc/driver_scc_source.h>
  26. static struct _starpu_mp_node *scc_mp_nodes[STARPU_MAXSCCDEVS];
  27. struct _starpu_scc_kernel
  28. {
  29. UT_hash_handle hh;
  30. char *name;
  31. starpu_scc_kernel_t func[STARPU_MAXSCCDEVS];
  32. } *kernels;
  33. starpu_pthread_mutex_t htbl_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  34. static struct _starpu_mp_node *_starpu_scc_src_memory_node_to_mp_node(unsigned memory_node)
  35. {
  36. int devid = _starpu_memory_node_get_devid(memory_node);
  37. STARPU_ASSERT(devid < STARPU_MAXSCCDEVS);
  38. return scc_mp_nodes[devid];
  39. }
  40. static void _starpu_scc_src_init_context(int devid)
  41. {
  42. /* Let's create the node structure, we'll communicate with the peer
  43. * through RCCE thanks to it */
  44. scc_mp_nodes[devid] = _starpu_mp_common_node_create(STARPU_SCC_SOURCE, devid);
  45. }
  46. static void _starpu_scc_src_deinit_context(int devid)
  47. {
  48. _starpu_mp_common_send_command(scc_mp_nodes[devid], STARPU_EXIT, NULL, 0);
  49. _starpu_mp_common_node_destroy(scc_mp_nodes[devid]);
  50. }
  51. static int _starpu_scc_src_execute_job(struct _starpu_job *j, struct _starpu_worker *args)
  52. {
  53. int ret;
  54. uint32_t mask = 0;
  55. STARPU_ASSERT(j);
  56. struct starpu_task *task = j->task;
  57. struct timespec codelet_start, codelet_end;
  58. int profiling = starpu_profiling_status_get();
  59. unsigned calibrate_model = 0;
  60. STARPU_ASSERT(task);
  61. struct starpu_codelet *cl = task->cl;
  62. STARPU_ASSERT(cl);
  63. if (cl->model && cl->model->benchmarking)
  64. calibrate_model = 1;
  65. ret = _starpu_fetch_task_input(j, mask);
  66. if (ret != 0)
  67. {
  68. /* there was not enough memory, so the input of
  69. * the codelet cannot be fetched ... put the
  70. * codelet back, and try it later */
  71. return -EAGAIN;
  72. }
  73. starpu_scc_kernel_t kernel = NULL;
  74. starpu_scc_func_t func = _starpu_task_get_scc_nth_implementation(j->task->cl, j->nimpl);
  75. if (func)
  76. {
  77. /* We execute the function contained in the codelet, it must return a
  78. * pointer to the function to execute on the device, either specified
  79. * directly by the user or by a call to starpu_scc_get_kernel().
  80. */
  81. kernel = func();
  82. }
  83. else
  84. {
  85. /* If user doesn't define any starpu_scc_func_t in cl->scc_funcs we try to use
  86. * cpu_funcs_name.
  87. */
  88. char *func_name = _starpu_task_get_cpu_name_nth_implementation(j->task->cl, j->nimpl);
  89. if (func_name)
  90. {
  91. starpu_scc_func_symbol_t symbol;
  92. _starpu_scc_src_register_kernel(&symbol, func_name);
  93. kernel = _starpu_scc_src_get_kernel(symbol);
  94. }
  95. }
  96. STARPU_ASSERT(kernel);
  97. _starpu_driver_start_job(args, j, &codelet_start, 0, profiling);
  98. _starpu_src_common_execute_kernel_from_task(scc_mp_nodes[args->devid], (void (*)(void)) kernel, 0, task);
  99. _starpu_driver_end_job(args, j, args->perf_arch, &codelet_end, 0, profiling);
  100. _starpu_driver_update_job_feedback(j, args, args->perf_arch, &codelet_start, &codelet_end, profiling);
  101. _starpu_push_task_output(j, mask);
  102. return 0;
  103. }
  104. void _starpu_scc_src_mp_deinit()
  105. {
  106. _starpu_scc_common_unmap_shared_memory();
  107. RCCE_finalize();
  108. }
  109. int _starpu_scc_src_register_kernel(starpu_scc_func_symbol_t *symbol, const char *func_name)
  110. {
  111. unsigned int func_name_size = (strlen(func_name) + 1) * sizeof(char);
  112. STARPU_PTHREAD_MUTEX_LOCK(&htbl_mutex);
  113. struct _starpu_scc_kernel *kernel;
  114. HASH_FIND_STR(kernels, func_name, kernel);
  115. if (kernel != NULL)
  116. {
  117. STARPU_PTHREAD_MUTEX_UNLOCK(&htbl_mutex);
  118. // Function already in the table.
  119. *symbol = kernel;
  120. return 0;
  121. }
  122. kernel = malloc(sizeof(*kernel));
  123. if (kernel == NULL)
  124. {
  125. STARPU_PTHREAD_MUTEX_UNLOCK(&htbl_mutex);
  126. return -ENOMEM;
  127. }
  128. kernel->name = malloc(func_name_size);
  129. if (kernel->name == NULL)
  130. {
  131. STARPU_PTHREAD_MUTEX_UNLOCK(&htbl_mutex);
  132. free(kernel);
  133. return -ENOMEM;
  134. }
  135. memcpy(kernel->name, func_name, func_name_size);
  136. HASH_ADD_STR(kernels, name, kernel);
  137. unsigned int nb_scc_devices = starpu_scc_worker_get_count();
  138. unsigned int i;
  139. for (i = 0; i < nb_scc_devices; ++i)
  140. kernel->func[i] = NULL;
  141. STARPU_PTHREAD_MUTEX_UNLOCK(&htbl_mutex);
  142. *symbol = kernel;
  143. return 0;
  144. }
  145. starpu_scc_kernel_t _starpu_scc_src_get_kernel(starpu_scc_func_symbol_t symbol)
  146. {
  147. int workerid = starpu_worker_get_id();
  148. /* This function has to be called in the codelet only, by the thread
  149. * which will handle the task */
  150. if (workerid < 0)
  151. return NULL;
  152. int devid = starpu_worker_get_devid(workerid);
  153. struct _starpu_scc_kernel *kernel = symbol;
  154. if (kernel->func[devid] == NULL)
  155. {
  156. struct _starpu_mp_node *node = scc_mp_nodes[devid];
  157. int ret = _starpu_src_common_lookup(node, (void (**)(void))&kernel->func[devid], kernel->name);
  158. if (ret)
  159. return NULL;
  160. }
  161. return kernel->func[devid];
  162. }
  163. unsigned _starpu_scc_src_get_device_count()
  164. {
  165. int nb_scc_devices;
  166. if (!_starpu_scc_common_is_mp_initialized())
  167. {
  168. return 0;
  169. }
  170. nb_scc_devices = RCCE_num_ues() - 1;
  171. nb_scc_devices = nb_scc_devices < 0 ? 0 : nb_scc_devices;
  172. return nb_scc_devices;
  173. }
  174. void _starpu_scc_exit_useless_node(int devid)
  175. {
  176. struct _starpu_mp_node *node = _starpu_mp_common_node_create(STARPU_SCC_SOURCE, devid);
  177. _starpu_mp_common_send_command(node, STARPU_EXIT, NULL, 0);
  178. _starpu_mp_common_node_destroy(node);
  179. }
  180. void _starpu_scc_src_init(struct _starpu_mp_node *node)
  181. {
  182. node->mp_connection.scc_nodeid = STARPU_TO_SCC_SINK_ID(node->peer_id);
  183. }
  184. /* Allocate memory on SCC.
  185. * Return 0 if OK or 1 if not.
  186. */
  187. int _starpu_scc_allocate_memory(void **addr, size_t size, unsigned memory_node)
  188. {
  189. return _starpu_src_common_allocate(_starpu_scc_src_memory_node_to_mp_node(memory_node),
  190. addr, size);
  191. }
  192. /* Free memory on SCC.
  193. */
  194. void _starpu_scc_free_memory(void *addr, unsigned memory_node)
  195. {
  196. return _starpu_src_common_free(_starpu_scc_src_memory_node_to_mp_node(memory_node),
  197. addr);
  198. }
  199. int _starpu_scc_allocate_shared_memory(void **addr, size_t size)
  200. {
  201. return (*addr = (void*)RCCE_shmalloc(size)) == NULL;
  202. }
  203. void _starpu_scc_free_shared_memory(void *addr)
  204. {
  205. RCCE_shfree(addr);
  206. }
  207. /* Assigns the offset to "offset" between "ptr" and the start of the shared memory.
  208. * Affect "dev_handle" with the start of the shared memory is useful for data
  209. * partionning.
  210. */
  211. void _starpu_scc_set_offset_in_shared_memory(void *ptr, void **dev_handle, size_t *offset)
  212. {
  213. /* We're on SCC... */
  214. if (_starpu_can_submit_scc_task())
  215. {
  216. if (!_starpu_scc_common_is_in_shared_memory(ptr))
  217. {
  218. fprintf(stderr, "The data (%p) you want to register does not seem to be allocated in shared memory. "
  219. "Please use starpu_malloc to do this.\n", ptr);
  220. STARPU_ABORT();
  221. }
  222. void *shm_addr = _starpu_scc_common_get_shared_memory_addr();
  223. if (dev_handle)
  224. *dev_handle = shm_addr;
  225. if (offset)
  226. *offset = ptr - shm_addr;
  227. }
  228. }
  229. /* Transfert SIZE bytes from the address pointed by SRC in the SRC_NODE memory
  230. * node to the address pointed by DST in the DST_NODE memory node
  231. */
  232. int _starpu_scc_copy_src_to_sink(void *src, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst, unsigned dst_node, size_t size)
  233. {
  234. return _starpu_src_common_copy_host_to_sink(_starpu_scc_src_memory_node_to_mp_node(dst_node),
  235. src, dst, size);
  236. }
  237. /* Transfert SIZE bytes from the address pointed by SRC in the SRC_NODE memory
  238. * node to the address pointed by DST in the DST_NODE memory node
  239. */
  240. int _starpu_scc_copy_sink_to_src(void *src, unsigned src_node, void *dst, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, size_t size)
  241. {
  242. return _starpu_src_common_copy_sink_to_host(_starpu_scc_src_memory_node_to_mp_node(src_node),
  243. src, dst, size);
  244. }
  245. int _starpu_scc_copy_sink_to_sink(void *src, unsigned src_node, void *dst, unsigned dst_node, size_t size)
  246. {
  247. return _starpu_src_common_copy_sink_to_sink(_starpu_scc_src_memory_node_to_mp_node(src_node),
  248. _starpu_scc_src_memory_node_to_mp_node(dst_node),
  249. src, dst, size);
  250. }
  251. void *_starpu_scc_src_worker(void *arg)
  252. {
  253. struct _starpu_worker *args = arg;
  254. int devid = args->devid;
  255. int workerid = args->workerid;
  256. struct _starpu_machine_config *config = args->config;
  257. unsigned memnode = args->memory_node;
  258. unsigned baseworkerid = args - config->workers;
  259. unsigned mp_nodeid = args->mp_nodeid;
  260. unsigned i;
  261. _starpu_worker_init(args, _STARPU_FUT_SCC_KEY);
  262. _starpu_scc_src_init_context(devid);
  263. args->status = STATUS_UNKNOWN;
  264. for (i = 0; i < config->topology.nmiccores[mp_nodeid]; i++)
  265. {
  266. struct _starpu_worker *worker = &config->workers[baseworkerid+i];
  267. snprintf(worker->name, sizeof(worker->name), "MIC %d core %u", mp_nodeid, i);
  268. }
  269. _STARPU_TRACE_WORKER_INIT_END;
  270. /* tell the main thread that this one is ready */
  271. STARPU_PTHREAD_MUTEX_LOCK(&args->mutex);
  272. args->worker_is_initialized = 1;
  273. STARPU_PTHREAD_COND_SIGNAL(&args->ready_cond);
  274. STARPU_PTHREAD_MUTEX_UNLOCK(&args->mutex);
  275. struct _starpu_job * j;
  276. struct starpu_task *task;
  277. int res;
  278. while (_starpu_machine_is_running())
  279. {
  280. _STARPU_TRACE_START_PROGRESS(memnode);
  281. _starpu_datawizard_progress(memnode, 1);
  282. _STARPU_TRACE_END_PROGRESS(memnode);
  283. task = _starpu_get_worker_task(args, workerid, memnode);
  284. if (!task)
  285. continue;
  286. j = _starpu_get_job_associated_to_task(task);
  287. /* can a SCC device do that task ? */
  288. if (!_STARPU_SCC_MAY_PERFORM(j))
  289. {
  290. /* this isn't a SCC task */
  291. _starpu_push_task_to_workers(task);
  292. continue;
  293. }
  294. _starpu_set_current_task(task);
  295. args->current_task = j->task;
  296. res = _starpu_scc_src_execute_job(j, args);
  297. _starpu_set_current_task(NULL);
  298. args->current_task = NULL;
  299. if (res)
  300. {
  301. switch (res)
  302. {
  303. case -EAGAIN:
  304. _STARPU_DISP("ouch, SCC could not actually run task %p, putting it back...\n", task);
  305. _starpu_push_task_to_workers(task);
  306. STARPU_ABORT();
  307. continue;
  308. default:
  309. STARPU_ASSERT(0);
  310. }
  311. }
  312. _starpu_handle_job_termination(j);
  313. }
  314. _STARPU_TRACE_WORKER_DEINIT_START;
  315. _starpu_handle_all_pending_node_data_requests(memnode);
  316. /* In case there remains some memory that was automatically
  317. * allocated by StarPU, we release it now. Note that data
  318. * coherency is not maintained anymore at that point ! */
  319. _starpu_free_all_automatically_allocated_buffers(memnode);
  320. _starpu_scc_src_deinit_context(args->devid);
  321. _STARPU_TRACE_WORKER_DEINIT_END(_STARPU_FUT_SCC_KEY);
  322. return NULL;
  323. }