- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的代码在结构上类似于Matlab中的以下代码:
bestConfiguration = 0;
bestConfAwesomeness = 0;
for i=1:X
% note that providing bestConfAwesomeness to the function helps it stop if it sees the current configuration is getting hopeless anyway
[configuration, awesomeness] = expensive_function(i, bestConfAwesomeness);
if awesomeness > bestConfAwesomeness
bestConfAwesomeness = awesomeness;
bestConfiguration = configuration;
end
end
X
可能会变得非常大。我正在尝试使此代码并行运行,因为
expensive_function()
需要很长时间才能运行。
for
更改为
parfor
,因为它不喜欢我在循环中更新最佳配置。
[allConfigurations, allAwesomeness] = deal(cell(1, X));
parfor i=1:X
% note that this is not ideal because I am forced to use 0 as the best awesomeness in all cases
[allConfigurations{i}, allAwesomeness{i}] = expensive_function(i, 0);
end
for i=1:X
configuration = allConfigurations{i};
awesomeness = allAwesomeness{i};
if awesomeness > bestConfAwesomeness
bestConfAwesomeness = awesomeness;
bestConfiguration = configuration;
end
endfor
parfor
也会强制我始终将
0
提供为最佳配置。
expensive_function(i)
),一旦工作人员返回,请查看它产生的数据并将其与到目前为止找到的最好的进行比较,并进行相应的更新。无需保存所有配置,这似乎是使
parfor
工作的唯一方法。
最佳答案
每次在循环中使用bestConfAwesomeness
意味着循环的迭代与顺序无关,因此PARFOR
为什么不满意。您可以采用的一种方法是使用SPMD
,并让每个工作人员并行执行expensiveFunction
,然后进行通信以更新bestConfAwesomeness
。像这样的东西:
bestConfiguration = 0;
bestConfAwesomeness = 0;
spmd
for idx = 1:ceil(X/numlabs)
myIdx = labindex + ((idx-1) * numlabs);
% should really guard against myIdx > X here.
[thisConf, thisAwesome] = expensiveFunction(myIdx, bestConfAwesomeness);
% Now, we must communicate to see if who is best
[bestConfiguration, bestAwesomeness] = reduceAwesomeness(...
bestConfiguration, bestConfAwesomeness, thisConf, thisAwesome);
end
end
function [bestConf, bestConfAwesome] = reduceAwesomeness(...
bestConf, bestConfAwesome, thisConf, thisAwesome)
% slightly lazy way of doing this, could be optimized
% but probably not worth it if conf & awesome both scalars.
allConfs = gcat(bestConf);
allAwesome = gcat(thisAwesome);
[maxThisTime, maxLoc] = max(allAwesome);
if maxThisTime > bestConfAwesome
bestConfAwesome = maxThisTime;
bestConf = allConfs(maxLoc);
end
end
关于multithreading - 在matlab中,parfor的替代方案不太严格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16330919/
我正在运行遗传算法,并且正在尝试并行化种群生成。 我的实际代码: Q = []; parfor i=1:halfPop pa = P(select(f),:); pb = P(sele
为什么即使 MATLAB 不应该转到 switch 语句的第一个 case,它也会抛出错误?下面是最小的例子: mycase = 2; non_crack_bytes = 1:6000; syn_cr
有人知道为什么这不起作用吗?我正在尝试运行 parfor 循环。我可以使用简单的 for 循环执行完全相同的代码,但我需要将其与 parfor 一起用于海量数据集。 parfor n = [1:row
在这段最小的可执行代码中: a = []; workers = parpool('local',4); someboolean = false; parfor i = 1:4 if someb
我正在使用 fminsearch 将模型拟合到多个数据集,并且我正在尝试并行执行它们。我的代码一直运行到 parfor 循环的开始,但是 parfor 循环似乎要花很长时间 才能开始! (不执行 pa
我有一组图像 (~10^7),它们包含在一个巨大的二进制文件中。我想使用我已有的功能有效地阅读和分析它们。代码中该用户函数 foo 的每次迭代大约需要 0.1 秒,因此代码使用读取数据库的简单 for
我正在运行一个带有 parfor 循环的小脚本。该脚本以以下行开头: parfor i=1:length(vX) fprintf('%d/%d\n',i,length(X)); ...
我有一个句柄类: classdef A
在 parfor 循环中,我试图调用一个访问 global 的函数但无济于事。 函数 function a = getA() global OPTIONS; a=OPTIONS.PROBL
我有一段代码可以在一系列图像中找到哈里斯角点。我需要对 92 张图像执行此操作,但速度相当慢。因此,我想并行运行代码。我下面的代码有一个与变量“corners”相关的错误 %% Harris corn
有人知道在 matlab 中使用 parfor 的这段简单代码中发生了什么吗?谢谢, 我将一个矩阵分成四个数组,并想独立更新每个数组中的元素 这是一个简单的版本: a = zeros(4,4); p
我在 parfor 循环中生成加扰的准蒙特卡罗数时遇到问题。 问题是,当我在 parfor 循环中生成多组这些数字时,每组中的数字最终都是相同的。我在下面包含一个非常简单的示例。 D = 3; M =
我正在远程的单个节点上运行并行 Matlab 作业 cluster .集群的每个节点都有 2 个处理器,每个处理器有 24 个内核,每个节点总共有 48 个内核。该作业包含一些顺序代码,后跟一个 pa
为什么我不能在这段代码中使用parfor? parfor i=1:r for j=1:N/r xr(j + (N/r) * (i-1)) = x(i + r * (j-1));
我知道一些不确定性源于 parfor 的并行性质,但我不明白为什么它应该是完全随机的。有没有办法强制 parfor 尊重(至少宽松地)循环的顺序?更具体地说,我希望在以下情况下这样做: parfor
我正在远程的单个节点上运行并行 Matlab 作业 cluster .集群的每个节点都有 2 个处理器,每个处理器有 24 个内核,每个节点总共有 48 个内核。该作业包含一些顺序代码,后跟一个 pa
我的计算基于二叉树,它采用变量 block (称为程序集)的两个先前实例并生成另一个。一个新的程序集是基于上层分支的两个程序集生成的,因此必须存储所有变量。 为此,我使用具有以下语法的元胞数组:Ass
我正在尝试在 MATLAB 的 parfor 循环中使用一个表。这给了我“透明度违规错误。请参阅有关透明度的并行计算工具箱”我正在尝试构建此表,以便我可以使用来自 MATLAB 分类学习器应用程序的训
我有一个 3 维网格,对于网格中的每个点,我想为大量时间步长计算时间相关函数 G(t),然后对每个网格点的 G 函数求和。使用 4 个 for 循环执行时间变得非常长,所以我试图使用 parfor 避
假设我有两个函数写在不同的脚本上,比如 function1.m 和 function2.m 这两个函数中的两个计算是独立的(一些输入可能是同样,例如说 function1(x,y) 和 functio
我是一名优秀的程序员,十分优秀!