【发布时间】:2019-11-13 22:36:52
【问题描述】:
对于满足特定条件的所有单元格,我必须遍历虚构矩阵 m * n 和 add + 1 的所有单元格。
我的幼稚解决方案如下:
#include <stdio.h>
__global__ void calculate_pi(int center, int *count) {
int x = threadIdx.x;
int y = blockIdx.x;
if (x*x + y*y <= center*center) {
*count++;
}
}
int main() {
int interactions;
printf("Enter the number of interactions: ");
scanf("%d", &interactions);
int l = sqrt(interactions);
int h_count = 0;
int *d_count;
cudaMalloc(&d_count, sizeof(int));
cudaMemcpy(&d_count, &h_count, sizeof(int), cudaMemcpyHostToDevice);
calculate_pi<<<l,l>>>(l/2, d_count);
cudaMemcpy(&h_count, d_count, sizeof(int), cudaMemcpyDeviceToHost);
cudaFree(d_count);
printf("Sum: %d\n", h_count);
return 0;
}
在我的用例中,interactions 的值可能非常大,无法分配l * l 的空间。
有人可以帮助我吗?欢迎提出任何建议。
【问题讨论】:
-
我不明白你的问题——你有一个“虚构的”矩阵(假设这意味着它实际上并不存在,而不是它很复杂),然后你想有条件地递增该矩阵中的条目 - 即您想要更改不存在的矩阵中的条目。这怎么可能行得通?
-
恕我直言,@talonmies,您完全错过了 O/P 使用
{threadIdx,blockIdx}.x进行间接映射的棘手想法(暂时不提状态<<< >>>-Shevron-operators 使用的实际参数)[x,y]-values 用于(通常非常大的试验/错误)pi 生成算法(a早期 C/S 课程中的经典教科书示例),是的,想象是一种情况的合法表达,在这种情况下,主体不需要被实例化为真实的存在,但仍然是合法的感兴趣的对象,并且可以与 :o 进一步合作)跨度> -
我使用了虚数数组这个术语,因为我不想实例化一个矩阵,例如 50,000 x 50,000。我读到了 Reduce 技术,它可以解决我的问题,但如果我做对了,我总是需要一个巨大的数组。
标签: parallel-processing cuda nvidia pi