add_vectors_cpp11.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 starpu. We here just
  18. * add two std::vector without copying them (0 copy).
  19. */
  20. #include <cassert>
  21. #include <vector>
  22. #ifdef PRINT_OUTPUT
  23. #include <iostream>
  24. #endif
  25. #include <starpu.h>
  26. #if !defined(STARPU_HAVE_CXX11)
  27. int main(int argc, char **argv)
  28. {
  29. return 77;
  30. }
  31. #else
  32. void cpu_kernel_add_vectors(void *buffers[], void *cl_arg)
  33. {
  34. // get the current task
  35. auto task = starpu_task_get_current();
  36. // get the user data (pointers to the vec_A, vec_B, vec_C std::vector)
  37. auto u_data0 = starpu_data_get_user_data(task->handles[0]); assert(u_data0);
  38. auto u_data1 = starpu_data_get_user_data(task->handles[1]); assert(u_data1);
  39. auto u_data2 = starpu_data_get_user_data(task->handles[2]); assert(u_data2);
  40. // cast void* in std::vector<char>*
  41. auto vec_A = static_cast<std::vector<char>*>(u_data0);
  42. auto vec_B = static_cast<std::vector<char>*>(u_data1);
  43. auto vec_C = static_cast<std::vector<char>*>(u_data2);
  44. // all the std::vector have to have the same size
  45. assert(vec_A->size() == vec_B->size() && vec_B->size() == vec_C->size());
  46. // performs the vector addition (vec_C[] = vec_A[] + vec_B[])
  47. for (size_t i = 0; i < vec_C->size(); i++)
  48. (*vec_C)[i] = (*vec_A)[i] + (*vec_B)[i];
  49. }
  50. int main(int argc, char **argv)
  51. {
  52. constexpr int vec_size = 1024;
  53. std::vector<char> vec_A(vec_size, 2); // all the vector is initialized to 2
  54. std::vector<char> vec_B(vec_size, 3); // all the vector is initialized to 3
  55. std::vector<char> vec_C(vec_size, 0); // all the vector is initialized to 0
  56. struct starpu_conf conf;
  57. starpu_conf_init(&conf);
  58. conf.nmpi_ms = 0;
  59. // initialize StarPU with default configuration
  60. auto ret = starpu_init(&conf);
  61. if (ret == -ENODEV)
  62. return 77;
  63. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  64. if (starpu_memory_nodes_get_numa_count() > 1)
  65. {
  66. starpu_shutdown();
  67. return 77;
  68. }
  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