specific_node.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2013,2015,2017 CNRS
  4. * Copyright (C) 2010,2011,2013,2014,2016-2019 Université de Bordeaux
  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. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <errno.h>
  21. #include <starpu.h>
  22. #include <stdlib.h>
  23. #include "../helper.h"
  24. /*
  25. * Test using the specific_nodes field by forcing the data to main memory
  26. * even if the task is run on a GPU (and actually doing the computation from
  27. * the CPU driving the GPU). It mixes such accesses and normal accesses from
  28. * the GPU
  29. */
  30. unsigned data, data2;
  31. void specific2_kernel(void *descr[], void *arg)
  32. {
  33. (void)arg;
  34. int node = starpu_task_get_current_data_node(0);
  35. STARPU_ASSERT(node >= 0);
  36. STARPU_ASSERT(starpu_node_get_kind(node) == STARPU_CPU_RAM);
  37. unsigned *dataptr = (unsigned*) STARPU_VARIABLE_GET_PTR(descr[0]);
  38. if (node == STARPU_MAIN_RAM)
  39. STARPU_ASSERT(dataptr == &data);
  40. (*dataptr)++;
  41. node = starpu_task_get_current_data_node(1);
  42. STARPU_ASSERT(node >= 0);
  43. STARPU_ASSERT(starpu_node_get_kind(node) == STARPU_CPU_RAM
  44. || node == starpu_worker_get_local_memory_node());
  45. dataptr = (unsigned*) STARPU_VARIABLE_GET_PTR(descr[1]);
  46. if (node == STARPU_MAIN_RAM)
  47. STARPU_ASSERT(dataptr == &data2);
  48. }
  49. static struct starpu_codelet specific2_cl =
  50. {
  51. .cpu_funcs = {specific2_kernel},
  52. .cuda_funcs = {specific2_kernel},
  53. .opencl_funcs = {specific2_kernel},
  54. .nbuffers = 2,
  55. .modes = {STARPU_RW, STARPU_RW},
  56. .specific_nodes = 1,
  57. .nodes = {STARPU_SPECIFIC_NODE_CPU, STARPU_SPECIFIC_NODE_LOCAL_OR_CPU},
  58. };
  59. void specific_kernel(void *descr[], void *arg)
  60. {
  61. (void)arg;
  62. int node = starpu_task_get_current_data_node(0);
  63. STARPU_ASSERT(node >= 0);
  64. STARPU_ASSERT(starpu_node_get_kind(node) == STARPU_CPU_RAM);
  65. unsigned *dataptr = (unsigned*) STARPU_VARIABLE_GET_PTR(descr[0]);
  66. if (node == STARPU_MAIN_RAM)
  67. STARPU_ASSERT(dataptr == &data);
  68. (*dataptr)++;
  69. node = starpu_task_get_current_data_node(1);
  70. STARPU_ASSERT(node == starpu_worker_get_local_memory_node());
  71. }
  72. static struct starpu_codelet specific_cl =
  73. {
  74. .cpu_funcs = {specific_kernel},
  75. .cuda_funcs = {specific_kernel},
  76. .opencl_funcs = {specific_kernel},
  77. .nbuffers = 2,
  78. .modes = {STARPU_RW, STARPU_RW},
  79. .specific_nodes = 1,
  80. .nodes = {STARPU_SPECIFIC_NODE_CPU, STARPU_SPECIFIC_NODE_LOCAL},
  81. };
  82. void cpu_codelet_unsigned_inc(void *descr[], void *arg)
  83. {
  84. (void)arg;
  85. unsigned *dataptr = (unsigned*) STARPU_VARIABLE_GET_PTR(descr[0]);
  86. (*dataptr)++;
  87. }
  88. #ifdef STARPU_USE_CUDA
  89. void cuda_codelet_unsigned_inc(void *descr[], void *cl_arg);
  90. #endif
  91. #ifdef STARPU_USE_OPENCL
  92. void opencl_codelet_unsigned_inc(void *buffers[], void *args);
  93. #endif
  94. static struct starpu_codelet cl =
  95. {
  96. .cpu_funcs = {cpu_codelet_unsigned_inc},
  97. #ifdef STARPU_USE_CUDA
  98. .cuda_funcs = {cuda_codelet_unsigned_inc},
  99. .cuda_flags = {STARPU_CUDA_ASYNC},
  100. #endif
  101. #ifdef STARPU_USE_OPENCL
  102. .opencl_funcs = {opencl_codelet_unsigned_inc},
  103. .opencl_flags = {STARPU_OPENCL_ASYNC},
  104. #endif
  105. .nbuffers = 1,
  106. .modes = {STARPU_RW},
  107. };
  108. #ifdef STARPU_USE_OPENCL
  109. struct starpu_opencl_program opencl_program;
  110. #endif
  111. int main(void)
  112. {
  113. starpu_data_handle_t data_handle, data_handle2;
  114. #ifdef STARPU_QUICK_CHECK
  115. unsigned ntasks = 10;
  116. #else
  117. unsigned ntasks = 1000;
  118. #endif
  119. int ret, ret2;
  120. ret = starpu_init(NULL);
  121. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  122. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  123. #ifdef STARPU_USE_OPENCL
  124. ret = starpu_opencl_load_opencl_from_file("tests/datawizard/opencl_codelet_unsigned_inc_kernel.cl",
  125. &opencl_program, NULL);
  126. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  127. #endif
  128. data = 0;
  129. data2 = 0;
  130. /* Create a void data which will be used as an exclusion mechanism. */
  131. starpu_variable_data_register(&data_handle, STARPU_MAIN_RAM, (uintptr_t) &data, sizeof(data));
  132. starpu_variable_data_register(&data_handle2, STARPU_MAIN_RAM, (uintptr_t) &data2, sizeof(data2));
  133. unsigned i;
  134. for (i = 0; i < ntasks; i++)
  135. {
  136. struct starpu_task *task = starpu_task_create();
  137. if (i%3 == 0)
  138. task->cl = &specific_cl;
  139. else if (i%3 == 1)
  140. task->cl = &specific2_cl;
  141. else
  142. task->cl = &cl;
  143. task->handles[0] = data_handle;
  144. task->handles[1] = data_handle2;
  145. ret = starpu_task_submit(task);
  146. if (ret == -ENODEV) goto enodev;
  147. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  148. }
  149. starpu_data_unregister(data_handle);
  150. starpu_data_unregister(data_handle2);
  151. ret = (data == ntasks) ? EXIT_SUCCESS : EXIT_FAILURE;
  152. #ifdef STARPU_USE_OPENCL
  153. ret2 = starpu_opencl_unload_opencl(&opencl_program);
  154. STARPU_CHECK_RETURN_VALUE(ret2, "starpu_opencl_unload_opencl");
  155. #endif
  156. starpu_shutdown();
  157. return ret;
  158. enodev:
  159. fprintf(stderr, "WARNING: No one can execute this task\n");
  160. /* yes, we do not perform the computation but we did detect that no one
  161. * could perform the kernel, so this is not an error from StarPU */
  162. starpu_shutdown();
  163. return STARPU_TEST_SKIPPED;
  164. }