scratch.c 4.2 KB

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