Using parfeval to call eval function not working - why?
25 次查看(过去 30 天)
显示 更早的评论
First of all, I know using eval at all is generally bad practice, but in this case it's kind of necessary. I have a piece of software where users (other programmers) need to be able to write their own functions to be called at a particular time. It includes a GUI where, before running the software, they can browse to the function that they want to run at a particular point in the process. The name of the function is saved to the GUI model and I use eval to make sure the correct function is run when the time comes. This software has been used for a couple years without issue so the eval aspect of this works just fine for our case.
Now I'm trying to update the software so that this function, that was generated by the user, is run on a parallel pool. An abstraction of my code would look like this:
P = parpool(1);
Q = parallel.pool.DataQueue;
afterEach(Q, @disp);
run_command = "test_func(Q)"; %This is a test function I created, normally this would contain a string with the user's function name and inputs
success = parfeval(P, @eval, 1, run_command);
I then have the function test_func defined as follows:
function test_func(Q)
send(Q, 1);
pause(2);
send(Q, 2);
pause(2);
send(Q, 3);
end
This code above does not display the numbers as expected. When I look at 'success', the FevalFuture, it shows an error message of:
"Incorrect use of '=' operator. Assign a value to a variable using '=' and compare values for equality using '=='." at line 39 of this file: C:\Program Files\MATLAB\R2021b\toolbox\parallel\cluster\+parallel\+internal\+queue\evaluateRequest.m.
I'm not sure if this file is part of the eval function or part of the parfeval function. So I guess I'm wondering why my original code which simply looked like
eval(run_command);
works just fine, but putting this into parfeval suddenly throws this random error?
If I change the code to pass test_func into parfeval directly, it works just fine.
P = parpool(1);
Q = parallel.pool.DataQueue;
afterEach(Q, @disp);
run_command = "test_func(Q)"; %This is a test function I created, normally this would contain a string with the user's function name and inputs
success = parfeval(P, @test_func, 1, Q);
So it seems like using @eval as the function passed to parfeval is causing some kind of issue. My question is why? Is this a combination that just doesn't work or is there something wrong with the way I am calling it?
Any insight would be appreciated, as I'm new to parallel computing. Thank you!
0 个评论
采纳的回答
Walter Roberson
2022-6-27
Transparency violation. parfor and parfeval need to know the names of the functions at analysis time so that it can copy the code to the workers before evaluation starts.
8 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Asynchronous Parallel Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!