variable.c 2.5 KB

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