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