variable.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU 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. * StarPU 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. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  18. #ifdef STARPU_QUICK_CHECK
  19. static unsigned niter = 500;
  20. #elif !defined(STARPU_LONG_CHECK)
  21. static unsigned niter = 5000;
  22. #else
  23. static unsigned niter = 50000;
  24. #endif
  25. extern void cpu_codelet(void *descr[], void *_args);
  26. #ifdef STARPU_USE_CUDA
  27. extern void cuda_codelet(void *descr[], void *_args);
  28. #endif
  29. #ifdef STARPU_USE_OPENCL
  30. extern void opencl_codelet(void *descr[], void *_args);
  31. struct starpu_opencl_program opencl_program;
  32. #endif
  33. int main(int argc, char **argv)
  34. {
  35. unsigned i;
  36. float foo;
  37. starpu_data_handle_t float_array_handle;
  38. struct starpu_codelet cl;
  39. int ret;
  40. ret = starpu_init(NULL);
  41. if (ret == -ENODEV) goto enodev;
  42. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  43. if (argc == 2) niter = atoi(argv[1]);
  44. foo = 0.0f;
  45. starpu_variable_data_register(&float_array_handle, STARPU_MAIN_RAM /* 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. starpu_codelet_init(&cl);
  52. cl.cpu_funcs[0] = cpu_codelet;
  53. cl.cpu_funcs_name[0] = "cpu_codelet";
  54. #ifdef STARPU_USE_CUDA
  55. cl.cuda_funcs[0] = cuda_codelet;
  56. #endif
  57. #ifdef STARPU_USE_OPENCL
  58. cl.opencl_funcs[0] = opencl_codelet;
  59. #endif
  60. cl.nbuffers = 1;
  61. cl.modes[0] = STARPU_RW;
  62. cl.model = NULL;
  63. cl.name = "variable_inc";
  64. for (i = 0; i < niter; i++)
  65. {
  66. struct starpu_task *task = starpu_task_create();
  67. task->cl = &cl;
  68. task->callback_func = NULL;
  69. task->handles[0] = float_array_handle;
  70. ret = starpu_task_submit(task);
  71. if (STARPU_UNLIKELY(ret == -ENODEV))
  72. {
  73. FPRINTF(stderr, "No worker may execute this task\n");
  74. starpu_data_unregister(float_array_handle);
  75. goto enodev;
  76. }
  77. }
  78. starpu_task_wait_for_all();
  79. /* update the array in RAM */
  80. starpu_data_unregister(float_array_handle);
  81. FPRINTF(stderr, "variable -> %f\n", foo);
  82. FPRINTF(stderr, "result is %scorrect\n", foo==niter?"":"IN");
  83. starpu_shutdown();
  84. return (foo == niter) ? EXIT_SUCCESS:EXIT_FAILURE;
  85. enodev:
  86. return 77;
  87. }