Share memory or get id of the worker access

조회 수: 6 (최근 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

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by