vector_scal.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 (size_t size, float vector[size], float factor)
  28. __attribute__ ((task));
  29. static void vector_scal_cpu (size_t size, float vector[size], float factor)
  30. __attribute__ ((task_implementation ("cpu", vector_scal)));
  31. static void
  32. vector_scal_cpu (size_t size, float vector[size], float factor)
  33. {
  34. size_t 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 (size_t size, float vector[size], float factor)
  42. __attribute__ ((task_implementation ("cpu", vector_scal)));
  43. static void
  44. vector_scal_sse (size_t 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 (size_t size, float vector[size], float factor)
  68. __attribute__ ((task_implementation ("opencl", vector_scal)));
  69. static void
  70. vector_scal_opencl (size_t 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. /* XXX : clSetKernelArg will not work with a size_t ... */
  86. int _size = size;
  87. err = clSetKernelArg (kernel, 0, sizeof (val), &val);
  88. err |= clSetKernelArg (kernel, 1, sizeof (_size), &_size);
  89. err |= clSetKernelArg (kernel, 2, sizeof (factor), &factor);
  90. if (err)
  91. STARPU_OPENCL_REPORT_ERROR (err);
  92. size_t global = _size, local = 1;
  93. err = clEnqueueNDRangeKernel (queue, kernel, 1, NULL, &global, &local, 0,
  94. NULL, &event);
  95. if (err != CL_SUCCESS)
  96. STARPU_OPENCL_REPORT_ERROR (err);
  97. clFinish (queue);
  98. starpu_opencl_collect_stats (event);
  99. clReleaseEvent (event);
  100. starpu_opencl_release_kernel (kernel);
  101. }
  102. #endif
  103. #ifdef STARPU_USE_CUDA
  104. /* Declaration of the CUDA implementation. The definition itself is in the
  105. `.cu' file itself. */
  106. extern void vector_scal_cuda (size_t size, float vector[size], float factor)
  107. __attribute__ ((task_implementation ("cuda", vector_scal)));
  108. #endif
  109. #define EPSILON 1e-3
  110. static bool
  111. check (size_t size, float vector[size], float factor)
  112. {
  113. size_t i;
  114. for (i = 0; i < size; i++)
  115. if (fabs(vector[i] - i * factor) > EPSILON)
  116. return false;
  117. return true;
  118. }
  119. int
  120. main (void)
  121. {
  122. bool valid;
  123. #pragma starpu initialize
  124. #ifdef STARPU_USE_OPENCL
  125. starpu_opencl_load_opencl_from_file ("vector_scal_opencl_kernel.cl",
  126. &cl_programs, "");
  127. #endif
  128. #define NX 0x100000
  129. #define FACTOR 3.14
  130. {
  131. float vector[NX] __attribute__ ((heap_allocated));
  132. #pragma starpu register vector
  133. size_t i;
  134. for (i = 0; i < NX; i++)
  135. vector[i] = (float) i;
  136. vector_scal (NX, vector, FACTOR);
  137. #pragma starpu wait
  138. #pragma starpu unregister vector
  139. valid = check (NX, vector, FACTOR);
  140. } /* VECTOR is automatically freed here. */
  141. #pragma starpu shutdown
  142. return valid ? EXIT_SUCCESS : EXIT_FAILURE;
  143. }