incrementer_cpp.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2020 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. /*
  17. * This is a small example of a C++ program using starpu. We here just
  18. * increment two values of a vector several times.
  19. */
  20. #include <starpu.h>
  21. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  22. #ifdef STARPU_USE_CUDA
  23. extern "C" void cuda_codelet(void *descr[], __attribute__ ((unused)) void *_args);
  24. #endif
  25. #ifdef STARPU_USE_OPENCL
  26. extern "C" void opencl_codelet(void *descr[], __attribute__ ((unused)) void *_args);
  27. struct starpu_opencl_program opencl_program;
  28. #endif
  29. extern "C" void cpu_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  30. {
  31. float *val = (float *)STARPU_VECTOR_GET_PTR(descr[0]);
  32. val[0] += 1.0f;
  33. val[1] += 1.0f;
  34. }
  35. int main(int argc, char **argv)
  36. {
  37. int ret = 0;
  38. starpu_data_handle_t float_array_handle;
  39. float float_array[4] __attribute__ ((aligned (16))) = { 0.0f, 0.0f, 0.0f, 0.0f};
  40. struct starpu_codelet cl;
  41. unsigned i;
  42. unsigned niter = 50;
  43. struct starpu_conf conf;
  44. starpu_conf_init(&conf);
  45. conf.nmic = 0;
  46. conf.nmpi_ms = 0;
  47. ret = starpu_init(&conf);
  48. if (ret == -ENODEV) return 77;
  49. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  50. starpu_vector_data_register(&float_array_handle, STARPU_MAIN_RAM, (uintptr_t)&float_array, 4, sizeof(float));
  51. #ifdef STARPU_USE_OPENCL
  52. ret = starpu_opencl_load_opencl_from_file("examples/incrementer/incrementer_kernels_opencl_kernel.cl", &opencl_program, NULL);
  53. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  54. #endif
  55. starpu_codelet_init(&cl);
  56. cl.cpu_funcs[0] = cpu_codelet;
  57. cl.cpu_funcs_name[0] = "cpu_codelet";
  58. #ifdef STARPU_USE_CUDA
  59. cl.cuda_funcs[0] = cuda_codelet;
  60. cl.cuda_flags[0] = STARPU_CUDA_ASYNC;
  61. #endif
  62. #ifdef STARPU_USE_OPENCL
  63. cl.opencl_funcs[0] = opencl_codelet;
  64. cl.opencl_flags[0] = STARPU_OPENCL_ASYNC;
  65. #endif
  66. cl.nbuffers = 1;
  67. cl.modes[0] = STARPU_RW;
  68. cl.name = "incrementer";
  69. for (i = 0; i < niter; i++)
  70. {
  71. ret = starpu_task_insert(&cl,
  72. STARPU_RW, float_array_handle,
  73. STARPU_TAG_ONLY, (starpu_tag_t) i,
  74. 0);
  75. if (STARPU_UNLIKELY(ret == -ENODEV))
  76. {
  77. FPRINTF(stderr, "No worker may execute this task\n");
  78. exit(77);
  79. }
  80. }
  81. starpu_task_wait_for_all();
  82. /* update the array in RAM */
  83. starpu_data_unregister(float_array_handle);
  84. FPRINTF(stderr, "array -> %f, %f, %f, %f\n", float_array[0],
  85. float_array[1], float_array[2], float_array[3]);
  86. #ifdef STARPU_USE_OPENCL
  87. ret = starpu_opencl_unload_opencl(&opencl_program);
  88. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  89. #endif
  90. starpu_shutdown();
  91. if (float_array[0] != niter || float_array[0] != float_array[1] + float_array[2] + float_array[3])
  92. {
  93. FPRINTF(stderr, "Incorrect result\n");
  94. return EXIT_FAILURE;
  95. }
  96. return EXIT_SUCCESS;
  97. }