cuda - I get malloc 3906 error when i try to fill my array -
this code
using namespace std; #include <iostream> #include <stdio.h> #include <stdlib.h> #define n 8000 void fillarray(int *data, int count){ for(int =0; < 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 data set of " << n << " integers."; return 0; }
as can see, fill array fillarray function in cpu side. fill array function gives error:
malloc.c 3906 : sysmalloc: assertion bla bla
what i'm missing here? try fill array. might me problem? event if remove add function in duration function error. problem here?
the error in creation of a
, b
arrays. @qwr , @talonmies said, using valgrind
(or windows substitute) can find source of kind of error:
==8288== invalid write of size 4 ==8288== @ 0x400dd2: fillarray(int*, int) (kernel.cu:11) ==8288== 0x400f79: main (kernel.cu:63) ==8288== address 0x62783e4 0 bytes after block of size 4 alloc'd ==8288== @ 0x4c2ba77: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==8288== 0x400f41: main (kernel.cu:57) ==8288== ==8288== invalid write of size 4 ==8288== @ 0x400dd2: fillarray(int*, int) (kernel.cu:11) ==8288== 0x400f8a: main (kernel.cu:64) ==8288== address 0x6278434 0 bytes after block of size 4 alloc'd ==8288== @ 0x4c2ba77: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==8288== 0x400f55: main (kernel.cu:58)
if 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, not allocating memory array, 1 integer.
when debugging cuda code, need use both gpu/device debugging tools (cuda-memcheck
, cuda-gdb
) , cpu/host tools (valgrind
), since errors can happen on both gpu , cpu. not forget compile 2 debug flags of nvcc
: -g
device code, , -g
host code.
you should delete
arrays @ end of main, good practice:
delete [] a; delete [] b;
Comments
Post a Comment