driver_scc_source.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015-2017 CNRS
  4. * Copyright (C) 2012,2016-2017 Inria
  5. * Copyright (C) 2013-2016 Université de Bordeaux
  6. * Copyright (C) 2013 Thibaut Lambert
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #include <starpu.h>
  20. #include <starpu_profiling.h>
  21. #include <core/sched_policy.h>
  22. #include <core/task.h>
  23. #include <common/uthash.h>
  24. #include <RCCE.h>
  25. #include <drivers/driver_common/driver_common.h>
  26. #include <drivers/mp_common/source_common.h>
  27. #include <drivers/scc/driver_scc_common.h>
  28. #include <drivers/scc/driver_scc_source.h>
  29. static struct _starpu_mp_node *scc_mp_nodes[STARPU_MAXSCCDEVS];
  30. struct _starpu_scc_kernel
  31. {
  32. UT_hash_handle hh;
  33. char *name;
  34. starpu_scc_kernel_t func[STARPU_MAXSCCDEVS];
  35. } *kernels;
  36. starpu_pthread_mutex_t htbl_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  37. static struct _starpu_mp_node *_starpu_scc_src_memory_node_to_mp_node(unsigned memory_node)
  38. {
  39. int devid = _starpu_memory_node_get_devid(memory_node);
  40. STARPU_ASSERT(devid < STARPU_MAXSCCDEVS);
  41. return scc_mp_nodes[devid];
  42. }
  43. static void _starpu_scc_src_init_context(int devid)
  44. {
  45. /* Let's create the node structure, we'll communicate with the peer
  46. * through RCCE thanks to it */
  47. scc_mp_nodes[devid] = _starpu_mp_common_node_create(STARPU_SCC_SOURCE, devid);
  48. }
  49. static void _starpu_scc_src_deinit_context(int devid)
  50. {
  51. _starpu_mp_common_send_command(scc_mp_nodes[devid], STARPU_EXIT, NULL, 0);
  52. _starpu_mp_common_node_destroy(scc_mp_nodes[devid]);
  53. }
  54. void (*_starpu_scc_src_get_kernel_from_job(const struct _starpu_mp_node *,struct _starpu_job *j))(void)
  55. {
  56. starpu_scc_kernel_t kernel = NULL;
  57. starpu_scc_func_t func = _starpu_task_get_scc_nth_implementation(j->task->cl, j->nimpl);
  58. if (func)
  59. {
  60. /* We execute the function contained in the codelet, it must return a
  61. * pointer to the function to execute on the device, either specified
  62. * directly by the user or by a call to starpu_scc_get_kernel().
  63. */
  64. kernel = func();
  65. }
  66. else
  67. {
  68. /* If user doesn't define any starpu_scc_func_t in cl->scc_funcs we try to use
  69. * cpu_funcs_name.
  70. */
  71. char *func_name = _starpu_task_get_cpu_name_nth_implementation(j->task->cl, j->nimpl);
  72. if (func_name)
  73. {
  74. starpu_scc_func_symbol_t symbol;
  75. _starpu_scc_src_register_kernel(&symbol, func_name);
  76. kernel = _starpu_scc_src_get_kernel(symbol);
  77. }
  78. }
  79. STARPU_ASSERT_MSG(kernel, "when STARPU_MIC is defined in 'where', mic_funcs or cpu_funcs_name has to be defined");
  80. return (void (*)(void))kernel;
  81. }
  82. void _starpu_scc_src_mp_deinit()
  83. {
  84. _starpu_scc_common_unmap_shared_memory();
  85. RCCE_finalize();
  86. }
  87. int _starpu_scc_src_register_kernel(starpu_scc_func_symbol_t *symbol, const char *func_name)
  88. {
  89. unsigned int func_name_size = (strlen(func_name) + 1) * sizeof(char);
  90. STARPU_PTHREAD_MUTEX_LOCK(&htbl_mutex);
  91. struct _starpu_scc_kernel *kernel;
  92. HASH_FIND_STR(kernels, func_name, kernel);
  93. if (kernel != NULL)
  94. {
  95. STARPU_PTHREAD_MUTEX_UNLOCK(&htbl_mutex);
  96. // Function already in the table.
  97. *symbol = kernel;
  98. return 0;
  99. }
  100. kernel = malloc(sizeof(*kernel));
  101. if (kernel == NULL)
  102. {
  103. STARPU_PTHREAD_MUTEX_UNLOCK(&htbl_mutex);
  104. return -ENOMEM;
  105. }
  106. kernel->name = malloc(func_name_size);
  107. if (kernel->name == NULL)
  108. {
  109. STARPU_PTHREAD_MUTEX_UNLOCK(&htbl_mutex);
  110. free(kernel);
  111. return -ENOMEM;
  112. }
  113. memcpy(kernel->name, func_name, func_name_size);
  114. HASH_ADD_STR(kernels, name, kernel);
  115. unsigned int nb_scc_devices = starpu_scc_worker_get_count();
  116. unsigned int i;
  117. for (i = 0; i < nb_scc_devices; ++i)
  118. kernel->func[i] = NULL;
  119. STARPU_PTHREAD_MUTEX_UNLOCK(&htbl_mutex);
  120. *symbol = kernel;
  121. return 0;
  122. }
  123. starpu_scc_kernel_t _starpu_scc_src_get_kernel(starpu_scc_func_symbol_t symbol)
  124. {
  125. int workerid = starpu_worker_get_id();
  126. /* This function has to be called in the codelet only, by the thread
  127. * which will handle the task */
  128. if (workerid < 0)
  129. return NULL;
  130. int devid = starpu_worker_get_devid(workerid);
  131. struct _starpu_scc_kernel *kernel = symbol;
  132. if (kernel->func[devid] == NULL)
  133. {
  134. struct _starpu_mp_node *node = scc_mp_nodes[devid];
  135. int ret = _starpu_src_common_lookup(node, (void (**)(void))&kernel->func[devid], kernel->name);
  136. if (ret)
  137. return NULL;
  138. }
  139. return kernel->func[devid];
  140. }
  141. unsigned _starpu_scc_src_get_device_count()
  142. {
  143. int nb_scc_devices;
  144. if (!_starpu_scc_common_is_mp_initialized())
  145. {
  146. return 0;
  147. }
  148. nb_scc_devices = RCCE_num_ues() - 1;
  149. nb_scc_devices = nb_scc_devices < 0 ? 0 : nb_scc_devices;
  150. return nb_scc_devices;
  151. }
  152. void _starpu_scc_exit_useless_node(int devid)
  153. {
  154. struct _starpu_mp_node *node = _starpu_mp_common_node_create(STARPU_SCC_SOURCE, devid);
  155. _starpu_mp_common_send_command(node, STARPU_EXIT, NULL, 0);
  156. _starpu_mp_common_node_destroy(node);
  157. }
  158. void _starpu_scc_src_init(struct _starpu_mp_node *node)
  159. {
  160. node->mp_connection.scc_nodeid = STARPU_TO_SCC_SINK_ID(node->peer_id);
  161. }
  162. /* Allocate memory on SCC.
  163. * Return 0 if OK or 1 if not.
  164. */
  165. int _starpu_scc_allocate_memory(void **addr, size_t size, unsigned memory_node)
  166. {
  167. return _starpu_src_common_allocate(_starpu_scc_src_memory_node_to_mp_node(memory_node),
  168. addr, size);
  169. }
  170. /* Free memory on SCC.
  171. */
  172. void _starpu_scc_free_memory(void *addr, unsigned memory_node)
  173. {
  174. return _starpu_src_common_free(_starpu_scc_src_memory_node_to_mp_node(memory_node),
  175. addr);
  176. }
  177. int _starpu_scc_allocate_shared_memory(void **addr, size_t size)
  178. {
  179. return (*addr = (void*)RCCE_shmalloc(size)) == NULL;
  180. }
  181. void _starpu_scc_free_shared_memory(void *addr)
  182. {
  183. RCCE_shfree(addr);
  184. }
  185. /* Assigns the offset to "offset" between "ptr" and the start of the shared memory.
  186. * Affect "dev_handle" with the start of the shared memory is useful for data
  187. * partionning.
  188. */
  189. void _starpu_scc_set_offset_in_shared_memory(void *ptr, void **dev_handle, size_t *offset)
  190. {
  191. /* We're on SCC... */
  192. if (_starpu_can_submit_scc_task())
  193. {
  194. if (!_starpu_scc_common_is_in_shared_memory(ptr))
  195. {
  196. _STARPU_MSG("The data (%p) you want to register does not seem to be allocated in shared memory. "
  197. "Please use starpu_malloc to do this.\n", ptr);
  198. STARPU_ABORT();
  199. }
  200. void *shm_addr = _starpu_scc_common_get_shared_memory_addr();
  201. if (dev_handle)
  202. *dev_handle = shm_addr;
  203. if (offset)
  204. *offset = ptr - shm_addr;
  205. }
  206. }
  207. /* Transfert SIZE bytes from the address pointed by SRC in the SRC_NODE memory
  208. * node to the address pointed by DST in the DST_NODE memory node
  209. */
  210. int _starpu_scc_copy_src_to_sink(void *src, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst, unsigned dst_node, size_t size)
  211. {
  212. return _starpu_src_common_copy_host_to_sink_sync(_starpu_scc_src_memory_node_to_mp_node(dst_node),
  213. src, dst, size);
  214. }
  215. /* Transfert SIZE bytes from the address pointed by SRC in the SRC_NODE memory
  216. * node to the address pointed by DST in the DST_NODE memory node
  217. */
  218. int _starpu_scc_copy_sink_to_src(void *src, unsigned src_node, void *dst, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, size_t size)
  219. {
  220. return _starpu_src_common_copy_sink_to_host_sync(_starpu_scc_src_memory_node_to_mp_node(src_node),
  221. src, dst, size);
  222. }
  223. int _starpu_scc_copy_sink_to_sink(void *src, unsigned src_node, void *dst, unsigned dst_node, size_t size)
  224. {
  225. return _starpu_src_common_copy_sink_to_sink_sync(_starpu_scc_src_memory_node_to_mp_node(src_node),
  226. _starpu_scc_src_memory_node_to_mp_node(dst_node),
  227. src, dst, size);
  228. }
  229. void *_starpu_scc_src_worker(void *arg)
  230. {
  231. struct _starpu_worker_set *args = arg;
  232. int devid = args->devid;
  233. int workerid = args->workerid;
  234. struct _starpu_machine_config *config = args->config;
  235. unsigned baseworkerid = args - config->workers;
  236. unsigned subworkerid = args->subworkerid;
  237. unsigned i;
  238. _starpu_driver_start(args, _STARPU_FUT_SCC_KEY, 0);
  239. _starpu_scc_src_init_context(subworkerid);
  240. for (i = 0; i < config->topology.nmiccores[devid]; i++)
  241. {
  242. struct _starpu_worker *worker = &config->workers[baseworkerid+i];
  243. snprintf(worker->name, sizeof(worker->name), "SCC %d core %u", devid, i);
  244. snprintf(worker->short_name, sizeof(worker->short_name), "SCC %d core %u", devid, i);
  245. }
  246. {
  247. char thread_name[16];
  248. snprintf(thread_name, sizeof(thread_name), "SCC %d", devid);
  249. starpu_pthread_setname(thread_name);
  250. }
  251. _STARPU_TRACE_WORKER_INIT_END(workerid);
  252. /* tell the main thread that this one is ready */
  253. STARPU_PTHREAD_MUTEX_LOCK(&args->mutex);
  254. args->status = STATUS_UNKNOWN;
  255. args->worker_is_initialized = 1;
  256. STARPU_PTHREAD_COND_SIGNAL(&args->ready_cond);
  257. STARPU_PTHREAD_MUTEX_UNLOCK(&args->mutex);
  258. _starpu_src_common_worker(args, baseworkerid, scc_mp_nodes[devid]);
  259. _STARPU_TRACE_WORKER_DEINIT_START;
  260. _starpu_scc_src_deinit_context(args->subworkerid);
  261. worker->worker_is_initialized = 0;
  262. _STARPU_TRACE_WORKER_DEINIT_END(_STARPU_FUT_SCC_KEY);
  263. return NULL;
  264. }