source_common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 <string.h>
  17. #include <pthread.h>
  18. #include <starpu.h>
  19. #include <datawizard/coherency.h>
  20. #include <drivers/mp_common/mp_common.h>
  21. int
  22. _starpu_src_common_sink_nbcores (const struct _starpu_mp_node *node, int *buf)
  23. {
  24. // Send a request to the sink NODE for the number of cores on it.
  25. enum _starpu_mp_command answer;
  26. void *arg;
  27. int arg_size = sizeof (int);
  28. _starpu_mp_common_send_command (node, STARPU_SINK_NBCORES, NULL, 0);
  29. answer = _starpu_mp_common_recv_command (node, &arg, &arg_size);
  30. STARPU_ASSERT (answer == STARPU_ANSWER_SINK_NBCORES && arg_size == sizeof (int));
  31. memcpy (buf, arg, arg_size);
  32. return 0;
  33. }
  34. /* Send a request to the sink linked to NODE for the pointer to the
  35. * function defined by FUNC_NAME.
  36. * In case of success, it returns 0 and FUNC_PTR contains the pointer ;
  37. * else it returns -ESPIPE if the function was not found.
  38. */
  39. int _starpu_src_common_lookup(struct _starpu_mp_node *node,
  40. void (**func_ptr)(void), const char *func_name)
  41. {
  42. enum _starpu_mp_command answer;
  43. void *arg;
  44. int arg_size;
  45. /* strlen ignore the terminating '\0' */
  46. arg_size = (strlen(func_name) + 1) * sizeof(char);
  47. _starpu_mp_common_send_command(node, STARPU_LOOKUP, (void *) func_name,
  48. arg_size);
  49. answer = _starpu_mp_common_recv_command(node, (void **) &arg,
  50. &arg_size);
  51. if (answer == STARPU_ERROR_LOOKUP)
  52. return -ESPIPE;
  53. /* We have to be sure the device answered the right question and the
  54. * answer has the right size */
  55. STARPU_ASSERT(answer == STARPU_ANSWER_LOOKUP &&
  56. arg_size == sizeof(*func_ptr));
  57. memcpy(func_ptr, arg, arg_size);
  58. return 0;
  59. }
  60. /* Send a message to the sink to execute a kernel.
  61. * The message sent has the form below :
  62. * [Function pointer on sink, number of interfaces, interfaces
  63. * (union _starpu_interface), cl_arg]
  64. */
  65. int _starpu_src_common_execute_kernel(const struct _starpu_mp_node *node,
  66. void (*kernel)(void), unsigned coreid,
  67. starpu_data_handle_t *handles,
  68. void **interfaces,
  69. unsigned nb_interfaces,
  70. void *cl_arg, size_t cl_arg_size)
  71. {
  72. unsigned id;
  73. void *buffer, *buffer_ptr, *arg = NULL;
  74. int buffer_size = 0, arg_size = 0;
  75. /* If the user didn't give any cl_arg, there is no need to send it */
  76. buffer_size =
  77. sizeof(kernel) + sizeof(coreid) + sizeof(nb_interfaces) +
  78. nb_interfaces * sizeof(union _starpu_interface);
  79. if (cl_arg)
  80. {
  81. STARPU_ASSERT(cl_arg_size);
  82. buffer_size += cl_arg_size;
  83. }
  84. /* We give to send_command a buffer we just allocated, which contains
  85. * a pointer to the function (sink-side), core on which execute this
  86. * function (sink-side), number of interfaces we send,
  87. * an array of generic (union) interfaces and the value of cl_arg */
  88. buffer_ptr = buffer = (void *) malloc(buffer_size);
  89. *(void(**)(void)) buffer = kernel;
  90. buffer_ptr += sizeof(kernel);
  91. *(unsigned *) buffer_ptr = coreid;
  92. buffer_ptr += sizeof(coreid);
  93. *(unsigned *) buffer_ptr = nb_interfaces;
  94. buffer_ptr += sizeof(nb_interfaces);
  95. /* Message-passing execution is a particular case as the codelet is
  96. * executed on a sink with a different memory, whereas a codelet is
  97. * executed on the host part for the other accelerators.
  98. * Thus we need to send a copy of each interface on the MP device */
  99. for (id = 0; id < nb_interfaces; id++)
  100. {
  101. starpu_data_handle_t handle = handles[id];
  102. memcpy (buffer_ptr, interfaces[id],
  103. handle->ops->interface_size);
  104. /* The sink side has no mean to get the type of each
  105. * interface, we use a union to make it generic and permit the
  106. * sink to go through the array */
  107. buffer_ptr += sizeof(union _starpu_interface);
  108. }
  109. if (cl_arg)
  110. memcpy(buffer_ptr, cl_arg, cl_arg_size);
  111. _starpu_mp_common_send_command(node, STARPU_EXECUTE, buffer, buffer_size);
  112. enum _starpu_mp_command answer = _starpu_mp_common_recv_command(node, &arg, &arg_size);
  113. if (answer == STARPU_ERROR_EXECUTE)
  114. return -EINVAL;
  115. STARPU_ASSERT(answer == STARPU_EXECUTION_SUBMITTED);
  116. free(buffer);
  117. return 0;
  118. }
  119. /* Launch the execution of the function KERNEL points to on the sink linked
  120. * to NODE. Returns 0 in case of success, -EINVAL if kernel is an invalid
  121. * pointer.
  122. * Data interfaces in task are send to the sink.
  123. */
  124. int _starpu_src_common_execute_kernel_from_task(const struct _starpu_mp_node *node,
  125. void (*kernel)(void), unsigned coreid,
  126. struct starpu_task *task)
  127. {
  128. return _starpu_src_common_execute_kernel(node, kernel, coreid,
  129. task->handles, task->interfaces, task->cl->nbuffers,
  130. task->cl_arg, task->cl_arg_size);
  131. }
  132. /* Send a request to the sink linked to the MP_NODE to allocate SIZE bytes on
  133. * the sink.
  134. * In case of success, it returns 0 and *ADDR contains the address of the
  135. * allocated area ;
  136. * else it returns 1 if the allocation fail.
  137. */
  138. int _starpu_src_common_allocate(const struct _starpu_mp_node *mp_node,
  139. void **addr, size_t size)
  140. {
  141. enum _starpu_mp_command answer;
  142. void *arg;
  143. int arg_size;
  144. _starpu_mp_common_send_command(mp_node, STARPU_ALLOCATE, &size,
  145. sizeof(size));
  146. answer = _starpu_mp_common_recv_command(mp_node, &arg, &arg_size);
  147. if (answer == STARPU_ERROR_ALLOCATE)
  148. return 1;
  149. STARPU_ASSERT(answer == STARPU_ANSWER_ALLOCATE &&
  150. arg_size == sizeof(*addr));
  151. memcpy(addr, arg, arg_size);
  152. return 0;
  153. }
  154. /* Send a request to the sink linked to the MP_NODE to deallocate the memory
  155. * area pointed by ADDR.
  156. */
  157. void _starpu_src_common_free(const struct _starpu_mp_node *mp_node,
  158. void *addr)
  159. {
  160. _starpu_mp_common_send_command(mp_node, STARPU_FREE, &addr, sizeof(addr));
  161. }
  162. /* Send SIZE bytes pointed by SRC to DST on the sink linked to the MP_NODE.
  163. */
  164. int _starpu_src_common_copy_host_to_sink(const struct _starpu_mp_node *mp_node,
  165. void *src, void *dst, size_t size)
  166. {
  167. struct _starpu_mp_transfer_command cmd = {size, dst};
  168. _starpu_mp_common_send_command(mp_node, STARPU_RECV_FROM_HOST, &cmd, sizeof(cmd));
  169. mp_node->dt_send(mp_node, src, size);
  170. return 0;
  171. }
  172. /* Receive SIZE bytes pointed by SRC on the sink linked to the MP_NODE and store them in DST.
  173. */
  174. int _starpu_src_common_copy_sink_to_host(const struct _starpu_mp_node *mp_node,
  175. void *src, void *dst, size_t size)
  176. {
  177. struct _starpu_mp_transfer_command cmd = {size, src};
  178. _starpu_mp_common_send_command(mp_node, STARPU_SEND_TO_HOST, &cmd, sizeof(cmd));
  179. mp_node->dt_recv(mp_node, dst, size);
  180. return 0;
  181. }
  182. /* Tell the sink linked to SRC_NODE to send SIZE bytes of data pointed by SRC
  183. * to the sink linked to DST_NODE. The latter store them in DST.
  184. */
  185. int _starpu_src_common_copy_sink_to_sink(const struct _starpu_mp_node *src_node,
  186. const struct _starpu_mp_node *dst_node, void *src, void *dst, size_t size)
  187. {
  188. enum _starpu_mp_command answer;
  189. void *arg;
  190. int arg_size;
  191. struct _starpu_mp_transfer_command_to_device cmd = {dst_node->peer_id, size, src};
  192. /* Tell source to send data to dest. */
  193. _starpu_mp_common_send_command(src_node, STARPU_SEND_TO_SINK, &cmd, sizeof(cmd));
  194. cmd.devid = src_node->peer_id;
  195. cmd.size = size;
  196. cmd.addr = dst;
  197. /* Tell dest to receive data from source. */
  198. _starpu_mp_common_send_command(dst_node, STARPU_RECV_FROM_SINK, &cmd, sizeof(cmd));
  199. /* Wait for answer from dest to know wether transfer is finished. */
  200. answer = _starpu_mp_common_recv_command(dst_node, &arg, &arg_size);
  201. STARPU_ASSERT(answer == STARPU_TRANSFER_COMPLETE);
  202. return 0;
  203. }
  204. /* 5 functions to determine the executable to run on the device (MIC, SCC,
  205. * MPI).
  206. */
  207. static void _starpu_src_common_cat_3(char *final, const char *first, const char *second,
  208. const char *third)
  209. {
  210. strcpy(final, first);
  211. strcat(final, second);
  212. strcat(final, third);
  213. }
  214. static void _starpu_src_common_cat_2(char *final, const char *first, const char *second)
  215. {
  216. _starpu_src_common_cat_3(final, first, second, "");
  217. }
  218. static void _starpu_src_common_dir_cat(char *final, const char *dir, const char *file)
  219. {
  220. if (file[0] == '/')
  221. ++file;
  222. size_t size = strlen(dir);
  223. if (dir[size - 1] == '/')
  224. _starpu_src_common_cat_2(final, dir, file);
  225. else
  226. _starpu_src_common_cat_3(final, dir, "/", file);
  227. }
  228. static int _starpu_src_common_test_suffixes(char *located_file_name, const char *base, const char **suffixes)
  229. {
  230. unsigned int i;
  231. for (i = 0; suffixes[i] != NULL; ++i)
  232. {
  233. _starpu_src_common_cat_2(located_file_name, base, suffixes[i]);
  234. if (access(located_file_name, R_OK) == 0)
  235. return 0;
  236. }
  237. return 1;
  238. }
  239. int _starpu_src_common_locate_file(char *located_file_name,
  240. const char *env_file_name, const char *env_mic_path,
  241. const char *config_file_name, const char *actual_file_name,
  242. const char **suffixes)
  243. {
  244. if (env_file_name != NULL)
  245. {
  246. if (access(env_file_name, R_OK) == 0)
  247. {
  248. strcpy(located_file_name, env_file_name);
  249. return 0;
  250. }
  251. else if(env_mic_path != NULL)
  252. {
  253. _starpu_src_common_dir_cat(located_file_name, env_mic_path, env_file_name);
  254. return access(located_file_name, R_OK);
  255. }
  256. }
  257. else if (config_file_name != NULL)
  258. {
  259. if (access(config_file_name, R_OK) == 0)
  260. {
  261. strcpy(located_file_name, config_file_name);
  262. return 0;
  263. }
  264. else if (env_mic_path != NULL)
  265. {
  266. _starpu_src_common_dir_cat(located_file_name, env_mic_path, config_file_name);
  267. return access(located_file_name, R_OK);
  268. }
  269. }
  270. else if (actual_file_name != NULL)
  271. {
  272. if (_starpu_src_common_test_suffixes(located_file_name, actual_file_name, suffixes) == 0)
  273. return 0;
  274. if (env_mic_path != NULL)
  275. {
  276. char actual_cpy[1024];
  277. strcpy(actual_cpy, actual_file_name);
  278. char *last = strrchr(actual_cpy, '/');
  279. while (last != NULL)
  280. {
  281. char tmp[1024];
  282. _starpu_src_common_dir_cat(tmp, env_mic_path, last);
  283. if (access(tmp, R_OK) == 0)
  284. {
  285. strcpy(located_file_name, tmp);
  286. return 0;
  287. }
  288. if (_starpu_src_common_test_suffixes(located_file_name, tmp, suffixes) == 0)
  289. return 0;
  290. *last = '\0';
  291. char *last_tmp = strrchr(actual_cpy, '/');
  292. *last = '/';
  293. last = last_tmp;
  294. }
  295. }
  296. }
  297. return 1;
  298. }