2016-04-23 64 views
-3

的任何實例是一些代碼,在C/CUDA正在做,但出於某種原因,一些錯誤似乎是被編譯停止該程序並運行。不太確定如何解決這些錯誤,嘗試更改演員表,以及多次校對整個代碼。沒有很大的用C編碼,它似乎並不像這個錯誤是問題是通過網上搜索的內容非常具體。任何見解或幫助將不勝感激。所以下面重載函數錯誤

#include <cstdlib> 
#include <stdio.h> 
#include <limits.h> 

using namespace std; 

const int NUMTHREADS = 1024; 

// Number of vertices in the graph 
#define V 9 

// A utility function to find the vertex with minimum distance value, from 
// the set of vertices not yet included in shortest path tree 
int minDistance(int dist[], bool sptSet[]) { 
// Initialize min value 
int min = INT_MAX, min_index; 

for (int v = 0; v < V; v++) 
if (sptSet[v] == false && dist[v] <= min) 
    min = dist[v], min_index = v; 

return min_index; 
} 

// A utility function to print the constructed distance array 
int printSolution(int dist[], int n) { 
printf("Vertex Distance from Source\n"); 
for (int i = 0; i < V; i++) 
printf("%d \t\t %d\n", i, dist[i]); 
}//end of method 

// Funtion that implements Dijkstra's single source shortest path algorithm 
// for a graph represented using adjacency matrix representation 

__global__ void updateDistance(bool* sptSet[],int* graph[],int* dist[],int u,int V){ 

Int i = threadIdx.x 

if(i<V){ 
if((!sptSet[i] && *graph[u*V+i] && dist[u] != INT_MAX && *dist[u]+graph[u*V+i] < *dist[i])) 
*dist[i] = *dist[u] + *graph[u*V+i]; 
} 
}//end of method 

long* generateAdjMatrix(int count){ 

long* randoMatrix = (long*)malloc(count*count*sizeof(long)); 
int i,j; 

srand(time(NULL)); 

for(i=0;i<count;i++){ 
for(j=0;j<count;j++){ 
if(i != j){ 
Long randomResult = rand()%2; 
randoMatrix[(i*count)+j] = randomResult; 
randoMatrix[(j*count)+i] = randomResult; 
} 
} 
} 
Return randoMatrix; 
}//end of method 

// driver program to test above function 
int main() { 

//gpu 
int *d_dist[V]; 
int *d_graph[V]; 
bool *d_sptSet[V]; 


int src = 0; 
long* matrix; 

int *dist[V];  // The output array. dist[i] will hold the shortest 
        // distance from src to i 

bool* sptSet = (bool)malloc(V*sizeof(bool)); 
int* graph = (int *)malloc(V*sizeof(int)); 
int* dist = (int *)malloc(V*sizeof(int)); 

Matrix = generateAdjMatrix(V); 

// Initialize all distances as INFINITE and stpSet[] as false 
for (int i = 0; i < V; i++) 
    dist[i] = INT_MAX, sptSet[i] = false; 

// Distance of source vertex from itself is always 0 
dist[src] = 0; 


//allocate GPU variables in memory 
cudaMalloc(&d_sptSet,(V*sizeof(bool))); 
cudaMalloc(&d_dist,(V*sizeof(int))); 
cudaMalloc(&d_graph,(V*sizeof(int))); 

// Find shortest path for all vertices 
for (int count = 0; count < V-1; count++) { 
    // Pick the minimum distance vertex from the set of vertices not 
    // yet processed. u is always equal to src in first iteration. 
    int u = minDistance(dist, sptSet); 

    // Mark the picked vertex as processed 
    sptSet[u] = true; 


//after updating distance, copy the updates to GPU 
cudaMemcpy(d_sptSet,sptSet,V*sizeof(bool),cudaMemcpyHostToDevice); 
cudaMemcpy(d_dist,dist,V*sizeof(int),cudaMemcpyHostToDevice); 
cudaMemcpy(d_graph,graph,V*sizeof(int),cudaMemcpyHostToDevice); 

    //call updatedistance 
updateDistance<<<V,NUMTHREADS>>>(d_sptSet,d_graph,d_dist,u,V); 

cudaMemcpy(dist,d_dist,V*sizeof(int),cudaMemcpyDeviceToHost); 
cudaMemcpy(sptSet,d_sptSet,V*sizeof(int),cudaMemcpyDeviceToHost); 
}//end of for 

// print the constructed distance array 
printSolution(dist, V); 


return 0; 
}//end of main 

我得到的錯誤是:

符合-expected一 「)」 39

重載函數 「cudaMalloc」 的 - 沒有實例的參數匹配(119線) (同樣爲線120和121)

-from線119-121,它也說 「參數的類型有:INT()[圖9中,無符號長」

+3

嚴重的是,[SO]不是免費錯誤固定服務。請不要把它當作一個。如果不能發現在線路37'中INT I = threadIdx.x'兩個錯誤,則需要修改某些基本的C++的參考材料。我已經投票結束了這一點。 – talonmies

+0

同意。嘗試修復這些基本的語法錯誤並回來。 –

回答

0

與Talonmies和吉斯達成一致。

此外,超過這個錯字您有其他類型的錯誤線40,並updateDistance的幾個街區的工作分配,你有相同的數據的所有塊不一定會表現良好。

最後,您不會爲緩衝區分配足夠的內存(V條目,在您的情況下,在合併位置的所有1024個線程訪問的情況下爲9)。

我相信你可能想要在編程指南中有更深入的瞭解,也許是一個徹底的教程。