scratch.c 2.4 KB

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