2
$\begingroup$

I am retrying this question since I didn’t do so properly last time:

I have implement a simply c++ CUDA example that takes A [m x 1] and B [1 x N] and adds them element wise with what I am used to being called “implicit” array expansion (MATLAB term). I have two questions:

  1. Why do I not run into errors with two threads simultaneously reading from the same element of a or b?
  2. Is this the “proper” way to perform these types of element wise operations where “element add” represent any number of custom functions that take any permutations of row and column matrixes and calculate on all permutations of combinations?
#include <stdio.h>

#define Row  500
#define Col  60000

__global__ void elementadd( int * a, int * b, int * c )
{

  int ro = blockIdx.x * blockDim.x + threadIdx.x;
  int co = blockIdx.y * blockDim.y + threadIdx.y;

  if (ro < Row && co < Col)
  {
    c[ro*Col + co] = a[ro] + b[co];
  }
}


int main()
{
  int *a, *b, *c_cpu, *c_gpu;

  float s1 = (float)(Row) * (float)(Col);
  float size1 = s1 *  sizeof (int); // Number of bytes of an N x N matrix
  int sizeR = Row * sizeof(int);
  int sizeC = Col * sizeof(int);

  // Allocate memory
  cudaMallocManaged (&a, sizeR);
  cudaMallocManaged (&b, sizeC);
  cudaMallocManaged (&c_cpu, size1);
  cudaMallocManaged (&c_gpu, size1);

//initialize 2d
for(int i = 0; i < Row; ++i){
  a[i] = i*2;
}

for(int i = 0; i < Col; ++i){
b[i] = i+2;
}

for(int ro = 0; ro < Row; ++ro){
for(int co = 0; co < Col; ++co){
c_gpu[ro*Col + co] = 0;
c_gpu[ro*Col + co] = 0;
}
}


  dim3 threads_per_block (32, 32, 1); // A 16 x 16 block threads
  dim3 number_of_blocks ((Row / threads_per_block.x) + 1, (Col / threads_per_block.y) + 1, 1);

  elementadd<<< number_of_blocks, threads_per_block >>> ( a, b, c_gpu );

  cudaDeviceSynchronize(); // Wait for the GPU to finish before proceeding

for(int ro = 0; ro < Row; ++ro){
for(int co = 0; co < Col; ++co){
printf("entry[%d][%d] is %d a:%d b:%d \n",ro,co,c_gpu[ro*Col + co],a[ro],b[co]);
}
}


  // Free all our allocated memory
  cudaFree(a); cudaFree(b);
  cudaFree( c_cpu ); cudaFree( c_gpu );
}
```
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.