3
$\begingroup$

I want to do nonsquare matrix multiplication using CUDA The input is two matrices: a and b

a = RandomReal[{0, 5}, {100, 4}];
b = RandomReal[{0, 5}, {4, 6}];

m = Dimensions[a][[1]];
nn = Dimensions[a][[2]];
k = Dimensions[b][[2]];

cc = ConstantArray[0, {m, k}]; (*output*)

for this case, the output matrix will have dimensions, 100 x 6

the CUDA code:

    code = "
  __global__ void squarematrixmult(double *a, double *b, double *c, \
mint m, mint nn, mint k)
  { 
      int row = blockIdx.y * blockDim.y + threadIdx.y; 
      int col = blockIdx.x * blockDim.x + threadIdx.x;
      float sum = 0;
      if( col < k && row < m) 
      {
          for(int i = 0; i < nn; i++) 
          {
              sum += a[row * nn + i] * b[i * k + col];
          }
          c[row * k + col] = sum;
      }
  } ";

the computation:

cudaFun = 
  CUDAFunctionLoad[code, 
   "squarematrixmult", {{_Real, _, "Input"}, {_Real, _, 
     "Input"}, {_Real, _, 
     "Output"}, _Integer, _Integer, _Integer}, {32, 32}];
res = cudaFun[a, b, cc, m, nn, k];

However, since the defined block size is {32,32}, the given answer is only for 32 elements. elements after 32 are all zeros. for this case, the output is 100 x 6, but only 32 x 6 have values (correct values), but else are zeros.

So how can i fix the way to define threads/blocks/grids in CUDAFunctionLoad? thank you so much

$\endgroup$
1
  • $\begingroup$ anybody has any idea about my question? $\endgroup$
    – Normi
    Commented Dec 10, 2018 at 1:42

1 Answer 1

2
$\begingroup$

it was long ago, maybe you already solved this issue, but you have to exchange the definitions of "col" and "row" and your code should be fine for any block size.

You probably took this formulas from

https://www.tutorialspoint.com/cuda/cuda_matrix_multiplication.htm

If you notice, they show this definition twice, you probably took the reversed one.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.