首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SPMD在更新公共变量时运行一系列作业

使用SPMD在更新公共变量时运行一系列作业
EN

Stack Overflow用户
提问于 2014-02-10 02:17:47
回答 1查看 661关注 0票数 0

目前,我正在尝试使用MATLAB 2013 b并行运行实验,这是非常耗时的。

一个加快速度的策略是利用一个实验的结果来“温暖地开始”下一个实验。在我的例子中,这有点复杂,因为每个实验都有一个n_types类型,我只能使用k类型的实验来加快另一个k类型的实验。

不幸的是,我不能用parfor函数实现这个策略,因为它需要每个作业都更新一个公共变量(该变量存储温暖的开始信息)。尽管如此,我听说使用spmd框架可以做到这一点。

我想知道是否有人能帮我把下面这个通用(不起作用的) parfor代码“转换”成spmd代码中的代码。

代码语言:javascript
复制
n_cores = %provided by user (# of workers that are available)
inputs  = %provided by user (n_jobs x 1 cell array of structs)
types   = %provided by user (n_types x 1 array of integer values)
n_jobs  = length(inputs)
n_types = length(unique(types))

outputs     = cell(n_jobs,1) %cell array to store job output
warm_starts = cell(0,n_types) %empty 0 x n_type cell array to store warm start data

matlabpool('open',n_cores)

parfor i = 1:length(jobs)

   %run myfun in parallel
   outputs{i} = myfun(inputs{i},warm_starts(types(i)));

   %update warm start data for experiments of this type with data from current experiment
   warm_starts{end+1,types(i)) = get_warm_start(job_outputs{i});

end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-10 09:36:51

我不太清楚您可能希望为每个warm_starts存储多少不同的type。我假设你只想存储一个。下面是你可能要做的事情:

代码语言:javascript
复制
jobs  = rand(1,97); % note prime number of jobs
types = randi([1, 5], size(jobs));
n_jobs = numel(jobs);
n_types = numel(unique(types));
warm_starts = cell(1, n_types);

spmd
    jobs_per_lab = ceil(n_jobs / numlabs);
    outputs = cell(jobs_per_lab, 1);
    for idx = 1:jobs_per_lab
        job_idx = idx + ((labindex-1)*jobs_per_lab);
        if job_idx > n_jobs
            % Off the end of 'jobs', no work to do
            this_warm_start = NaN;
            this_type       = NaN;
        else
            this_type = types(job_idx);
            if ~isempty(warm_starts{this_type})
                this_warm_start = warm_starts{this_type};
            else
                this_warm_start = 0;
            end
            outputs{idx} = this_warm_start + types(job_idx) * jobs(job_idx); % some function goes here
            this_warm_start = rand();
        end
        % All-to-all communication to exchange 'this_warm_start' values.
        % After this, each worker has a 2 x numlabs cell array of warm starts and types
        all_warm_starts_this_round = gcat({this_type; this_warm_start}, 2);
        for w = 1:numlabs
            warm_start_type = all_warm_starts_this_round{1, w};
            warm_start_value = all_warm_starts_this_round{2, w};
            if ~isnan(warm_start_type)
                warm_starts{warm_start_type} = warm_start_value;
            end
        end
    end
    % Finally, collect all results on lab 1
    outputs = gcat(outputs, 1, 1);
end
% Dereference the Composite
outputs = outputs{1};

我在那里所做的主要工作是手动分割工作,以便每个工人在一段“作业”上操作,然后在每一轮之后使用GCAT广播温暖的开始信息。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21668036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档