Error utilizing parfor loops with MTEX calcGrains function

6 views (last 30 days)
Hi All,
I am segmenting regions within my ebsd data under the mtex toolbox, and then using the parallel computing toolbox (parfor loop) to utilize calcGrains within each segmented region. I am able to run the calcGrains function within the command window, or within separate code, but when I include it in my parfor loop, it throws some odd errors.
**Here is my code:**
%% Specify Crystal and Specimen Symmetries
% crystal symmetry
CS = {...
'notIndexed',...
crystalSymmetry('m-3m', [4 4 4], 'mineral', 'Aluminum'),...
'notIndexed',...
'notIndexed'};
%% convention
% plotting convention
setMTEXpref('xAxisDirection','east');
setMTEXpref('zAxisDirection','intoPlane');
%% Specify File Names
% path to files
pname = 'C:\Users\Cole\Desktop\Code\6061_EBSD_Analysis';
% which files to be imported
fname = [pname '\Retreating.ctf'];
%% Import the Data
% create an EBSD variable containing the data
ebsd = EBSD.load(fname,CS,'interface','ctf',...
'convertEuler2SpatialReferenceFrame');
%% convention
% plotting convention
setMTEXpref('xAxisDirection','east');
setMTEXpref('zAxisDirection','intoPlane');
%% Segment Regions
% Define the dimensions of the entire data set
totalWidth = 1300; % in microns
totalHeight = 3000; % in microns
% Define the step size
stepSize = 100; % in microns
% Calculate the number of regions
numRegions = floor(totalHeight / stepSize);
% Initialize a cell array to store the EBSD regions
ebsdRegions = cell(1, numRegions);
% Iterate over the regions
for regionIndex = 1:numRegions
% Calculate the ymin and ymax for the current region
ymin = (regionIndex - 1) * stepSize;
ymax = regionIndex * stepSize;
% Define the region of interest
region = [400 ymin 1300 (ymax - ymin)];
% Use inpolygon to conditionally select data within the region
condition = inpolygon(ebsd, region);
% Create a new EBSD data set containing data within the region
ebsdRegion = ebsd(condition);
% Store the ebsdRegion in a variable with the desired syntax
variableName = ['ebsd' num2str(ymin)];
eval([variableName ' = ebsdRegion;']);
% Store the ebsdRegion in the cell array
ebsdRegions{regionIndex} = ebsdRegion;
end
%% Compute Grains for each 100 um region
% Set the number of iterations
N = 30;
stepSize = 100; % Step size in microns
% Preallocate arrays to store the results
grains = cell(1, N);
grainIds = cell(1, N);
% Store ebsd variables in a cell array
ebsdData = cell(1, N);
for i = 1:N
ymin = (i - 1) * stepSize; % Calculate the ymin for the current region
variableName = ['ebsd' num2str(ymin)];
ebsdData{i} = evalin('base', variableName); % Retrieve the EBSD region
end
%% Parallel loop
% Preallocate arrays to store the results
tempGrains = cell(1, N);
% Parallel loop
parfor i = 1:N
ymin = (i - 1) * stepSize; % Calculate the ymin for the current region
ebsdRegion = ebsdData{i}; % Retrieve the EBSD region
ebsdRegion = ebsdRegion('Aluminum'); % Apply necessary filters
% Perform calculations in parallel
tempGrains{i} = calcGrains(ebsdRegion);
end
% Assign the results to the cell array
grains = cell(1, N);
parfor i = 1:N
grains{i} = tempGrains{i};
end
% Access the results from grains and grainIds arrays
for i = 1:N
ymin = (i - 1) * stepSize; % Calculate the ymin for the current region
variableName = ['grains' num2str(ymin)];
assignin('base', variableName, grains{i});
end
**Here is the error that is thrown:**
Error using euler2quat
Invalid default value for property 'CS' in class 'orientation':
SWITCH expression must be a scalar or a character vector.
Error in rotation.byEuler (line 27)
q = euler2quat(varargin{:});
Error in symmetry/calcQuat (line 186)
rot = {rotation.byEuler(0,0,0)};
Error in crystalSymmetry (line 158)
if isempty(rot), rot = symmetry.calcQuat(id,axes); end
Error in gbc_angle (line 4)
d = angle(orientation(q(Dl),CS),orientation(q(Dr),CS));
Error in EBSD/calcGrains/doSegmentation (line 210)
connect(ndx) = feval(['gbc_' gbc],...
Error in EBSD/calcGrains (line 74)
[A_Db,I_DG] = doSegmentation(I_FD,ebsd,varargin{:});
Error in Retreating_Grain_Step (line 100)
parfor i = 1:N
I would really appreciate any help. Thank-you

Answers (1)

Walter Roberson
Walter Roberson on 8 Jul 2023
parfor operates in a different process that does not have access to the base workspace established by the client.
  2 Comments
Cole Franz
Cole Franz on 9 Jul 2023
Is there a way I can explicitely give the parfor loop access to my workspace? One confusing aspect to this problem is that the code worked for me a month or two ago, but since then it has been giving me issues.
Walter Roberson
Walter Roberson on 9 Jul 2023
No. There is no way to give parfor access to shared memory (other than to use operating system facilities to create shared memory segments... or perhaps memmap a file)
If you need variables from the base workspace to be available to the workers then use parfevalOnAll() to run a function that initializes the local base workspaces with appropriate values.

Sign in to comment.

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Tags

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!