scratch.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include <starpu.h>
  20. #include <stdlib.h>
  21. #define NLOOPS 128
  22. #define VECTORSIZE 1024
  23. static unsigned *A;
  24. starpu_data_handle A_handle, B_handle;
  25. static unsigned var = 0;
  26. #ifdef STARPU_USE_CUDA
  27. extern void cuda_f(void *descr[], __attribute__ ((unused)) void *_args);
  28. #endif
  29. static void cpu_f(void *descr[], __attribute__ ((unused)) void *_args)
  30. {
  31. unsigned *v = (unsigned *)STARPU_GET_VECTOR_PTR(descr[0]);
  32. unsigned *tmp = (unsigned *)STARPU_GET_VECTOR_PTR(descr[1]);
  33. unsigned nx = STARPU_GET_VECTOR_NX(descr[0]);
  34. size_t elemsize = STARPU_GET_VECTOR_ELEMSIZE(descr[0]);
  35. memcpy(tmp, v, nx*elemsize);
  36. unsigned i;
  37. for (i = 0; i < nx; i++)
  38. {
  39. v[i] = tmp[i] + 1;
  40. }
  41. }
  42. static starpu_codelet cl_f = {
  43. .where = STARPU_CPU|STARPU_CUDA,
  44. .cpu_func = cpu_f,
  45. #ifdef STARPU_USE_CUDA
  46. .cuda_func = cuda_f,
  47. #endif
  48. .nbuffers = 2
  49. };
  50. int main(int argc, char **argv)
  51. {
  52. starpu_init(NULL);
  53. A = calloc(VECTORSIZE, sizeof(unsigned));
  54. starpu_vector_data_register(&A_handle, 0, (uintptr_t)A, VECTORSIZE, sizeof(unsigned));
  55. starpu_vector_data_register(&B_handle, -1, (uintptr_t)NULL, VECTORSIZE, sizeof(unsigned));
  56. unsigned loop;
  57. for (loop = 0; loop < NLOOPS; loop++)
  58. {
  59. struct starpu_task *task_f = starpu_task_create();
  60. task_f->cl = &cl_f;
  61. task_f->buffers[0].handle = A_handle;
  62. task_f->buffers[0].mode = STARPU_RW;
  63. task_f->buffers[1].handle = B_handle;
  64. task_f->buffers[1].mode = STARPU_SCRATCH;
  65. int ret = starpu_task_submit(task_f);
  66. if (ret == -ENODEV)
  67. goto enodev;
  68. }
  69. starpu_task_wait_for_all();
  70. /* Make sure that data A is in main memory */
  71. starpu_data_sync_with_mem(A_handle, STARPU_R);
  72. /* Check result */
  73. unsigned i;
  74. for (i = 0; i < VECTORSIZE; i++)
  75. {
  76. STARPU_ASSERT(A[i] == NLOOPS);
  77. }
  78. starpu_data_release_from_mem(A_handle);
  79. starpu_shutdown();
  80. return 0;
  81. enodev:
  82. /* No one can execute that task, this is not a bug, just an incomplete
  83. * test :) */
  84. return 0;
  85. }