vector_scal_plugin.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013,2015,2017 CNRS
  4. * Copyright (C) 2013 Université de Bordeaux
  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. /* This example showcases features of the StarPU GCC plug-in. It defines a
  19. "vector scaling" task with multiple CPU implementations, an OpenCL
  20. implementation, and a CUDA implementation.
  21. Compiling it without `-fplugin=starpu.so' yields valid sequential code. */
  22. #include <math.h>
  23. #include <stdbool.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. /* Declare and define the standard CPU implementation. */
  27. static void vector_scal (unsigned int size, float vector[size], float factor)
  28. __attribute__ ((task));
  29. /* The CPU implementation. */
  30. static void
  31. vector_scal (unsigned int size, float vector[size], float factor)
  32. {
  33. unsigned int i;
  34. for (i = 0; i < size; i++)
  35. vector[i] *= factor;
  36. }
  37. #if defined STARPU_GCC_PLUGIN && defined __SSE__
  38. /* The SSE-capable CPU implementation. */
  39. #include <xmmintrin.h>
  40. static void vector_scal_sse (unsigned int size, float vector[size], float factor)
  41. __attribute__ ((task_implementation ("cpu", vector_scal)));
  42. static void
  43. vector_scal_sse (unsigned int size, float vector[size], float factor)
  44. {
  45. unsigned int n_iterations = size / 4;
  46. __m128 *VECTOR = (__m128 *) vector;
  47. __m128 _FACTOR STARPU_ATTRIBUTE_ALIGNED(16);
  48. _FACTOR = _mm_set1_ps (factor);
  49. unsigned int i;
  50. for (i = 0; i < n_iterations; i++)
  51. VECTOR[i] = _mm_mul_ps (_FACTOR, VECTOR[i]);
  52. unsigned int remainder = size % 4;
  53. if (remainder != 0)
  54. {
  55. unsigned int start = 4 * n_iterations;
  56. for (i = start; i < start + remainder; ++i)
  57. vector[i] = factor * vector[i];
  58. }
  59. }
  60. #endif /* __SSE__ */
  61. /* Declaration and definition of the OpenCL implementation. */
  62. #if defined STARPU_GCC_PLUGIN && defined STARPU_USE_OPENCL
  63. #include <starpu_opencl.h>
  64. /* The OpenCL programs, loaded from `main'. */
  65. static struct starpu_opencl_program cl_programs;
  66. static void vector_scal_opencl (unsigned int size, float vector[size], float factor)
  67. __attribute__ ((task_implementation ("opencl", vector_scal)));
  68. static void
  69. vector_scal_opencl (unsigned int size, float vector[size], float factor)
  70. {
  71. int id, devid, err;
  72. cl_kernel kernel;
  73. cl_command_queue queue;
  74. cl_event event;
  75. cl_mem val = (cl_mem) vector;
  76. id = starpu_worker_get_id ();
  77. devid = starpu_worker_get_devid (id);
  78. /* Prepare to invoke the kernel. In the future, this will be largely
  79. automated. */
  80. err = starpu_opencl_load_kernel (&kernel, &queue, &cl_programs,
  81. "vector_mult_opencl", devid);
  82. if (err != CL_SUCCESS)
  83. STARPU_OPENCL_REPORT_ERROR (err);
  84. err = clSetKernelArg (kernel, 0, sizeof (size), &size);
  85. err |= clSetKernelArg (kernel, 1, sizeof (val), &val);
  86. err |= clSetKernelArg (kernel, 2, sizeof (factor), &factor);
  87. if (err)
  88. STARPU_OPENCL_REPORT_ERROR (err);
  89. size_t global = size, local = 1;
  90. err = clEnqueueNDRangeKernel (queue, kernel, 1, NULL, &global, &local, 0,
  91. NULL, &event);
  92. if (err != CL_SUCCESS)
  93. STARPU_OPENCL_REPORT_ERROR (err);
  94. clFinish (queue);
  95. starpu_opencl_collect_stats (event);
  96. clReleaseEvent (event);
  97. starpu_opencl_release_kernel (kernel);
  98. }
  99. #endif
  100. #ifdef STARPU_USE_CUDA
  101. /* Declaration of the CUDA implementation. The definition itself is in the
  102. `.cu' file itself. */
  103. extern void vector_scal_cuda (unsigned int size, float vector[size], float factor)
  104. __attribute__ ((task_implementation ("cuda", vector_scal)));
  105. #endif
  106. #define EPSILON 1e-3
  107. static bool
  108. check (unsigned int size, float vector[size], float factor)
  109. {
  110. unsigned int i;
  111. for (i = 0; i < size; i++)
  112. {
  113. if (fabs(vector[i] - i * factor) > i*factor*EPSILON)
  114. {
  115. fprintf(stderr, "%.2f != %.2f\n", vector[i], i*factor);
  116. return false;
  117. }
  118. }
  119. fprintf(stderr, "computation is correct\n");
  120. return true;
  121. }
  122. int
  123. main (void)
  124. {
  125. bool valid;
  126. #pragma starpu initialize
  127. #if defined STARPU_GCC_PLUGIN && defined STARPU_USE_OPENCL
  128. starpu_opencl_load_opencl_from_file ("vector_scal_opencl_kernel.cl",
  129. &cl_programs, "");
  130. #endif
  131. #define NX 0x100000
  132. #define FACTOR 3.14
  133. {
  134. float vector[NX] __attribute__ ((heap_allocated, registered));
  135. unsigned int i;
  136. for (i = 0; i < NX; i++)
  137. vector[i] = (float) i;
  138. vector_scal (NX, vector, FACTOR);
  139. #pragma starpu wait
  140. #pragma starpu acquire vector
  141. valid = check (NX, vector, FACTOR);
  142. #pragma starpu release vector
  143. } /* VECTOR is automatically freed here. */
  144. #pragma starpu shutdown
  145. return valid ? EXIT_SUCCESS : EXIT_FAILURE;
  146. }