sink_common.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 <dlfcn.h>
  17. #ifdef STARPU_USE_MIC
  18. #include <common/COISysInfo_common.h>
  19. #endif
  20. #include <starpu.h>
  21. #include <common/config.h>
  22. #include <common/utils.h>
  23. #include <drivers/mp_common/mp_common.h>
  24. #include "sink_common.h"
  25. /* Return the sink kind of the running process, based on the value of the
  26. * STARPU_SINK environment variable.
  27. * If there is no valid value retrieved, return STARPU_INVALID_KIND
  28. */
  29. static enum _starpu_mp_node_kind _starpu_sink_common_get_kind(void)
  30. {
  31. /* Environment varible STARPU_SINK must be defined when running on sink
  32. * side : let's use it to get the kind of node we're running on */
  33. char *node_kind = getenv("STARPU_SINK");
  34. STARPU_ASSERT(node_kind);
  35. if (!strcmp(node_kind, "STARPU_MIC"))
  36. return STARPU_MIC_SINK;
  37. else if (!strcmp(node_kind, "STARPU_SCC"))
  38. return STARPU_SCC_SINK;
  39. else if (!strcmp(node_kind, "STARPU_MPI"))
  40. return STARPU_MPI_SINK;
  41. else
  42. return STARPU_INVALID_KIND;
  43. }
  44. void
  45. _starpu_sink_nbcores (const struct _starpu_mp_node *node)
  46. {
  47. // Process packet received from `_starpu_src_common_sink_cores'.
  48. int nbcores = 1;
  49. #ifdef STARPU_USE_MIC
  50. // XXX I currently only support MIC for now.
  51. if (STARPU_MIC_SINK == _starpu_sink_common_get_kind ())
  52. nbcores = COISysGetCoreCount();
  53. #endif
  54. _starpu_mp_common_send_command (node, STARPU_ANSWER_SINK_NBCORES,
  55. &nbcores, sizeof (int));
  56. }
  57. /* Receive paquet from _starpu_src_common_execute_kernel in the form below :
  58. * [Function pointer on sink, number of interfaces, interfaces
  59. * (union _starpu_interface), cl_arg]
  60. * Then call the function given, passing as argument an array containing the
  61. * addresses of the received interfaces
  62. */
  63. void _starpu_sink_common_execute(const struct _starpu_mp_node *node,
  64. void *arg, int arg_size)
  65. {
  66. unsigned id = 0;
  67. void *arg_ptr = arg;
  68. void (*kernel)(void **, void *) = NULL;
  69. unsigned coreid = 0;
  70. unsigned nb_interfaces = 0;
  71. void *interfaces[STARPU_NMAXBUFS];
  72. void *cl_arg;
  73. kernel = *(void(**)(void **, void *)) arg_ptr;
  74. arg_ptr += sizeof(kernel);
  75. coreid = *(unsigned *) arg_ptr;
  76. arg_ptr += sizeof(coreid);
  77. nb_interfaces = *(unsigned *) arg_ptr;
  78. arg_ptr += sizeof(nb_interfaces);
  79. /* The function needs an array pointing to each interface it needs
  80. * during execution. As in sink-side there is no mean to know which
  81. * kind of interface to expect, the array is composed of unions of
  82. * interfaces, thus we expect the same size anyway */
  83. for (id = 0; id < nb_interfaces; id++)
  84. {
  85. interfaces[id] = arg_ptr;
  86. arg_ptr += sizeof(union _starpu_interface);
  87. }
  88. /* Was cl_arg sent ? */
  89. if (arg_size > arg_ptr - arg)
  90. cl_arg = arg_ptr;
  91. else
  92. cl_arg = NULL;
  93. //_STARPU_DEBUG("telling host that we have submitted the task %p.\n", kernel);
  94. /* XXX: in the future, we will not have to directly execute the kernel
  95. * but submit it to the correct local worker. */
  96. _starpu_mp_common_send_command(node, STARPU_EXECUTION_SUBMITTED,
  97. NULL, 0);
  98. //_STARPU_DEBUG("executing the task %p\n", kernel);
  99. /* XXX: we keep the synchronous execution model on the sink side for
  100. * now. */
  101. kernel(interfaces, cl_arg);
  102. //_STARPU_DEBUG("telling host that we have finished the task %p.\n", kernel);
  103. _starpu_mp_common_send_command(node, STARPU_EXECUTION_COMPLETED,
  104. &coreid, sizeof(coreid));
  105. }
  106. static void _starpu_sink_common_lookup(const struct _starpu_mp_node *node,
  107. char *func_name)
  108. {
  109. void (*func)(void);
  110. void *dl_handle = dlopen(NULL, RTLD_NOW);
  111. func = dlsym(dl_handle, func_name);
  112. //_STARPU_DEBUG("Looked up %s, got %p\n", func_name, func);
  113. /* If we couldn't find the function, let's send an error to the host.
  114. * The user probably made a mistake in the name */
  115. if (func)
  116. _starpu_mp_common_send_command(node, STARPU_ANSWER_LOOKUP,
  117. &func, sizeof(func));
  118. else
  119. _starpu_mp_common_send_command(node, STARPU_ERROR_LOOKUP,
  120. NULL, 0);
  121. }
  122. void _starpu_sink_common_allocate(const struct _starpu_mp_node *mp_node,
  123. void *arg, int arg_size)
  124. {
  125. STARPU_ASSERT(arg_size == sizeof(size_t));
  126. void *addr = malloc(*(size_t *)(arg));
  127. /* If the allocation fail, let's send an error to the host.
  128. */
  129. if (addr)
  130. _starpu_mp_common_send_command(mp_node, STARPU_ANSWER_ALLOCATE,
  131. &addr, sizeof(addr));
  132. else
  133. _starpu_mp_common_send_command(mp_node, STARPU_ERROR_ALLOCATE,
  134. NULL, 0);
  135. }
  136. void _starpu_sink_common_free(const struct _starpu_mp_node *mp_node STARPU_ATTRIBUTE_UNUSED,
  137. void *arg, int arg_size)
  138. {
  139. STARPU_ASSERT(arg_size == sizeof(void *));
  140. free(*(void **)(arg));
  141. }
  142. static void _starpu_sink_common_copy_from_host(const struct _starpu_mp_node *mp_node,
  143. void *arg, int arg_size)
  144. {
  145. STARPU_ASSERT(arg_size == sizeof(struct _starpu_mp_transfer_command));
  146. struct _starpu_mp_transfer_command *cmd = (struct _starpu_mp_transfer_command *)arg;
  147. mp_node->dt_recv(mp_node, cmd->addr, cmd->size);
  148. }
  149. static void _starpu_sink_common_copy_to_host(const struct _starpu_mp_node *mp_node,
  150. void *arg, int arg_size)
  151. {
  152. STARPU_ASSERT(arg_size == sizeof(struct _starpu_mp_transfer_command));
  153. struct _starpu_mp_transfer_command *cmd = (struct _starpu_mp_transfer_command *)arg;
  154. mp_node->dt_send(mp_node, cmd->addr, cmd->size);
  155. }
  156. static void _starpu_sink_common_copy_from_sink(const struct _starpu_mp_node *mp_node,
  157. void *arg, int arg_size)
  158. {
  159. STARPU_ASSERT(arg_size == sizeof(struct _starpu_mp_transfer_command_to_device));
  160. struct _starpu_mp_transfer_command_to_device *cmd = (struct _starpu_mp_transfer_command_to_device *)arg;
  161. mp_node->dt_recv_from_device(mp_node, cmd->devid, cmd->addr, cmd->size);
  162. _starpu_mp_common_send_command(mp_node, STARPU_TRANSFER_COMPLETE, NULL, 0);
  163. }
  164. static void _starpu_sink_common_copy_to_sink(const struct _starpu_mp_node *mp_node,
  165. void *arg, int arg_size)
  166. {
  167. STARPU_ASSERT(arg_size == sizeof(struct _starpu_mp_transfer_command_to_device));
  168. struct _starpu_mp_transfer_command_to_device *cmd = (struct _starpu_mp_transfer_command_to_device *)arg;
  169. mp_node->dt_send_to_device(mp_node, cmd->devid, cmd->addr, cmd->size);
  170. }
  171. /* Function looping on the sink, waiting for tasks to execute.
  172. * If the caller is the host, don't do anything.
  173. */
  174. void _starpu_sink_common_worker(void)
  175. {
  176. struct _starpu_mp_node *node = NULL;
  177. enum _starpu_mp_command command = STARPU_EXIT;
  178. int arg_size = 0;
  179. void *arg = NULL;
  180. enum _starpu_mp_node_kind node_kind = _starpu_sink_common_get_kind();
  181. if (node_kind == STARPU_INVALID_KIND)
  182. _STARPU_ERROR("No valid sink kind retrieved, use the"
  183. "STARPU_SINK environment variable to specify"
  184. "this\n");
  185. /* Create and initialize the node */
  186. node = _starpu_mp_common_node_create(node_kind, -1);
  187. while ((command = _starpu_mp_common_recv_command(node, &arg, &arg_size)) != STARPU_EXIT)
  188. {
  189. switch(command)
  190. {
  191. case STARPU_EXECUTE:
  192. node->execute(node, arg, arg_size);
  193. break;
  194. case STARPU_SINK_NBCORES:
  195. node->nbcores (node);
  196. break;
  197. case STARPU_LOOKUP:
  198. _starpu_sink_common_lookup(node, (char *) arg);
  199. break;
  200. case STARPU_ALLOCATE:
  201. node->allocate(node, arg, arg_size);
  202. break;
  203. case STARPU_FREE:
  204. node->free(node, arg, arg_size);
  205. break;
  206. case STARPU_RECV_FROM_HOST:
  207. _starpu_sink_common_copy_from_host(node, arg, arg_size);
  208. break;
  209. case STARPU_SEND_TO_HOST:
  210. _starpu_sink_common_copy_to_host(node, arg, arg_size);
  211. break;
  212. case STARPU_RECV_FROM_SINK:
  213. _starpu_sink_common_copy_from_sink(node, arg, arg_size);
  214. break;
  215. case STARPU_SEND_TO_SINK:
  216. _starpu_sink_common_copy_to_sink(node, arg, arg_size);
  217. break;
  218. default:
  219. printf("Oops, command %x unrecognized\n", command);
  220. }
  221. }
  222. /* Deinitialize the node and release it */
  223. _starpu_mp_common_node_destroy(node);
  224. exit(0);
  225. }