Share memory or get id of the worker access

5 views (last 30 days)
Chaitanya Sanghavi
Chaitanya Sanghavi on 8 May 2018
Edited: Helper on 11 May 2018
Hello, I am new to the parallel computing toolbox and having the following issue when using parfor.
Aim: I want to assemble a matrix using multi-threading for Finite Element discretizations.
Possible Way: 1. I was planning to use parafor to get the local assemblies and then sum up all the local assemblies. (The local assemblies mean the task for one thread.) The issue is I need to associate the local entries to the global entries and need an access of the global variables in each thread. I try the following which does not work.
nnz = 2000;
Threads =4;
loop = nnz/Threads;
A = zeros(nnz,1);
parfor i = 1:4
b = i;
A((i-1)*loop+1:loop*i) = b;
end
2. Another way, was to get to manually create all the data structures for each thread. Do the global assembly in each thread. And then add them up. But for this I need the thread id access. I do not know if this possible.
Other ways can be suggested. Thanks in advance.

Answers (1)

Helper
Helper on 11 May 2018
Edited: Helper on 11 May 2018
The reason for why the first way does not work is the "A" variable is not a valid sliced variable. Variables within "parfor" have different meanings. Please refer to the following documentation links for more information:
There is another way to do this, which is using "spmd". So we could have:
spmd
A((labindex-1)*loop+1:loop*labindex) = labindex;
end
Please refer to the following documentation links for more information regarding "spmd" and "labindex": spmd; labindex

Categories

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

Community Treasure Hunt

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

Start Hunting!