add_vectors_interface.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010-2011, 2013-2015 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. static const struct starpu_data_copy_methods vector_cpp_copy_data_methods_s =
  128. {
  129. .can_copy = NULL,
  130. .ram_to_ram = NULL,
  131. .ram_to_cuda = NULL,
  132. .ram_to_opencl = NULL,
  133. .ram_to_mic = NULL,
  134. .cuda_to_ram = NULL,
  135. .cuda_to_cuda = NULL,
  136. .cuda_to_opencl = NULL,
  137. .opencl_to_ram = NULL,
  138. .opencl_to_cuda = NULL,
  139. .opencl_to_opencl = NULL,
  140. .mic_to_ram = NULL,
  141. .scc_src_to_sink = NULL,
  142. .scc_sink_to_src = NULL,
  143. .scc_sink_to_sink = NULL,
  144. .ram_to_mpi_ms = NULL,
  145. .mpi_ms_to_ram = NULL,
  146. .mpi_ms_to_mpi_ms = NULL,
  147. #ifdef STARPU_USE_CUDA
  148. .ram_to_cuda_async = NULL,
  149. .cuda_to_ram_async = NULL,
  150. .cuda_to_cuda_async = NULL,
  151. #else
  152. .ram_to_cuda_async = NULL,
  153. .cuda_to_ram_async = NULL,
  154. .cuda_to_cuda_async = NULL,
  155. #endif
  156. #if defined(STARPU_USE_OPENCL) && !defined(__CUDACC__)
  157. .ram_to_opencl_async = NULL,
  158. .opencl_to_ram_async = NULL,
  159. .opencl_to_opencl_async = NULL,
  160. #else
  161. .ram_to_opencl_async = NULL,
  162. .opencl_to_ram_async = NULL,
  163. .opencl_to_opencl_async = NULL,
  164. #endif
  165. .ram_to_mpi_ms_async = NULL,
  166. .mpi_ms_to_ram_async = NULL,
  167. .mpi_ms_to_mpi_ms_async = NULL,
  168. .ram_to_mic_async = NULL,
  169. .mic_to_ram_async = NULL,
  170. .any_to_any = vector_interface_copy_any_to_any,
  171. };
  172. static void register_vector_cpp_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface);
  173. static starpu_ssize_t allocate_vector_cpp_buffer_on_node(void *data_interface_, unsigned dst_node);
  174. static void *vector_cpp_handle_to_pointer(starpu_data_handle_t handle, unsigned node);
  175. static void free_vector_cpp_buffer_on_node(void *data_interface, unsigned node);
  176. static void free_vector_cpp_buffer_on_node(void *data_interface, unsigned node);
  177. static size_t vector_cpp_interface_get_size(starpu_data_handle_t handle);
  178. static uint32_t footprint_vector_cpp_interface_crc32(starpu_data_handle_t handle);
  179. static int vector_cpp_compare(void *data_interface_a, void *data_interface_b);
  180. static void display_vector_cpp_interface(starpu_data_handle_t handle, FILE *f);
  181. static int pack_vector_cpp_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count);
  182. static int unpack_vector_cpp_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count);
  183. static starpu_ssize_t vector_cpp_describe(void *data_interface, char *buf, size_t size);
  184. static struct starpu_data_interface_ops interface_vector_cpp_ops =
  185. {
  186. .register_data_handle = register_vector_cpp_handle,
  187. .allocate_data_on_node = allocate_vector_cpp_buffer_on_node,
  188. .free_data_on_node = free_vector_cpp_buffer_on_node,
  189. .copy_methods = &vector_cpp_copy_data_methods_s,
  190. .handle_to_pointer = vector_cpp_handle_to_pointer,
  191. .get_size = vector_cpp_interface_get_size,
  192. .footprint = footprint_vector_cpp_interface_crc32,
  193. .compare = vector_cpp_compare,
  194. .display = display_vector_cpp_interface,
  195. .describe = vector_cpp_describe,
  196. .interfaceid = STARPU_UNKNOWN_INTERFACE_ID,
  197. .interface_size = sizeof(struct vector_cpp_interface),
  198. .is_multiformat = 0,
  199. .dontcache = 0,
  200. .get_mf_ops = NULL,
  201. .pack_data = pack_vector_cpp_handle,
  202. .unpack_data = unpack_vector_cpp_handle,
  203. .name = (char *) "VECTOR_CPP_INTERFACE"
  204. };
  205. static void *vector_cpp_handle_to_pointer(starpu_data_handle_t handle, unsigned node)
  206. {
  207. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  208. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *)
  209. starpu_data_get_interface_on_node(handle, node);
  210. return (void*) vector_interface->ptr;
  211. }
  212. static void register_vector_cpp_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  213. {
  214. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *) data_interface;
  215. unsigned node;
  216. for (node = 0; node < STARPU_MAXNODES; node++)
  217. {
  218. struct vector_cpp_interface *local_interface = (struct vector_cpp_interface *)
  219. starpu_data_get_interface_on_node(handle, node);
  220. if (node == home_node)
  221. {
  222. local_interface->ptr = vector_interface->ptr;
  223. local_interface->dev_handle = vector_interface->dev_handle;
  224. local_interface->offset = vector_interface->offset;
  225. local_interface->vec = vector_interface->vec;
  226. }
  227. else
  228. {
  229. local_interface->ptr = 0;
  230. local_interface->dev_handle = 0;
  231. local_interface->offset = 0;
  232. local_interface->vec = NULL;;
  233. }
  234. local_interface->id = vector_interface->id;
  235. local_interface->nx = vector_interface->nx;
  236. local_interface->elemsize = vector_interface->elemsize;
  237. local_interface->slice_base = vector_interface->slice_base;
  238. }
  239. }
  240. /* declare a new data with the vector interface */
  241. void vector_cpp_data_register(starpu_data_handle_t *handleptr, int home_node,
  242. std::vector<MY_TYPE>* vec, uint32_t nx, size_t elemsize)
  243. {
  244. struct vector_cpp_interface vector =
  245. {
  246. .id = STARPU_UNKNOWN_INTERFACE_ID,
  247. .ptr = (uintptr_t) &(*vec)[0],
  248. .dev_handle = (uintptr_t) &(*vec)[0],
  249. .offset = 0,
  250. .nx = nx,
  251. .elemsize = elemsize,
  252. .vec = vec,
  253. .slice_base = 0
  254. };
  255. starpu_data_register(handleptr, home_node, &vector, &interface_vector_cpp_ops);
  256. }
  257. void vector_cpp_ptr_register(starpu_data_handle_t handle, unsigned node,
  258. uintptr_t ptr, uintptr_t dev_handle, size_t offset)
  259. {
  260. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *) starpu_data_get_interface_on_node(handle, node);
  261. starpu_data_ptr_register(handle, node);
  262. vector_interface->ptr = ptr;
  263. vector_interface->dev_handle = dev_handle;
  264. vector_interface->offset = offset;
  265. }
  266. /* offer an access to the data parameters */
  267. uint32_t vector_cpp_get_nx(starpu_data_handle_t handle)
  268. {
  269. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *)
  270. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  271. return vector_interface->nx;
  272. }
  273. static uint32_t footprint_vector_cpp_interface_crc32(starpu_data_handle_t handle)
  274. {
  275. return starpu_hash_crc32c_be(vector_cpp_get_nx(handle), 0);
  276. }
  277. static int vector_cpp_compare(void *data_interface_a, void *data_interface_b)
  278. {
  279. struct vector_cpp_interface *vector_a = (struct vector_cpp_interface *) data_interface_a;
  280. struct vector_cpp_interface *vector_b = (struct vector_cpp_interface *) data_interface_b;
  281. /* Two vectors are considered compatible if they have the same size */
  282. return ((vector_a->nx == vector_b->nx)
  283. && (vector_a->elemsize == vector_b->elemsize));
  284. }
  285. static void display_vector_cpp_interface(starpu_data_handle_t handle, FILE *f)
  286. {
  287. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *)
  288. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  289. fprintf(f, "%u\t", vector_interface->nx);
  290. }
  291. static int pack_vector_cpp_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count)
  292. {
  293. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  294. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *)
  295. starpu_data_get_interface_on_node(handle, node);
  296. *count = vector_interface->nx*vector_interface->elemsize;
  297. if (ptr != NULL)
  298. {
  299. starpu_malloc_flags(ptr, *count, 0);
  300. memcpy(*ptr, (void*)vector_interface->ptr, vector_interface->elemsize*vector_interface->nx);
  301. }
  302. return 0;
  303. }
  304. static int unpack_vector_cpp_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  305. {
  306. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  307. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *)
  308. starpu_data_get_interface_on_node(handle, node);
  309. STARPU_ASSERT(count == vector_interface->elemsize * vector_interface->nx);
  310. memcpy((void*)vector_interface->ptr, ptr, count);
  311. return 0;
  312. }
  313. static size_t vector_cpp_interface_get_size(starpu_data_handle_t handle)
  314. {
  315. size_t size;
  316. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *)
  317. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  318. size = vector_interface->nx*vector_interface->elemsize;
  319. return size;
  320. }
  321. size_t vector_cpp_get_elemsize(starpu_data_handle_t handle)
  322. {
  323. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *)
  324. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  325. return vector_interface->elemsize;
  326. }
  327. /* memory allocation/deallocation primitives for the vector interface */
  328. /* returns the size of the allocated area */
  329. static starpu_ssize_t allocate_vector_cpp_buffer_on_node(void *data_interface_, unsigned dst_node)
  330. {
  331. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *) data_interface_;
  332. uint32_t nx = vector_interface->nx;
  333. size_t elemsize = vector_interface->elemsize;
  334. starpu_ssize_t allocated_memory;
  335. const my_allocator<char> allocator(dst_node);
  336. std::vector<MY_TYPE> * vec = new std::vector<MY_TYPE>(nx, 0, allocator);
  337. vector_interface->vec = vec;
  338. if (!vector_interface->vec)
  339. return -ENOMEM;
  340. allocated_memory = nx*elemsize;
  341. /* update the data properly in consequence */
  342. vector_interface->ptr = (uintptr_t) &((*vec)[0]);
  343. vector_interface->dev_handle = (uintptr_t) &((*vec)[0]);
  344. vector_interface->offset = 0;
  345. return allocated_memory;
  346. }
  347. static void free_vector_cpp_buffer_on_node(void *data_interface, unsigned node)
  348. {
  349. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *) data_interface;
  350. uint32_t nx = vector_interface->nx;
  351. size_t elemsize = vector_interface->elemsize;
  352. delete vector_interface->vec;
  353. }
  354. static int vector_interface_copy_any_to_any(void *src_interface, unsigned src_node,
  355. void *dst_interface, unsigned dst_node, void *async_data)
  356. {
  357. struct vector_cpp_interface *src_vector = (struct vector_cpp_interface *) src_interface;
  358. struct vector_cpp_interface *dst_vector = (struct vector_cpp_interface *) dst_interface;
  359. int ret;
  360. ret = starpu_interface_copy(src_vector->dev_handle, src_vector->offset, src_node,
  361. dst_vector->dev_handle, dst_vector->offset, dst_node,
  362. src_vector->nx*src_vector->elemsize, async_data);
  363. return ret;
  364. }
  365. static starpu_ssize_t vector_cpp_describe(void *data_interface, char *buf, size_t size)
  366. {
  367. struct vector_cpp_interface *vector = (struct vector_cpp_interface *) data_interface;
  368. return snprintf(buf, size, "V%ux%u",
  369. (unsigned) vector->nx,
  370. (unsigned) vector->elemsize);
  371. }
  372. void cpu_kernel_add_vectors(void *buffers[], void *cl_arg)
  373. {
  374. std::vector<MY_TYPE>* vec_A = VECTOR_CPP_GET_VEC(buffers[0]);
  375. std::vector<MY_TYPE>* vec_B = VECTOR_CPP_GET_VEC(buffers[1]);
  376. std::vector<MY_TYPE>* vec_C = VECTOR_CPP_GET_VEC(buffers[2]);
  377. // all the std::vector have to have the same size
  378. assert(vec_A->size() == vec_B->size() && vec_B->size() == vec_C->size());
  379. // performs the vector addition (vec_C[] = vec_A[] + vec_B[])
  380. for (size_t i = 0; i < vec_C->size(); i++)
  381. (*vec_C)[i] = (*vec_A)[i] + (*vec_B)[i];
  382. }
  383. #define VEC_SIZE 1024
  384. int main(int argc, char **argv)
  385. {
  386. struct starpu_conf conf;
  387. starpu_conf_init(&conf);
  388. conf.nmic = 0;
  389. conf.nscc = 0;
  390. conf.nmpi_ms = 0;
  391. // initialize StarPU with default configuration
  392. int ret = starpu_init(&conf);
  393. if (ret == -ENODEV)
  394. return 77;
  395. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  396. /* Test data transfers between NUMA nodes if available */
  397. unsigned last_numa_node = starpu_memory_nodes_get_numa_count() - 1;
  398. const my_allocator<char> allocator_main_ram(STARPU_MAIN_RAM);
  399. const my_allocator<char> allocator_last_numa(last_numa_node);
  400. std::vector<MY_TYPE> vec_A(VEC_SIZE, 2, allocator_main_ram); // all the vector is initialized to 2
  401. std::vector<MY_TYPE> vec_B(VEC_SIZE, 3, allocator_main_ram); // all the vector is initialized to 3
  402. std::vector<MY_TYPE> vec_C(VEC_SIZE, 0, allocator_last_numa); // all the vector is initialized to 0
  403. // StarPU data registering
  404. starpu_data_handle_t spu_vec_A;
  405. starpu_data_handle_t spu_vec_B;
  406. starpu_data_handle_t spu_vec_C;
  407. // give the data of the vector to StarPU (C array)
  408. vector_cpp_data_register(&spu_vec_A, STARPU_MAIN_RAM, &vec_A, vec_A.size(), sizeof(char));
  409. vector_cpp_data_register(&spu_vec_B, STARPU_MAIN_RAM, &vec_B, vec_B.size(), sizeof(char));
  410. vector_cpp_data_register(&spu_vec_C, last_numa_node, &vec_C, vec_C.size(), sizeof(char));
  411. // create the StarPU codelet
  412. starpu_codelet cl;
  413. starpu_codelet_init(&cl);
  414. cl.cpu_funcs [0] = cpu_kernel_add_vectors;
  415. cl.cpu_funcs_name[0] = "cpu_kernel_add_vectors";
  416. cl.nbuffers = 3;
  417. cl.modes [0] = STARPU_R;
  418. cl.modes [1] = STARPU_R;
  419. cl.modes [2] = STARPU_W;
  420. cl.name = "add_vectors";
  421. // submit a new StarPU task to execute
  422. ret = starpu_task_insert(&cl,
  423. STARPU_R, spu_vec_A,
  424. STARPU_R, spu_vec_B,
  425. STARPU_W, spu_vec_C,
  426. 0);
  427. if (ret == -ENODEV)
  428. {
  429. // StarPU data unregistering
  430. starpu_data_unregister(spu_vec_C);
  431. starpu_data_unregister(spu_vec_B);
  432. starpu_data_unregister(spu_vec_A);
  433. // terminate StarPU, no task can be submitted after
  434. starpu_shutdown();
  435. return 77;
  436. }
  437. STARPU_CHECK_RETURN_VALUE(ret, "task_submit::add_vectors");
  438. // wait the task
  439. starpu_task_wait_for_all();
  440. // StarPU data unregistering
  441. starpu_data_unregister(spu_vec_C);
  442. starpu_data_unregister(spu_vec_B);
  443. starpu_data_unregister(spu_vec_A);
  444. // terminate StarPU, no task can be submitted after
  445. starpu_shutdown();
  446. // check results
  447. bool fail = false;
  448. int i = 0;
  449. while (!fail && i < VEC_SIZE)
  450. fail = vec_C[i++] != 5;
  451. if (fail)
  452. {
  453. #ifdef PRINT_OUTPUT
  454. std::cout << "Example failed..." << std::endl;
  455. #endif
  456. return EXIT_FAILURE;
  457. }
  458. else
  459. {
  460. #ifdef PRINT_OUTPUT
  461. std::cout << "Example successfully passed!" << std::endl;
  462. #endif
  463. return EXIT_SUCCESS;
  464. }
  465. }