我想知道在尝试使用MATLAB的写时复制(懒惰复制)机制来链接来自单元阵列中多个单元的相同大矩阵时,是否有办法确定我是否做得对.
例如:
img = randn(500);
[dx,dy] = gradient(img);
S = cell(2,2);
S{1,1} = dx.^2;
S{2,2} = dy.^2;
S{1,2} = dx.*dy;
S{2,1} = S{1,2}; % should be a reference, as long as not modified
Run Code Online (Sandbox Code Playgroud)
但看看输出whos:
>> whos
Name Size Bytes Class Attributes
S 2x2 8000448 cell
dx 500x500 2000000 double
dy 500x500 2000000 double
img 500x500 2000000 double
Run Code Online (Sandbox Code Playgroud)
我本来希望看到S占用6 MB,而不是8 MB.
有没有办法验证程序中没有错误,那两个单元格仍然在最后引用相同的数组?
我知道这个功能memory,但遗憾的是它只适用于Windows平台(我在MacOS上).
编辑:
在编辑答案之前,我使用了一个未记录的函数,该函数具有意外行为,并且其签名在不同版本的 MATLAB 之间不稳定,因此在这里我提供了 @CrisLuengo 答案的扩展版本。
我们可以使用哈希映射来存储mxArray递归函数中数据元素及其关联的唯一地址check_shared并获取数据的大小。请注意,这里我们可以检查单元格内的共享状态,但无法检查单元格外部且与单元格元素具有相同地址的元素。*
#include "mex.h"
#include <unordered_map>
typedef std::unordered_map<void *,const mxArray *> TableType;
TableType check_shared(const mxArray* arr, TableType table = TableType())
{
switch (mxGetClassID(arr)) {
case mxCELL_CLASS:
for(int i = 0; i < mxGetNumberOfElements (arr); i++) {
table = check_shared(mxGetCell (arr,i), std::move(table));
}
break;
case mxSTRUCT_CLASS:
for (int i = 0; i < mxGetNumberOfFields (arr); i++) {
for (int j = 0; j < mxGetNumberOfElements (arr); j++) {
table = check_shared(mxGetFieldByNumber (arr, j, i), std::move(table));
}
}
break;
case mxVOID_CLASS:
case mxFUNCTION_CLASS:
case mxUNKNOWN_CLASS:
return table;
}
if (!mxIsEmpty (arr)) {
void* data = mxGetData(arr);
table[data] = arr;
}
return table;
}
uint64_t actual_size(const TableType& table)
{
uint64_t sz = 0;
for (const auto& entry : table) {
const mxArray * arr = entry.second;
sz += mxGetElementSize (arr) * mxGetNumberOfElements (arr);
}
return sz;
}
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
TableType table = check_shared(prhs[0]);
plhs[0] = mxCreateNumericMatrix(1,1, mxUINT64_CLASS, mxREAL );
uint64_t* result = static_cast<uint64_t*>(mxGetData (plhs[0]));
result[0] = actual_size(table);
}
Run Code Online (Sandbox Code Playgroud)
(*)支持基本数据类型,例如 cell、和数值数组。struct对于未知的数据结构和 classdef 对象,该函数返回零。