spmv_cuda.cu 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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 <stdint.h>
  17. #define MIN(a,b) ((a)<(b)?(a):(b))
  18. extern "C" __global__
  19. void spmv_kernel(uint32_t nnz, uint32_t nrow, float *nzval, uint32_t *colind, uint32_t *rowptr,
  20. uint32_t firstentry, uint32_t elemsize,
  21. float *vecin, uint32_t nx_in, uint32_t elemsize1, float * vecout, uint32_t nx_out, uint32_t elemsize2)
  22. {
  23. /* only one dimension is used here */
  24. unsigned nthreads = gridDim.x*blockDim.x;
  25. unsigned threadid = threadIdx.x + blockIdx.x*blockDim.x;
  26. unsigned rowstart = threadid * ((nrow + (nthreads - 1))/nthreads);
  27. unsigned rowend = MIN(nrow, (threadid+1) * ((nrow + (nthreads - 1))/nthreads));
  28. unsigned row;
  29. for (row = rowstart; row < rowend; row++)
  30. {
  31. float tmp = 0.0f;
  32. unsigned index;
  33. unsigned firstindex = rowptr[row] - firstentry;
  34. unsigned lastindex = rowptr[row+1] - firstentry;
  35. for (index = firstindex; index < lastindex; index++)
  36. {
  37. tmp += nzval[index]*vecin[colind[index]];
  38. }
  39. vecout[row] = tmp;
  40. }
  41. }
  42. extern "C" __global__
  43. void spmv_kernel_2(uint32_t nnz, uint32_t nrow, float *nzval, uint32_t *colind, uint32_t *rowptr,
  44. uint32_t firstentry, uint32_t elemsize,
  45. float *vecin, uint32_t nx_in, uint32_t elemsize1, float * vecout, uint32_t nx_out, uint32_t elemsize2)
  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" __global__
  65. void spmv_kernel_3(uint32_t nnz, uint32_t nrow, float *nzval, uint32_t *colind, uint32_t *rowptr,
  66. uint32_t firstentry, uint32_t elemsize,
  67. float *vecin, uint32_t nx_in, uint32_t elemsize1, float * vecout, uint32_t nx_out, uint32_t elemsize2)
  68. {
  69. /* only one dimension is used here */
  70. unsigned block_rowstart = blockIdx.x*( (nrow + gridDim.x - 1)/gridDim.x );
  71. unsigned block_rowend = MIN((blockIdx.x+1)*( (nrow + gridDim.x - 1)/gridDim.x ), nrow);
  72. unsigned row;
  73. for (row = block_rowstart + threadIdx.x; row < block_rowend; row+=blockDim.x)
  74. {
  75. float tmp = 0.0f;
  76. unsigned index;
  77. unsigned firstindex = rowptr[row] - firstentry;
  78. unsigned lastindex = rowptr[row+1] - firstentry;
  79. for (index = firstindex; index < lastindex; index++)
  80. {
  81. tmp += nzval[index]*vecin[colind[index]];
  82. }
  83. vecout[row] = tmp;
  84. }
  85. }