【发布时间】:2017-05-19 03:03:28
【问题描述】:
我已经尝试了一段时间,并且遇到了已经发布的看似相似的问题,但是由于某种原因,我仍然无法清除错误。正如我所看到的那样,我实际上想将 2D 矩阵作为 1D 数组传递给内核。我不确定我的语法哪里出错了,但是在我提供给内核的变量和内核期望的参数方面存在冲突。
__global__ void calculatePath(int source, int target, int *cost, int distance){
int t_id = blockIdx.x * blockDim.x + threadIdx.x;
int dist[50];
int prev[50];
int selected[50]={0};
int num_path[50];
int d, m, min, start, j;
if ((t_id > 0) && (t_id < N)){
dist[t_id] = IN;
prev[t_id] = -1;
}
这是我的内核函数,它的参数都是整数,除了“cost”,它是一个指向整数数组的指针。
int main(int argc, char **argv){
int h_num_path[N];
int h_distance = 0;
int h_cost[N][N],i,j,co;
int h_source;
int h_target;
printf("\tShortest Path Algorithm(DIJKSRTRA's ALGORITHM\n\n");
for(i=0;i< N;i++)
for(j=0;j< N;j++)
h_cost[i][j] = IN;
//*********************
srand ( time(NULL));
for(int x=1;x< N;x++) {
for (int y = x + 1; y < N; y++) {
h_cost[x][y] = h_cost[y][x] = (rand() % 100) + 1;
}
}
printf("\nEnter The Source: ");
scanf("%d", &h_source);
printf("\nEnter The target: ");
scanf("%d", &h_target);
int *d_num_path;
int *d_cost;
int *d_source;
int *d_target;
int *d_dist;
int *d_prev;
int *d_distance;
cudaMalloc(&d_num_path, sizeof(int)*N);
cudaMalloc(&d_cost, sizeof(int)*N*N);
cudaMalloc((void**) &d_source, sizeof(int));
cudaMalloc((void**) &d_target, sizeof(int));
cudaMalloc((void**) &d_dist, sizeof(int)*N);
cudaMalloc((void**) &d_distance, sizeof(int));
cudaMemcpy(d_source, &h_source, sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(d_target, &h_target, sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(d_cost, h_cost, sizeof(int)*N*N, cudaMemcpyHostToDevice);
cudaMemcpy(d_distance, &h_distance, sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(d_num_path, &h_num_path, sizeof(int)*N, cudaMemcpyHostToDevice);
clock_t before;
before = clock();
calculatePath<<<N/512 + 1, 512>>>(d_source, d_target, d_cost, d_distance);
clock_t time_taken = clock() - before;
cudaMemcpy(&h_num_path, d_num_path, sizeof(int)*N, cudaMemcpyDeviceToHost);
cudaMemcpy(&h_distance, d_distance, sizeof(int), cudaMemcpyDeviceToHost);
cudaFree(d_num_path);
cudaFree(d_cost);
cudaFree(d_source);
cudaFree(d_target);
cudaFree(d_dist);
cudaFree(d_prev);
cudaFree(d_distance);
printf("\nShortest Path: %d \n",co);
printf("%s %.4f %s", "Time taken:", time_taken/1000.0, "seconds");
return 0;
}
在内核调用中,我收到“'int *' 类型的参数与 'int' 类型的参数不兼容”的错误,但我相信我的 d_cost 已经是一个指针。我很高兴能直截了当,因为我确信我忽略了一些小事。
【问题讨论】:
-
I'm not sure where I've gone wrong in my syntax阅读您的错误/警告argument of type “int *” is incompatible with parameter of type “int” ll -
这就是错误消息告诉你的内容:你相信
d_cost是一个指针,它就是。但该函数需要int。 -
@JpDizzy 你知道 Argument 和 parameter 有什么区别吗?
-
cudaMalloc()的函数签名是什么? -
不,它不需要 C 标签,这就是我删除它的原因。两次。这是一个 CUDA 问题,代码不是 C
标签: cuda