incrementer_cpp.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010-2011, 2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  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. #include <starpu.h>
  19. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  20. #ifdef STARPU_USE_CUDA
  21. extern "C" void cuda_codelet(void *descr[], __attribute__ ((unused)) void *_args);
  22. #endif
  23. #ifdef STARPU_USE_OPENCL
  24. extern "C" void opencl_codelet(void *descr[], __attribute__ ((unused)) void *_args);
  25. struct starpu_opencl_program opencl_program;
  26. #endif
  27. void cpu_codelet(void *descr[], __attribute__ ((unused)) void *_args)
  28. {
  29. float *val = (float *)STARPU_VECTOR_GET_PTR(descr[0]);
  30. val[0] += 1.0f;
  31. val[1] += 1.0f;
  32. }
  33. int main(int argc, char **argv)
  34. {
  35. int ret = 0;
  36. starpu_data_handle_t float_array_handle;
  37. float float_array[4] __attribute__ ((aligned (16))) = { 0.0f, 0.0f, 0.0f, 0.0f};
  38. struct starpu_codelet cl;
  39. unsigned i;
  40. unsigned niter = 50;
  41. ret = starpu_init(NULL);
  42. if (ret == -ENODEV) return 77;
  43. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  44. starpu_vector_data_register(&float_array_handle, STARPU_MAIN_RAM, (uintptr_t)&float_array, 4, sizeof(float));
  45. #ifdef STARPU_USE_OPENCL
  46. ret = starpu_opencl_load_opencl_from_file("examples/incrementer/incrementer_kernels_opencl_kernel.cl", &opencl_program, NULL);
  47. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  48. #endif
  49. starpu_codelet_init(&cl);
  50. cl.cpu_funcs[0] = cpu_codelet;
  51. #ifdef STARPU_USE_CUDA
  52. cl.cuda_funcs[0] = cuda_codelet;
  53. #endif
  54. #ifdef STARPU_USE_OPENCL
  55. cl.opencl_funcs[0] = opencl_codelet;
  56. #endif
  57. cl.nbuffers = 1;
  58. cl.modes[0] = STARPU_RW;
  59. cl.name = "incrementer";
  60. for (i = 0; i < niter; i++)
  61. {
  62. ret = starpu_insert_task(&cl,
  63. STARPU_RW, float_array_handle,
  64. 0);
  65. if (STARPU_UNLIKELY(ret == -ENODEV))
  66. {
  67. FPRINTF(stderr, "No worker may execute this task\n");
  68. exit(77);
  69. }
  70. }
  71. starpu_task_wait_for_all();
  72. /* update the array in RAM */
  73. starpu_data_unregister(float_array_handle);
  74. FPRINTF(stderr, "array -> %f, %f, %f, %f\n", float_array[0],
  75. float_array[1], float_array[2], float_array[3]);
  76. #ifdef STARPU_USE_OPENCL
  77. ret = starpu_opencl_unload_opencl(&opencl_program);
  78. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  79. #endif
  80. starpu_shutdown();
  81. if (float_array[0] != niter || float_array[0] != float_array[1] + float_array[2] + float_array[3])
  82. {
  83. FPRINTF(stderr, "Incorrect result\n");
  84. return EXIT_FAILURE;
  85. }
  86. return EXIT_SUCCESS;
  87. }