axpy_partition_gpu.cu 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2016 Inria
  4. * Copyright (C) 2017 CNRS
  5. * Copyright (C) 2016 Uppsala University
  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 creates two dumb vectors, splits them into chunks, and for each pair of
  20. * chunk, run axpy on them.
  21. */
  22. #include <starpu.h>
  23. #include "axpy_partition_gpu.h"
  24. #include <stdio.h>
  25. //This code demonstrates how to transform a kernel to execute on a given set of GPU SMs.
  26. // Original kernel
  27. __global__ void saxpy(int n, float a, float *x, float *y)
  28. {
  29. int i = blockIdx.x*blockDim.x + threadIdx.x;
  30. if (i<n) y[i] = a*x[i] + y[i];
  31. }
  32. // Transformed kernel
  33. __global__ void saxpy_partitioned(__P_KARGS, int n, float a, float *x, float *y)
  34. {
  35. __P_BEGIN;
  36. __P_LOOPX;
  37. int i = blockid.x*blockDim.x + threadIdx.x; // note that blockIdx is replaced.
  38. if (i<n) y[i] = a*x[i] + y[i];
  39. __P_LOOPEND;
  40. }
  41. extern "C" void cuda_axpy(void *descr[], void *_args)
  42. {
  43. float a = *((float *)_args);
  44. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  45. float *x = (float *)STARPU_VECTOR_GET_PTR(descr[0]);
  46. float *y = (float *)STARPU_VECTOR_GET_PTR(descr[1]);
  47. int SM_mapping_start = -1;
  48. int SM_mapping_end = -1;
  49. int SM_allocation = -1;
  50. cudaStream_t stream = starpu_cuda_get_local_stream();
  51. int workerid = starpu_worker_get_id();
  52. starpu_sched_ctx_get_sms_interval(workerid, &SM_mapping_start, &SM_mapping_end);
  53. SM_allocation = SM_mapping_end - SM_mapping_start;
  54. int dimensions = 512;
  55. //partitioning setup
  56. // int SM_mapping_start = 0;
  57. // int SM_allocation = 13;
  58. __P_HOSTSETUP(saxpy_partitioned,dim3(dimensions,1,1),dimensions,0,SM_mapping_start,SM_allocation,stream);
  59. saxpy_partitioned<<<width,dimensions,0,stream>>>(__P_HKARGS,n,a,x,y);
  60. }