Parallel Toolbox + net.cdf: net.cdf function works with for, but not with parfor?

3 views (last 30 days)
Hello again. I have a question on how to use Parallel Toolbox to read net.cdf file using netcdf functions. Structure-wise, here is the code.
% assume that file_ID and loc_str already exist.
% file_ID = output from netcdf.open
% loc_str = char for excel filename
vars_taken = 47; % so the data have 47 variables. Dimensions not count.
parfor var_count = 3:vars_taken+2 % variables are listed in 3rd to 49th.
% determine what variable is it?
[varname,~,~,~] = netcdf.inqVar(file_id,var_count);
% get variable ID
vars_id = netcdf.inqVarID(file_id,varname);
% % get attribute name and value of that variable
vars_attname = netcdf.inqAttName(file_id,vars_id,1); % variable name
vars_attval = netcdf.getAtt(file_id,vars_id,vars_attname);
unit_attname = netcdf.inqAttName(file_id,vars_id,2); % units
unit_attval = netcdf.getAtt(file_id,vars_id,unit_attname);
% write on excel. This has resolved on my previous question
sheet_name = 'Values';
writematrix(time_data,[loc_str varname '.xlsx'],"Sheet",sheet_name,"Range",'B1:Y1');
writematrix(vars_attval,[loc_str varname '.xlsx'],"Sheet",sheet_name,"Range",'AA2');
writematrix(unit_attval,[loc_str varname '.xlsx'],"Sheet",sheet_name,"Range",'AA3');
disp([varname ' done']); % show if it's done
end
netcdf.close(file_id);
This produce the following error
---
Error using netcdf.inqVar (line 26)
The NetCDF library encountered an error during execution of 'inqVar' function - 'Not a valid ID (NC_EBADID)'.
Error in Untitled_parfor (line 95)
parfor var_count = 3:vars_taken+2
---
But if I replace parfor with for, no error, disp are showing properly, and I get Excel files as intended.
I need this parfor so when I read the actual data it can finish faster (approx. 6 hours for a month data without parallel). How can I do it? Thank you.

Accepted Answer

Walter Roberson
Walter Roberson on 7 Apr 2022
matlab.internal.imagesci.netcdflib() creates internal resources that are not automatically copied to workers.
See https://www.mathworks.com/help/parallel-computing/parallel.pool.constant.html#buzshy6-1 for an example of creating a parpool constant by executing a function on each worker -- in this case you would do the netcdf.open on each worker.
  7 Comments
Daniele Massaro
Daniele Massaro on 7 Nov 2022
Dear Walter,
Sorry for my late reply. Eventually your suggestion worked out, but with this code I do not manage to change the number of workers (either increase or reduce it). For example, with the above code it doesn't use 10, but 12. DO you have any ideas?
Thanks,
Daniele
Walter Roberson
Walter Roberson on 8 Nov 2022
The ,10 in the parfor statement is the maximum number of workers that the statements will be dispatched to.
The documentation does not indicate that it controls the size of the pool in any way. If a pool already exists with 12 workers then it would send to 10 of the 12 workers, leaving the other two workers existing but idle.

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!