mp_common.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2013 Thibaut Lambert
  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. #ifndef __MP_COMMON_H__
  18. #define __MP_COMMON_H__
  19. /** @file */
  20. #include <semaphore.h>
  21. #include <starpu.h>
  22. #include <common/config.h>
  23. #include <common/list.h>
  24. #include <common/barrier.h>
  25. #include <common/thread.h>
  26. #include <datawizard/interfaces/data_interface.h>
  27. #include <datawizard/copy_driver.h>
  28. #ifdef STARPU_USE_MP
  29. #ifdef STARPU_USE_MIC
  30. #include <scif.h>
  31. #endif /* STARPU_USE_MIC */
  32. #define BUFFER_SIZE 65536
  33. #define STARPU_MP_SRC_NODE 0
  34. #define STARPU_MP_SINK_NODE(a) ((a) + 1)
  35. #define STARPU_MP_COMMON_REPORT_ERROR(node, status) \
  36. (node)->report_error(__starpu_func__, __FILE__, __LINE__, (status))
  37. enum _starpu_mp_command
  38. {
  39. STARPU_MP_COMMAND_EXIT,
  40. STARPU_MP_COMMAND_EXECUTE,
  41. STARPU_MP_COMMAND_EXECUTE_DETACHED,
  42. STARPU_MP_COMMAND_ERROR_EXECUTE,
  43. STARPU_MP_COMMAND_ERROR_EXECUTE_DETACHED,
  44. STARPU_MP_COMMAND_LOOKUP,
  45. STARPU_MP_COMMAND_ANSWER_LOOKUP,
  46. STARPU_MP_COMMAND_ERROR_LOOKUP,
  47. STARPU_MP_COMMAND_ALLOCATE,
  48. STARPU_MP_COMMAND_ANSWER_ALLOCATE,
  49. STARPU_MP_COMMAND_ERROR_ALLOCATE,
  50. STARPU_MP_COMMAND_FREE,
  51. /** Synchronous send */
  52. STARPU_MP_COMMAND_RECV_FROM_HOST,
  53. STARPU_MP_COMMAND_SEND_TO_HOST,
  54. STARPU_MP_COMMAND_RECV_FROM_SINK,
  55. STARPU_MP_COMMAND_SEND_TO_SINK,
  56. /** Asynchronous send */
  57. STARPU_MP_COMMAND_RECV_FROM_HOST_ASYNC,
  58. STARPU_MP_COMMAND_RECV_FROM_HOST_ASYNC_COMPLETED,
  59. STARPU_MP_COMMAND_SEND_TO_HOST_ASYNC,
  60. STARPU_MP_COMMAND_SEND_TO_HOST_ASYNC_COMPLETED,
  61. STARPU_MP_COMMAND_RECV_FROM_SINK_ASYNC,
  62. STARPU_MP_COMMAND_RECV_FROM_SINK_ASYNC_COMPLETED,
  63. STARPU_MP_COMMAND_SEND_TO_SINK_ASYNC,
  64. STARPU_MP_COMMAND_SEND_TO_SINK_ASYNC_COMPLETED,
  65. STARPU_MP_COMMAND_TRANSFER_COMPLETE,
  66. STARPU_MP_COMMAND_SINK_NBCORES,
  67. STARPU_MP_COMMAND_ANSWER_SINK_NBCORES,
  68. STARPU_MP_COMMAND_EXECUTION_SUBMITTED,
  69. STARPU_MP_COMMAND_EXECUTION_COMPLETED,
  70. STARPU_MP_COMMAND_EXECUTION_DETACHED_SUBMITTED,
  71. STARPU_MP_COMMAND_EXECUTION_DETACHED_COMPLETED,
  72. STARPU_MP_COMMAND_PRE_EXECUTION,
  73. STARPU_MP_COMMAND_SYNC_WORKERS,
  74. };
  75. const char *_starpu_mp_common_command_to_string(const int command);
  76. enum _starpu_mp_node_kind
  77. {
  78. STARPU_NODE_MIC_SINK,
  79. STARPU_NODE_MIC_SOURCE,
  80. STARPU_NODE_MPI_SINK,
  81. STARPU_NODE_MPI_SOURCE,
  82. STARPU_NODE_INVALID_KIND
  83. };
  84. const char *_starpu_mp_common_node_kind_to_string(const int kind);
  85. union _starpu_mp_connection
  86. {
  87. #ifdef STARPU_USE_MIC
  88. scif_epd_t mic_endpoint;
  89. #endif
  90. #ifdef STARPU_USE_MPI_MASTER_SLAVE
  91. int mpi_remote_nodeid;
  92. #endif
  93. };
  94. struct _starpu_mp_transfer_command
  95. {
  96. size_t size;
  97. void *addr;
  98. void *event;
  99. };
  100. struct _starpu_mp_transfer_command_to_device
  101. {
  102. int devid;
  103. size_t size;
  104. void *addr;
  105. void *event;
  106. };
  107. LIST_TYPE(mp_barrier,
  108. int id;
  109. starpu_pthread_barrier_t before_work_barrier;
  110. starpu_pthread_barrier_t after_work_barrier;
  111. );
  112. LIST_TYPE(mp_message,
  113. enum _starpu_mp_command type;
  114. char *buffer;
  115. int size;
  116. );
  117. struct mp_task
  118. {
  119. void (*kernel)(void **, void *);
  120. void **interfaces;
  121. unsigned nb_interfaces;
  122. void *cl_arg;
  123. unsigned cl_arg_size;
  124. void *cl_ret;
  125. unsigned cl_ret_size;
  126. unsigned coreid;
  127. enum starpu_codelet_type type;
  128. int is_parallel_task;
  129. int combined_workerid;
  130. int detached;
  131. struct mp_barrier* mp_barrier;
  132. };
  133. LIST_TYPE(_starpu_mp_event,
  134. struct _starpu_async_channel event;
  135. void * remote_event;
  136. enum _starpu_mp_command answer_cmd;
  137. );
  138. /** Message-passing working node, whether source
  139. * or sink */
  140. struct _starpu_mp_node
  141. {
  142. enum _starpu_mp_node_kind kind;
  143. int baseworkerid;
  144. /*the number of core on the device
  145. * Must be initialized during init function*/
  146. int nb_cores;
  147. /*Is starpu running*/
  148. int is_running;
  149. /** Buffer used for scif data transfers, allocated
  150. * during node initialization.
  151. * Size : BUFFER_SIZE */
  152. void *buffer;
  153. /** For sink : -1.
  154. * For host : index of the sink = devid.
  155. */
  156. int peer_id;
  157. /** Only MIC use this for now !!
  158. * This is the devid both for the sink and the host. */
  159. int devid;
  160. /** Only MIC use this for now !!
  161. * Is the number ok MIC on the system. */
  162. unsigned int nb_mp_sinks;
  163. /** Connection used for command passing between the host thread and the
  164. * sink it controls */
  165. union _starpu_mp_connection mp_connection;
  166. /** Only MIC use this for now !!
  167. * Connection used for data transfers between the host and his sink. */
  168. union _starpu_mp_connection host_sink_dt_connection;
  169. /** Mutex to protect the interleaving of communications when using one thread per node,
  170. * for instance, when a thread transfers piece of data and an other wants to use
  171. * a sink_to_sink communication */
  172. starpu_pthread_mutex_t connection_mutex;
  173. /** Only MIC use this for now !!
  174. * Only sink use this for now !!
  175. * Connection used for data transfer between devices.
  176. * A sink opens a connection with each other sink,
  177. * thus each sink can directly send data to each other.
  178. * For sink :
  179. * - sink_sink_dt_connections[i] is the connection to the sink number i.
  180. * - sink_sink_dt_connections[j] is not initialized for the sink number j. */
  181. union _starpu_mp_connection *sink_sink_dt_connections;
  182. /** This list contains events
  183. * about asynchronous request
  184. */
  185. struct _starpu_mp_event_list event_list;
  186. /** */
  187. starpu_pthread_barrier_t init_completed_barrier;
  188. /** table to store pointer of the thread workers*/
  189. void* thread_table;
  190. /*list where threads add messages to send to the source node */
  191. struct mp_message_list message_queue;
  192. starpu_pthread_mutex_t message_queue_mutex;
  193. /*list of barrier for combined worker*/
  194. struct mp_barrier_list barrier_list;
  195. starpu_pthread_mutex_t barrier_mutex;
  196. /*table where worker comme pick task*/
  197. struct mp_task ** run_table;
  198. struct mp_task ** run_table_detached;
  199. sem_t * sem_run_table;
  200. /** Node general functions */
  201. void (*init) (struct _starpu_mp_node *node);
  202. void (*launch_workers) (struct _starpu_mp_node *node);
  203. void (*deinit) (struct _starpu_mp_node *node);
  204. void (*report_error) (const char *, const char *, const int, const int);
  205. /** Message passing */
  206. int (*mp_recv_is_ready) (const struct _starpu_mp_node *);
  207. void (*mp_send) (const struct _starpu_mp_node *, void *, int);
  208. void (*mp_recv) (const struct _starpu_mp_node *, void *, int);
  209. /** Data transfers */
  210. void (*dt_send) (const struct _starpu_mp_node *, void *, int, void *);
  211. void (*dt_recv) (const struct _starpu_mp_node *, void *, int, void *);
  212. void (*dt_send_to_device) (const struct _starpu_mp_node *, int, void *, int, void *);
  213. void (*dt_recv_from_device) (const struct _starpu_mp_node *, int, void *, int, void *);
  214. /** Test async transfers */
  215. unsigned int (*dt_test) (struct _starpu_async_channel *);
  216. void (*(*get_kernel_from_job) (const struct _starpu_mp_node *,struct _starpu_job *))(void);
  217. void (*(*lookup) (const struct _starpu_mp_node *, char* ))(void);
  218. void (*bind_thread) (const struct _starpu_mp_node *, int,int *,int);
  219. void (*execute) (struct _starpu_mp_node *, void *, int);
  220. void (*allocate) (const struct _starpu_mp_node *, void *, int);
  221. void (*free) (const struct _starpu_mp_node *, void *, int);
  222. };
  223. struct _starpu_mp_node * _starpu_mp_common_node_create(enum _starpu_mp_node_kind node_kind, int peer_devid) STARPU_ATTRIBUTE_MALLOC;
  224. void _starpu_mp_common_node_destroy(struct _starpu_mp_node *node);
  225. void _starpu_mp_common_send_command(const struct _starpu_mp_node *node,
  226. const enum _starpu_mp_command command,
  227. void *arg, int arg_size);
  228. enum _starpu_mp_command _starpu_mp_common_recv_command(const struct _starpu_mp_node *node, void **arg, int *arg_size);
  229. #endif /* STARPU_USE_MP */
  230. #endif /* __MP_COMMON_H__ */