vector_scal_cpu.c 2.6 KB

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