vectorize mldivide

조회 수: 4 (최근 30일)
Matthias Klemm
Matthias Klemm 2011년 12월 9일
Currently I'm using mldivide in the following way: A data vector and a number of model vectors which are added up to approximate the data. I want to determine the scaling factors (coefficients) of the model vectors to approximate the data (least squares...).
example: data [1024,1]; %1 data vector with 1024 elements models [1024,4]; %4 model vectors
coeff = models\data;
Now I want to run hundreds/thousands of those linear optimizations concurrently (even using an identical data vector but thats not so much the point). As mldivide doesn't support this I'm forced to run this in a loop. To use parfor doesn't get me any speedup (even takes 2x longer).
  댓글 수: 3
David Provencher
David Provencher 2011년 12월 9일
Are the optimization problems completely different from one another (i.e. the data and model vectors change every time)? Or is the data vector the same?
Sean de Wolski
Sean de Wolski 2011년 12월 9일
Also look at spmd

댓글을 달려면 로그인하십시오.

답변 (1개)

Titus Edelhofer
Titus Edelhofer 2011년 12월 9일
Hi,
there is probably not too much you can do (as long as the models differ from run to run). On the other hand:
x = rand(1024,4);
y = ones(1024, 1);
results = zeros(1000, 4);
tic,
for i=1:1000
results(i,:) = (x\y)';
end
time=toc
%
time =
0.1164
The loop shouldn't be all that bad for the size of your problem...?
Titus
  댓글 수: 1
Matthias Klemm
Matthias Klemm 2011년 12월 9일
Thank you all for your answers!
@David: The model vectors change every time, the data vector is the same.
I'm sorry I should have given much more information.
The problem arose from an attempt to vectorize lsqnonneg as ~40% of my computation time are spent on lsqnonneg. Inside lsqnonneg ~50% are spent on mldivide. I could 'vectorize' lsqnonneg except for mldivide which (of course) doesn't have much of an effect.
example code (not vectorized):
nVecs = 1000;
model = rand(1024,4,nVecs);
data = ones(1024,1);
coeff = zeros(4,nVecs);
tic
for i = 1:nVecs
coeff(:,i) = lsqnonneg(squeeze(model(:,:,i)),data);
end
toc
The code above is called a few hundred times by a nonlinear optimization algorithm, e.g. I had 30000 calls of lsqnonneg (=15 sec) and a total runtime of ~38 sec. This does not sound too bad but if this is just one pixel of an image it is... The call of each lsqnonneg is probably too short to gain something from parfor (instead I run pixels in parallel using parfor). Still I think I could gain some speed if I wouldn't need that outer loop by either having a vectorized mldivide or being able to use mldivide in bsxfun.
Is it really so uncommon to have multiple independent linear optimizations?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by