variable.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 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 <starpu.h>
  18. #include <pthread.h>
  19. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  20. static unsigned niter = 50000;
  21. extern void cpu_codelet(void *descr[], __attribute__ ((unused)) void *_args);
  22. #ifdef STARPU_USE_CUDA
  23. extern void cuda_codelet(void *descr[], __attribute__ ((unused)) void *_args);
  24. #endif
  25. #ifdef STARPU_USE_OPENCL
  26. #include <starpu_opencl.h>
  27. extern void opencl_codelet(void *descr[], __attribute__ ((unused)) void *_args);
  28. struct starpu_opencl_program opencl_program;
  29. #endif
  30. int main(int argc, char **argv)
  31. {
  32. unsigned i;
  33. float foo;
  34. starpu_data_handle_t float_array_handle;
  35. struct starpu_codelet cl = {};
  36. int ret;
  37. ret = starpu_init(NULL);
  38. if (ret == -ENODEV) goto enodev;
  39. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  40. #ifdef STARPU_SLOW_MACHINE
  41. niter /= 100;
  42. #endif
  43. if (argc == 2) niter = atoi(argv[1]);
  44. foo = 0.0f;
  45. starpu_variable_data_register(&float_array_handle, 0 /* home node */,
  46. (uintptr_t)&foo, sizeof(float));
  47. #ifdef STARPU_USE_OPENCL
  48. ret = starpu_opencl_load_opencl_from_file("examples/basic_examples/variable_kernels_opencl_kernel.cl", &opencl_program, NULL);
  49. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  50. #endif
  51. cl.where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL;
  52. cl.cpu_funcs[0] = cpu_codelet;
  53. #ifdef STARPU_USE_CUDA
  54. cl.cuda_funcs[0] = cuda_codelet;
  55. #endif
  56. #ifdef STARPU_USE_OPENCL
  57. cl.opencl_funcs[0] = opencl_codelet;
  58. #endif
  59. cl.nbuffers = 1;
  60. cl.modes[0] = STARPU_RW;
  61. cl.model = NULL;
  62. for (i = 0; i < niter; i++)
  63. {
  64. struct starpu_task *task = starpu_task_create();
  65. int ret;
  66. task->cl = &cl;
  67. task->callback_func = NULL;
  68. task->handles[0] = float_array_handle;
  69. ret = starpu_task_submit(task);
  70. if (STARPU_UNLIKELY(ret == -ENODEV))
  71. {
  72. FPRINTF(stderr, "No worker may execute this task\n");
  73. exit(0);
  74. }
  75. }
  76. starpu_task_wait_for_all();
  77. /* update the array in RAM */
  78. starpu_data_unregister(float_array_handle);
  79. FPRINTF(stderr, "variable -> %f\n", foo);
  80. starpu_shutdown();
  81. return 0;
  82. enodev:
  83. starpu_shutdown();
  84. return 77;
  85. }