scratch.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 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 a scratch data, using it just for temporary storage
  24. */
  25. #ifdef STARPU_QUICK_CHECK
  26. # define NLOOPS 8
  27. # define VECTORSIZE 128
  28. #else
  29. # define NLOOPS 128
  30. # define VECTORSIZE 1024
  31. #endif
  32. static unsigned *A;
  33. starpu_data_handle_t A_handle, B_handle;
  34. //static unsigned var = 0;
  35. #ifdef STARPU_USE_CUDA
  36. extern void cuda_f(void *descr[], void *_args);
  37. #endif
  38. #ifdef STARPU_USE_OPENCL
  39. extern void opencl_f(void *buffers[], void *args);
  40. #endif
  41. void cpu_f(void *descr[], void *arg)
  42. {
  43. (void)arg;
  44. STARPU_SKIP_IF_VALGRIND;
  45. unsigned *v = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  46. unsigned *tmp = (unsigned *)STARPU_VECTOR_GET_PTR(descr[1]);
  47. unsigned nx = STARPU_VECTOR_GET_NX(descr[0]);
  48. size_t elemsize = STARPU_VECTOR_GET_ELEMSIZE(descr[0]);
  49. memcpy(tmp, v, nx*elemsize);
  50. unsigned i;
  51. for (i = 0; i < nx; i++)
  52. {
  53. v[i] = tmp[i] + 1;
  54. }
  55. }
  56. static struct starpu_codelet cl_f =
  57. {
  58. .cpu_funcs = {cpu_f},
  59. #ifdef STARPU_USE_CUDA
  60. .cuda_funcs = {cuda_f},
  61. .cuda_flags = {STARPU_CUDA_ASYNC},
  62. #endif
  63. #ifdef STARPU_USE_OPENCL
  64. .opencl_funcs = {opencl_f},
  65. .opencl_flags = {STARPU_OPENCL_ASYNC},
  66. #endif
  67. .cpu_funcs_name = {"cpu_f"},
  68. .nbuffers = 2,
  69. .modes = {STARPU_RW, STARPU_SCRATCH}
  70. };
  71. #ifdef STARPU_USE_OPENCL
  72. struct starpu_opencl_program opencl_program;
  73. #endif
  74. int main(int argc, char **argv)
  75. {
  76. int ret;
  77. ret = starpu_initialize(NULL, &argc, &argv);
  78. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  79. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  80. #ifdef STARPU_USE_OPENCL
  81. ret = starpu_opencl_load_opencl_from_file("tests/datawizard/scratch_opencl_kernel.cl",
  82. &opencl_program, NULL);
  83. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  84. #endif
  85. A = (unsigned *) calloc(VECTORSIZE, sizeof(unsigned));
  86. starpu_vector_data_register(&A_handle, STARPU_MAIN_RAM, (uintptr_t)A, VECTORSIZE, sizeof(unsigned));
  87. starpu_vector_data_register(&B_handle, -1, (uintptr_t)NULL, VECTORSIZE, sizeof(unsigned));
  88. unsigned loop;
  89. for (loop = 0; loop < NLOOPS; loop++)
  90. {
  91. struct starpu_task *task_f = starpu_task_create();
  92. task_f->cl = &cl_f;
  93. task_f->handles[0] = A_handle;
  94. task_f->handles[1] = B_handle;
  95. ret = starpu_task_submit(task_f);
  96. if (ret == -ENODEV) goto enodev;
  97. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  98. }
  99. ret = starpu_task_wait_for_all();
  100. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  101. starpu_data_unregister(A_handle);
  102. starpu_data_unregister(B_handle);
  103. #ifdef STARPU_USE_OPENCL
  104. ret = starpu_opencl_unload_opencl(&opencl_program);
  105. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  106. #endif
  107. starpu_shutdown();
  108. /* Check result */
  109. unsigned i;
  110. ret = EXIT_SUCCESS;
  111. for (i = 0; i < VECTORSIZE; i++)
  112. {
  113. if (A[i] != NLOOPS)
  114. {
  115. FPRINTF(stderr, "Error: Incorrect value A[%u] = %u != %d\n", i, A[i], NLOOPS);
  116. ret = EXIT_FAILURE;
  117. break;
  118. }
  119. }
  120. free(A);
  121. STARPU_RETURN(ret);
  122. enodev:
  123. starpu_data_unregister(A_handle);
  124. starpu_data_unregister(B_handle);
  125. #ifdef STARPU_USE_OPENCL
  126. ret = starpu_opencl_unload_opencl(&opencl_program);
  127. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  128. #endif
  129. starpu_shutdown();
  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. fprintf(stderr, "WARNING: No one can execute this task\n");
  133. return STARPU_TEST_SKIPPED;
  134. }