How to know in advance if creating matrix will run out of memory?

조회 수: 1 (최근 30일)
I need to create a matrix of size N-by-M as part of a script, where N and M are inputs to the script.
So I will know N and M, and then depending on their size choose do one of the following three:
i) I want to create a gpu matrix when N and M are small enough that the relevant matrix will fit on gpu without creating an out of memory error.
ii) When N and M are too large for this I want to create the matrix on the cpu instead.
iii) If N and M are so large that I would run out of memory on the cpu then I want to create the matrix as a sparse matrix on the cpu.
Is there a way, once I know N and M, to check if the matrix will be possible to create?
[I could use 'try-catch-end', with try just erroring when creating a matrix too large for memory, but I would prefer something cleaner as I suspect that runtime to do the try-catch on a matrix too large for memory is wasteful]

채택된 답변

Raymond Norris
Raymond Norris 2021년 12월 21일
Look at the memory command, but it's only for Windows operating system. The gpuDevice command will return AvailableMemory of a GPU. This can get you started. Keep in mind memory needed to operate on it (e.g., adding matrices, fft, etc.). It might suprise you how much memory gets used on a GPU with several matrices.
  댓글 수: 2
Robert Kirkby
Robert Kirkby 2021년 12월 21일
Thanks.
I'm on linux so memory won't work. But checking gpu memory is much more important to me so that will help.
Is there an easy way to tell how much memory X will be used in advance?, e.g., that a N-by-M double floating point matrix will use X gigabytes?
Raymond Norris
Raymond Norris 2021년 12월 21일
Try
sz = N * M * size-of-unit / 1024 ^ units;
size-of-unit depends on int8 . . . int64 (i.e., 1 . . . 8) and units depends on MB or GB (i.e., 2 or 3). For example, size of N,M double matrix in GBs would be
sz = N * M * 8 / 1024 ^ 3;

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by