下面是一些在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 a&#34;)&#34;第39行
- 没有重载函数的实例&#34; cudaMalloc&#34;匹配参数(第119行) (120行和121行相同)
- 从第119-121行开始,它还说&#34;参数类型是:int()[9,unsigned long&#34;
答案 0 :(得分:0)
同意Talonmies和Regis。
此外,除了这个错字之外,您还有其他类型的错误行40,并且您的updateDistance与多个块的工作分布不一定表现良好,因为您所有的块都处理相同的数据。
最后,您没有为缓冲区分配足够的内存(V条目,在您的情况下,在合并位置的所有1024个线程都可以访问9条)。
我相信您可能希望深入了解编程指南,也许是一个全面的教程。