add_vectors.cpp 4.9 KB

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