scratch.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 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 <stdio.h>
  18. #include <unistd.h>
  19. #include <errno.h>
  20. #include <starpu.h>
  21. #include <stdlib.h>
  22. #include "../common/helper.h"
  23. #define NLOOPS 128
  24. #define VECTORSIZE 1024
  25. static unsigned *A;
  26. starpu_data_handle A_handle, B_handle;
  27. //static unsigned var = 0;
  28. #ifdef STARPU_USE_CUDA
  29. extern void cuda_f(void *descr[], __attribute__ ((unused)) void *_args);
  30. #endif
  31. static void cpu_f(void *descr[], __attribute__ ((unused)) void *_args)
  32. {
  33. unsigned *v = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  34. unsigned *tmp = (unsigned *)STARPU_VECTOR_GET_PTR(descr[1]);
  35. unsigned nx = STARPU_VECTOR_GET_NX(descr[0]);
  36. size_t elemsize = STARPU_VECTOR_GET_ELEMSIZE(descr[0]);
  37. memcpy(tmp, v, nx*elemsize);
  38. unsigned i;
  39. for (i = 0; i < nx; i++)
  40. {
  41. v[i] = tmp[i] + 1;
  42. }
  43. }
  44. static starpu_codelet cl_f = {
  45. .where = STARPU_CPU|STARPU_CUDA,
  46. .cpu_func = cpu_f,
  47. #ifdef STARPU_USE_CUDA
  48. .cuda_func = cuda_f,
  49. #endif
  50. .nbuffers = 2
  51. };
  52. int main(int argc, char **argv)
  53. {
  54. int ret;
  55. ret = starpu_init(NULL);
  56. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  57. A = (unsigned *) calloc(VECTORSIZE, sizeof(unsigned));
  58. starpu_vector_data_register(&A_handle, 0, (uintptr_t)A, VECTORSIZE, sizeof(unsigned));
  59. starpu_vector_data_register(&B_handle, -1, (uintptr_t)NULL, VECTORSIZE, sizeof(unsigned));
  60. unsigned loop;
  61. for (loop = 0; loop < NLOOPS; loop++)
  62. {
  63. struct starpu_task *task_f = starpu_task_create();
  64. task_f->cl = &cl_f;
  65. task_f->buffers[0].handle = A_handle;
  66. task_f->buffers[0].mode = STARPU_RW;
  67. task_f->buffers[1].handle = B_handle;
  68. task_f->buffers[1].mode = STARPU_SCRATCH;
  69. ret = starpu_task_submit(task_f);
  70. if (ret == -ENODEV) goto enodev;
  71. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  72. }
  73. ret = starpu_task_wait_for_all();
  74. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  75. /* Make sure that data A is in main memory */
  76. ret = starpu_data_acquire(A_handle, STARPU_R);
  77. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  78. /* Check result */
  79. unsigned i;
  80. for (i = 0; i < VECTORSIZE; i++)
  81. {
  82. STARPU_ASSERT(A[i] == NLOOPS);
  83. }
  84. starpu_data_release(A_handle);
  85. starpu_data_unregister(A_handle);
  86. starpu_data_unregister(B_handle);
  87. starpu_shutdown();
  88. return 0;
  89. enodev:
  90. starpu_data_unregister(A_handle);
  91. starpu_data_unregister(B_handle);
  92. starpu_shutdown();
  93. /* yes, we do not perform the computation but we did detect that no one
  94. * could perform the kernel, so this is not an error from StarPU */
  95. fprintf(stderr, "WARNING: No one can execute this task\n");
  96. return 77;
  97. }