Reputation: 55
this is my code
using namespace std;
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#define N 8000
void fillArray(int *data, int count){
for(int i =0; i < count; i++)
data[i] = (int) rand() / ((int) RAND_MAX);
}
__global__ void add(int* a, int *b){
int add = 0;
int tid = threadIdx.x + blockIdx.x * blockDim.x;
if(tid < N){
add = a[tid] + b[tid];
}
}
__global__ void subtract(int* a, int *b){
int subtract = 0;
int tid = threadIdx.x + blockIdx.x * blockDim.x;
if(tid < N){
subtract = a[tid] - b[tid];
}
}
float duration(int *devA, int *devB, int blocksPerGrid, int threadsPerBlock){
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start,0);
cudaEventRecord(stop,0);
cudaEventSynchronize(stop);
cudaMalloc((void**) &devA, N * sizeof(int));
cudaMalloc((void**) &devB, N * sizeof(int));
add<<<blocksPerGrid, threadsPerBlock>>>(devA,devB);
float elapsedTime;
cudaEventElapsedTime(&elapsedTime,start,stop);
cudaEventDestroy(start);
cudaEventDestroy(stop);
return elapsedTime;
}
int main(void) {
int *a = new int(N);
int *b = new int(N);
float dur = 0 ;
fillArray(a, N);
fillArray(b, N);
dur = duration(a,b,N,1);
cout << "Global memory version:\n";
cout << "Process completed in " << dur;
cout << "for a data set of " << N << " integers.";
return 0;
}
As you can see, i fill my array with fillArray function in CPU side. But fill array function gives error:
malloc.c 3906 : sYSMalloc: Assertion bla bla
What I'm missing here? I just try to fill array. What might me the problem? Event if i remove the add function in duration function i get this error. What is the problem here?
Upvotes: 0
Views: 109
Reputation: 8976
The error is in the creation of your a
and b
arrays. As @QWR and @talonmies said, using valgrind
(or any Windows substitute) can help you find the source of that kind of error:
==8288== Invalid write of size 4
==8288== at 0x400DD2: fillArray(int*, int) (kernel.cu:11)
==8288== by 0x400F79: main (kernel.cu:63)
==8288== Address 0x62783e4 is 0 bytes after a block of size 4 alloc'd
==8288== at 0x4C2BA77: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8288== by 0x400F41: main (kernel.cu:57)
==8288==
==8288== Invalid write of size 4
==8288== at 0x400DD2: fillArray(int*, int) (kernel.cu:11)
==8288== by 0x400F8A: main (kernel.cu:64)
==8288== Address 0x6278434 is 0 bytes after a block of size 4 alloc'd
==8288== at 0x4C2BA77: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8288== by 0x400F55: main (kernel.cu:58)
If you change:
int *a = new int(N);
int *b = new int(N);
To:
int *a = new int[N];
int *b = new int[N];
The error disappears. Indeed, you were not allocating memory for an array, but just for one integer.
When debugging a CUDA code, you need to use both GPU/device debugging tools (cuda-memcheck
, cuda-gdb
) and CPU/host tools (valgrind
), since errors can happen on both GPU and CPU. Do not forget to compile with the two debug flags of nvcc
: -G
for device code, and -g
for host code.
You should also delete
your arrays at the end of your main, for good practice:
delete [] a;
delete [] b;
Upvotes: 2