vector_scal.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #include <starpu_opencl.h>
  69. /* The OpenCL programs, loaded from `main'. */
  70. static struct starpu_opencl_program cl_programs;
  71. static void vector_scal_opencl (size_t size, float vector[size], float factor)
  72. __attribute__ ((task_implementation ("opencl", vector_scal)));
  73. static void
  74. vector_scal_opencl (size_t size, float vector[size], float factor)
  75. {
  76. int id, devid, err;
  77. cl_kernel kernel;
  78. cl_command_queue queue;
  79. cl_event event;
  80. cl_mem val = (cl_mem) vector;
  81. id = starpu_worker_get_id ();
  82. devid = starpu_worker_get_devid (id);
  83. /* Prepare to invoke the kernel. In the future, this will be largely
  84. automated. */
  85. err = starpu_opencl_load_kernel (&kernel, &queue, &cl_programs,
  86. "vector_mult_opencl", devid);
  87. if (err != CL_SUCCESS)
  88. STARPU_OPENCL_REPORT_ERROR (err);
  89. err = clSetKernelArg (kernel, 0, sizeof (val), &val);
  90. err |= clSetKernelArg (kernel, 1, sizeof (size), &size);
  91. err |= clSetKernelArg (kernel, 2, sizeof (factor), &factor);
  92. if (err)
  93. STARPU_OPENCL_REPORT_ERROR (err);
  94. size_t global = 1, local = 1;
  95. err = clEnqueueNDRangeKernel (queue, kernel, 1, NULL, &global, &local, 0,
  96. NULL, &event);
  97. if (err != CL_SUCCESS)
  98. STARPU_OPENCL_REPORT_ERROR (err);
  99. clFinish (queue);
  100. starpu_opencl_collect_stats (event);
  101. clReleaseEvent (event);
  102. starpu_opencl_release_kernel (kernel);
  103. }
  104. #endif
  105. #define EPSILON 1e-3
  106. static int
  107. check (size_t size, float vector[size], float factor)
  108. {
  109. size_t i;
  110. for (i = 0; i < size; i++)
  111. if (vector[i] - i * factor > EPSILON)
  112. return 1;
  113. return 0;
  114. }
  115. int
  116. main (void)
  117. {
  118. #pragma starpu initialize
  119. #ifdef STARPU_USE_OPENCL
  120. starpu_opencl_load_opencl_from_file ("vector_scal_opencl_kernel.cl",
  121. &cl_programs, "");
  122. #endif
  123. #define NX 0x100000
  124. #define FACTOR 3.14
  125. float vector[NX] __attribute__ ((heap_allocated));
  126. #pragma starpu register vector
  127. size_t i;
  128. for (i = 0; i < NX; i++)
  129. vector[i] = (float) i;
  130. vector_scal (NX, vector, FACTOR);
  131. #pragma starpu wait
  132. #pragma starpu shutdown
  133. return check (NX, vector, FACTOR) ? EXIT_FAILURE : EXIT_SUCCESS;
  134. }