If I run this program I get "an illegal memory access was encountered in matrixMulti.cu at line 48" error. I searched and tried a lot. So I hope somebody can help me.
Line 48 : HANDLE_ERROR ( cudaMemcpy(array, devarray, NNsizeof(int), cudaMemcpyDeviceToHost) );
The program is just to get into CUDA. I tried to implement a matrix multiplication.
#include <iostream>
#include<cuda.h>
#include <stdio.h>
using namespace std;
#define HANDLE_ERROR( err ) ( HandleError( err, __FILE__, __LINE__ ) )
void printVec(int** a, int n);
static void HandleError( cudaError_t err, const char *file, int line )
{
if (err != cudaSuccess)
{
printf( "%s in %s at line %d\n", cudaGetErrorString( err ),
file, line );
exit( EXIT_FAILURE );
}
}
void checkCUDAError(const char *msg)
{
cudaError_t err = cudaGetLastError();
if( cudaSuccess != err)
{
fprintf(stderr, "Cuda error: %s: %s.\n", msg,
cudaGetErrorString( err) );
exit(EXIT_FAILURE);
}
}
__global__ void MatrixMulti(int** a, int** b) {
b[0][0]=4;
}
int main() {
int N =10;
int** array, **devarray;
array = new int*[N];
for(int i = 0; i < N; i++) {
array[i] = new int[N];
}
HANDLE_ERROR ( cudaMalloc((void**)&devarray, N*N*sizeof(int) ) );
HANDLE_ERROR ( cudaMemcpy(devarray, array, N*N*sizeof(int), cudaMemcpyHostToDevice) );
MatrixMulti<<<1,1>>>(array,devarray);
HANDLE_ERROR ( cudaMemcpy(array, devarray, N*N*sizeof(int), cudaMemcpyDeviceToHost) );
HANDLE_ERROR ( cudaFree(devarray) );
printVec(array,N);
return 0;
}
void printVec(int** a , int n) {
for(int i =0 ; i < n; i++) {
for ( int j = 0; j <n; j++) {
cout<< a[i][j] <<" ";
}
cout<<" "<<endl;
}
}