main Content

采用afterEachandafterAllto Run Callback Functions

You create aFuturewhen you run functions in the background or on a parallel pool using帕菲瓦尔,,,,Parfevalonall,,,,afterEach, 或者afterAll。您可以使用afterEachandafterAll在一个或多个之后自动运行回调函数Future对象完成。

  • If you useafterEach,Matlab®每次运行回调函数Futureobject finishes. If theFuture数组有melements, the MATLAB client runs the callback functionm时代。

  • If you useafterEach,Matlab毕竟运行回调函数Futureobject finish. If theFuture数组有m元素,MATLAB客户端运行回调函数仅运行一次。

称呼afterEachon帕菲瓦尔Computations

您可以使用afterEach在每个结果上自动调用功能帕菲瓦尔计算。

采用帕菲瓦尔to compute random vectors in the workers. With default preferences,帕菲瓦尔创建一个Parpool如果还没有创建一个,则会自动。

为了idx = 1:10 f(idx) = parfeval(@rand, 1, 1000, 1);结尾

Display the maximum element in each of those vectors after they are created.afterEach准备就绪时,在每个未来的输出中执行功能句柄。

Aftereach(f, @(r)disp(max(r)),0);
0.9975 0.9990 0.9982 0.9991 0.9982 0.9998 0.9999 0.9986 0.9996 0.9990

称呼afterAllon帕菲瓦尔Computations

您可以使用afterAllto automatically invoke functions on all of the combined outputs of your帕菲瓦尔计算。

采用帕菲瓦尔to compute random vectors in the workers. With default preferences,帕菲瓦尔创建一个Parpool如果还没有创建一个,则会自动。

为了idx = 1:10 f(idx) = parfeval(@rand, 1, 1000, 1);结尾

创建所有这些向量的媒介中的最大元素。afterAll当所有期货都准备就绪时,在所有期货的组合输出上执行功能句柄。

Afterall(f, @(r)disp(max(r)),0);
0.9998

结合afterEachandafterAll

您可以结合使用afterEachandafterAllto automatically invoke more functions on the results of futures. BothafterEachandafterAll生成未来可以再次使用的变量afterEachandafterAll

采用帕菲瓦尔to compute random vectors in the workers. With default preferences,帕菲瓦尔创建一个Parpool如果还没有创建一个,则会自动。

为了idx = 1:10 f(idx)= parfeval(@rand,1,1000,1);结尾
Starting parallel pool (parpool) using the 'local' profile ... connected to 8 workers.

计算每个vecto最大的元素rs when they become ready.afterEach在每个将来准备就绪时,执行功能处理功能,并创建另一个未来以保持结果。

maxFuture = afterEach(f, @(r) max(r), 1);

要计算它们之间的最低价值,请致电afterAllon this new future.afterAllexecutes a function on the combined output arguments of all the futures after they all complete. In this case,afterAllexecutes the function最小on the outputs ofmaxFuture完成并创建另一个未来以保持结果。

最小Future = afterAll(maxFuture, @(r) min(r), 1);

You can fetch the result using获取图获取图waits until the future completes to gather the results.

获取图(minFuture)
ANS = 0.9973

您可以检查afterEach通过打电话获取图on its future variable.

获取图(maxFuture)
ans =10×10.9996 0.9989 0.9994 0.9973 1.0000 1.0000 0.9989 0.9994 0.9998 0.99999

Update User Interface Asynchronously UsingafterEachandafterAll

此示例显示了如何在计算完成后更新用户界面。当您将计算卸载给工人时帕菲瓦尔,在工人执行这些计算时,所有用户界面均响应迅速。在此示例中,您使用侍应还是等待to create a simple user interface.

  • 采用afterEachto update the user interface after each computation completes.

  • 采用afterAllto update the user interface after all the computations complete.

采用侍应还是等待to create a figure handle,H。当您使用时afterEachorafterAll, 这侍应还是等待Function updates the figure handle. For more information about handle objects, seeHandle Object Behavior

H= waitbar(0,'等待...');

采用帕菲瓦尔计算随机矩阵特征值的实际部分。有默认的首选项,帕菲瓦尔创建一个parallel pool automatically if one is not already created.

为了idx = 1:100 f(idx)= parfeval(@(n)真实(eig(randn(n))),1,5e2);结尾

您可以使用afterEach在每个结果上自动调用功能帕菲瓦尔计算。采用afterEachto compute the largest value in each of the output arrays after each future completes.

maxfuture = aftereach(f,@max,1);

您可以使用状态property to obtain the status of futures. Create a logical array where the状态期货的财产F“完成的”。采用meanto calculate the fraction of finished futures. Then, create an anonymous functionUpdateWaitbar。The function changes the fractional wait bar length ofHto the fraction of finished futures.

updateWaitBar = @(〜)waitbar(均值({f.state} ==“完成的”),h);

采用afterEachandUpdateWaitbarto update the fractional wait bar length after each future inmaxFuturecompletes. UseafterAllanddelete完成所有计算完成后关闭等待栏。

UpdateWaitbarFutures = afterEach(f,updateWaitbar,0); afterAll(updateWaitbarFutures,@(~) delete(h),0);

采用afterAllandH是togramto show a histogram of the results inmaxFutureafter all the futures complete.

showshistogramfuture = afterall(maxfuture,@直方图,0);

处理未来变量中的错误

当未来变量的计算导致错误时,默认情况下afterEach不会在失败的元素上评估其功能。例如,如果要处理任何错误,例如,您有要更新的用户界面,则可以使用名称值对PassFuture。设置为true, 这Future variable is passed to the callback function. You can call获取图在其上,处理输出并处理任何可能的错误。

使用使用帕菲瓦尔。有默认的首选项,帕菲瓦尔创建一个Parpool如果还没有创建一个,则会自动。如果你的帕菲瓦尔computations result in an error, the future variable errors, and itsError属性反映了它。

errorFuture = parfeval(@(n) randn(n), 0, 0.5); wait(errorFuture); errorFuture.Error
ans =ParallelException with properties: identifier: 'MATLAB:NonIntegerInput' message: 'Size inputs must be integers.' cause: {} remotecause: {[1×1 MException]} stack: [1×1 struct]

If you useafterEach在那个未来,回调函数不会在未来错误的元素上评估。在下面的代码中,msgbox没有执行,因为未来错误。

afterEach(errorFuture, @() msgbox('Operation completed'),0);

要处理导致错误的期货,请使用名称值对PassFuture打电话时afterEach。未来变量传递给回调函数,而不是其输出。称呼获取图on the future variable, and process its outputs. If the future results in an error,获取图throws an error that you can catch and handle. The following code shows an error dialog box.

Aftereach(errorfuture,@handleerror,0,'PassFuture', 真的);

FunctionHandleError(f)tryoutput =获取图(f);%用输出做点事抓住errordlg('Operation failed');结尾结尾