gpu_mult.cu 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2018 Alexis Juven
  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 <stdint.h>
  18. #include <stdio.h>
  19. __global__ void gpuMultKernel
  20. (
  21. uint32_t nxC, uint32_t nyC, uint32_t nyA,
  22. uint32_t ldA, uint32_t ldB, uint32_t ldC,
  23. float * subA, float * subB, float * subC
  24. )
  25. {
  26. uint32_t id, i, j, k;
  27. float sum;
  28. id = blockIdx.x * blockDim.x + threadIdx.x;
  29. i = id % nxC;
  30. j = id / nxC;
  31. if (j >= nyC){
  32. return;
  33. }
  34. sum = 0.;
  35. for (k = 0 ; k < nyA ; k++){
  36. sum += subA[i + k*ldA] * subB[k + j*ldB];
  37. }
  38. subC[i + j*ldC] = sum;
  39. }
  40. #define THREADS_PER_BLOCK 64
  41. extern "C" void gpu_mult(void * descr[], void * args)
  42. {
  43. float * d_subA, * d_subB, * d_subC;
  44. uint32_t nxC, nyC, nyA;
  45. uint32_t ldA, ldB, ldC;
  46. uint32_t nblocks;
  47. d_subA = (float *) STARPU_MATRIX_GET_PTR(descr[0]);
  48. d_subB = (float *) STARPU_MATRIX_GET_PTR(descr[1]);
  49. d_subC = (float *) STARPU_MATRIX_GET_PTR(descr[2]);
  50. nxC = STARPU_MATRIX_GET_NX(descr[2]);
  51. nyC = STARPU_MATRIX_GET_NY(descr[2]);
  52. nyA = STARPU_MATRIX_GET_NY(descr[0]);
  53. ldA = STARPU_MATRIX_GET_LD(descr[0]);
  54. ldB = STARPU_MATRIX_GET_LD(descr[1]);
  55. ldC = STARPU_MATRIX_GET_LD(descr[2]);
  56. nblocks = (nxC * nyC + THREADS_PER_BLOCK - 1)/THREADS_PER_BLOCK;
  57. gpuMultKernel
  58. <<< nblocks, THREADS_PER_BLOCK, 0, starpu_cuda_get_local_stream()
  59. >>> (nxC, nyC, nyA, ldA, ldB, ldC, d_subA, d_subB, d_subC);
  60. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  61. }