vector_scal.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 is a simple example that is primarily meant to test the features
  20. offered by the StarPU GCC plug-in.
  21. Currently tested features :
  22. - multi-implementations
  23. - CPU codelet
  24. Features to test in a near future :
  25. - CUDA
  26. - Filters
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. /* Declare and define the standard CPU implementation. */
  31. static void vector_scal (size_t size, float vector[size], float factor)
  32. __attribute__ ((task));
  33. static void vector_scal_cpu (size_t size, float vector[size], float factor)
  34. __attribute__ ((task_implementation ("cpu", vector_scal)));
  35. static void
  36. vector_scal_cpu (size_t size, float vector[size], float factor)
  37. {
  38. size_t i;
  39. for (i = 0; i < size; i++)
  40. vector[i] *= factor;
  41. }
  42. #ifdef __SSE__
  43. /* The SSE-capable CPU implementation. */
  44. #include <xmmintrin.h>
  45. static void vector_scal_sse (size_t size, float vector[size], float factor)
  46. __attribute__ ((task_implementation ("cpu", vector_scal)));
  47. static void
  48. vector_scal_sse (size_t size, float vector[size], float factor)
  49. {
  50. unsigned int n_iterations = size / 4;
  51. __m128 *VECTOR = (__m128 *) vector;
  52. __m128 _FACTOR __attribute__ ((aligned (16)));
  53. _FACTOR = _mm_set1_ps (factor);
  54. unsigned int i;
  55. for (i = 0; i < n_iterations; i++)
  56. VECTOR[i] = _mm_mul_ps (_FACTOR, VECTOR[i]);
  57. unsigned int remainder = size % 4;
  58. if (remainder != 0)
  59. {
  60. unsigned int start = 4 * n_iterations;
  61. for (i = start; i < start + remainder; ++i)
  62. vector[i] = factor * vector[i];
  63. }
  64. }
  65. #endif /* __SSE__ */
  66. /* Declaration and definition of the OpenCL implementation. */
  67. #ifdef STARPU_USE_OPENCL
  68. /* The OpenCL programs, loaded from `main'. */
  69. static struct starpu_opencl_program cl_programs;
  70. static void vector_scal_opencl (size_t size, float vector[size], float factor)
  71. __attribute__ ((task_implementation ("opencl", vector_scal)));
  72. static void
  73. vector_scal_opencl (size_t size, float vector[size], float factor)
  74. {
  75. int id, devid, err;
  76. cl_kernel kernel;
  77. cl_command_queue queue;
  78. cl_event event;
  79. cl_mem val = (cl_mem) vector;
  80. id = starpu_worker_get_id ();
  81. devid = starpu_worker_get_devid (id);
  82. /* Prepare to invoke the kernel. In the future, this will be largely
  83. automated. */
  84. err = starpu_opencl_load_kernel (&kernel, &queue, &programs,
  85. "vector_mult_opencl", devid);
  86. if (err != CL_SUCCESS)
  87. STARPU_OPENCL_REPORT_ERROR (err);
  88. err = clSetKernelArg (kernel, 0, sizeof (val), &val);
  89. err |= clSetKernelArg (kernel, 1, sizeof (size), &size);
  90. err |= clSetKernelArg (kernel, 2, sizeof (factor), &factor);
  91. if (err)
  92. STARPU_OPENCL_REPORT_ERROR (err);
  93. size_t global = 1, local = 1;
  94. err = clEnqueueNDRangeKernel (queue, kernel, 1, NULL, &global, &local, 0,
  95. NULL, &event);
  96. if (err != CL_SUCCESS)
  97. STARPU_OPENCL_REPORT_ERROR (err);
  98. clFinish (queue);
  99. starpu_opencl_collect_stats (event);
  100. clReleaseEvent (event);
  101. starpu_opencl_release_kernel (kernel);
  102. }
  103. #endif
  104. #define EPSILON 1e-3
  105. static int
  106. check (size_t size, float vector[size], float factor)
  107. {
  108. size_t i;
  109. for (i = 0; i < size; i++)
  110. if (vector[i] - i * factor > EPSILON)
  111. return 1;
  112. return 0;
  113. }
  114. int
  115. main (void)
  116. {
  117. #pragma starpu initialize
  118. #ifdef STARPU_USE_OPENCL
  119. starpu_opencl_load_opencl_from_file ("vector_scal_opencl_kernel.cl",
  120. &cl_programs, NULL);
  121. #endif
  122. #define NX 0x100000
  123. #define FACTOR 3.14
  124. float vector[NX] __attribute__ ((heap_allocated));
  125. #pragma starpu register vector
  126. size_t i;
  127. for (i = 0; i < NX; i++)
  128. vector[i] = (float) i;
  129. vector_scal (NX, vector, FACTOR);
  130. #pragma starpu wait
  131. #pragma starpu shutdown
  132. return check (NX, vector, FACTOR) ? EXIT_FAILURE : EXIT_SUCCESS;
  133. }