add_vectors_cpp11.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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, 2017 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 without copying them (0 copy).
  21. */
  22. #include <cassert>
  23. #include <vector>
  24. #ifdef PRINT_OUTPUT
  25. #include <iostream>
  26. #endif
  27. #include <starpu.h>
  28. #include "../helper.h"
  29. #if !defined(STARPU_HAVE_CXX11) || defined(STARPU_USE_NUMA)
  30. int main(int argc, char **argv)
  31. {
  32. return 77;
  33. }
  34. #else
  35. void cpu_kernel_add_vectors(void *buffers[], void *cl_arg)
  36. {
  37. // get the current task
  38. auto task = starpu_task_get_current();
  39. // get the user data (pointers to the vec_A, vec_B, vec_C std::vector)
  40. auto u_data0 = starpu_data_get_user_data(task->handles[0]); assert(u_data0);
  41. auto u_data1 = starpu_data_get_user_data(task->handles[1]); assert(u_data1);
  42. auto u_data2 = starpu_data_get_user_data(task->handles[2]); assert(u_data2);
  43. // cast void* in std::vector<char>*
  44. auto vec_A = static_cast<std::vector<char>*>(u_data0);
  45. auto vec_B = static_cast<std::vector<char>*>(u_data1);
  46. auto vec_C = static_cast<std::vector<char>*>(u_data2);
  47. // all the std::vector have to have the same size
  48. assert(vec_A->size() == vec_B->size() && vec_B->size() == vec_C->size());
  49. // performs the vector addition (vec_C[] = vec_A[] + vec_B[])
  50. for (size_t i = 0; i < vec_C->size(); i++)
  51. (*vec_C)[i] = (*vec_A)[i] + (*vec_B)[i];
  52. }
  53. int main(int argc, char **argv)
  54. {
  55. constexpr int vec_size = 1024;
  56. std::vector<char> vec_A(vec_size, 2); // all the vector is initialized to 2
  57. std::vector<char> vec_B(vec_size, 3); // all the vector is initialized to 3
  58. std::vector<char> vec_C(vec_size, 0); // all the vector is initialized to 0
  59. struct starpu_conf conf;
  60. starpu_conf_init(&conf);
  61. conf.nmic = 0;
  62. conf.nscc = 0;
  63. conf.nmpi_ms = 0;
  64. // initialize StarPU with default configuration
  65. auto ret = starpu_init(&conf);
  66. if (ret == -ENODEV)
  67. return 77;
  68. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  69. // StarPU data registering
  70. starpu_data_handle_t spu_vec_A;
  71. starpu_data_handle_t spu_vec_B;
  72. starpu_data_handle_t spu_vec_C;
  73. // give the data of the vector to StarPU (C array)
  74. starpu_vector_data_register(&spu_vec_A, STARPU_MAIN_RAM, (uintptr_t)vec_A.data(), vec_A.size(), sizeof(char));
  75. starpu_vector_data_register(&spu_vec_B, STARPU_MAIN_RAM, (uintptr_t)vec_B.data(), vec_B.size(), sizeof(char));
  76. starpu_vector_data_register(&spu_vec_C, STARPU_MAIN_RAM, (uintptr_t)vec_C.data(), vec_C.size(), sizeof(char));
  77. // pass the pointer to the C++ vector object to StarPU
  78. starpu_data_set_user_data(spu_vec_A, (void*)&vec_A);
  79. starpu_data_set_user_data(spu_vec_B, (void*)&vec_B);
  80. starpu_data_set_user_data(spu_vec_C, (void*)&vec_C);
  81. // create the StarPU codelet
  82. starpu_codelet cl;
  83. starpu_codelet_init(&cl);
  84. cl.cpu_funcs [0] = cpu_kernel_add_vectors;
  85. cl.cpu_funcs_name[0] = "cpu_kernel_add_vectors";
  86. cl.nbuffers = 3;
  87. cl.modes [0] = STARPU_R;
  88. cl.modes [1] = STARPU_R;
  89. cl.modes [2] = STARPU_W;
  90. cl.name = "add_vectors";
  91. // submit a new StarPU task to execute
  92. ret = starpu_task_insert(&cl,
  93. STARPU_R, spu_vec_A,
  94. STARPU_R, spu_vec_B,
  95. STARPU_W, spu_vec_C,
  96. 0);
  97. if (ret == -ENODEV)
  98. {
  99. // StarPU data unregistering
  100. starpu_data_unregister(spu_vec_C);
  101. starpu_data_unregister(spu_vec_B);
  102. starpu_data_unregister(spu_vec_A);
  103. // terminate StarPU, no task can be submitted after
  104. starpu_shutdown();
  105. return 77;
  106. }
  107. STARPU_CHECK_RETURN_VALUE(ret, "task_submit::add_vectors");
  108. // wait the task
  109. starpu_task_wait_for_all();
  110. // StarPU data unregistering
  111. starpu_data_unregister(spu_vec_C);
  112. starpu_data_unregister(spu_vec_B);
  113. starpu_data_unregister(spu_vec_A);
  114. // terminate StarPU, no task can be submitted after
  115. starpu_shutdown();
  116. // check results
  117. auto fail = false;
  118. auto i = 0;
  119. while (!fail && i < vec_size)
  120. fail = vec_C[i++] != 5;
  121. if (fail)
  122. {
  123. #ifdef PRINT_OUTPUT
  124. std::cout << "Example failed..." << std::endl;
  125. #endif
  126. return EXIT_FAILURE;
  127. }
  128. else
  129. {
  130. #ifdef PRINT_OUTPUT
  131. std::cout << "Example successfully passed!" << std::endl;
  132. #endif
  133. return EXIT_SUCCESS;
  134. }
  135. }
  136. #endif