incrementer.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2011, 2013-2015 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013 CNRS
  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. /*
  18. * This is just a small example which increments two values of a vector several times.
  19. */
  20. #include <starpu.h>
  21. static unsigned niter = 50000;
  22. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  23. #ifdef STARPU_USE_CUDA
  24. extern void cuda_codelet(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args);
  25. #endif
  26. #ifdef STARPU_USE_OPENCL
  27. extern void opencl_codelet(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args);
  28. struct starpu_opencl_program opencl_program;
  29. #endif
  30. void cpu_codelet(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  31. {
  32. float *val = (float *)STARPU_VECTOR_GET_PTR(descr[0]);
  33. val[0] += 1.0f; val[1] += 1.0f;
  34. }
  35. int main(int argc, char **argv)
  36. {
  37. int ret = 0;
  38. ret = starpu_init(NULL);
  39. if (ret == -ENODEV)
  40. return 77;
  41. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  42. #ifdef STARPU_QUICK_CHECK
  43. niter /= 100;
  44. #endif
  45. if (argc == 2)
  46. niter = atoi(argv[1]);
  47. float float_array[4] STARPU_ATTRIBUTE_ALIGNED(16) = { 0.0f, 0.0f, 0.0f, 0.0f};
  48. starpu_data_handle_t float_array_handle;
  49. starpu_vector_data_register(&float_array_handle, STARPU_MAIN_RAM /* home node */,
  50. (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. struct starpu_codelet cl =
  56. {
  57. .cpu_funcs = {cpu_codelet},
  58. .cpu_funcs_name = {"cpu_codelet"},
  59. #ifdef STARPU_USE_CUDA
  60. .cuda_funcs = {cuda_codelet},
  61. .cuda_flags = {STARPU_CUDA_ASYNC},
  62. #endif
  63. #ifdef STARPU_USE_OPENCL
  64. .opencl_funcs = {opencl_codelet},
  65. .opencl_flags = {STARPU_OPENCL_ASYNC},
  66. #endif
  67. .nbuffers = 1,
  68. .modes = {STARPU_RW},
  69. .name = "increment"
  70. };
  71. double start;
  72. double end;
  73. start = starpu_timing_now();
  74. unsigned i;
  75. for (i = 0; i < niter; i++)
  76. {
  77. struct starpu_task *task = starpu_task_create();
  78. task->cl = &cl;
  79. task->callback_func = NULL;
  80. task->handles[0] = float_array_handle;
  81. ret = starpu_task_submit(task);
  82. if (STARPU_UNLIKELY(ret == -ENODEV))
  83. {
  84. FPRINTF(stderr, "No worker may execute this task\n");
  85. exit(0);
  86. }
  87. }
  88. starpu_task_wait_for_all();
  89. /* update the array in RAM */
  90. starpu_data_unregister(float_array_handle);
  91. end = starpu_timing_now();
  92. FPRINTF(stderr, "array -> %f, %f, %f, %f\n", float_array[0],
  93. float_array[1], float_array[2], float_array[3]);
  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. ret = 1;
  98. }
  99. double timing = end - start;
  100. FPRINTF(stderr, "%u elems took %f ms\n", niter, timing/1000);
  101. starpu_shutdown();
  102. return ret;
  103. }