Passing composite variable to mex file

1 次查看(过去 30 天)
I am running an spmd, and I am wondering if there is a way to pass a composite variable to a mex file without "aggregating" it first. The following is a (simple) example. Say I have the following code.
parpool(2);
spmd
test = labindex + zeros(1000, 2);
end
result = sum([test{:}], 2);
Suppose I would like to replace the sum function with a C/mex implementation of it. It is possible to do something like the following:
test_cellarray = test(:);
result = sum_function(test_cellarray); % Not a built-in function
where sum_function takes in cell arrays. However, in the above "solution", the first line takes up the vast majority of the time.
Is it possible to pass "test" directly to a mex function and access the parts for different workers through pointers?
Thanks for the help.
  2 个评论
James Tursa
James Tursa 2022-1-31
What exactly is the test variable? I.e., what does "whos test" show?
Vivek Bhattacharya
Vivek Bhattacharya 2022-1-31
@James Tursa -- This is the output from inspecting the variables. (Including test_cellarray for illustration too.)
>> test
test =
Worker 1: class = double, size = [1000 2]
Worker 2: class = double, size = [1000 2]
>> test_cellarray
test_cellarray =
2×1 cell array
{1000×2 double}
{1000×2 double}
>> whos test test_cellarray
Name Size Bytes Class Attributes
test 1x2 265 Composite
test_cellarray 2x1 32208 cell

请先登录,再进行评论。

采纳的回答

Edric Ellis
Edric Ellis 2022-1-31
Unfortunately, there is no way to access the elements of a Composite from a MEX file. Is there any way you could run the MEX file directly on the workers? If you need to run it on each element, that would probably be the best way.
  2 个评论
Vivek Bhattacharya
Vivek Bhattacharya 2022-1-31
Thanks, @Edric Ellis! Yes, I'm currently doing most of the computation within the spmd loop, but there is one part of the calculation that requires the entire vector of output from each worker and can't be done individually on each worker. (Perhaps the sum is a bad example on that dimension.) I was hoping that it would be possible to send pointers to each worker's data to a mex function (or something like that), but good to know that that's not possible.
Edric Ellis
Edric Ellis 2022-1-31
You might be better off collecting the data on a single worker, and working with it there. Worker-to-worker communication is generally more efficient than worker-to-client. I.e. something like:
spmd
myVal = someComputation();
catDim = 1; % dimension to concatenate results
targetWorker = 1;
allVals = gcat(myVal, catDim, targetWorker);
if labindex == targetWorker
endResult = doStuff(allVals);
end
end

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by