variable.c 2.5 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 <starpu.h>
  17. #include <pthread.h>
  18. static unsigned niter = 50000;
  19. #ifdef STARPU_USE_CUDA
  20. extern void cuda_codelet(void *descr[], __attribute__ ((unused)) void *_args);
  21. #endif
  22. #ifdef STARPU_USE_OPENCL
  23. extern void opencl_codelet(void *descr[], __attribute__ ((unused)) void *_args);
  24. #endif
  25. extern void cuda_codelet_host(float *tab);
  26. void cpu_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  27. {
  28. float *val = (float *)STARPU_GET_VECTOR_PTR(descr[0]);
  29. *val += 1.0f;
  30. }
  31. int main(int argc, char **argv)
  32. {
  33. unsigned i;
  34. float foo;
  35. starpu_data_handle float_array_handle;
  36. starpu_codelet cl;
  37. starpu_init(NULL);
  38. if (argc == 2) niter = atoi(argv[1]);
  39. foo = 0.0f;
  40. starpu_register_variable_data(&float_array_handle, 0 /* home node */,
  41. (uintptr_t)&foo, sizeof(float));
  42. #ifdef STARPU_USE_OPENCL
  43. _starpu_opencl_compile_source_to_opencl("examples/variable/variable_kernels_opencl_codelet.cl");
  44. #endif
  45. cl.where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL;
  46. cl.cpu_func = cpu_codelet;
  47. #ifdef STARPU_USE_CUDA
  48. cl.cuda_func = cuda_codelet;
  49. #endif
  50. #ifdef STARPU_USE_OPENCL
  51. cl.opencl_func = opencl_codelet;
  52. #endif
  53. cl.nbuffers = 1;
  54. cl.model = NULL;
  55. for (i = 0; i < niter; i++)
  56. {
  57. struct starpu_task *task = starpu_task_create();
  58. int ret;
  59. task->cl = &cl;
  60. task->callback_func = NULL;
  61. task->buffers[0].handle = float_array_handle;
  62. task->buffers[0].mode = STARPU_RW;
  63. ret = starpu_task_submit(task);
  64. if (STARPU_UNLIKELY(ret == -ENODEV))
  65. {
  66. fprintf(stderr, "No worker may execute this task\n");
  67. exit(0);
  68. }
  69. }
  70. starpu_task_wait_for_all();
  71. /* update the array in RAM */
  72. starpu_data_sync_with_mem(float_array_handle, STARPU_R);
  73. fprintf(stderr, "variable -> %f\n", foo);
  74. starpu_data_release_from_mem(float_array_handle);
  75. starpu_shutdown();
  76. return 0;
  77. }