2014-11-21 164 views
-1

我正在製作一個CUDA程序,該程序實現對N個數字進行操作的數據並行前綴和計算。我的代碼也應該使用隨機數生成器在主機上生成數字。但是,在嘗試編譯時,我似乎總是遇到int main結尾括號中的「無法識別的令牌」和「預期聲明」錯誤。我正在Linux上運行代碼。CUDA編程:編譯錯誤

#include <stdio.h> 
#include <cuda.h> 
#include <stdlib.h> 
#include <math.h> 


__global__ void gpu_cal(int *a,int i, int n) { 
     int tid = blockIdx.x * blockDim.x + threadIdx.x; 
     if(tid>=i && tid < n) { 
        a[tid] = a[tid]+a[tid-i]; 
     } 
} 


int main(void) 
{   
     int key; 
     int *dev_a; 
     int N=10;//size of 1D array 
     int B=1;//blocks in the grid 
     int T=10;//threads in a block 

     do{ 

        printf ("Some limitations:\n"); 
        printf ("   Maximum number of threads per block = 1024\n"); 
        printf ("   Maximum sizes of x-dimension of thread block = 1024\n"); 
        printf ("   Maximum size of each dimension of grid of thread blocks = 65535\n"); 
        printf ("   N<=B*T\n"); 

        do{ 
           printf("Enter size of array in one dimension, currently %d\n",N); 
           scanf("%d",&N); 
           printf("Enter size of blocks in the grid, currently %d\n",B); 
           scanf("%d",&B); 
           printf("Enter size of threads in a block, currently %d\n",T); 
           scanf("%d",&T); 

           if(N>B*T) 
              printf("N>B*T, this will result in an incorrect result generated by GPU, please try again\n"); 
           if(T>1024) 
              printf("T>1024, this will result in an incorrect result generated by GPU, please try again\n"); 
         }while((N>B*T)||(T>1024)); 

        cudaEvent_t start, stop;      // using cuda events to measure time 
        float elapsed_time_ms1, elapsed_time_ms3; 

        int a[N],gpu_result[N];//for result generated by GPU 
        int cpu_result[N];//CPU result 


        cudaMalloc((void**)&dev_a,N * sizeof(int));//allocate memory on GPU 
        int i,j; 

        srand(1); //initialize random number generator 
        for (i=0; i < N; i++) // load array with some numbers 
        a[i] = (int)rand() ; 

        cudaMemcpy(dev_a, a , N*sizeof(int),cudaMemcpyHostToDevice);//load data from host to device 

        cudaEventCreate(&start);     // instrument code to measure start time 
        cudaEventCreate(&stop); 

        cudaEventRecord(start, 0); 

        //GPU computation 
        for(j=0;j<log(N)/log(2);j++){ 

        gpu_cal<<<B,T>>>(dev_a,pow(2,j),N); 

        cudaThreadSynchronize(); 

        } 

        cudaMemcpy(gpu_result,dev_a,N*sizeof(int),cudaMemcpyDeviceToHost); 

        cudaEventRecord(stop, 0);  // instrument code to measue end time 
        cudaEventSynchronize(stop); 
        cudaEventElapsedTime(&elapsed_time_ms1, start, stop); 

        printf("\n\n\nTime to calculate results on GPU: %f ms.\n", elapsed_time_ms1); // print out execution time 

        //CPU computation 
        cudaEventRecord(start, 0);  

        for(i=0;i<N;i++) 
        { 
           cpu_result[i]=0; 
           for(j=0;j<=i;j++) 
           { 
              cpu_result[i]=cpu_result[i]+a[j]; 
           } 
        } 

        cudaEventRecord(stop, 0);  // instrument code to measue end time 
        cudaEventSynchronize(stop); 
        cudaEventElapsedTime(&elapsed_time_ms3, start, stop); 

        printf("Time to calculate results on CPU: %f ms.\n\n", elapsed_time_ms3); // print out execution time 


     //Error check 
        for(i=0;i < N;i++) { 
           if (gpu_result[i] != cpu_result[i]) { 
              printf("ERROR!!! CPU and GPU create different answers\n"); 
              break; 
           } 
        } 
     //Calculate speedup 

        printf("Speedup on GPU compared to CPU= %f\n", (float) elapsed_time_ms3/(float) elapsed_time_ms1); 

        printf("\nN=%d",N); 
        printf("\nB=%d",B); 
        printf("\nT=%d",T); 

        printf("\n\n\nEnter '1' to repeat, or other integer to terminate\n"); 
        scanf("%d",&key); 

     }while(key == 1); 

     cudaFree(dev_a);//deallocation 

     return 0; 
}​ 

回答

1

代碼中最後的}是一個Unicode字符。如果刪除這整行,並重新鍵入},則錯誤將消失。

0

你的代碼有兩個編譯錯誤。

首先,末尾括號是一個unicode字符,因此您應該將代碼重新保存爲unicode或刪除並重寫最後一個末尾括號。

其次,int型變量N的在此行中使用 - int a[N],gpu_result[N];//for result generated by GPU

被宣佈的int類型,但它不是在C或C++編譯器允許的,所以你應該改變的N個聲明爲const int的N.