采用afterEach
andafterAll
to Run Callback Functions
You create aFuture
when you run functions in the background or on a parallel pool using帕菲瓦尔
,,,,Parfevalonall
,,,,afterEach
, 或者afterAll
。您可以使用afterEach
andafterAll
在一个或多个之后自动运行回调函数Future
对象完成。
If you use
afterEach
,Matlab®每次运行回调函数Future
object finishes. If theFuture
数组有m
elements, the MATLAB client runs the callback functionm
时代。If you use
afterEach
,Matlab毕竟运行回调函数Future
object finish. If theFuture
数组有m
元素,MATLAB客户端运行回调函数仅运行一次。
称呼afterEach
on帕菲瓦尔
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
称呼afterAll
on帕菲瓦尔
Computations
您可以使用afterAll
to 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
结合afterEach
andafterAll
您可以结合使用afterEach
andafterAll
to automatically invoke more functions on the results of futures. BothafterEach
andafterAll
生成未来可以再次使用的变量afterEach
andafterAll
。
采用帕菲瓦尔
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);
要计算它们之间的最低价值,请致电afterAll
on this new future.afterAll
executes a function on the combined output arguments of all the futures after they all complete. In this case,afterAll
executes 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 UsingafterEach
andafterAll
此示例显示了如何在计算完成后更新用户界面。当您将计算卸载给工人时帕菲瓦尔
,在工人执行这些计算时,所有用户界面均响应迅速。在此示例中,您使用侍应还是等待
to create a simple user interface.
采用
afterEach
to update the user interface after each computation completes.采用
afterAll
to update the user interface after all the computations complete.
采用侍应还是等待
to create a figure handle,H
。当您使用时afterEach
orafterAll
, 这侍应还是等待
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
在每个结果上自动调用功能帕菲瓦尔
计算。采用afterEach
to 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
是“完成的”
。采用mean
to calculate the fraction of finished futures. Then, create an anonymous functionUpdateWaitbar
。The function changes the fractional wait bar length ofH
to the fraction of finished futures.
updateWaitBar = @(〜)waitbar(均值({f.state} ==“完成的”),h);
采用afterEach
andUpdateWaitbar
to update the fractional wait bar length after each future inmaxFuture
completes. UseafterAll
anddelete
完成所有计算完成后关闭等待栏。
UpdateWaitbarFutures = afterEach(f,updateWaitbar,0); afterAll(updateWaitbarFutures,@(~) delete(h),0);
采用afterAll
andH是togram
to show a histogram of the results inmaxFuture
after 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');结尾结尾