add_vectors_cpp11.cpp 4.8 KB

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