迭代第二个周期,CUDA 中的总和减少
Iteration second cycle with sum reduction in CUDA
>我必须将此代码从c ++并行化到CUDA C
for(ihist = 0; ihist < numhist; ihist++){
for(iwin = 0; iwin<numwin; iwin++){
denwham[ihist] += (numbinwin[iwin]/g[iwin])*exp(F[iwin]-U[ihist]);
}
Punnorm[ihist] = numwham[ihist]/denwham[ihist];
}
在 CUDA C 中,使用总和约简:
extern __shared__ float sdata[];
int tx = threadIdx.x;
int i=blockIdx.x;
int j=blockIdx.y;
float sum=0.0;
float temp=0.0;
temp=U[j];
if(tx<numwin)
{
sum=(numbinwin[tx]/g[tx])*exp(F[tx]- temp);
sdata[tx] = sum;
__syncthreads();
}
for(int offset = blockDim.x / 2;offset > 0;offset >>= 1)
{
if(tx < offset)
{
// add a partial sum upstream to our own
sdata[tx] += sdata[tx + offset];
}
__syncthreads();
}
// finally, thread 0 writes the result
if(threadIdx.x == 0)
{
// note that the result is per-block
// not per-thread
denwham[i] = sdata[0];
for(int k=0;k<numhist;k++)
Punnorm[k] = numwham[k]/denwham[k];
}
并通过以下方式初始化它:
int smem_sz = (256)*sizeof(float);
dim3 Block(numhist,numhist,1);
NewProbabilitiesKernel<<<Block,256,smem_sz>>>(...);
我的问题是我无法使用exp
迭代U,我已经尝试了以下方法:
a) loop for/while inside the kernel that iterates over U
b) iterate by thread
c) iterate to block
所有这些尝试导致我在C++代码和代码 cuda 之间得出不同的结果。如果我放一个常量而不是 U [i],代码工作正常!
你有什么想法可以帮助我吗?
谢谢。
看起来您可以将U
移出内部循环
for(iwin = 0; iwin<numwin; iwin++){
denwham += numbinwin[iwin] / g[iwin] * exp(F[iwin]);
}
for(ihist = 0; ihist < numhist; ihist++){
Punnorm[ihist] = numwham[ihist] / denwham * exp(U[ihist]);
}
更新
之后,您可以使用 2 个简单内核而不是 1 个复杂内核来完成任务。
- 将内核还原为计算
denwham
; - 一维转换内核以计算
Punnorm
;
相关文章:
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- 等待整个 omp 块完成,然后再调用第二个函数
- 我想在C++中读取一些多个字符,但它永远不会读取第二个字符
- 如何在创建自定义迭代器时获得 std::p air 的第一个和第二个?
- WinAPI 在单击第一个对话框上的按钮控件并销毁第一个对话框后创建第二个对话框
- 将第二个 GATT 服务添加到 Movesense 容器
- 如何在 c++ 中根据第二个元素按降序对列表进行排序
- 对的排序向量 (std::vector<pair<int, int>>) 按对的第一个元素搜索并更新第二个元素值
- 比较 2 个向量并从第二个向量中删除在第一个 - c++ 中找不到的元素
- 为什么这个程序没有打印返回的迭代器的正确第二个元素?
- 使第二个类的构造函数成为第一个类中的友元函数
- 为什么第二个代码给出了预期的结果,而第一个代码却没有?
- 为什么第一个代码块产生垃圾值,而第二个代码块将类成员的值相加?
- 打印无序映射的第二个元素,即集合
- 我有一个类,它创建了另一个类的实例.如何将变量通过第一个类传递到第二个类的实例化中?
- 需要使用模板查找数组的第二个最小和最小值
- c++:交换向量中所有元组的第一个和第二个元素
- 如果条件,当我想第二个参数时
- 将内容从第一个文件("constituencies")移动到第二个文件("temp")并在之后重命名时,我的文件被删除
- 迭代第二个周期,CUDA 中的总和减少