vector_scal.c 6.5 KB

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