scratch.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #define NLOOPS 128
  23. #define VECTORSIZE 1024
  24. static unsigned *A;
  25. starpu_data_handle A_handle, B_handle;
  26. //static unsigned var = 0;
  27. #ifdef STARPU_USE_CUDA
  28. extern void cuda_f(void *descr[], __attribute__ ((unused)) void *_args);
  29. #endif
  30. static void cpu_f(void *descr[], __attribute__ ((unused)) void *_args)
  31. {
  32. unsigned *v = (unsigned *)STARPU_VECTOR_GET_PTR(descr[0]);
  33. unsigned *tmp = (unsigned *)STARPU_VECTOR_GET_PTR(descr[1]);
  34. unsigned nx = STARPU_VECTOR_GET_NX(descr[0]);
  35. size_t elemsize = STARPU_VECTOR_GET_ELEMSIZE(descr[0]);
  36. memcpy(tmp, v, nx*elemsize);
  37. unsigned i;
  38. for (i = 0; i < nx; i++)
  39. {
  40. v[i] = tmp[i] + 1;
  41. }
  42. }
  43. static starpu_codelet cl_f = {
  44. .where = STARPU_CPU|STARPU_CUDA,
  45. .cpu_func = cpu_f,
  46. #ifdef STARPU_USE_CUDA
  47. .cuda_func = cuda_f,
  48. #endif
  49. .nbuffers = 2
  50. };
  51. int main(int argc, char **argv)
  52. {
  53. starpu_init(NULL);
  54. A = (unsigned *) calloc(VECTORSIZE, sizeof(unsigned));
  55. starpu_vector_data_register(&A_handle, 0, (uintptr_t)A, VECTORSIZE, sizeof(unsigned));
  56. starpu_vector_data_register(&B_handle, -1, (uintptr_t)NULL, VECTORSIZE, sizeof(unsigned));
  57. unsigned loop;
  58. for (loop = 0; loop < NLOOPS; loop++)
  59. {
  60. struct starpu_task *task_f = starpu_task_create();
  61. task_f->cl = &cl_f;
  62. task_f->buffers[0].handle = A_handle;
  63. task_f->buffers[0].mode = STARPU_RW;
  64. task_f->buffers[1].handle = B_handle;
  65. task_f->buffers[1].mode = STARPU_SCRATCH;
  66. int ret = starpu_task_submit(task_f);
  67. if (ret == -ENODEV)
  68. goto enodev;
  69. }
  70. starpu_task_wait_for_all();
  71. /* Make sure that data A is in main memory */
  72. starpu_data_acquire(A_handle, STARPU_R);
  73. /* Check result */
  74. unsigned i;
  75. for (i = 0; i < VECTORSIZE; i++)
  76. {
  77. STARPU_ASSERT(A[i] == NLOOPS);
  78. }
  79. starpu_data_release(A_handle);
  80. starpu_shutdown();
  81. return 0;
  82. enodev:
  83. /* No one can execute that task, this is not a bug, just an incomplete
  84. * test :) */
  85. return 77;
  86. }