vector_scal.c 4.8 KB

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