我花了很多时间试图找出发生了什么?问题是我无法从我的主机代码调用这个简单的内核。我敢肯定,这个错误会立即对某些人显着,但我觉得我很可能没有理由浪费了很多时间。所以我非常感谢任何帮助。简单程序中的CUDA问题
这是我的.cpp代码
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <windows.h>
#include <shrUtils.h>
#include <cutil_inline.h>
#include <cutil_gl_inline.h>
#include <cuda.h>
CUfunction reduce0; //i've used many ways to declare my kernel function,but.....
int main(int argc , char *argv[]){
int i,N,sum;
int *data;
int *Md;
srand (time(NULL));
N=(int)pow((float)2,(float)atoi(argv[1]));
data=(int *)malloc(N * sizeof(int));
for (i=0;i<N;i++){
data[i]=rand() % 10 + 1;
}
cudaMalloc((void**) &Md, N);
clock_t start = clock();
dim3 dimBlock(512,0);
dim3 dimGrid(1,1);
reduce0<<< dimGrid,dimBlock >>>(Md,Md);
sum=0;
for(i=0;i<N;i++){
sum=sum+data[i];
}
printf("Sum of the %d-array is %d \n", N , sum);
printf("Time elapsed: %f\n", ((double)clock() - start)/CLOCKS_PER_SEC);
return 0;
}
这里是我的代码.CU
__global__ void reduce0(int*g_idata, int*g_odata){
extern __shared__ int sdata[];
// each thread loadsone element from global to shared mem
unsigned int tid = threadIdx.x;
unsigned int i= blockIdx.x*blockDim.x+ threadIdx.x;
sdata[tid] = g_idata[i];
__syncthreads();
// do reduction in shared mem
for(unsigned int s=1; s < blockDim.x; s *= 2) {
if(tid % (2*s) == 0){
sdata[tid] += sdata[tid + s];
}
__syncthreads();
}
// write result for this block to global mem
if(tid == 0) g_odata[blockIdx.x] = sdata[0];
}
那么请问我应该怎么做才能调用内核?在编译时,它不识别这个符号“< < <”,并且就reduce0()而言,只有在.cpp声明时才能识别它!请有人帮助我终于开始真正的cuda事情!
你是如何调用编译器? – 2011-05-01 15:16:02