add_vectors_interface.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), 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. /*
  17. * This is a small example of a C++ program using STL and starpu. We here just
  18. * add two std::vector with duplicating vectors. StarPU achieves data
  19. * transfers between objects.
  20. */
  21. #if defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNU_MINOR < 9))
  22. int main(int argc, char **argv)
  23. {
  24. return 77;
  25. }
  26. #else
  27. #include <cassert>
  28. #include <vector>
  29. #ifdef PRINT_OUTPUT
  30. #include <iostream>
  31. #endif
  32. #include <starpu.h>
  33. #define MY_TYPE char, my_allocator<char>
  34. /* create an allocator to put data on the correct NUMA node */
  35. template <class T>
  36. class my_allocator
  37. {
  38. public:
  39. typedef size_t size_type;
  40. typedef ptrdiff_t difference_type;
  41. typedef T* pointer;
  42. typedef const T* const_pointer;
  43. typedef T& reference;
  44. typedef const T& const_reference;
  45. typedef T value_type;
  46. my_allocator()
  47. {
  48. this->node = STARPU_MAIN_RAM;
  49. }
  50. my_allocator(const my_allocator& a)
  51. {
  52. node = a.get_node();
  53. }
  54. explicit my_allocator(const unsigned thenode)
  55. {
  56. this->node = thenode;
  57. }
  58. pointer allocate(size_type n, const void * = 0)
  59. {
  60. T* t = (T*) starpu_malloc_on_node(this->node, n * sizeof(T));
  61. return t;
  62. }
  63. void deallocate(void* p, size_type n)
  64. {
  65. if (p)
  66. {
  67. starpu_free_on_node(this->node, (uintptr_t) p, n * sizeof(T));
  68. }
  69. }
  70. unsigned get_node() const
  71. {
  72. return node;
  73. }
  74. pointer address(reference x) const
  75. {
  76. return &x;
  77. }
  78. const_pointer address(const_reference x) const
  79. {
  80. return &x;
  81. }
  82. my_allocator<T>& operator=(const my_allocator&ref)
  83. {
  84. node = ref.node;
  85. return *this;
  86. }
  87. void construct(pointer p, const T& val)
  88. {
  89. new ((T*) p) T(val);
  90. }
  91. void destroy(pointer p)
  92. {
  93. p->~T();
  94. }
  95. size_type max_size() const
  96. {
  97. return size_type(-1);
  98. }
  99. template <class U>
  100. struct rebind
  101. {
  102. typedef my_allocator<U> other;
  103. };
  104. template <class U>
  105. explicit my_allocator(const my_allocator<U>&ref)
  106. {
  107. node = ref.node;
  108. }
  109. template <class U>
  110. my_allocator<U>& operator=(const my_allocator<U>&ref)
  111. {
  112. node = ref.node;
  113. return *this;
  114. }
  115. private:
  116. unsigned node;
  117. };
  118. /*
  119. * Create a new interface to catch C++ vector and make appropriate data transfers
  120. */
  121. struct vector_cpp_interface
  122. {
  123. enum starpu_data_interface_id id;
  124. uintptr_t ptr;
  125. uintptr_t dev_handle;
  126. size_t offset;
  127. uint32_t nx;
  128. size_t elemsize;
  129. std::vector<MY_TYPE>* vec;
  130. uint32_t slice_base;
  131. };
  132. #define VECTOR_CPP_GET_VEC(interface) ({ (((struct vector_cpp_interface *)(interface))->vec); })
  133. static int vector_interface_copy_any_to_any(void *src_interface, unsigned src_node,
  134. void *dst_interface, unsigned dst_node, void *async_data);
  135. #if __cplusplus >= 201103L
  136. static const struct starpu_data_copy_methods vector_cpp_copy_data_methods_s =
  137. {
  138. .can_copy = NULL,
  139. .ram_to_ram = NULL,
  140. .ram_to_cuda = NULL,
  141. .ram_to_opencl = NULL,
  142. .ram_to_fpga = NULL,
  143. .ram_to_mic = NULL,
  144. .cuda_to_ram = NULL,
  145. .cuda_to_cuda = NULL,
  146. .opencl_to_ram = NULL,
  147. .opencl_to_opencl = NULL,
  148. .fpga_to_ram = NULL,
  149. .mic_to_ram = NULL,
  150. .ram_to_mpi_ms = NULL,
  151. .mpi_ms_to_ram = NULL,
  152. .mpi_ms_to_mpi_ms = NULL,
  153. .ram_to_cuda_async = NULL,
  154. .cuda_to_ram_async = NULL,
  155. .cuda_to_cuda_async = NULL,
  156. .ram_to_opencl_async = NULL,
  157. .opencl_to_ram_async = NULL,
  158. .opencl_to_opencl_async = NULL,
  159. .ram_to_fpga_async = NULL,
  160. .fpga_to_ram_async = NULL,
  161. .ram_to_mic_async = NULL,
  162. .mic_to_ram_async = NULL,
  163. .ram_to_mpi_ms_async = NULL,
  164. .mpi_ms_to_ram_async = NULL,
  165. .mpi_ms_to_mpi_ms_async = NULL,
  166. .any_to_any = vector_interface_copy_any_to_any,
  167. };
  168. #else
  169. static const struct starpu_data_copy_methods vector_cpp_copy_data_methods_s =
  170. {
  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. NULL,
  194. NULL,
  195. NULL,
  196. NULL,
  197. NULL,
  198. NULL,
  199. vector_interface_copy_any_to_any,
  200. };
  201. #endif
  202. static void register_vector_cpp_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface);
  203. static starpu_ssize_t allocate_vector_cpp_buffer_on_node(void *data_interface_, unsigned dst_node);
  204. static void *vector_cpp_to_pointer(void *data_interface, unsigned node);
  205. static int vector_cpp_pointer_is_inside(void *data_interface, unsigned node, void *ptr);
  206. static void free_vector_cpp_buffer_on_node(void *data_interface, unsigned node);
  207. static void free_vector_cpp_buffer_on_node(void *data_interface, unsigned node);
  208. static size_t vector_cpp_interface_get_size(starpu_data_handle_t handle);
  209. static uint32_t footprint_vector_cpp_interface_crc32(starpu_data_handle_t handle);
  210. static int vector_cpp_compare(void *data_interface_a, void *data_interface_b);
  211. static void display_vector_cpp_interface(starpu_data_handle_t handle, FILE *f);
  212. static int pack_vector_cpp_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count);
  213. static int peek_vector_cpp_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count);
  214. static int unpack_vector_cpp_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count);
  215. static starpu_ssize_t vector_cpp_describe(void *data_interface, char *buf, size_t size);
  216. #if __cplusplus >= 201103L
  217. static struct starpu_data_interface_ops interface_vector_cpp_ops =
  218. {
  219. .register_data_handle = register_vector_cpp_handle,
  220. .allocate_data_on_node = allocate_vector_cpp_buffer_on_node,
  221. .free_data_on_node = free_vector_cpp_buffer_on_node,
  222. .init = NULL,
  223. .copy_methods = &vector_cpp_copy_data_methods_s,
  224. .handle_to_pointer = NULL,
  225. .to_pointer = vector_cpp_to_pointer,
  226. .pointer_is_inside = vector_cpp_pointer_is_inside,
  227. .get_size = vector_cpp_interface_get_size,
  228. .get_alloc_size = NULL,
  229. .get_max_size = NULL,
  230. .footprint = footprint_vector_cpp_interface_crc32,
  231. .alloc_footprint = NULL,
  232. .compare = vector_cpp_compare,
  233. .alloc_compare = NULL,
  234. .display = display_vector_cpp_interface,
  235. .describe = vector_cpp_describe,
  236. .interfaceid = STARPU_UNKNOWN_INTERFACE_ID,
  237. .interface_size = sizeof(struct vector_cpp_interface),
  238. .is_multiformat = 0,
  239. .dontcache = 0,
  240. .get_mf_ops = NULL,
  241. .pack_data = pack_vector_cpp_handle,
  242. .peek_data = peek_vector_cpp_handle,
  243. .unpack_data = unpack_vector_cpp_handle,
  244. .name = (char *) "VECTOR_CPP_INTERFACE"
  245. };
  246. #else
  247. static struct starpu_data_interface_ops interface_vector_cpp_ops =
  248. {
  249. register_vector_cpp_handle,
  250. allocate_vector_cpp_buffer_on_node,
  251. free_vector_cpp_buffer_on_node,
  252. NULL,
  253. &vector_cpp_copy_data_methods_s,
  254. vector_cpp_to_pointer,
  255. vector_cpp_pointer_is_inside,
  256. vector_cpp_interface_get_size,
  257. NULL,
  258. NULL,
  259. footprint_vector_cpp_interface_crc32,
  260. NULL,
  261. vector_cpp_compare,
  262. NULL,
  263. display_vector_cpp_interface,
  264. vector_cpp_describe,
  265. STARPU_UNKNOWN_INTERFACE_ID,
  266. sizeof(struct vector_cpp_interface),
  267. 0,
  268. 0,
  269. NULL,
  270. pack_vector_cpp_handle,
  271. peek_vector_cpp_handle,
  272. unpack_vector_cpp_handle,
  273. (char *) "VECTOR_CPP_INTERFACE"
  274. };
  275. #endif
  276. static void *vector_cpp_to_pointer(void *data_interface, unsigned node)
  277. {
  278. (void) node;
  279. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *) data_interface;
  280. return (void*) vector_interface->ptr;
  281. }
  282. static int vector_cpp_pointer_is_inside(void *data_interface, unsigned int node, void *ptr)
  283. {
  284. (void) node;
  285. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *) data_interface;
  286. return (char*) ptr >= (char*) vector_interface->ptr &&
  287. (char*) ptr < (char*) vector_interface->ptr + vector_interface->nx*vector_interface->elemsize;
  288. }
  289. static void register_vector_cpp_handle(starpu_data_handle_t handle, unsigned home_node, void *data_interface)
  290. {
  291. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *) data_interface;
  292. unsigned node;
  293. for (node = 0; node < STARPU_MAXNODES; node++)
  294. {
  295. struct vector_cpp_interface *local_interface = (struct vector_cpp_interface *)
  296. starpu_data_get_interface_on_node(handle, node);
  297. if (node == home_node)
  298. {
  299. local_interface->ptr = vector_interface->ptr;
  300. local_interface->dev_handle = vector_interface->dev_handle;
  301. local_interface->offset = vector_interface->offset;
  302. local_interface->vec = vector_interface->vec;
  303. }
  304. else
  305. {
  306. local_interface->ptr = 0;
  307. local_interface->dev_handle = 0;
  308. local_interface->offset = 0;
  309. local_interface->vec = NULL;
  310. }
  311. local_interface->id = vector_interface->id;
  312. local_interface->nx = vector_interface->nx;
  313. local_interface->elemsize = vector_interface->elemsize;
  314. local_interface->slice_base = vector_interface->slice_base;
  315. }
  316. }
  317. /* declare a new data with the vector interface */
  318. void vector_cpp_data_register(starpu_data_handle_t *handleptr, int home_node,
  319. std::vector<MY_TYPE>* vec, uint32_t nx, size_t elemsize)
  320. {
  321. #if __cplusplus >= 201103L
  322. struct vector_cpp_interface vector =
  323. {
  324. .id = STARPU_UNKNOWN_INTERFACE_ID,
  325. .ptr = (uintptr_t) &(*vec)[0],
  326. .dev_handle = (uintptr_t) &(*vec)[0],
  327. .offset = 0,
  328. .nx = nx,
  329. .elemsize = elemsize,
  330. .vec = vec,
  331. .slice_base = 0
  332. };
  333. #else
  334. struct vector_cpp_interface vector =
  335. {
  336. STARPU_UNKNOWN_INTERFACE_ID,
  337. (uintptr_t) &(*vec)[0],
  338. (uintptr_t) &(*vec)[0],
  339. 0,
  340. nx,
  341. elemsize,
  342. vec,
  343. 0
  344. };
  345. #endif
  346. if (interface_vector_cpp_ops.interfaceid == STARPU_UNKNOWN_INTERFACE_ID)
  347. {
  348. interface_vector_cpp_ops.interfaceid = (enum starpu_data_interface_id )starpu_data_interface_get_next_id();
  349. }
  350. starpu_data_register(handleptr, home_node, &vector, &interface_vector_cpp_ops);
  351. }
  352. /* offer an access to the data parameters */
  353. uint32_t vector_cpp_get_nx(starpu_data_handle_t handle)
  354. {
  355. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *)
  356. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  357. return vector_interface->nx;
  358. }
  359. static uint32_t footprint_vector_cpp_interface_crc32(starpu_data_handle_t handle)
  360. {
  361. return starpu_hash_crc32c_be(vector_cpp_get_nx(handle), 0);
  362. }
  363. static int vector_cpp_compare(void *data_interface_a, void *data_interface_b)
  364. {
  365. struct vector_cpp_interface *vector_a = (struct vector_cpp_interface *) data_interface_a;
  366. struct vector_cpp_interface *vector_b = (struct vector_cpp_interface *) data_interface_b;
  367. /* Two vectors are considered compatible if they have the same size */
  368. return ((vector_a->nx == vector_b->nx)
  369. && (vector_a->elemsize == vector_b->elemsize));
  370. }
  371. static void display_vector_cpp_interface(starpu_data_handle_t handle, FILE *f)
  372. {
  373. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *)
  374. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  375. fprintf(f, "%u\t", vector_interface->nx);
  376. }
  377. static int pack_vector_cpp_handle(starpu_data_handle_t handle, unsigned node, void **ptr, starpu_ssize_t *count)
  378. {
  379. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  380. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *)
  381. starpu_data_get_interface_on_node(handle, node);
  382. *count = vector_interface->nx*vector_interface->elemsize;
  383. if (ptr != NULL)
  384. {
  385. *ptr = (void*) starpu_malloc_on_node_flags(node, *count, 0);
  386. memcpy(*ptr, (void*)vector_interface->ptr, vector_interface->elemsize*vector_interface->nx);
  387. }
  388. return 0;
  389. }
  390. static int peek_vector_cpp_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  391. {
  392. STARPU_ASSERT(starpu_data_test_if_allocated_on_node(handle, node));
  393. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *)
  394. starpu_data_get_interface_on_node(handle, node);
  395. STARPU_ASSERT(count == vector_interface->elemsize * vector_interface->nx);
  396. memcpy((void*)vector_interface->ptr, ptr, count);
  397. return 0;
  398. }
  399. static int unpack_vector_cpp_handle(starpu_data_handle_t handle, unsigned node, void *ptr, size_t count)
  400. {
  401. peek_vector_cpp_handle(handle, node, ptr, count);
  402. starpu_free_on_node_flags(node, (uintptr_t)ptr, count, 0);
  403. return 0;
  404. }
  405. static size_t vector_cpp_interface_get_size(starpu_data_handle_t handle)
  406. {
  407. size_t size;
  408. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *)
  409. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  410. size = vector_interface->nx*vector_interface->elemsize;
  411. return size;
  412. }
  413. size_t vector_cpp_get_elemsize(starpu_data_handle_t handle)
  414. {
  415. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *)
  416. starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  417. return vector_interface->elemsize;
  418. }
  419. /* memory allocation/deallocation primitives for the vector interface */
  420. /* returns the size of the allocated area */
  421. static starpu_ssize_t allocate_vector_cpp_buffer_on_node(void *data_interface_, unsigned dst_node)
  422. {
  423. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *) data_interface_;
  424. uint32_t nx = vector_interface->nx;
  425. size_t elemsize = vector_interface->elemsize;
  426. starpu_ssize_t allocated_memory;
  427. const my_allocator<char> allocator(dst_node);
  428. std::vector<MY_TYPE> * vec = new std::vector<MY_TYPE>(nx, 0, allocator);
  429. vector_interface->vec = vec;
  430. if (!vector_interface->vec)
  431. return -ENOMEM;
  432. allocated_memory = nx*elemsize;
  433. /* update the data properly in consequence */
  434. vector_interface->ptr = (uintptr_t) &((*vec)[0]);
  435. vector_interface->dev_handle = (uintptr_t) &((*vec)[0]);
  436. vector_interface->offset = 0;
  437. return allocated_memory;
  438. }
  439. static void free_vector_cpp_buffer_on_node(void *data_interface, unsigned node)
  440. {
  441. struct vector_cpp_interface *vector_interface = (struct vector_cpp_interface *) data_interface;
  442. delete vector_interface->vec;
  443. }
  444. static int vector_interface_copy_any_to_any(void *src_interface, unsigned src_node,
  445. void *dst_interface, unsigned dst_node, void *async_data)
  446. {
  447. struct vector_cpp_interface *src_vector = (struct vector_cpp_interface *) src_interface;
  448. struct vector_cpp_interface *dst_vector = (struct vector_cpp_interface *) dst_interface;
  449. int ret;
  450. ret = starpu_interface_copy(src_vector->dev_handle, src_vector->offset, src_node,
  451. dst_vector->dev_handle, dst_vector->offset, dst_node,
  452. src_vector->nx*src_vector->elemsize, async_data);
  453. return ret;
  454. }
  455. static starpu_ssize_t vector_cpp_describe(void *data_interface, char *buf, size_t size)
  456. {
  457. struct vector_cpp_interface *vector = (struct vector_cpp_interface *) data_interface;
  458. return snprintf(buf, size, "V%ux%u",
  459. (unsigned) vector->nx,
  460. (unsigned) vector->elemsize);
  461. }
  462. /*
  463. * End of interface
  464. */
  465. /* Kernel using STL objects */
  466. void cpu_kernel_add_vectors(void *buffers[], void *cl_arg)
  467. {
  468. std::vector<MY_TYPE>* vec_A = VECTOR_CPP_GET_VEC(buffers[0]);
  469. std::vector<MY_TYPE>* vec_B = VECTOR_CPP_GET_VEC(buffers[1]);
  470. std::vector<MY_TYPE>* vec_C = VECTOR_CPP_GET_VEC(buffers[2]);
  471. // all the std::vector have to have the same size
  472. assert(vec_A->size() == vec_B->size() && vec_B->size() == vec_C->size());
  473. // performs the vector addition (vec_C[] = vec_A[] + vec_B[])
  474. for (size_t i = 0; i < vec_C->size(); i++)
  475. (*vec_C)[i] = (*vec_A)[i] + (*vec_B)[i];
  476. }
  477. #define VEC_SIZE 1024
  478. int main(int argc, char **argv)
  479. {
  480. struct starpu_conf conf;
  481. bool fail;
  482. starpu_conf_init(&conf);
  483. conf.nmic = 0;
  484. conf.nmpi_ms = 0;
  485. // initialize StarPU with default configuration
  486. int ret = starpu_init(&conf);
  487. if (ret == -ENODEV)
  488. return 77;
  489. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  490. {
  491. /* Test data transfers between NUMA nodes if available */
  492. unsigned last_numa_node = starpu_memory_nodes_get_numa_count() - 1;
  493. const my_allocator<char> allocator_main_ram(STARPU_MAIN_RAM);
  494. const my_allocator<char> allocator_last_numa(last_numa_node);
  495. std::vector<MY_TYPE> vec_A(VEC_SIZE, 2, allocator_main_ram); // all the vector is initialized to 2
  496. std::vector<MY_TYPE> vec_B(VEC_SIZE, 3, allocator_main_ram); // all the vector is initialized to 3
  497. std::vector<MY_TYPE> vec_C(VEC_SIZE, 0, allocator_last_numa); // all the vector is initialized to 0
  498. // StarPU data registering
  499. starpu_data_handle_t spu_vec_A;
  500. starpu_data_handle_t spu_vec_B;
  501. starpu_data_handle_t spu_vec_C;
  502. // give the data of the vector to StarPU (C array)
  503. vector_cpp_data_register(&spu_vec_A, STARPU_MAIN_RAM, &vec_A, vec_A.size(), sizeof(char));
  504. vector_cpp_data_register(&spu_vec_B, STARPU_MAIN_RAM, &vec_B, vec_B.size(), sizeof(char));
  505. vector_cpp_data_register(&spu_vec_C, last_numa_node, &vec_C, vec_C.size(), sizeof(char));
  506. // create the StarPU codelet
  507. starpu_codelet cl;
  508. starpu_codelet_init(&cl);
  509. cl.cpu_funcs [0] = cpu_kernel_add_vectors;
  510. cl.cpu_funcs_name[0] = "cpu_kernel_add_vectors";
  511. cl.nbuffers = 3;
  512. cl.modes [0] = STARPU_R;
  513. cl.modes [1] = STARPU_R;
  514. cl.modes [2] = STARPU_W;
  515. cl.name = "add_vectors";
  516. // submit a new StarPU task to execute
  517. ret = starpu_task_insert(&cl,
  518. STARPU_R, spu_vec_A,
  519. STARPU_R, spu_vec_B,
  520. STARPU_W, spu_vec_C,
  521. 0);
  522. if (ret == -ENODEV)
  523. {
  524. // StarPU data unregistering
  525. starpu_data_unregister(spu_vec_C);
  526. starpu_data_unregister(spu_vec_B);
  527. starpu_data_unregister(spu_vec_A);
  528. // terminate StarPU, no task can be submitted after
  529. starpu_shutdown();
  530. return 77;
  531. }
  532. STARPU_CHECK_RETURN_VALUE(ret, "task_submit::add_vectors");
  533. // wait the task
  534. starpu_task_wait_for_all();
  535. // StarPU data unregistering
  536. starpu_data_unregister(spu_vec_C);
  537. starpu_data_unregister(spu_vec_B);
  538. starpu_data_unregister(spu_vec_A);
  539. // check results
  540. fail = false;
  541. int i = 0;
  542. while (!fail && i < VEC_SIZE)
  543. fail = vec_C[i++] != 5;
  544. }
  545. // terminate StarPU, no task can be submitted after
  546. starpu_shutdown();
  547. if (fail)
  548. {
  549. #ifdef PRINT_OUTPUT
  550. std::cout << "Example failed..." << std::endl;
  551. #endif
  552. return EXIT_FAILURE;
  553. }
  554. else
  555. {
  556. #ifdef PRINT_OUTPUT
  557. std::cout << "Example successfully passed!" << std::endl;
  558. #endif
  559. return EXIT_SUCCESS;
  560. }
  561. }
  562. #endif