gpu_mult.cu 2.0 KB

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