vector_scal.c 4.9 KB

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