add_vectors_interface.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010-2011, 2013-2015, 2017 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2016 CNRS
  5. * Copyright (C) 2012 INRIA
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. /*
  19. * This is a small example of a C++ program using starpu. We here just
  20. * add two std::vector with duplicating vectors. StarPU achieves data
  21. * transfers between objects.
  22. */
  23. /* TODO : add support with old version of C++ about name fields in struct */
  24. #include <cassert>
  25. #include <vector>
  26. #ifdef PRINT_OUTPUT
  27. #include <iostream>
  28. #endif
  29. #include <starpu.h>
  30. #define MY_TYPE char, my_allocator<char>
  31. /* create an allocator to put data on the correct NUMA node */
  32. template <class T>
  33. class my_allocator
  34. {
  35. public:
  36. typedef size_t size_type;
  37. typedef ptrdiff_t difference_type;
  38. typedef T* pointer;
  39. typedef const T* const_pointer;
  40. typedef T& reference;
  41. typedef const T& const_reference;
  42. typedef T value_type;
  43. my_allocator()
  44. {
  45. this->node = STARPU_MAIN_RAM;
  46. }
  47. my_allocator(const my_allocator& a)
  48. {
  49. node = a.get_node();
  50. }
  51. my_allocator(const unsigned node)
  52. {
  53. this->node = node;
  54. }
  55. pointer allocate(size_type n, const void * = 0)
  56. {
  57. T* t = (T*) starpu_malloc_on_node(this->node, n * sizeof(T));
  58. return t;
  59. }
  60. void deallocate(void* p, size_type n)
  61. {
  62. if (p)
  63. {
  64. starpu_free_on_node(this->node, (uintptr_t) p, n * sizeof(T));
  65. }
  66. }
  67. unsigned get_node() const
  68. {
  69. return node;
  70. }
  71. pointer address(reference x) const
  72. {
  73. return &x;
  74. }
  75. const_pointer address(const_reference x) const
  76. {
  77. return &x;
  78. }
  79. my_allocator<T>& operator=(const my_allocator&)
  80. {
  81. return *this;
  82. }
  83. void construct(pointer p, const T& val)
  84. {
  85. new ((T*) p) T(val);
  86. }
  87. void destroy(pointer p)
  88. {
  89. p->~T();
  90. }
  91. size_type max_size() const
  92. {
  93. return size_type(-1);
  94. }
  95. template <class U>
  96. struct rebind
  97. {
  98. typedef my_allocator<U> other;
  99. };
  100. template <class U>
  101. my_allocator(const my_allocator<U>&)
  102. {
  103. }
  104. template <class U>
  105. my_allocator& operator=(const my_allocator<U>&)
  106. {
  107. return *this;
  108. }
  109. private:
  110. unsigned node;
  111. };
  112. /* Create a new interface to catch C++ vector and make appropriate data transfers */
  113. struct vector_cpp_interface
  114. {
  115. enum starpu_data_interface_id id;
  116. uintptr_t ptr;
  117. uintptr_t dev_handle;
  118. size_t offset;
  119. uint32_t nx;
  120. size_t elemsize;
  121. std::vector<MY_TYPE>* vec;
  122. uint32_t slice_base;
  123. };
  124. #define VECTOR_CPP_GET_VEC(interface) ({ (((struct vector_cpp_interface *)(interface))->vec); })
  125. static int vector_interface_copy_any_to_any(void *src_interface, unsigned src_node,
  126. void *dst_interface, unsigned dst_node, void *async_data);
  127. #if __cplusplus >= 201103L
  128. static const struct starpu_data_copy_methods vector_cpp_copy_data_methods_s =
  129. {
  130. .can_copy = NULL,
  131. .ram_to_ram = NULL,
  132. .ram_to_cuda = NULL,
  133. .ram_to_opencl = NULL,
  134. .ram_to_mic = NULL,
  135. .cuda_to_ram = NULL,
  136. .cuda_to_cuda = NULL,
  137. .cuda_to_opencl = NULL,
  138. .opencl_to_ram = NULL,
  139. .opencl_to_cuda = NULL,
  140. .opencl_to_opencl = NULL,
  141. .mic_to_ram = NULL,
  142. .scc_src_to_sink = NULL,
  143. .scc_sink_to_src = NULL,
  144. .scc_sink_to_sink = NULL,
  145. .ram_to_mpi_ms = NULL,
  146. .mpi_ms_to_ram = NULL,
  147. .mpi_ms_to_mpi_ms = NULL,
  148. .ram_to_cuda_async = NULL,
  149. .cuda_to_ram_async = NULL,
  150. .cuda_to_cuda_async = NULL,
  151. .ram_to_opencl_async = NULL,
  152. .opencl_to_ram_async = NULL,
  153. .opencl_to_opencl_async = NULL,
  154. .ram_to_mpi_ms_async = NULL,
  155. .mpi_ms_to_ram_async = NULL,
  156. .mpi_ms_to_mpi_ms_async = NULL,
  157. .ram_to_mic_async = NULL,
  158. .mic_to_ram_async = NULL,
  159. .any_to_any = vector_interface_copy_any_to_any,
  160. };
  161. #else
  162. static const struct starpu_data_copy_methods vector_cpp_copy_data_methods_s =
  163. {
  164. NULL,
  165. NULL,
  166. NULL,
  167. NULL,
  168. NULL,
  169. NULL,
  170. NULL,
  171. NULL,
  172. NULL,
  173. NULL,
  174. NULL,
  175. NULL,
  176. NULL,
  177. NULL,
  178. NULL,
  179. NULL,
  180. NULL,
  181. NULL,
  182. NULL,
  183. NULL,
  184. NULL,
  185. NULL,
  186. NULL,
  187. NULL,
  188. NULL,
  189. NULL,
  190. NULL,
  191. NULL,
  192. NULL,
  193. vector_interface_copy_any_to_any,
  194. };
  195. #endif
  196. static void register_vector_cpp_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface);
  197. static starpu_ssize_t allocate_vector_cpp_buffer_on_node(void *data_interface_, unsigned dst_node);
  198. static void *vector_cpp_handle_to_pointer(starpu_data_handle_t handle, unsigned node);
  199. static void free_vector_cpp_buffer_on_node(void *data_interface, unsigned node);
  200. static void free_vector_cpp_buffer_on_node(void *data_interface, unsigned node);
  201. static size_t vector_cpp_interface_get_size(starpu_data_handle_t handle);
  202. static uint32_t footprint_vector_cpp_interface_crc32(starpu_data_handle_t handle);
  203. static int vector_cpp_compare(void *data_interface_a, void *data_interface_b);
  204. static void display_vector_cpp_interface(starpu_data_handle_t handle, FILE *f);
  205. static int pack_vector_cpp_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count);
  206. static int unpack_vector_cpp_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count);
  207. static starpu_ssize_t vector_cpp_describe(void *data_interface, char *buf, size_t size);
  208. static struct starpu_data_interface_ops interface_vector_cpp_ops =
  209. {
  210. .register_data_handle = register_vector_cpp_handle,
  211. .allocate_data_on_node = allocate_vector_cpp_buffer_on_node,
  212. .free_data_on_node = free_vector_cpp_buffer_on_node,
  213. .copy_methods = &vector_cpp_copy_data_methods_s,
  214. .handle_to_pointer = vector_cpp_handle_to_pointer,
  215. .get_size = vector_cpp_interface_get_size,
  216. .footprint = footprint_vector_cpp_interface_crc32,
  217. .compare = vector_cpp_compare,
  218. .display = display_vector_cpp_interface,
  219. .describe = vector_cpp_describe,
  220. .interfaceid = STARPU_UNKNOWN_INTERFACE_ID,
  221. .interface_size = sizeof(struct vector_cpp_interface),
  222. .is_multiformat = 0,
  223. .dontcache = 0,
  224. .get_mf_ops = NULL,
  225. .pack_data = pack_vector_cpp_handle,
  226. .unpack_data = unpack_vector_cpp_handle,
  227. .name = (char *) "VECTOR_CPP_INTERFACE"
  228. };
  229. static void *vector_cpp_handle_to_pointer(starpu_data_handle_t handle, unsigned node)
  230. {
  231. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  232. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *)
  233. starpu_data_get_interface_on_node(handle, node);
  234. return (void*) vector_interface->ptr;
  235. }
  236. static void register_vector_cpp_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  237. {
  238. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *) data_interface;
  239. unsigned node;
  240. for (node = 0; node < STARPU_MAXNODES; node++)
  241. {
  242. struct vector_cpp_interface *local_interface = (struct vector_cpp_interface *)
  243. starpu_data_get_interface_on_node(handle, node);
  244. if (node == home_node)
  245. {
  246. local_interface->ptr = vector_interface->ptr;
  247. local_interface->dev_handle = vector_interface->dev_handle;
  248. local_interface->offset = vector_interface->offset;
  249. local_interface->vec = vector_interface->vec;
  250. }
  251. else
  252. {
  253. local_interface->ptr = 0;
  254. local_interface->dev_handle = 0;
  255. local_interface->offset = 0;
  256. local_interface->vec = NULL;;
  257. }
  258. local_interface->id = vector_interface->id;
  259. local_interface->nx = vector_interface->nx;
  260. local_interface->elemsize = vector_interface->elemsize;
  261. local_interface->slice_base = vector_interface->slice_base;
  262. }
  263. }
  264. /* declare a new data with the vector interface */
  265. void vector_cpp_data_register(starpu_data_handle_t *handleptr, int home_node,
  266. std::vector<MY_TYPE>* vec, uint32_t nx, size_t elemsize)
  267. {
  268. struct vector_cpp_interface vector =
  269. {
  270. .id = STARPU_UNKNOWN_INTERFACE_ID,
  271. .ptr = (uintptr_t) &(*vec)[0],
  272. .dev_handle = (uintptr_t) &(*vec)[0],
  273. .offset = 0,
  274. .nx = nx,
  275. .elemsize = elemsize,
  276. .vec = vec,
  277. .slice_base = 0
  278. };
  279. starpu_data_register(handleptr, home_node, &vector, &interface_vector_cpp_ops);
  280. }
  281. void vector_cpp_ptr_register(starpu_data_handle_t handle, unsigned node,
  282. uintptr_t ptr, uintptr_t dev_handle, size_t offset)
  283. {
  284. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *) starpu_data_get_interface_on_node(handle, node);
  285. starpu_data_ptr_register(handle, node);
  286. vector_interface->ptr = ptr;
  287. vector_interface->dev_handle = dev_handle;
  288. vector_interface->offset = offset;
  289. }
  290. /* offer an access to the data parameters */
  291. uint32_t vector_cpp_get_nx(starpu_data_handle_t handle)
  292. {
  293. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *)
  294. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  295. return vector_interface->nx;
  296. }
  297. static uint32_t footprint_vector_cpp_interface_crc32(starpu_data_handle_t handle)
  298. {
  299. return starpu_hash_crc32c_be(vector_cpp_get_nx(handle), 0);
  300. }
  301. static int vector_cpp_compare(void *data_interface_a, void *data_interface_b)
  302. {
  303. struct vector_cpp_interface *vector_a = (struct vector_cpp_interface *) data_interface_a;
  304. struct vector_cpp_interface *vector_b = (struct vector_cpp_interface *) data_interface_b;
  305. /* Two vectors are considered compatible if they have the same size */
  306. return ((vector_a->nx == vector_b->nx)
  307. && (vector_a->elemsize == vector_b->elemsize));
  308. }
  309. static void display_vector_cpp_interface(starpu_data_handle_t handle, FILE *f)
  310. {
  311. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *)
  312. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  313. fprintf(f, "%u\t", vector_interface->nx);
  314. }
  315. static int pack_vector_cpp_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count)
  316. {
  317. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  318. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *)
  319. starpu_data_get_interface_on_node(handle, node);
  320. *count = vector_interface->nx*vector_interface->elemsize;
  321. if (ptr != NULL)
  322. {
  323. starpu_malloc_flags(ptr, *count, 0);
  324. memcpy(*ptr, (void*)vector_interface->ptr, vector_interface->elemsize*vector_interface->nx);
  325. }
  326. return 0;
  327. }
  328. static int unpack_vector_cpp_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  329. {
  330. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  331. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *)
  332. starpu_data_get_interface_on_node(handle, node);
  333. STARPU_ASSERT(count == vector_interface->elemsize * vector_interface->nx);
  334. memcpy((void*)vector_interface->ptr, ptr, count);
  335. return 0;
  336. }
  337. static size_t vector_cpp_interface_get_size(starpu_data_handle_t handle)
  338. {
  339. size_t size;
  340. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *)
  341. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  342. size = vector_interface->nx*vector_interface->elemsize;
  343. return size;
  344. }
  345. size_t vector_cpp_get_elemsize(starpu_data_handle_t handle)
  346. {
  347. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *)
  348. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  349. return vector_interface->elemsize;
  350. }
  351. /* memory allocation/deallocation primitives for the vector interface */
  352. /* returns the size of the allocated area */
  353. static starpu_ssize_t allocate_vector_cpp_buffer_on_node(void *data_interface_, unsigned dst_node)
  354. {
  355. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *) data_interface_;
  356. uint32_t nx = vector_interface->nx;
  357. size_t elemsize = vector_interface->elemsize;
  358. starpu_ssize_t allocated_memory;
  359. const my_allocator<char> allocator(dst_node);
  360. std::vector<MY_TYPE> * vec = new std::vector<MY_TYPE>(nx, 0, allocator);
  361. vector_interface->vec = vec;
  362. if (!vector_interface->vec)
  363. return -ENOMEM;
  364. allocated_memory = nx*elemsize;
  365. /* update the data properly in consequence */
  366. vector_interface->ptr = (uintptr_t) &((*vec)[0]);
  367. vector_interface->dev_handle = (uintptr_t) &((*vec)[0]);
  368. vector_interface->offset = 0;
  369. return allocated_memory;
  370. }
  371. static void free_vector_cpp_buffer_on_node(void *data_interface, unsigned node)
  372. {
  373. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *) data_interface;
  374. uint32_t nx = vector_interface->nx;
  375. size_t elemsize = vector_interface->elemsize;
  376. delete vector_interface->vec;
  377. }
  378. static int vector_interface_copy_any_to_any(void *src_interface, unsigned src_node,
  379. void *dst_interface, unsigned dst_node, void *async_data)
  380. {
  381. struct vector_cpp_interface *src_vector = (struct vector_cpp_interface *) src_interface;
  382. struct vector_cpp_interface *dst_vector = (struct vector_cpp_interface *) dst_interface;
  383. int ret;
  384. ret = starpu_interface_copy(src_vector->dev_handle, src_vector->offset, src_node,
  385. dst_vector->dev_handle, dst_vector->offset, dst_node,
  386. src_vector->nx*src_vector->elemsize, async_data);
  387. return ret;
  388. }
  389. static starpu_ssize_t vector_cpp_describe(void *data_interface, char *buf, size_t size)
  390. {
  391. struct vector_cpp_interface *vector = (struct vector_cpp_interface *) data_interface;
  392. return snprintf(buf, size, "V%ux%u",
  393. (unsigned) vector->nx,
  394. (unsigned) vector->elemsize);
  395. }
  396. void cpu_kernel_add_vectors(void *buffers[], void *cl_arg)
  397. {
  398. std::vector<MY_TYPE>* vec_A = VECTOR_CPP_GET_VEC(buffers[0]);
  399. std::vector<MY_TYPE>* vec_B = VECTOR_CPP_GET_VEC(buffers[1]);
  400. std::vector<MY_TYPE>* vec_C = VECTOR_CPP_GET_VEC(buffers[2]);
  401. // all the std::vector have to have the same size
  402. assert(vec_A->size() == vec_B->size() && vec_B->size() == vec_C->size());
  403. // performs the vector addition (vec_C[] = vec_A[] + vec_B[])
  404. for (size_t i = 0; i < vec_C->size(); i++)
  405. (*vec_C)[i] = (*vec_A)[i] + (*vec_B)[i];
  406. }
  407. #define VEC_SIZE 1024
  408. int main(int argc, char **argv)
  409. {
  410. struct starpu_conf conf;
  411. starpu_conf_init(&conf);
  412. conf.nmic = 0;
  413. conf.nscc = 0;
  414. conf.nmpi_ms = 0;
  415. // initialize StarPU with default configuration
  416. int ret = starpu_init(&conf);
  417. if (ret == -ENODEV)
  418. return 77;
  419. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  420. /* Test data transfers between NUMA nodes if available */
  421. unsigned last_numa_node = starpu_memory_nodes_get_numa_count() - 1;
  422. const my_allocator<char> allocator_main_ram(STARPU_MAIN_RAM);
  423. const my_allocator<char> allocator_last_numa(last_numa_node);
  424. std::vector<MY_TYPE> vec_A(VEC_SIZE, 2, allocator_main_ram); // all the vector is initialized to 2
  425. std::vector<MY_TYPE> vec_B(VEC_SIZE, 3, allocator_main_ram); // all the vector is initialized to 3
  426. std::vector<MY_TYPE> vec_C(VEC_SIZE, 0, allocator_last_numa); // all the vector is initialized to 0
  427. // StarPU data registering
  428. starpu_data_handle_t spu_vec_A;
  429. starpu_data_handle_t spu_vec_B;
  430. starpu_data_handle_t spu_vec_C;
  431. // give the data of the vector to StarPU (C array)
  432. vector_cpp_data_register(&spu_vec_A, STARPU_MAIN_RAM, &vec_A, vec_A.size(), sizeof(char));
  433. vector_cpp_data_register(&spu_vec_B, STARPU_MAIN_RAM, &vec_B, vec_B.size(), sizeof(char));
  434. vector_cpp_data_register(&spu_vec_C, last_numa_node, &vec_C, vec_C.size(), sizeof(char));
  435. // create the StarPU codelet
  436. starpu_codelet cl;
  437. starpu_codelet_init(&cl);
  438. cl.cpu_funcs [0] = cpu_kernel_add_vectors;
  439. cl.cpu_funcs_name[0] = "cpu_kernel_add_vectors";
  440. cl.nbuffers = 3;
  441. cl.modes [0] = STARPU_R;
  442. cl.modes [1] = STARPU_R;
  443. cl.modes [2] = STARPU_W;
  444. cl.name = "add_vectors";
  445. // submit a new StarPU task to execute
  446. ret = starpu_task_insert(&cl,
  447. STARPU_R, spu_vec_A,
  448. STARPU_R, spu_vec_B,
  449. STARPU_W, spu_vec_C,
  450. 0);
  451. if (ret == -ENODEV)
  452. {
  453. // StarPU data unregistering
  454. starpu_data_unregister(spu_vec_C);
  455. starpu_data_unregister(spu_vec_B);
  456. starpu_data_unregister(spu_vec_A);
  457. // terminate StarPU, no task can be submitted after
  458. starpu_shutdown();
  459. return 77;
  460. }
  461. STARPU_CHECK_RETURN_VALUE(ret, "task_submit::add_vectors");
  462. // wait the task
  463. starpu_task_wait_for_all();
  464. // StarPU data unregistering
  465. starpu_data_unregister(spu_vec_C);
  466. starpu_data_unregister(spu_vec_B);
  467. starpu_data_unregister(spu_vec_A);
  468. // terminate StarPU, no task can be submitted after
  469. starpu_shutdown();
  470. // check results
  471. bool fail = false;
  472. int i = 0;
  473. while (!fail && i < VEC_SIZE)
  474. fail = vec_C[i++] != 5;
  475. if (fail)
  476. {
  477. #ifdef PRINT_OUTPUT
  478. std::cout << "Example failed..." << std::endl;
  479. #endif
  480. return EXIT_FAILURE;
  481. }
  482. else
  483. {
  484. #ifdef PRINT_OUTPUT
  485. std::cout << "Example successfully passed!" << std::endl;
  486. #endif
  487. return EXIT_SUCCESS;
  488. }
  489. }