vector_scal_cpu_template.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012 Inria
  4. * Copyright (C) 2010-2012,2015,2017 CNRS
  5. * Copyright (C) 2013 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. /*
  19. * This example complements vector_scal.c: here we implement a CPU version.
  20. */
  21. #ifndef __VECTOR_SCAL_CPU_TEMPLATE_H__
  22. #define __VECTOR_SCAL_CPU_TEMPLATE_H__
  23. #include <starpu.h>
  24. #ifdef __SSE__
  25. #include <xmmintrin.h>
  26. #endif
  27. /* This kernel takes a buffer and scales it by a constant factor */
  28. #define VECTOR_SCAL_CPU_FUNC(func_name) \
  29. void func_name(void *buffers[], void *cl_arg) \
  30. { \
  31. unsigned i; \
  32. float *factor = (float *) cl_arg; \
  33. \
  34. /* \
  35. * The "buffers" array matches the task->handles array: for instance \
  36. * task->handles[0] is a handle that corresponds to a data with \
  37. * vector "interface", so that the first entry of the array in the \
  38. * codelet is a pointer to a structure describing such a vector (ie. \
  39. * struct starpu_vector_interface *). Here, we therefore manipulate \
  40. * the buffers[0] element as a vector: nx gives the number of elements \
  41. * in the array, ptr gives the location of the array (that was possibly \
  42. * migrated/replicated), and elemsize gives the size of each elements. \
  43. */ \
  44. \
  45. struct starpu_vector_interface *vector = (struct starpu_vector_interface *) buffers[0]; \
  46. \
  47. /* length of the vector */ \
  48. unsigned n = STARPU_VECTOR_GET_NX(vector); \
  49. \
  50. /* get a pointer to the local copy of the vector : note that we have to \
  51. * cast it in (float *) since a vector could contain any type of \
  52. * elements so that the .ptr field is actually a uintptr_t */ \
  53. float *val = (float *)STARPU_VECTOR_GET_PTR(vector); \
  54. \
  55. /* scale the vector */ \
  56. for (i = 0; i < n; i++) \
  57. val[i] *= *factor; \
  58. }
  59. #ifdef __SSE__
  60. #define VECTOR_SCAL_SSE_FUNC(func_name) \
  61. void func_name(void *buffers[], void *cl_arg) \
  62. { \
  63. float *vector = (float *) STARPU_VECTOR_GET_PTR(buffers[0]); \
  64. unsigned int n = STARPU_VECTOR_GET_NX(buffers[0]); \
  65. unsigned int n_iterations = n/4; \
  66. \
  67. __m128 *VECTOR = (__m128*) vector; \
  68. __m128 FACTOR STARPU_ATTRIBUTE_ALIGNED(16); \
  69. float factor = *(float *) cl_arg; \
  70. FACTOR = _mm_set1_ps(factor); \
  71. \
  72. unsigned int i; \
  73. for (i = 0; i < n_iterations; i++) \
  74. VECTOR[i] = _mm_mul_ps(FACTOR, VECTOR[i]); \
  75. \
  76. unsigned int remainder = n%4; \
  77. if (remainder != 0) \
  78. { \
  79. unsigned int start = 4 * n_iterations; \
  80. for (i = start; i < start+remainder; ++i) \
  81. { \
  82. vector[i] = factor * vector[i]; \
  83. } \
  84. } \
  85. }
  86. #else /* !__SSE__ */
  87. #define VECTOR_SCAL_SSE_FUNC(func_name)
  88. #endif /* !__SSE__ */
  89. #endif /* !__VECTOR_SCAL_CPU_TEMPLATE_H__ */