这是我的代码
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;
}
如您所见,我在 CPU 端使用 fillArray 函数填充我的数组。但填充数组函数给出错误:
malloc.c 3906 : sYSMalloc: Assertion bla bla
我在这里缺少什么?我只是尝试填充数组。我可能有什么问题?事件如果我删除持续时间函数中的添加函数,我会收到此错误。这里有什么问题?