spmv_cuda.cu 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2008-2011,2014-2016 Université de Bordeaux
  4. * Copyright (C) 2010,2012,2015,2017 CNRS
  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. /* CUDA kernel for SPMV */
  18. #include <starpu.h>
  19. #define MIN(a,b) ((a)<(b)?(a):(b))
  20. extern "C" __global__ void spmv_kernel(uint32_t nnz, uint32_t nrow, float *nzval, uint32_t *colind, uint32_t *rowptr,
  21. uint32_t firstentry, uint32_t elemsize,
  22. float *vecin, uint32_t nx_in, uint32_t elemsize1, float * vecout, uint32_t nx_out, uint32_t elemsize2)
  23. {
  24. /* only one dimension is used here */
  25. unsigned nthreads = gridDim.x*blockDim.x;
  26. unsigned threadid = threadIdx.x + blockIdx.x*blockDim.x;
  27. unsigned rowstart = threadid * ((nrow + (nthreads - 1))/nthreads);
  28. unsigned rowend = MIN(nrow, (threadid+1) * ((nrow + (nthreads - 1))/nthreads));
  29. unsigned row;
  30. for (row = rowstart; row < rowend; row++)
  31. {
  32. float tmp = 0.0f;
  33. unsigned index;
  34. unsigned firstindex = rowptr[row] - firstentry;
  35. unsigned lastindex = rowptr[row+1] - firstentry;
  36. for (index = firstindex; index < lastindex; index++)
  37. {
  38. tmp += nzval[index]*vecin[colind[index]];
  39. }
  40. vecout[row] = tmp;
  41. }
  42. }
  43. extern "C" __global__ void spmv_kernel_3(uint32_t nnz, uint32_t nrow, float *nzval, uint32_t *colind, uint32_t *rowptr,
  44. uint32_t firstentry,
  45. float *vecin, uint32_t nx_in, float * vecout, uint32_t nx_out)
  46. {
  47. /* only one dimension is used here */
  48. unsigned block_rowstart = blockIdx.x*( (nrow + gridDim.x - 1)/gridDim.x );
  49. unsigned block_rowend = MIN((blockIdx.x+1)*( (nrow + gridDim.x - 1)/gridDim.x ), nrow);
  50. unsigned row;
  51. for (row = block_rowstart + threadIdx.x; row < block_rowend; row+=blockDim.x)
  52. {
  53. float tmp = 0.0f;
  54. unsigned index;
  55. unsigned firstindex = rowptr[row] - firstentry;
  56. unsigned lastindex = rowptr[row+1] - firstentry;
  57. for (index = firstindex; index < lastindex; index++)
  58. {
  59. tmp += nzval[index]*vecin[colind[index]];
  60. }
  61. vecout[row] = tmp;
  62. }
  63. }
  64. extern "C" void spmv_kernel_cuda(void *descr[], void *args)
  65. {
  66. uint32_t nnz = STARPU_CSR_GET_NNZ(descr[0]);
  67. uint32_t nrow = STARPU_CSR_GET_NROW(descr[0]);
  68. float *nzval = (float *)STARPU_CSR_GET_NZVAL(descr[0]);
  69. uint32_t *colind = STARPU_CSR_GET_COLIND(descr[0]);
  70. uint32_t *rowptr = STARPU_CSR_GET_ROWPTR(descr[0]);
  71. uint32_t firstentry = STARPU_CSR_GET_FIRSTENTRY(descr[0]);
  72. float *vecin = (float *)STARPU_VECTOR_GET_PTR(descr[1]);
  73. uint32_t nx_in = STARPU_VECTOR_GET_NX(descr[1]);
  74. float *vecout = (float *)STARPU_VECTOR_GET_PTR(descr[2]);
  75. uint32_t nx_out = STARPU_VECTOR_GET_NX(descr[2]);
  76. dim3 dimBlock(8, 1);
  77. dim3 dimGrid(512, 1);
  78. spmv_kernel_3<<<dimGrid, dimBlock, 0, starpu_cuda_get_local_stream()>>>
  79. (nnz, nrow, nzval, colind, rowptr, firstentry, vecin, nx_in, vecout, nx_out);
  80. }