vector_scal_cpu_template.h 5.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), 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. /*
  17. * This example complements vector_scal.c: here we implement a CPU version.
  18. */
  19. #ifndef __VECTOR_SCAL_CPU_TEMPLATE_H__
  20. #define __VECTOR_SCAL_CPU_TEMPLATE_H__
  21. #include <starpu.h>
  22. #ifdef __SSE__
  23. #include <xmmintrin.h>
  24. #endif
  25. /* This kernel takes a buffer and scales it by a constant factor */
  26. #define VECTOR_SCAL_CPU_FUNC(func_name) \
  27. void func_name(void *buffers[], void *cl_arg) \
  28. { \
  29. unsigned i; \
  30. float *factor = (float *) cl_arg; \
  31. \
  32. /* \
  33. * The "buffers" array matches the task->handles array: for instance \
  34. * task->handles[0] is a handle that corresponds to a data with \
  35. * vector "interface", so that the first entry of the array in the \
  36. * codelet is a pointer to a structure describing such a vector (ie. \
  37. * struct starpu_vector_interface *). Here, we therefore manipulate \
  38. * the buffers[0] element as a vector: nx gives the number of elements \
  39. * in the array, ptr gives the location of the array (that was possibly \
  40. * migrated/replicated), and elemsize gives the size of each elements. \
  41. */ \
  42. \
  43. struct starpu_vector_interface *vector = (struct starpu_vector_interface *) buffers[0]; \
  44. \
  45. /* length of the vector */ \
  46. unsigned n = STARPU_VECTOR_GET_NX(vector); \
  47. \
  48. /* get a pointer to the local copy of the vector : note that we have to \
  49. * cast it in (float *) since a vector could contain any type of \
  50. * elements so that the .ptr field is actually a uintptr_t */ \
  51. float *val = (float *)STARPU_VECTOR_GET_PTR(vector); \
  52. \
  53. /* scale the vector */ \
  54. for (i = 0; i < n; i++) \
  55. val[i] *= *factor; \
  56. }
  57. #ifdef __SSE__
  58. #define VECTOR_SCAL_SSE_FUNC(func_name) \
  59. void func_name(void *buffers[], void *cl_arg) \
  60. { \
  61. float *vector = (float *) STARPU_VECTOR_GET_PTR(buffers[0]); \
  62. unsigned int n = STARPU_VECTOR_GET_NX(buffers[0]); \
  63. unsigned int n_iterations = n/4; \
  64. \
  65. __m128 *VECTOR = (__m128*) vector; \
  66. __m128 FACTOR STARPU_ATTRIBUTE_ALIGNED(16); \
  67. float factor = *(float *) cl_arg; \
  68. FACTOR = _mm_set1_ps(factor); \
  69. \
  70. unsigned int i; \
  71. for (i = 0; i < n_iterations; i++) \
  72. VECTOR[i] = _mm_mul_ps(FACTOR, VECTOR[i]); \
  73. \
  74. unsigned int remainder = n%4; \
  75. if (remainder != 0) \
  76. { \
  77. unsigned int start = 4 * n_iterations; \
  78. for (i = start; i < start+remainder; ++i) \
  79. { \
  80. vector[i] = factor * vector[i]; \
  81. } \
  82. } \
  83. }
  84. #else /* !__SSE__ */
  85. #define VECTOR_SCAL_SSE_FUNC(func_name)
  86. #endif /* !__SSE__ */
  87. #endif /* !__VECTOR_SCAL_CPU_TEMPLATE_H__ */