MATLAB的parfeval函数如何工作? [英] How does MATLAB's parfeval function work?

查看:1030
本文介绍了MATLAB的parfeval函数如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MATLAB文档中,我们有一个parfeval函数的代码示例.我对此有一些疑问.这是代码:

In the MATLAB documentation we have a code example for the parfeval function. I have some questions about it. This is the code:

p = gcp();
%// To request multiple evaluations, use a loop.
for idx = 1:10
    f(idx) = parfeval(p,@magic,1,idx); % Square size determined by idx
end
%// Collect the results as they become available.
magicResults = cell(1,10);
for idx = 1:10
    %// fetchNext blocks until next results are available.
    [completedIdx,value] = fetchNext(f);
    magicResults{completedIdx} = value;
    fprintf('Got result with index: %d.\n', completedIdx);
end

    • parfeval如何工作?
    • 此函数是否将对magic函数的每个(idx)评估都发送给特定工作人员?
    • How does parfeval work?
    • Does this function send every (idx) evaluational of the magic function to a specific worker?

如果我们只有一行代码f = parfeval(p,@magic,1,10);:

If we'd only have one line of code f = parfeval(p,@magic,1,10);:

  • 此代码将如何工作?
  • 它将仅将评估发送给一位工作人员并返回输出吗?
  • parfevalparfevalOnAll有什么区别?
  • How would this code work?
  • Will it send evaluation only to one worker and returns the output?
  • What is the difference between parfeval and parfevalOnAll?
  • fetchNextfetchOutputs有什么区别?
  • 为什么在第一个for循环和第二个循环之后需要fetchNext才能获得结果?
  • 第一次循环后我们没有所有结果吗?为什么?
  • 我认为我们正在等待工人在第二个循环中完成该过程.这是真的?我们不能没有任何循环吗?
  • What is the difference between fetchNext and fetchOutputs?
  • Why do we need fetchNext after the first for loop and a second loop to get the results?
  • We don't have all results after first loop? Why?
  • I think we are waiting for workers to complete the process in second loop. Is this true? Can't we do it without any loop?

我认为我们可以在第二个循环中使用magicResults{idx} = fetchOutputs(f(idx));.我们有相同的结果.这两种结构有什么区别?

I think we can use magicResults{idx} = fetchOutputs(f(idx)); in the second loop. We have the same results. What is difference between these two structures?

推荐答案

    • parfeval如何工作?
    • 此函数是否将对magic函数的每个(idx)评估都发送给特定工作人员?
    • How does parfeval work?
    • Does this function send every (idx) evaluational of the magic function to a specific worker?

文档中:parfeval请求在一个并行池.您可以使用cancel取消执行.它只是将其发送给任何自由工作者.使用parfevalOnAll对所有工作程序执行功能. parfeval的作用是提供一个通信框架(parallel.FevalFuture),以查找功能评估是否完成以及结果如何.

From the documentation: parfeval requests asynchronous execution on a worker in a parallel pool. You can cancel the execution with cancel. It just sends it to any free worker. Use parfevalOnAll to execute a function on all workers. What parfeval does is providing the communication framework (parallel.FevalFuture) to find out if a the function evaluation is finished and what were the results.

  1. 如果我们只有一行代码f = parfeval(p,@magic,1,10);:
    • 此代码将如何工作?
    • 它将仅将评估发送给一位工作人员并返回输出吗?
    • parfevalparfevalOnAll有什么区别?
  1. If we'd only have one line of code f = parfeval(p,@magic,1,10);:
    • How would this code work?
    • Will it send evaluation only to one worker and returns the output?
    • What is the difference between parfeval and parfevalOnAll?

它只会将评估发送给一个工作人员(我想是下一个免费的),您可以通过调用fetchOutputs来获取输出(之前将等待完成).但是parfevalOnAll在所有工作程序上执行该功能-可能没有很多有用的用例. parfeval用于大多数/所有计算工作.

It would send evaluation to only one worker (the next free one I guess) and you can fetch the output by calling fetchOutputs (will wait for finished before). parfevalOnAll however executes the function on all workers - there are probably not very many useful usecases for this. Use parfeval for most/all of your computation jobs.

    • fetchNextfetchOutputs有什么区别?
    • 为什么在第一个for循环和第二个循环之后需要fetchNext才能获得结果?
    • 第一次循环后我们没有所有结果吗?为什么?
    • 我认为我们正在等待工人在第二个循环中完成该过程.这是真的?我们不能没有任何循环吗?
    • What is the difference between fetchNext and fetchOutputs?
    • Why do we need fetchNext after the first for loop and a second loop to get the results?
    • We don't have all results after first loop? Why?
    • I think we are waiting for workers to complete the process in second loop. Is this true? Can't we do it without any loop?

fetchNext等待直到FEvalFutures列表中的任何一个函数求值完成,然后返回索引和结果. fetchOutputs只能在单个FEvalFuture上运行,并等待该操作并仅返回结果.因此,如果您已安排了多个并行函数评估,请使用fetchNext;如果只有一个,请使用fetchOutputs.

fetchNext waits until any one of the function evaluations in a list of FEvalFutures is finished and returns the index and the result. fetchOutputs just works on a single FEvalFuture and waits for this and returns the result only. So use fetchNextif you have scheduled several parallel function evaluations and fetchOutputs if there was only one.

为什么要循环?好吧,您已经计划了10个评估,因此您应该等待这10个评估的结果.您知道您必须调用fetchNext 10次,因此使用循环.为了不等待太久,您可以使用fetchNext,它会尽早返回(当至少有一个结果时),但是您仍然必须调用它10次.您可以改为使用while循环,但可以使用一个循环.示例中的for循环就足够了.

Why a loop? Well you have scheduled 10 evaluations, so you should wait for the results of these 10. You know you must call fetchNext 10 times, therefore use a loop. In order not to wait too long you use fetchNext which comes back as early as possible (when there is at least one result), but you still have to call it 10 times. You could do with a while loop instead but a loop it will be. The for loop from the example is adequate.

  1. 我认为我们可以在第二个循环中使用magicResults{idx} = fetchOutputs(f(idx));.我们有相同的结果.之间有什么区别 这两个结构?
  1. I think we can use magicResults{idx} = fetchOutputs(f(idx)); in the second loop. We have the same results. What is difference between these two structures?

是的,您可以使用它,但是您很可能要等更长的时间,才能获得第一个结果.这通常效率低下.

Yes, you could use it, but then you would very probably wait longer until the first result is available to you. This is usually inefficient.

原因是结果是异步到达的(因为它们是异步执行的).那么,为什么要等待第一个,如果第二个已经可用(并且可能会进一步处理).最好使用fetchNext等待下一个可用.

The reason is that the results arrive asynchroneously (as they are executed asynchroneously). So why waiting for the first one, if the second one is already available (and could be further processed maybe..). Better wait for the next one available using fetchNext.

我希望能够弄清楚Matlab如何尝试处理异步性.它们基本上具有一个函数fetchNext,该函数会在一系列异步操作中的至少一个完成后立即返回.这通常是一种非常有效的设计模式.我强烈建议使用它.

I hope it got clear how Matlab tries to deal with asynchronicity. They basically have a function fetchNext which returns as soon as at least one of a bunch of asynchronous operations is finished. This is usually a very efficient design pattern. I strongly recommend to use it.

这篇关于MATLAB的parfeval函数如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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