specific_node.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2014, 2016 Université de Bordeaux
  4. * Copyright (C) 2012, 2013, 2015 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 <config.h>
  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. starpu_data_handle_t data_handle;
  31. unsigned data;
  32. void specific_kernel(STARPU_ATTRIBUTE_UNUSED void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  33. {
  34. /* We do not protect this variable because it is only accessed when the
  35. * "data_handle" piece of data is accessed. */
  36. unsigned *dataptr = (unsigned*) STARPU_VARIABLE_GET_PTR(descr[0]);
  37. STARPU_ASSERT(dataptr == &data);
  38. (*dataptr)++;
  39. }
  40. static struct starpu_codelet specific_cl =
  41. {
  42. .cpu_funcs = {specific_kernel},
  43. .cuda_funcs = {specific_kernel},
  44. .opencl_funcs = {specific_kernel},
  45. .nbuffers = 1,
  46. .modes = {STARPU_RW},
  47. .specific_nodes = 1,
  48. .nodes = {STARPU_MAIN_RAM},
  49. };
  50. #ifdef STARPU_USE_CUDA
  51. void cuda_codelet_unsigned_inc(void *descr[], STARPU_ATTRIBUTE_UNUSED void *cl_arg);
  52. #endif
  53. #ifdef STARPU_USE_OPENCL
  54. void opencl_codelet_unsigned_inc(void *buffers[], void *args);
  55. #endif
  56. static struct starpu_codelet cl =
  57. {
  58. .cpu_funcs = {specific_kernel},
  59. #ifdef STARPU_USE_CUDA
  60. .cuda_funcs = {cuda_codelet_unsigned_inc},
  61. .cuda_flags = {STARPU_CUDA_ASYNC},
  62. #endif
  63. #ifdef STARPU_USE_OPENCL
  64. .opencl_funcs = {opencl_codelet_unsigned_inc},
  65. .opencl_flags = {STARPU_OPENCL_ASYNC},
  66. #endif
  67. .nbuffers = 1,
  68. .modes = {STARPU_RW},
  69. };
  70. #ifdef STARPU_USE_OPENCL
  71. struct starpu_opencl_program opencl_program;
  72. #endif
  73. int main(STARPU_ATTRIBUTE_UNUSED int argc, STARPU_ATTRIBUTE_UNUSED char **argv)
  74. {
  75. #ifdef STARPU_QUICK_CHECK
  76. unsigned ntasks = 10;
  77. #else
  78. unsigned ntasks = 1000;
  79. #endif
  80. int ret, ret2;
  81. ret = starpu_init(NULL);
  82. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  83. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  84. #ifdef STARPU_USE_OPENCL
  85. ret = starpu_opencl_load_opencl_from_file("tests/datawizard/opencl_codelet_unsigned_inc_kernel.cl",
  86. &opencl_program, NULL);
  87. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  88. #endif
  89. data = 0;
  90. /* Create a void data which will be used as an exclusion mechanism. */
  91. starpu_variable_data_register(&data_handle, STARPU_MAIN_RAM, (uintptr_t) &data, sizeof(data));
  92. unsigned i;
  93. for (i = 0; i < ntasks; i++)
  94. {
  95. struct starpu_task *task = starpu_task_create();
  96. if (i%2)
  97. task->cl = &specific_cl;
  98. else
  99. task->cl = &cl;
  100. task->handles[0] = data_handle;
  101. ret = starpu_task_submit(task);
  102. if (ret == -ENODEV) goto enodev;
  103. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  104. }
  105. starpu_data_unregister(data_handle);
  106. ret = (data == ntasks) ? EXIT_SUCCESS : EXIT_FAILURE;
  107. #ifdef STARPU_USE_OPENCL
  108. ret2 = starpu_opencl_unload_opencl(&opencl_program);
  109. STARPU_CHECK_RETURN_VALUE(ret2, "starpu_opencl_unload_opencl");
  110. #endif
  111. starpu_shutdown();
  112. return ret;
  113. enodev:
  114. fprintf(stderr, "WARNING: No one can execute this task\n");
  115. /* yes, we do not perform the computation but we did detect that no one
  116. * could perform the kernel, so this is not an error from StarPU */
  117. starpu_shutdown();
  118. return STARPU_TEST_SKIPPED;
  119. }