specific_node.c 5.3 KB

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