vector_scal.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012 INRIA
  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. /*
  20. * This is a very simple example that is primarily meant to test the
  21. * features offered by the StarPU GCC plug-in.
  22. *
  23. * Currently tested features :
  24. * - multi-implementations
  25. * - CPU codelet
  26. *
  27. * Features to test in a near future :
  28. * - CUDA
  29. * - OpenCL
  30. * - Filters
  31. */
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #ifdef __SSE__
  35. #include <xmmintrin.h>
  36. #endif
  37. #define NX 16
  38. #define FACTOR 3.14
  39. static float vector[NX];
  40. static void vector_scal(float *v, size_t n, float factor)
  41. __attribute__ ((task));
  42. static void vector_scal_cpu(float *v, size_t n, float factor)
  43. __attribute__ ((task_implementation ("cpu", vector_scal)));
  44. #ifdef __SSE__
  45. static void vector_scal_sse(float *v, size_t n, float factor)
  46. __attribute__ ((task_implementation ("cpu", vector_scal)));
  47. #endif /* !__SSE__ */
  48. static void
  49. vector_scal_cpu(float *v, size_t n, float factor)
  50. {
  51. int i;
  52. for (i = 0; i < n; i++)
  53. v[i] *= factor;
  54. }
  55. #ifdef __SSE__
  56. static void
  57. vector_scal_sse(float *vector, size_t n, float factor)
  58. {
  59. unsigned int n_iterations = n/4;
  60. __m128 *VECTOR = (__m128*) vector;
  61. __m128 _FACTOR __attribute__((aligned(16)));
  62. _FACTOR = _mm_set1_ps(factor);
  63. unsigned int i;
  64. for (i = 0; i < n_iterations; i++)
  65. VECTOR[i] = _mm_mul_ps(_FACTOR, VECTOR[i]);
  66. unsigned int remainder = n%4;
  67. if (remainder != 0)
  68. {
  69. unsigned int start = 4 * n_iterations;
  70. for (i = start; i < start+remainder; ++i)
  71. {
  72. vector[i] = factor * vector[i];
  73. }
  74. }
  75. }
  76. #endif /* !__SSE__ */
  77. static void
  78. init_data(void)
  79. {
  80. int i;
  81. for (i = 0; i < NX; i++)
  82. vector[i] = (float) i;
  83. }
  84. #define EPSILON 1e-3
  85. static int
  86. check(void)
  87. {
  88. int i;
  89. for (i = 0; i < NX; i++)
  90. if (vector[i] - i*FACTOR > EPSILON)
  91. return 1;
  92. return 0;
  93. }
  94. int
  95. main(void)
  96. {
  97. #pragma starpu initialize
  98. init_data();
  99. #pragma starpu register &vector NX
  100. vector_scal(vector, NX, FACTOR);
  101. #pragma starpu wait
  102. #pragma starpu unregister &vector
  103. #pragma starpu shutdown
  104. return check()?EXIT_FAILURE:EXIT_SUCCESS;
  105. }