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?