Issue with optimizing vectorized code with logical matrixes

1 view (last 30 days)
Hello all,
I've ran into an issue with my code where one line,
gamma(loc4) = theta_c2(loc4);
takes considerably longer to run than other simular lines of code regadless of where it is placed or what computer is used. Thats really the part that confuses me. If it was an memeory issue I would think it'd happen consisetnyly in the same place (ex, always line #). More memory does help the code run faster, but that only relieves a symptom. If it was the use of logical matrixes to overwrite I'd think any line with
gamma(loc#) = theta(loc#)
would have issues. However, it's only that one line.
Tests done with a 300x1080x2201 array with 32gb of memory and cleared workspace. gamma is prealocated with zeros.
Any help with finding the cause to a posible soultion to this issue would be greatly appreciated.
  2 Comments
Walter Roberson
Walter Roberson on 13 Oct 2023
What is nnz(loc4) and nnz(loc1) and size(loc4) and size(loc1) ?
Side note: using theta_s1(:,:,:) as an expression is a waste of time compared to using just the variable name theta_s1 except in one of the following circumstances:
  1. the variable theta_s1 has 4 or more dimensions, in which case theta_s1(:,:,:) is equivalent to reshape(theta_s1,size(theta_s1,1),size(theta_s1,2),[]) ; OR
  2. you have reason to deliberately remove the possibility for "in-place" operations
With respect to in-place operations: in cases where you have a function and a variable in the function and you have a call to a second function with the general form
YourVariable = some_function(YourVariable,maybe_other_variables)
where the same variable is input and output, and there are no other variables that are sharing memory with YourVariable, then the called function is generally permitted to write the output right on top of the input, instead of new memory being allocated to store the result. But when you use indexing on the variable, like theta(:,:,:) then the result is considered an expression rather than the original variable and storing on top of the original variable would not be permitted.
This is a pretty obscure behaviour that very few people have reason to care about... and if you are not deliberately trying to trigger it then just use theta_s1 instead of theta_s1(:,:,:)
Bruno Luong
Bruno Luong on 13 Oct 2023
"Side note: using theta_s1(:,:,:) as an expression is a waste of time compared to using just the variable name theta_s"
There is no reason extra time is significant (no data are copied since data with colon is shared with the original data), and indeed I don't observe extra time with this test script:
theta = rand(300,1080,2201);
timecolonindexingmethods(theta);
Elapsed time is 0.526216 seconds. Elapsed time is 0.470275 seconds.
clear
function loc = timecolonindexingmethods(theta)
tic
loc = theta + theta <= 1e-3;
toc
tic
loc = theta(:,:,:) + theta(:,:,:) <= 1e-3;
toc
end

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 13 Oct 2023
My guess is that you run into memory problem.
During the assigment
gamma(loc#) = theta(loc#)
There are perhaps 3 copy of a big matrices
gamma, theta, and intermediate temporary, copy on write array and possibly smaller sub arrays. The size of 4 of such array is
(300*1080*2201*8*4)/1e9 = 22Gb
You might ry to do simple for-loop
for i = reshape(locx, 1, [])
gamma(i) = theta(i);
end
I believe such statement save the copy on write array.

More Answers (1)

Andrew Matteson
Andrew Matteson on 13 Oct 2023
I found the issue, basicly that line of code was the only one actually assigning values (has to do with my inputs). Other lines are unused hence why it takes so long. Overall leads to arrays being created that don't need to be. Totally memory issue. I've added arguments and a switch-case structure to make parts of my code optional.

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!