add_vectors.cpp 4.7 KB

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