vector_scal.c 2.8 KB

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