vector_scal.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-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. /*
  17. * This example demonstrates how to use StarPU to scale an array by a factor.
  18. * It shows how to manipulate data with StarPU's data management library.
  19. * 1- how to declare a piece of data to StarPU (starpu_vector_data_register)
  20. * 2- how to describe which data are accessed by a task (task->handles[0])
  21. * 3- how a kernel can manipulate the data (buffers[0].vector.ptr)
  22. */
  23. #include <starpu.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <math.h>
  27. #define NX 204800
  28. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  29. extern void scal_cpu_func(void *buffers[], void *_args);
  30. extern void scal_cpu_func_icc(void *buffers[], void *_args);
  31. extern void scal_sse_func(void *buffers[], void *_args);
  32. extern void scal_sse_func_icc(void *buffers[], void *_args);
  33. extern void scal_cuda_func(void *buffers[], void *_args);
  34. extern void scal_opencl_func(void *buffers[], void *_args);
  35. static struct starpu_perfmodel vector_scal_model =
  36. {
  37. .type = STARPU_HISTORY_BASED,
  38. .symbol = "vector_scal"
  39. };
  40. static struct starpu_perfmodel vector_scal_energy_model =
  41. {
  42. .type = STARPU_HISTORY_BASED,
  43. .symbol = "vector_scal_energy"
  44. };
  45. static struct starpu_codelet cl =
  46. {
  47. /* CPU implementation of the codelet */
  48. .cpu_funcs =
  49. {
  50. scal_cpu_func
  51. #if defined(STARPU_HAVE_ICC) && !defined(__KNC__) && !defined(__KNF__)
  52. , scal_cpu_func_icc
  53. #endif
  54. #ifdef __SSE__
  55. , scal_sse_func
  56. #if defined(STARPU_HAVE_ICC) && !defined(__KNC__) && !defined(__KNF__)
  57. , scal_sse_func_icc
  58. #endif
  59. #endif
  60. },
  61. .cpu_funcs_name =
  62. {
  63. "scal_cpu_func",
  64. #if defined(STARPU_HAVE_ICC) && !defined(__KNC__) && !defined(__KNF__)
  65. "scal_cpu_func_icc",
  66. #endif
  67. #ifdef __SSE__
  68. "scal_sse_func",
  69. #if defined(STARPU_HAVE_ICC) && !defined(__KNC__) && !defined(__KNF__)
  70. "scal_sse_func_icc"
  71. #endif
  72. #endif
  73. },
  74. #ifdef STARPU_USE_CUDA
  75. /* CUDA implementation of the codelet */
  76. .cuda_funcs = {scal_cuda_func},
  77. .cuda_flags = {STARPU_CUDA_ASYNC},
  78. #endif
  79. #ifdef STARPU_USE_OPENCL
  80. /* OpenCL implementation of the codelet */
  81. .opencl_funcs = {scal_opencl_func},
  82. .opencl_flags = {STARPU_OPENCL_ASYNC},
  83. #endif
  84. .nbuffers = 1,
  85. .modes = {STARPU_RW},
  86. .model = &vector_scal_model,
  87. .energy_model = &vector_scal_energy_model
  88. };
  89. #ifdef STARPU_USE_OPENCL
  90. struct starpu_opencl_program opencl_program;
  91. #endif
  92. static int approximately_equal(float a, float b)
  93. {
  94. #ifdef STARPU_HAVE_NEARBYINTF
  95. int ai = (int) nearbyintf(a * 1000.0);
  96. int bi = (int) nearbyintf(b * 1000.0);
  97. #elif defined(STARPU_HAVE_RINTF)
  98. int ai = (int) rintf(a * 1000.0);
  99. int bi = (int) rintf(b * 1000.0);
  100. #else
  101. #error "Please define either nearbyintf or rintf."
  102. #endif
  103. return ai == bi;
  104. }
  105. int main(void)
  106. {
  107. /* We consider a vector of float that is initialized just as any of C
  108. * data */
  109. float vector[NX];
  110. unsigned i;
  111. for (i = 0; i < NX; i++)
  112. vector[i] = (i+1.0f);
  113. /* Initialize StarPU with default configuration */
  114. int ret = starpu_init(NULL);
  115. if (ret == -ENODEV) goto enodev;
  116. FPRINTF(stderr, "[BEFORE] 1-th element : %3.2f\n", vector[1]);
  117. FPRINTF(stderr, "[BEFORE] (NX-1)th element: %3.2f\n", vector[NX-1]);
  118. #ifdef STARPU_USE_OPENCL
  119. ret = starpu_opencl_load_opencl_from_file("examples/basic_examples/vector_scal_opencl_kernel.cl",
  120. &opencl_program, NULL);
  121. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  122. #endif
  123. /* Tell StaPU to associate the "vector" vector with the "vector_handle"
  124. * identifier. When a task needs to access a piece of data, it should
  125. * refer to the handle that is associated to it.
  126. * In the case of the "vector" data interface:
  127. * - the first argument of the registration method is a pointer to the
  128. * handle that should describe the data
  129. * - the second argument is the memory node where the data (ie. "vector")
  130. * resides initially: STARPU_MAIN_RAM stands for an address in main memory, as
  131. * opposed to an adress on a GPU for instance.
  132. * - the third argument is the adress of the vector in RAM
  133. * - the fourth argument is the number of elements in the vector
  134. * - the fifth argument is the size of each element.
  135. */
  136. starpu_data_handle_t vector_handle;
  137. starpu_memory_pin(vector, sizeof(vector));
  138. starpu_vector_data_register(&vector_handle, STARPU_MAIN_RAM, (uintptr_t)vector, NX, sizeof(vector[0]));
  139. float factor = 3.14;
  140. /* create a synchronous task: any call to starpu_task_submit will block
  141. * until it is terminated */
  142. struct starpu_task *task = starpu_task_create();
  143. task->synchronous = 1;
  144. task->cl = &cl;
  145. /* the codelet manipulates one buffer in RW mode */
  146. task->handles[0] = vector_handle;
  147. /* an argument is passed to the codelet, beware that this is a
  148. * READ-ONLY buffer and that the codelet may be given a pointer to a
  149. * COPY of the argument */
  150. task->cl_arg = &factor;
  151. task->cl_arg_size = sizeof(factor);
  152. /* execute the task on any eligible computational ressource */
  153. ret = starpu_task_submit(task);
  154. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  155. /* StarPU does not need to manipulate the array anymore so we can stop
  156. * monitoring it */
  157. starpu_data_unregister(vector_handle);
  158. starpu_memory_unpin(vector, sizeof(vector));
  159. #ifdef STARPU_USE_OPENCL
  160. ret = starpu_opencl_unload_opencl(&opencl_program);
  161. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  162. #endif
  163. /* terminate StarPU, no task can be submitted after */
  164. starpu_shutdown();
  165. ret = approximately_equal(vector[1], (1+1.0f) * factor) && approximately_equal(vector[NX-1], (NX-1+1.0f) * factor);
  166. FPRINTF(stderr, "[AFTER] 1-th element : %3.2f (should be %3.2f)\n", vector[1], (1+1.0f) * factor);
  167. FPRINTF(stderr, "[AFTER] (NX-1)-th element: %3.2f (should be %3.2f)\n", vector[NX-1], (NX-1+1.0f) * factor);
  168. FPRINTF(stderr, "[AFTER] Computation is%s correct\n", ret?"":" NOT");
  169. return (ret ? EXIT_SUCCESS : EXIT_FAILURE);
  170. enodev:
  171. return 77;
  172. }