vector_scal_cpu.c 2.7 KB

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