vector_scal.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. - OpenCL
  27. - Filters
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. /* Declare and define the standard CPU implementation. */
  32. static void vector_scal (size_t size, float vector[size], float factor)
  33. __attribute__ ((task));
  34. static void vector_scal_cpu (size_t size, float vector[size], float factor)
  35. __attribute__ ((task_implementation ("cpu", vector_scal)));
  36. static void
  37. vector_scal_cpu (size_t size, float vector[size], float factor)
  38. {
  39. size_t i;
  40. for (i = 0; i < size; i++)
  41. vector[i] *= factor;
  42. }
  43. #ifdef __SSE__
  44. /* The SSE-capable CPU implementation. */
  45. #include <xmmintrin.h>
  46. static void vector_scal_sse (size_t size, float vector[size], float factor)
  47. __attribute__ ((task_implementation ("cpu", vector_scal)));
  48. static void
  49. vector_scal_sse (size_t size, float vector[size], float factor)
  50. {
  51. unsigned int n_iterations = size / 4;
  52. __m128 *VECTOR = (__m128 *) vector;
  53. __m128 _FACTOR __attribute__ ((aligned (16)));
  54. _FACTOR = _mm_set1_ps (factor);
  55. unsigned int i;
  56. for (i = 0; i < n_iterations; i++)
  57. VECTOR[i] = _mm_mul_ps (_FACTOR, VECTOR[i]);
  58. unsigned int remainder = size % 4;
  59. if (remainder != 0)
  60. {
  61. unsigned int start = 4 * n_iterations;
  62. for (i = start; i < start + remainder; ++i)
  63. vector[i] = factor * vector[i];
  64. }
  65. }
  66. #endif /* __SSE__ */
  67. #define EPSILON 1e-3
  68. static int
  69. check (size_t size, float vector[size], float factor)
  70. {
  71. size_t i;
  72. for (i = 0; i < size; i++)
  73. if (vector[i] - i * factor > EPSILON)
  74. return 1;
  75. return 0;
  76. }
  77. int
  78. main (void)
  79. {
  80. #pragma starpu initialize
  81. #define NX 0x100000
  82. #define FACTOR 3.14
  83. float vector[NX] __attribute__ ((heap_allocated));
  84. #pragma starpu register vector
  85. size_t i;
  86. for (i = 0; i < NX; i++)
  87. vector[i] = (float) i;
  88. vector_scal (NX, vector, FACTOR);
  89. #pragma starpu wait
  90. #pragma starpu shutdown
  91. return check (NX, vector, FACTOR) ? EXIT_FAILURE : EXIT_SUCCESS;
  92. }