scratch.c 4.2 KB

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