scratch.c 4.2 KB

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