我花了很多時間試圖找出發生了什麼?問題是我無法從我的主機代碼調用這個簡單的內核。我敢肯定,這個錯誤會立即對某些人顯着,但我覺得我很可能沒有理由浪費了很多時間。所以我非常感謝任何幫助。簡單程序中的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