gpt4 book ai didi

C++:不确定代码是否是多线程的

转载 作者:行者123 更新时间:2023-11-28 06:56:30 26 4
gpt4 key购买 nike

我正在处理一小段代码,需要花费大量时间才能完成,所以我正在考虑使用 pthread(我几乎不理解,但我认为我可以更快地掌握)或使用多线程一些 GPGPU 实现(可能是 OpenCL,因为我家里有 AMD 卡,我办公室使用的 PC 有各种 NVIDIA 卡)

   while(sDead < (unsigned long) nrPoints*nrPoints) {

pPoint1 = distrib(*rng);
pPoint2 = distrib(*rng);

outAxel = -1;

if(pPoint1 != pPoint2) {

point1 = space->getPointRef(pPoint1);
point2 = space->getPointRef(pPoint2);

outAxel = point1->influencedBy(point2, distThres);

if(outAxel == 0 || outAxel == 1)
sDead++;
else
sDead = 0;

}

i++;
}

其中 distrib 是 a = 0 且 b = nrPoints-1 的 uniform_int_distribution。为清楚起见,这是我正在使用的结构:

class Space{
vector<Point> points
(more stuff)
}

class Point {
vector<Coords> coordinates
(more stuff)
}

struct Coords{
char Range
bool TypeOfCoord
char Coord
}

对于所有 PointPoint[x].Coord[y].Range == Point[z],coordinates 的长度都相同。所有 xyz 的 Coord[y].RangeTypeOfCoord 也是如此。

一些背景:在 while 循环的每次运行期间,测试来自 space 的两个随机绘制的 Point 的交互。 influencedBy() 检查 point1point2 是否彼此足够接近(距离取决于某些指标,但归结为相似性Coord.如果距离小于distThres,可以进行交互)进行交互。交互意味着不等于另一个对象中相应的 CoordCoord 变量之一被翻转为等于它。这减少了 Point 之间的距离,但也改变了更改的 pointSpace 中每个其他 point 的距离>,因此我的问题是这是否是多线程的。正如我所说,我是多线程的新手,我不确定我是否可以安全地实现一个功能来解决这个问题,所以我一直在寻找您的意见。也非常欢迎提出建议。

E:可以找到 influencedby() 函数(以及它依次调用的函数)here .我没有包含的函数,例如 getFeature() 和 getNrFeatures() 很小,不可能有太大贡献。请注意,我在这个问题中使用了对象的通用名称,但如果我在其他代码中替换它们,我可能会搞砸或使它更加困惑,所以我把原来的名称留在那里。备案:

Space = CultSpace
Point = CultVec
Points = Points
Coordinates = Feats
Coords = Feature
TypeOfCoord = Nomin
Coord = Trait

最佳答案

(选择“回答”是因为格式允许更好的呈现。这不是您所要求的,但让我们先澄清一下。)

稍后

在条件变为真之前,循环执行的频率是多少?

while(sDead < (unsigned long) nrPoints*nrPoints) {

可能收获不大,但是:

pPoint1 = distrib(*rng);
do {
pPoint2 = distrib(*rng);
while( pPoint1 == pPoint2 );


outAxel = -1;

getPointRef 的成本如何?空间线性搜索?

point1 = space->getPointRef(pPoint1);
point2 = space->getPointRef(pPoint2);

outAxel = point1->influencedBy(point2, distThres);

是否真的有必要在“翻转”后立即重新计算“变化点到空间中每个其他点的距离”?

关于C++:不确定代码是否是多线程的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23111124/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com