driver_mpi_source.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015 Mathieu Lirzin <mthl@openmailbox.org>
  4. * Copyright (C) 2016, 2017 Inria
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <mpi.h>
  18. #include <errno.h>
  19. #include <starpu.h>
  20. #include <drivers/mpi/driver_mpi_source.h>
  21. #include <drivers/mpi/driver_mpi_common.h>
  22. #include <datawizard/memory_nodes.h>
  23. #include <drivers/driver_common/driver_common.h>
  24. #include <drivers/mp_common/source_common.h>
  25. /* Mutex for concurrent access to the table.
  26. */
  27. starpu_pthread_mutex_t htbl_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  28. /* Structure used by host to store informations about a kernel executable on
  29. * a MPI MS device : its name, and its address on each device.
  30. * If a kernel has been initialized, then a lookup has already been achieved and the
  31. * device knows how to call it, else the host still needs to do a lookup.
  32. */
  33. struct _starpu_mpi_ms_kernel
  34. {
  35. UT_hash_handle hh;
  36. char *name;
  37. starpu_mpi_ms_kernel_t func[STARPU_MAXMPIDEVS];
  38. } *kernels;
  39. /* Array of structures containing all the informations useful to send
  40. * and receive informations with devices */
  41. struct _starpu_mp_node *mpi_ms_nodes[STARPU_MAXMPIDEVS];
  42. struct _starpu_mp_node *_starpu_mpi_ms_src_get_actual_thread_mp_node()
  43. {
  44. struct _starpu_worker *actual_worker = _starpu_get_local_worker_key();
  45. STARPU_ASSERT(actual_worker);
  46. int devid = actual_worker->devid;
  47. STARPU_ASSERT(devid >= 0 && devid < STARPU_MAXMPIDEVS);
  48. return mpi_ms_nodes[devid];
  49. }
  50. void _starpu_mpi_source_init(struct _starpu_mp_node *node)
  51. {
  52. _starpu_mpi_common_mp_initialize_src_sink(node);
  53. //TODO
  54. }
  55. void _starpu_mpi_source_deinit(struct _starpu_mp_node *node)
  56. {
  57. }
  58. struct _starpu_mp_node *_starpu_mpi_src_get_mp_node_from_memory_node(int memory_node)
  59. {
  60. int devid = _starpu_memory_node_get_devid(memory_node);
  61. STARPU_ASSERT_MSG(devid >= 0 && devid < STARPU_MAXMPIDEVS, "bogus devid %d for memory node %d\n", devid, memory_node);
  62. return mpi_ms_nodes[devid];
  63. }
  64. int _starpu_mpi_src_allocate_memory(void ** addr, size_t size, unsigned memory_node)
  65. {
  66. const struct _starpu_mp_node *mp_node = _starpu_mpi_src_get_mp_node_from_memory_node(memory_node);
  67. return _starpu_src_common_allocate(mp_node, addr, size);
  68. }
  69. void _starpu_mpi_source_free_memory(void *addr, unsigned memory_node)
  70. {
  71. struct _starpu_mp_node *mp_node = _starpu_mpi_src_get_mp_node_from_memory_node(memory_node);
  72. _starpu_src_common_free(mp_node, addr);
  73. }
  74. /* Transfert SIZE bytes from the address pointed by SRC in the SRC_NODE memory
  75. * node to the address pointed by DST in the DST_NODE memory node
  76. */
  77. int _starpu_mpi_copy_ram_to_mpi_sync(void *src, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst, unsigned dst_node, size_t size)
  78. {
  79. struct _starpu_mp_node *mp_node = _starpu_mpi_src_get_mp_node_from_memory_node(dst_node);
  80. return _starpu_src_common_copy_host_to_sink_sync(mp_node, src, dst, size);
  81. }
  82. /* Transfert SIZE bytes from the address pointed by SRC in the SRC_NODE memory
  83. * node to the address pointed by DST in the DST_NODE memory node
  84. */
  85. int _starpu_mpi_copy_mpi_to_ram_sync(void *src, unsigned src_node, void *dst, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, size_t size)
  86. {
  87. struct _starpu_mp_node *mp_node = _starpu_mpi_src_get_mp_node_from_memory_node(src_node);
  88. return _starpu_src_common_copy_sink_to_host_sync(mp_node, src, dst, size);
  89. }
  90. int _starpu_mpi_copy_sink_to_sink_sync(void *src, unsigned src_node, void *dst, unsigned dst_node, size_t size)
  91. {
  92. return _starpu_src_common_copy_sink_to_sink_sync(_starpu_mpi_src_get_mp_node_from_memory_node(src_node),
  93. _starpu_mpi_src_get_mp_node_from_memory_node(dst_node),
  94. src, dst, size);
  95. }
  96. int _starpu_mpi_copy_mpi_to_ram_async(void *src, unsigned src_node, void *dst, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, size_t size, void * event)
  97. {
  98. struct _starpu_mp_node *mp_node = _starpu_mpi_src_get_mp_node_from_memory_node(src_node);
  99. return _starpu_src_common_copy_sink_to_host_async(mp_node, src, dst, size, event);
  100. }
  101. int _starpu_mpi_copy_ram_to_mpi_async(void *src, unsigned src_node STARPU_ATTRIBUTE_UNUSED, void *dst, unsigned dst_node, size_t size, void * event)
  102. {
  103. struct _starpu_mp_node *mp_node = _starpu_mpi_src_get_mp_node_from_memory_node(dst_node);
  104. return _starpu_src_common_copy_host_to_sink_async(mp_node, src, dst, size, event);
  105. }
  106. int _starpu_mpi_copy_sink_to_sink_async(void *src, unsigned src_node, void *dst, unsigned dst_node, size_t size, void * event)
  107. {
  108. return _starpu_src_common_copy_sink_to_sink_async(_starpu_mpi_src_get_mp_node_from_memory_node(src_node),
  109. _starpu_mpi_src_get_mp_node_from_memory_node(dst_node),
  110. src, dst, size, event);
  111. }
  112. int _starpu_mpi_ms_src_register_kernel(starpu_mpi_ms_func_symbol_t *symbol, const char *func_name)
  113. {
  114. unsigned int func_name_size = (strlen(func_name) + 1) * sizeof(char);
  115. STARPU_PTHREAD_MUTEX_LOCK(&htbl_mutex);
  116. struct _starpu_mpi_ms_kernel *kernel;
  117. HASH_FIND_STR(kernels, func_name, kernel);
  118. if (kernel != NULL)
  119. {
  120. STARPU_PTHREAD_MUTEX_UNLOCK(&htbl_mutex);
  121. // Function already in the table.
  122. *symbol = kernel;
  123. return 0;
  124. }
  125. kernel = malloc(sizeof(*kernel));
  126. if (kernel == NULL)
  127. {
  128. STARPU_PTHREAD_MUTEX_UNLOCK(&htbl_mutex);
  129. return -ENOMEM;
  130. }
  131. kernel->name = malloc(func_name_size);
  132. if (kernel->name == NULL)
  133. {
  134. STARPU_PTHREAD_MUTEX_UNLOCK(&htbl_mutex);
  135. free(kernel);
  136. return -ENOMEM;
  137. }
  138. memcpy(kernel->name, func_name, func_name_size);
  139. HASH_ADD_STR(kernels, name, kernel);
  140. unsigned int nb_mpi_devices = _starpu_mpi_src_get_device_count();
  141. unsigned int i;
  142. for (i = 0; i < nb_mpi_devices; ++i)
  143. kernel->func[i] = NULL;
  144. STARPU_PTHREAD_MUTEX_UNLOCK(&htbl_mutex);
  145. *symbol = kernel;
  146. return 0;
  147. }
  148. starpu_mpi_ms_kernel_t _starpu_mpi_ms_src_get_kernel(starpu_mpi_ms_func_symbol_t symbol)
  149. {
  150. int workerid = starpu_worker_get_id();
  151. /* This function has to be called in the codelet only, by the thread
  152. * which will handle the task */
  153. if (workerid < 0)
  154. return NULL;
  155. int devid = starpu_worker_get_devid(workerid);
  156. struct _starpu_mpi_ms_kernel *kernel = symbol;
  157. if (kernel->func[devid] == NULL)
  158. {
  159. struct _starpu_mp_node *node = mpi_ms_nodes[devid];
  160. int ret = _starpu_src_common_lookup(node, (void (**)(void))&kernel->func[devid], kernel->name);
  161. if (ret)
  162. return NULL;
  163. }
  164. return kernel->func[devid];
  165. }
  166. starpu_mpi_ms_kernel_t _starpu_mpi_ms_src_get_kernel_from_codelet(struct starpu_codelet *cl, unsigned nimpl)
  167. {
  168. starpu_mpi_ms_kernel_t kernel = NULL;
  169. starpu_mpi_ms_func_t func = _starpu_task_get_mpi_ms_nth_implementation(cl, nimpl);
  170. if (func)
  171. {
  172. /* We execute the function contained in the codelet, it must return a
  173. * pointer to the function to execute on the device, either specified
  174. * directly by the user or by a call to starpu_mic_get_func().
  175. */
  176. kernel = func();
  177. }
  178. else
  179. {
  180. /* If user dont define any starpu_mic_fun_t in cl->mic_func we try to use
  181. * cpu_func_name.
  182. */
  183. const char *func_name = _starpu_task_get_cpu_name_nth_implementation(cl, nimpl);
  184. if (func_name)
  185. {
  186. starpu_mpi_ms_func_symbol_t symbol;
  187. _starpu_mpi_ms_src_register_kernel(&symbol, func_name);
  188. kernel = _starpu_mpi_ms_src_get_kernel(symbol);
  189. }
  190. }
  191. STARPU_ASSERT_MSG(kernel, "when STARPU_MPI_MS is defined in 'where', mpi_ms_funcs or cpu_funcs_name has to be defined and the function be non-static");
  192. return kernel;
  193. }
  194. void(* _starpu_mpi_ms_src_get_kernel_from_job(const struct _starpu_mp_node *node STARPU_ATTRIBUTE_UNUSED, struct _starpu_job *j))(void)
  195. {
  196. starpu_mpi_ms_kernel_t kernel = NULL;
  197. starpu_mpi_ms_func_t func = _starpu_task_get_mpi_ms_nth_implementation(j->task->cl, j->nimpl);
  198. if (func)
  199. {
  200. /* We execute the function contained in the codelet, it must return a
  201. * pointer to the function to execute on the device, either specified
  202. * directly by the user or by a call to starpu_mpi_ms_get_func().
  203. */
  204. kernel = func();
  205. }
  206. else
  207. {
  208. /* If user dont define any starpu_mpi_ms_fun_t in cl->mpi_ms_func we try to use
  209. * cpu_func_name.
  210. */
  211. const char *func_name = _starpu_task_get_cpu_name_nth_implementation(j->task->cl, j->nimpl);
  212. if (func_name)
  213. {
  214. starpu_mpi_ms_func_symbol_t symbol;
  215. _starpu_mpi_ms_src_register_kernel(&symbol, func_name);
  216. kernel = _starpu_mpi_ms_src_get_kernel(symbol);
  217. }
  218. }
  219. STARPU_ASSERT(kernel);
  220. return (void (*)(void))kernel;
  221. }
  222. unsigned _starpu_mpi_src_get_device_count()
  223. {
  224. int nb_mpi_devices;
  225. if (!_starpu_mpi_common_is_mp_initialized())
  226. return 0;
  227. MPI_Comm_size(MPI_COMM_WORLD, &nb_mpi_devices);
  228. //Remove one for master
  229. nb_mpi_devices = nb_mpi_devices - 1;
  230. return nb_mpi_devices;
  231. }
  232. void *_starpu_mpi_src_worker(void *arg)
  233. {
  234. #ifndef STARPU_MPI_MASTER_SLAVE_MULTIPLE_THREAD
  235. struct _starpu_worker_set *worker_set_mpi = (struct _starpu_worker_set *) arg;
  236. int nbsinknodes = _starpu_mpi_src_get_device_count();
  237. int workersetnum;
  238. for (workersetnum = 0; workersetnum < nbsinknodes; workersetnum++)
  239. {
  240. struct _starpu_worker_set * worker_set = &worker_set_mpi[workersetnum];
  241. #else
  242. struct _starpu_worker_set *worker_set = arg;
  243. #endif
  244. /* As all workers of a set share common data, we just use the first
  245. * * one for intializing the following stuffs. */
  246. struct _starpu_worker *baseworker = &worker_set->workers[0];
  247. struct _starpu_machine_config *config = baseworker->config;
  248. unsigned baseworkerid = baseworker - config->workers;
  249. unsigned devid = baseworker->devid;
  250. unsigned i;
  251. /* unsigned memnode = baseworker->memory_node; */
  252. _starpu_driver_start(baseworker, _STARPU_FUT_MPI_KEY, 0);
  253. #ifdef STARPU_USE_FXT
  254. for (i = 1; i < worker_set->nworkers; i++)
  255. _starpu_worker_start(&worker_set->workers[i], _STARPU_FUT_MPI_KEY, 0);
  256. #endif
  257. // Current task for a thread managing a worker set has no sense.
  258. _starpu_set_current_task(NULL);
  259. for (i = 0; i < config->topology.nmpicores[devid]; i++)
  260. {
  261. struct _starpu_worker *worker = &config->workers[baseworkerid+i];
  262. snprintf(worker->name, sizeof(worker->name), "MPI_MS %u core %u", devid, i);
  263. snprintf(worker->short_name, sizeof(worker->short_name), "MPI_MS %u.%u", devid, i);
  264. }
  265. #ifndef STARPU_MPI_MASTER_SLAVE_MULTIPLE_THREAD
  266. {
  267. char thread_name[16];
  268. snprintf(thread_name, sizeof(thread_name), "MPI_MS");
  269. starpu_pthread_setname(thread_name);
  270. }
  271. #else
  272. {
  273. char thread_name[16];
  274. snprintf(thread_name, sizeof(thread_name), "MPI_MS %u", devid);
  275. starpu_pthread_setname(thread_name);
  276. }
  277. #endif
  278. for (i = 0; i < worker_set->nworkers; i++)
  279. {
  280. struct _starpu_worker *worker = &worker_set->workers[i];
  281. _STARPU_TRACE_WORKER_INIT_END(worker->workerid);
  282. }
  283. #ifndef STARPU_MPI_MASTER_SLAVE_MULTIPLE_THREAD
  284. _starpu_src_common_init_switch_env(workersetnum);
  285. } /* for */
  286. /* set the worker zero for the main thread */
  287. for (workersetnum = 0; workersetnum < nbsinknodes; workersetnum++)
  288. {
  289. struct _starpu_worker_set * worker_set = &worker_set_mpi[workersetnum];
  290. struct _starpu_worker *baseworker = &worker_set->workers[0];
  291. #endif
  292. /* tell the main thread that this one is ready */
  293. STARPU_PTHREAD_MUTEX_LOCK(&worker_set->mutex);
  294. baseworker->status = STATUS_UNKNOWN;
  295. worker_set->set_is_initialized = 1;
  296. STARPU_PTHREAD_COND_SIGNAL(&worker_set->ready_cond);
  297. STARPU_PTHREAD_MUTEX_UNLOCK(&worker_set->mutex);
  298. #ifndef STARPU_MPI_MASTER_SLAVE_MULTIPLE_THREAD
  299. }
  300. #endif
  301. #ifndef STARPU_MPI_MASTER_SLAVE_MULTIPLE_THREAD
  302. _starpu_src_common_workers_set(worker_set_mpi, nbsinknodes, mpi_ms_nodes);
  303. #else
  304. _starpu_src_common_worker(worker_set, baseworkerid, mpi_ms_nodes[devid]);
  305. #endif
  306. return NULL;
  307. }