specific_node.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2014, 2016 Université de Bordeaux
  4. * Copyright (C) 2012, 2013, 2015, 2017 CNRS
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <errno.h>
  20. #include <starpu.h>
  21. #include <stdlib.h>
  22. #include "../helper.h"
  23. /*
  24. * Test using the specific_nodes field by forcing the data to main memory
  25. * even if the task is run on a GPU (and actually doing the computation from
  26. * the CPU driving the GPU). It mixes such accesses and normal accesses from
  27. * the GPU
  28. */
  29. starpu_data_handle_t data_handle;
  30. unsigned data;
  31. void specific_kernel(void *descr[], void *arg)
  32. {
  33. (void)arg;
  34. unsigned *dataptr = (unsigned*) STARPU_VARIABLE_GET_PTR(descr[0]);
  35. STARPU_ASSERT(dataptr == &data);
  36. (*dataptr)++;
  37. }
  38. static struct starpu_codelet specific_cl =
  39. {
  40. .cpu_funcs = {specific_kernel},
  41. .cuda_funcs = {specific_kernel},
  42. .opencl_funcs = {specific_kernel},
  43. .nbuffers = 1,
  44. .modes = {STARPU_RW},
  45. .specific_nodes = 1,
  46. .nodes = {STARPU_MAIN_RAM},
  47. };
  48. void cpu_codelet_unsigned_inc(void *descr[], void *arg)
  49. {
  50. (void)arg;
  51. unsigned *dataptr = (unsigned*) STARPU_VARIABLE_GET_PTR(descr[0]);
  52. (*dataptr)++;
  53. }
  54. #ifdef STARPU_USE_CUDA
  55. void cuda_codelet_unsigned_inc(void *descr[], void *cl_arg);
  56. #endif
  57. #ifdef STARPU_USE_OPENCL
  58. void opencl_codelet_unsigned_inc(void *buffers[], void *args);
  59. #endif
  60. static struct starpu_codelet cl =
  61. {
  62. .cpu_funcs = {cpu_codelet_unsigned_inc},
  63. #ifdef STARPU_USE_CUDA
  64. .cuda_funcs = {cuda_codelet_unsigned_inc},
  65. .cuda_flags = {STARPU_CUDA_ASYNC},
  66. #endif
  67. #ifdef STARPU_USE_OPENCL
  68. .opencl_funcs = {opencl_codelet_unsigned_inc},
  69. .opencl_flags = {STARPU_OPENCL_ASYNC},
  70. #endif
  71. .nbuffers = 1,
  72. .modes = {STARPU_RW},
  73. };
  74. #ifdef STARPU_USE_OPENCL
  75. struct starpu_opencl_program opencl_program;
  76. #endif
  77. int main(void)
  78. {
  79. #ifdef STARPU_QUICK_CHECK
  80. unsigned ntasks = 10;
  81. #else
  82. unsigned ntasks = 1000;
  83. #endif
  84. int ret, ret2;
  85. ret = starpu_init(NULL);
  86. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  87. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  88. #ifdef STARPU_USE_OPENCL
  89. ret = starpu_opencl_load_opencl_from_file("tests/datawizard/opencl_codelet_unsigned_inc_kernel.cl",
  90. &opencl_program, NULL);
  91. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  92. #endif
  93. data = 0;
  94. /* Create a void data which will be used as an exclusion mechanism. */
  95. starpu_variable_data_register(&data_handle, STARPU_MAIN_RAM, (uintptr_t) &data, sizeof(data));
  96. unsigned i;
  97. for (i = 0; i < ntasks; i++)
  98. {
  99. struct starpu_task *task = starpu_task_create();
  100. if (i%2)
  101. task->cl = &specific_cl;
  102. else
  103. task->cl = &cl;
  104. task->handles[0] = data_handle;
  105. ret = starpu_task_submit(task);
  106. if (ret == -ENODEV) goto enodev;
  107. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  108. }
  109. starpu_data_unregister(data_handle);
  110. ret = (data == ntasks) ? EXIT_SUCCESS : EXIT_FAILURE;
  111. #ifdef STARPU_USE_OPENCL
  112. ret2 = starpu_opencl_unload_opencl(&opencl_program);
  113. STARPU_CHECK_RETURN_VALUE(ret2, "starpu_opencl_unload_opencl");
  114. #endif
  115. starpu_shutdown();
  116. return ret;
  117. enodev:
  118. fprintf(stderr, "WARNING: No one can execute this task\n");
  119. /* yes, we do not perform the computation but we did detect that no one
  120. * could perform the kernel, so this is not an error from StarPU */
  121. starpu_shutdown();
  122. return STARPU_TEST_SKIPPED;
  123. }