一台机器上Octave中的并行计算-包和示例 [英] Parallel computing in Octave on a single machine -- package and example

查看:76
本文介绍了一台机器上Octave中的并行计算-包和示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一台机器(而不是集群)上并行化Octave中的for循环.不久前我问了一个有关Octave并行版本的问题 以八度为单位的并行计算

I would like to parallelize a for loop in Octave on a single machine (as opposed to a cluster). I asked a question about a parallel version of Octave a while ago parallel computing in octave

答案表明我下载了一个并行计算程序包.该软件包似乎主要针对集群计算,但是它确实提到了单机并行计算,但是不清楚如何运行甚至是并行循环.

And the answer suggested that I download a parallel computing package, which I did. The package seems largely geared to cluster computing, but it did mention single machine parallel computing, but was not clear on how to run even a parallel loop.

我还在SO上发现了另一个与此有关的问题,但是在Octave中并行化循环并没有找到好的答案: 与Octave并行运行的循环部分吗?

I also found another question on SO about this, but I did not find a good answer for parallelizing loops in Octave: Running portions of a loop in parallel with Octave?

有人知道我在哪里可以找到在Octave中并行运行for循环的示例吗?

Does anyone know where I can find an example of running a for loop in parallel in Octave???

推荐答案

我正在计算大量的RGB直方图.我需要使用显式循环来做到这一点.因此,每个直方图的计算都需要花费大量时间.因此,并行运行计算是有意义的.在Octave中,有一个由Jaroslav Hajek编写的(实验性)函数 parcellfun .

I am computing large number of RGB histograms. I need to use explicit loops to do it. Therefore computation of each histogram takes noticeable time. For this reason running the computations in parallel makes sense. In Octave there is an (experimental) function parcellfun written by Jaroslav Hajek that can be used to do it.

我的原始循环

histograms = zeros(size(files,2), bins^3);
  % calculate histogram for each image
  for c = 1 : size(files,2)
    I = imread(fullfile(dir, files{c}));
    h = myhistRGB(I, bins);
    histograms(c, :) = h(:); % change to 1D vector
  end

要使用parcellfun,我需要将循环主体重构为一个单独的函数.

To use parcellfun, I need to refactor the body of my loop into a separate function.

function histogram = loadhistogramp(file)
  I = imread(fullfile('.', file));
  h = myhistRGB(I, 8);
  histogram = h(:); % change to 1D vector
end

然后我可以这样称呼

histograms = parcellfun(8, @loadhistogramp, files);

我在计算机上做了一个小型基准测试.它是4个启用了英特尔超线程的物理核心.

I did a small benchmark on my computer. It is 4 physical cores with Intel HyperThreading enabled.

我的原始代码

tic(); histograms2 = loadhistograms('images.txt', 8); toc();
warning: your version of GraphicsMagick limits images to 8 bits per pixel
Elapsed time is 107.515 seconds.

使用parcellfun

With parcellfun

octave:1> pkg load general; tic(); histograms = loadhistogramsp('images.txt', 8); toc();
parcellfun: 0/178 jobs donewarning: your version of GraphicsMagick limits images to 8 bits per pixel
warning: your version of GraphicsMagick limits images to 8 bits per pixel
warning: your version of GraphicsMagick limits images to 8 bits per pixel
warning: your version of GraphicsMagick limits images to 8 bits per pixel
warning: your version of GraphicsMagick limits images to 8 bits per pixel
warning: your version of GraphicsMagick limits images to 8 bits per pixel
warning: your version of GraphicsMagick limits images to 8 bits per pixel
warning: your version of GraphicsMagick limits images to 8 bits per pixel
parcellfun: 178/178 jobs done
Elapsed time is 29.02 seconds.

(并行和串行版本的结果相同(仅转置).

(The results from the parallel and serial version were the same (only transposed).

octave:6> sum(sum((histograms'.-histograms2).^2))
ans = 0

当我重复几次时,运行时间一直都差不多.并行版本的运行时间约为30秒(大约2秒),包含4、8和16个子进程)

When I repeated this several times, the running times were pretty much the same all the time. The parallel version was running around 30 second (+- approx 2s) with both 4, 8 and also 16 subprocesses)

这篇关于一台机器上Octave中的并行计算-包和示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆