How does MATLAB's parfeval function work?

前端 未结 1 2083
死守一世寂寞
死守一世寂寞 2021-02-10 13:57

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 re         


        
相关标签:
1条回答
  • 2021-02-10 14:31
      • How does parfeval work?
      • Does this function send every (idx) evaluational of the magic function to a specific worker?

    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. 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?

    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.

      • 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 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.

    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. 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.

    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.

    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.

    0 讨论(0)
提交回复
热议问题