If the memory copy occurs when execute an assignment or build a temporary object?

3 views (last 30 days)
For example,
A =zeros(10, 10) %is a matrix.
B = A; % statement 1
C = A(:,6:end); % statement 2
D = C *C'; % statement 3
E = A(:,6:end)*A(:,6:end)'; %statement 4
1)
I wonder if the statement 1 copys the memory of A to B?
I learned from the textbook about the function call mechanism of MATLAB. Whether an argument will be copied depends on whether the value of this argument is changed inside the function. I want to know if this mechanism is consistent in MATLAB.
If the above mechanism is consistent in MATLAB, my understanding is whether memory will be copied should be judged according to the context. If B does not change in the subsequent program, it should only be a reference of A without copying memory. Copy will occur only when B changes the matrix content. Is is correct?
2)
If the statement 2 copys memory?
3)
Like the above question, I want to know whether the construction of A(:,6:end) and A(:,6:end)' temporary objects will copy memory, and whether statement 3 and statement 4 are equivalent in memory consumption and running speed?
Many thanks!

Answers (1)

Walter Roberson
Walter Roberson on 7 Oct 2021
I wonder if the statement 1 copys the memory of A to B
No. B will be assigned as a reference to the same memory as A.
If the statement 2 copys memory?
Traditionally, the right hand side would have copied memory. A few releases ago, MATLAB added an internal mechanism for referring to sub-portions of an array without copying the memory, but it is difficult to find documentation of that mechanism. I believe that in the context of an assignment the mechanism would ultimately cause memory to be copied -- that is, I believe that the reference mechanism is read-only for temporary reference to values that are going to be used in calculations. But documentation is scarce on that point.
| want to know whether the construction of A(:,6:end) and A(:,6:end)' temporary objects will copy memory
We are not certain. The model is that each of the two A(:,6:end) would have copied memory independently -- but it was always plausible that as an optimization, MATLAB might have noticed that A(:,6:end) and the following A(:,6:end) referred to the same place, so it is possible that it would only have copied the memory once instead of twice. However, that possibility feels unlikely, since A(:,6:end)*A(:,6:end)' is properly parsed as A(:,6:end)*(A(:,6:end)') and the transpose operation needs to copy memory -- but we cannot rule out the possibility that it optimizes down to a single copy of A(:,6:end) .
and whether statement 3 and statement 4 are equivalent in memory consumption and running speed
Oddly there are special internal rules for all-zero arrays that complicate the situation. It would be better to test with non-zero arrays.
N = 8e3;
A = rand(N, N);
tic;
C = A(:,6:end);
D = C*C';
toc
Elapsed time is 4.601447 seconds.
tic
E = A(:,6:end) * A(:,6:end)';
toc
Elapsed time is 8.997006 seconds.
tic;
C = A(:,6:end);
D = C*C';
toc
Elapsed time is 6.481838 seconds.
tic
E = A(:,6:end) * A(:,6:end)';
toc
Elapsed time is 11.920557 seconds.
I think we have to conclude that the temporary variable form uses less time.
(I run each of the calculations twice in case some kind of optimization occurs after the first time.)
  1 Comment
Ye Cao
Ye Cao on 7 Oct 2021
Thank you very much!
In fact, I moved from BLAS to Matlab. In BLAS, the submatrix costs nothing. We only need to have a pointer holding the beginging address of the submatrix and set the leading dimension of ths submatrix to be the same as the that of the original matrix.
Why does MATLAB need to reconstruct a submatrix so that it needs to copy out the memory of the submatrix from the original matrix?Why B = A is only a reference but B = A(:,6:end) build a new object?
Memory consumption is very important, especially when submitting to a supercomputer. We must estimate the maximum memory consumption.
From your advice, I think in the statement E = A(:, 6:end)*(A(:,6:end)'), the A(:,6:end) will not copy memory because it is an read-only temporary object, however, the A(:,6:end)' copys memory. The C = A(:,6:end) statement will copy memory.
1)
I want to know whether the left hand side of C=A(:,6:end) can be declared as a constant object so that memory will not be copied?
2)
For the E = A(:, 6:end)*(A(:,6:end)'), I can hardly understand. In MKL (or other BLAS realizations), there are two ways to do that:
//C := alpha*op(A)*op(B) + beta*C
void cblas_dgemm (
const CBLAS_LAYOUT Layout,
const CBLAS_TRANSPOSE transa, // -------------------- Set to no transpose
const CBLAS_TRANSPOSE transb, // -------------------- Set to transpose
const MKL_INT m,
const MKL_INT n,
const MKL_INT k,
const double alpha,
const double *a, // -------------------- The begining address of A(:, 6:end)
const MKL_INT lda, // -------------------- leading dimension of the original matrix A
const double *b, // -------------------- The begining address of A(:,6:end)
const MKL_INT ldb, // -------------------- leading dimension of the original matrix A
const double beta, // -------------------- Set to zero
double *c,
const MKL_INT ldc
);
//C := alpha*A*A' + beta*C,
void cblas_dsyrk (
const CBLAS_LAYOUT Layout,
const CBLAS_UPLO uplo,
const CBLAS_TRANSPOSE trans, // -------------------- set to no transpose
const MKL_INT n,
const MKL_INT k,
const double alpha,
const double *a, // -------------------- the begining address of A(:, 6:end)
const MKL_INT lda, // -------------------- leading dimension of the original matrix A
const double beta,
double *c,
const MKL_INT ldc
);
I believe in the first program, BLAS will not copy memory for the transpose flag is set to either "transpose" or "no transpose". I am not sure for the second, but I guess it won't copy memory for the transposed matrix.
Would you please comment more about why MATLAB consumes more memory?

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!