MATLAB Answers

Share memory or get id of the worker access

5 ビュー (過去 30 日間)
Chaitanya Sanghavi
Chaitanya Sanghavi 2018 年 5 月 8 日
編集済み: Helper 2018 年 5 月 11 日
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.

回答 (1 件)

Helper
Helper 2018 年 5 月 11 日
編集済み: Helper 2018 年 5 月 11 日
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

Community Treasure Hunt

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

Start Hunting!

Translated by