incrementer_cpp.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010-2011, 2013-2015 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014 CNRS
  5. * Copyright (C) 2012 INRIA
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. /*
  19. * This is a small example of a C++ program using starpu. We here just
  20. * increment two values of a vector several times.
  21. */
  22. #include <starpu.h>
  23. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  24. #ifdef STARPU_USE_CUDA
  25. extern "C" void cuda_codelet(void *descr[], __attribute__ ((unused)) void *_args);
  26. #endif
  27. #ifdef STARPU_USE_OPENCL
  28. extern "C" void opencl_codelet(void *descr[], __attribute__ ((unused)) void *_args);
  29. struct starpu_opencl_program opencl_program;
  30. #endif
  31. extern "C" void cpu_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  32. {
  33. float *val = (float *)STARPU_VECTOR_GET_PTR(descr[0]);
  34. val[0] += 1.0f;
  35. val[1] += 1.0f;
  36. }
  37. int main(int argc, char **argv)
  38. {
  39. int ret = 0;
  40. starpu_data_handle_t float_array_handle;
  41. float float_array[4] __attribute__ ((aligned (16))) = { 0.0f, 0.0f, 0.0f, 0.0f};
  42. struct starpu_codelet cl;
  43. unsigned i;
  44. unsigned niter = 50;
  45. struct starpu_conf conf;
  46. starpu_conf_init(&conf);
  47. conf.nmic = 0;
  48. conf.nscc = 0;
  49. conf.nmpi_ms = 0;
  50. ret = starpu_init(&conf);
  51. if (ret == -ENODEV) return 77;
  52. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  53. starpu_vector_data_register(&float_array_handle, STARPU_MAIN_RAM, (uintptr_t)&float_array, 4, sizeof(float));
  54. #ifdef STARPU_USE_OPENCL
  55. ret = starpu_opencl_load_opencl_from_file("examples/incrementer/incrementer_kernels_opencl_kernel.cl", &opencl_program, NULL);
  56. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  57. #endif
  58. starpu_codelet_init(&cl);
  59. cl.cpu_funcs[0] = cpu_codelet;
  60. cl.cpu_funcs_name[0] = "cpu_codelet";
  61. #ifdef STARPU_USE_CUDA
  62. cl.cuda_funcs[0] = cuda_codelet;
  63. cl.cuda_flags[0] = STARPU_CUDA_ASYNC;
  64. #endif
  65. #ifdef STARPU_USE_OPENCL
  66. cl.opencl_funcs[0] = opencl_codelet;
  67. cl.opencl_flags[0] = STARPU_OPENCL_ASYNC;
  68. #endif
  69. cl.nbuffers = 1;
  70. cl.modes[0] = STARPU_RW;
  71. cl.name = "incrementer";
  72. for (i = 0; i < niter; i++)
  73. {
  74. ret = starpu_task_insert(&cl,
  75. STARPU_RW, float_array_handle,
  76. STARPU_TAG_ONLY, (starpu_tag_t) i,
  77. 0);
  78. if (STARPU_UNLIKELY(ret == -ENODEV))
  79. {
  80. FPRINTF(stderr, "No worker may execute this task\n");
  81. exit(77);
  82. }
  83. }
  84. starpu_task_wait_for_all();
  85. /* update the array in RAM */
  86. starpu_data_unregister(float_array_handle);
  87. FPRINTF(stderr, "array -> %f, %f, %f, %f\n", float_array[0],
  88. float_array[1], float_array[2], float_array[3]);
  89. #ifdef STARPU_USE_OPENCL
  90. ret = starpu_opencl_unload_opencl(&opencl_program);
  91. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  92. #endif
  93. starpu_shutdown();
  94. if (float_array[0] != niter || float_array[0] != float_array[1] + float_array[2] + float_array[3])
  95. {
  96. FPRINTF(stderr, "Incorrect result\n");
  97. return EXIT_FAILURE;
  98. }
  99. return EXIT_SUCCESS;
  100. }