gpu_mult.cu 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2018 Alexis Juven
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu.h>
  18. extern "C" {
  19. #include <starpu_cuda.h>
  20. }
  21. #include <stdint.h>
  22. #include <stdio.h>
  23. __global__ void gpuMultKernel
  24. (
  25. uint32_t nxC, uint32_t nyC, uint32_t nyA,
  26. uint32_t ldA, uint32_t ldB, uint32_t ldC,
  27. float * subA, float * subB, float * subC
  28. )
  29. {
  30. uint32_t id, i, j, k;
  31. float sum;
  32. id = blockIdx.x * blockDim.x + threadIdx.x;
  33. i = id % nxC;
  34. j = id / nxC;
  35. if (j >= nyC){
  36. return;
  37. }
  38. sum = 0.;
  39. for (k = 0 ; k < nyA ; k++){
  40. sum += subA[i + k*ldA] * subB[k + j*ldB];
  41. }
  42. subC[i + j*ldC] = sum;
  43. }
  44. #define THREADS_PER_BLOCK 64
  45. extern "C" void gpu_mult(void * descr[], void * args)
  46. {
  47. float * d_subA, * d_subB, * d_subC;
  48. uint32_t nxC, nyC, nyA;
  49. uint32_t ldA, ldB, ldC;
  50. uint32_t nblocks;
  51. d_subA = (float *) STARPU_MATRIX_GET_PTR(descr[0]);
  52. d_subB = (float *) STARPU_MATRIX_GET_PTR(descr[1]);
  53. d_subC = (float *) STARPU_MATRIX_GET_PTR(descr[2]);
  54. nxC = STARPU_MATRIX_GET_NX(descr[2]);
  55. nyC = STARPU_MATRIX_GET_NY(descr[2]);
  56. nyA = STARPU_MATRIX_GET_NY(descr[0]);
  57. ldA = STARPU_MATRIX_GET_LD(descr[0]);
  58. ldB = STARPU_MATRIX_GET_LD(descr[1]);
  59. ldC = STARPU_MATRIX_GET_LD(descr[2]);
  60. nblocks = (nxC * nyC + THREADS_PER_BLOCK - 1)/THREADS_PER_BLOCK;
  61. gpuMultKernel
  62. <<< nblocks, THREADS_PER_BLOCK, 0, NULL /*starpu_cuda_get_local_stream()*/
  63. >>> (nxC, nyC, nyA, ldA, ldB, ldC, d_subA, d_subB, d_subC);
  64. cudaError_t status = cudaGetLastError();
  65. if (status != cudaSuccess) STARPU_CUDA_REPORT_ERROR(status);
  66. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  67. }