scratch.c 3.9 KB

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