specific_node.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 specific_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 == starpu_worker_get_local_memory_node());
  43. }
  44. static struct starpu_codelet specific_cl =
  45. {
  46. .cpu_funcs = {specific_kernel},
  47. .cuda_funcs = {specific_kernel},
  48. .opencl_funcs = {specific_kernel},
  49. .nbuffers = 2,
  50. .modes = {STARPU_RW, STARPU_RW},
  51. .specific_nodes = 1,
  52. .nodes = {STARPU_SPECIFIC_NODE_CPU, STARPU_SPECIFIC_NODE_LOCAL},
  53. };
  54. void cpu_codelet_unsigned_inc(void *descr[], void *arg)
  55. {
  56. (void)arg;
  57. unsigned *dataptr = (unsigned*) STARPU_VARIABLE_GET_PTR(descr[0]);
  58. (*dataptr)++;
  59. }
  60. #ifdef STARPU_USE_CUDA
  61. void cuda_codelet_unsigned_inc(void *descr[], void *cl_arg);
  62. #endif
  63. #ifdef STARPU_USE_OPENCL
  64. void opencl_codelet_unsigned_inc(void *buffers[], void *args);
  65. #endif
  66. static struct starpu_codelet cl =
  67. {
  68. .cpu_funcs = {cpu_codelet_unsigned_inc},
  69. #ifdef STARPU_USE_CUDA
  70. .cuda_funcs = {cuda_codelet_unsigned_inc},
  71. .cuda_flags = {STARPU_CUDA_ASYNC},
  72. #endif
  73. #ifdef STARPU_USE_OPENCL
  74. .opencl_funcs = {opencl_codelet_unsigned_inc},
  75. .opencl_flags = {STARPU_OPENCL_ASYNC},
  76. #endif
  77. .nbuffers = 1,
  78. .modes = {STARPU_RW},
  79. };
  80. #ifdef STARPU_USE_OPENCL
  81. struct starpu_opencl_program opencl_program;
  82. #endif
  83. int main(void)
  84. {
  85. starpu_data_handle_t data_handle, data_handle2;
  86. #ifdef STARPU_QUICK_CHECK
  87. unsigned ntasks = 10;
  88. #else
  89. unsigned ntasks = 1000;
  90. #endif
  91. int ret, ret2;
  92. ret = starpu_init(NULL);
  93. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  94. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  95. #ifdef STARPU_USE_OPENCL
  96. ret = starpu_opencl_load_opencl_from_file("tests/datawizard/opencl_codelet_unsigned_inc_kernel.cl",
  97. &opencl_program, NULL);
  98. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  99. #endif
  100. data = 0;
  101. data2 = 0;
  102. /* Create a void data which will be used as an exclusion mechanism. */
  103. starpu_variable_data_register(&data_handle, STARPU_MAIN_RAM, (uintptr_t) &data, sizeof(data));
  104. starpu_variable_data_register(&data_handle2, STARPU_MAIN_RAM, (uintptr_t) &data2, sizeof(data2));
  105. unsigned i;
  106. for (i = 0; i < ntasks; i++)
  107. {
  108. struct starpu_task *task = starpu_task_create();
  109. if (i%2)
  110. task->cl = &specific_cl;
  111. else
  112. task->cl = &cl;
  113. task->handles[0] = data_handle;
  114. task->handles[1] = data_handle2;
  115. ret = starpu_task_submit(task);
  116. if (ret == -ENODEV) goto enodev;
  117. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  118. }
  119. starpu_data_unregister(data_handle);
  120. starpu_data_unregister(data_handle2);
  121. ret = (data == ntasks) ? EXIT_SUCCESS : EXIT_FAILURE;
  122. #ifdef STARPU_USE_OPENCL
  123. ret2 = starpu_opencl_unload_opencl(&opencl_program);
  124. STARPU_CHECK_RETURN_VALUE(ret2, "starpu_opencl_unload_opencl");
  125. #endif
  126. starpu_shutdown();
  127. return ret;
  128. enodev:
  129. fprintf(stderr, "WARNING: No one can execute this task\n");
  130. /* yes, we do not perform the computation but we did detect that no one
  131. * could perform the kernel, so this is not an error from StarPU */
  132. starpu_shutdown();
  133. return STARPU_TEST_SKIPPED;
  134. }