vector_scal.c 6.0 KB

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