- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
编辑 3:我需要每个线程在全局内存中写入和读取一个私有(private)位置。下面我发布了一个显示我的问题的工作代码。下面,我将列出涉及的主要变量和结构。
变量:
srcArr_h
(host) --> srcArr_d
(device) : [0, COLORLEVELS
] 范围内的随机 float 组,维度由 ARRDIM
auxD
(device) : 维度数组 ARRDIM
* ARRDIM
将最终结果保存在设备中auxH
(host) : 维度数组 ARRDIM
* ARRDIM
将最终结果保存在 hostc_glob_d
(设备):为每个线程保留一个私有(private)位置 COLORLEVELS
的数组,大小由 num_threads
* 色阶
idx
(device) : 当前线程的标识号我的问题:在内核中,我为每个值 ic
更新了 c_glob[idx]
(ic
∈ [0, COLORLEVELS
]), 即 c_glob[idx][ic]
。我使用 c_glob[idx][COLORLEVELS]
来计算存储在 auxD
中的最终结果 g0
。我的问题是我的最终结果是错误的。复制到 auxH 的结果显示我得到的数字至少比预期大一个数量级,甚至是奇怪的数字,表明我的操作可能会溢出。
帮助:我做错了什么?如何让每个线程写入和读取全局内存中的每个私有(private)位置?现在我正在使用 ARRDIM
= 512 进行调试,但我的目标是使其适用于 ARRDIM
~ 10^4,从而创建一个 c_glob
10^4*10^4 个线程的数组)。我想每次运行允许的线程总数会有问题。所以我想知道您是否可以针对我的问题提出任何其他解决方案。
谢谢。
#include <string>
#include <stdint.h>
#include <iostream>
#include <stdio.h>
#include "cuPrintf.cu"
using namespace std;
#define ARRDIM 512
#define COLORLEVELS 4
__global__ void gpuKernel
(
float *sa, float *aux,
size_t memPitchAux, int w,
float *c_glob
)
{
float sc_loc[COLORLEVELS];
float g0=0.0f;
int tidx = blockIdx.x * blockDim.x + threadIdx.x;
int tidy = blockIdx.y * blockDim.y + threadIdx.y;
int idx = tidy * memPitchAux/4 + tidx;
for(int ic=0; ic<COLORLEVELS; ic++)
{
sc_loc[ic] = ((float)(ic*ic));
}
for(int is=0; is<COLORLEVELS; is++)
{
int ic = fabs(sa[tidy*w +tidx]);
c_glob[tidy * COLORLEVELS + tidx + ic] += 1.0f;
}
for(int ic=0; ic<COLORLEVELS; ic++)
{
g0 += c_glob[tidy * COLORLEVELS + tidx + ic]*sc_loc[ic];
}
aux[idx] = g0;
}
int main(int argc, char* argv[])
{
/*
* array src host and device
*/
int heightSrc = ARRDIM;
int widthSrc = ARRDIM;
cudaSetDevice(0);
float *srcArr_h, *srcArr_d;
size_t nBytesSrcArr = sizeof(float)*heightSrc * widthSrc;
srcArr_h = (float *)malloc(nBytesSrcArr); // Allocate array on host
cudaMalloc((void **) &srcArr_d, nBytesSrcArr); // Allocate array on device
cudaMemset((void*)srcArr_d,0,nBytesSrcArr); // set to zero
int totArrElm = heightSrc*widthSrc;
for(int ic=0; ic<totArrElm; ic++)
{
srcArr_h[ic] = (float)(rand() % COLORLEVELS);
}
cudaMemcpy( srcArr_d, srcArr_h,nBytesSrcArr,cudaMemcpyHostToDevice);
/*
* auxiliary buffer auxD to save final results
*/
float *auxD;
size_t auxDPitch;
cudaMallocPitch((void**)&auxD,&auxDPitch,widthSrc*sizeof(float),heightSrc);
cudaMemset2D(auxD, auxDPitch, 0, widthSrc*sizeof(float), heightSrc);
/*
* auxiliary buffer auxH allocation + initialization on host
*/
size_t auxHPitch;
auxHPitch = widthSrc*sizeof(float);
float *auxH = (float *) malloc(heightSrc*auxHPitch);
/*
* kernel launch specs
*/
int thpb_x = 16;
int thpb_y = 16;
int blpg_x = (int) widthSrc/thpb_x;
int blpg_y = (int) heightSrc/thpb_y;
int num_threads = blpg_x * thpb_x + blpg_y * thpb_y;
/*
* c_glob: array that reserves a private location of COLORLEVELS floats for each thread
*/
int cglob_w = COLORLEVELS;
int cglob_h = num_threads;
float *c_glob_d;
size_t c_globDPitch;
cudaMallocPitch((void**)&c_glob_d,&c_globDPitch,cglob_w*sizeof(float),cglob_h);
cudaMemset2D(c_glob_d, c_globDPitch, 0, cglob_w*sizeof(float), cglob_h);
/*
* kernel launch
*/
dim3 dimBlock(thpb_x,thpb_y, 1);
dim3 dimGrid(blpg_x,blpg_y,1);
gpuKernel<<<dimGrid,dimBlock>>>(srcArr_d,auxD, auxDPitch, widthSrc, c_glob_d);
cudaThreadSynchronize();
cudaMemcpy2D(auxH,auxHPitch,
auxD,auxDPitch,
auxHPitch, heightSrc,
cudaMemcpyDeviceToHost);
cudaThreadSynchronize();
float min = auxH[0];
float max = auxH[0];
float f;
string str;
for(int i=0; i<widthSrc*heightSrc; i++)
{
if(min > auxH[i])
min = auxH[i];
if(max < auxH[i])
max = auxH[i];
}
cudaFree(srcArr_d);
cudaFree(auxD);
cudaFree(c_glob_d);
}
最佳答案
您决定既不显示完整代码,也不显示重现您问题的缩小尺寸。因此,无法对以下可能的解决方案进行测试和验证。
我认为您已经找到问题的根源:多个线程正试图并行写入相同的内存位置。这是导致竞争条件的情况。例如,请参阅演示文稿的第四张幻灯片 "CUDA C: race conditions, atomics, locks, mutex, and warps" .
竞争条件有一个蛮力解决方案:原子函数。它们在 CUDA C 编程指南的第 B.12 节中进行了描述。所以你可以尝试通过更改行来解决你的问题
c[ic] += 1.0f;
到
atomicAdd(&c[ic],1);
您将为此修复付出性能代价:原子操作序列化代码以避免竞争条件。
我已经提到原子函数是解决您问题的一种蛮力解决方案,因为通过适本地重新考虑实现,您可以找到避免它们的方法。但由于您提供的细节非常少,目前还无法确定。
关于c++ - CUDA 线程在全局内存中的私有(private)位置写入错误结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19935683/
如果需要在类外访问静态(例如单例),可以选择公共(public)静态而不是私有(private)静态,而当不需要公开函数时首选私有(private)静态(否则未命名的命名空间就可以了)——在这种情况下
在互联网上进行了一些搜索,但找不到简单的答案。我的问题集是在 Android 框架中使用 Java,但我相信这也是标准的 Java 行为。我理解 final 和 private 的定义,它们都用于变量
我有这个代码: public final class Board { private final int[][] blocks; private final int N; pr
对我来说,过去作为 Objective-C 开发人员很简单。一个类需要公开的每个字段都是一个属性,每个私有(private)字段都是一个没有 getter 或 setter 的实例变量。但我经常看到人
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我有一个在 Docker 容器中运行的应用程序。它需要来自公司私有(private) NPM 注册表(Sinopia)的一些私有(private)模块,并且访问这些需要用户身份验证。 Dockerfi
我试图理解 C# 使用 getters 和 setters 自动声明变量与 java 声明之间的区别。 在java中我通常这样做: private int test; public int getTe
我在 Azure 中创建了 VNET。我放入了一个子集 Azure Private Link,它在 VNET 之外和另一台虚拟机中调用 Azure Function。 当我尝试通过专用 IP 调用专用
我在 Azure 中创建了 VNET。我放入了一个子集 Azure Private Link,它在 VNET 之外和另一台虚拟机中调用 Azure Function。 当我尝试通过专用 IP 调用专用
我目前正在使用 Objective-C(适用于 iPhone)构建游戏。 为此,出于性能/复杂性原因,我略微打破了 MVC,并为 View (渲染器)提供了对模型的直接引用。这是因为它应该以 60fp
我已经在 ubuntu 上成功配置了 2 个虚拟主机站点(基于名称的虚拟主机)。我的 apache 版本是 2.2.22。 这两个站点都在本地主机上工作。 /etc/hosts 条目 127.0.0.
考虑下面的类 public class A { private final Map cache; public HeavyObject getThing(); } 假设不能泄漏对缓存
我有一个类,它有一个方法,我希望它只能被它的子对象访问,而不能被这个包中的其他类访问。 Modifier | Class | Package | Subclass | World ———————
本文实例讲述了JavaScript中的公有、私有、特权和静态成员用法。分享给大家供大家参考。具体分析如下: 下面的内容是在《JavaScript.DOM高级程序设计》里面摘抄出来的,比较容易理解,
我有一个用例,我已将其简化为以下程序: public class A { private int x = 100; class B { private int y = ne
问题: 类声明如下: class Select { public: template static Iterator function(Iterator , Iterator , bo
我是一名初级 PHP 程序员。我还有很多东西要学。这就是我问这个问题的原因。在一个类中,您有一个公共(public)函数,您可以从该类外部调用它。有时你有一个私有(private)函数,你可以在私有(
问题是: 何时使用私有(private)函数,何时使用嵌套函数? (我在问 F# 但也许答案可能与其他功能语言相关) 一个小例子 namespace SomeName module BinaryRea
我发现工作表中仍然可以使用私有(private)函数。它们是隐藏的,但如果用户输入他们的名字,他们就会被调用。为什么?它应该以这种方式工作吗?有没有办法完全阻止用户定义的函数在 VBA 项目之外使用?
所以我最近开始尝试使用 Kotlin,我偶然发现了这个: If a top-level declaration is marked private, it is private to the pack
我是一名优秀的程序员,十分优秀!