为什么MATLAB的置换不需要额外的内存? [英] Why does MATLAB's permute not need extra memory?

查看:75
本文介绍了为什么MATLAB的置换不需要额外的内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

置换操作需要向输出输出不同矩阵,它是

The permutation operation needs to output a different matrix to the output, it's not like reshape, where the data is not modified, permute does modify the data.

但是,如果测试多维排列的内存使用情况,则它与所使用的变量相同.因此,我的问题是,如何 MATLAB如何执行此置换以避免使用任何额外的内存?

However, if one tests the memory usage of a multidimensional permutation, it's the same as the variable used. So, my question is, how does MATLAB execute this permutation in order to avoid using any extra memory?

额外的问题:是否存在MATLAB实际使用额外内存的情况?

Extra question: Is there any scenario in which MATLAB actually uses extra memory?

测试代码:

function out=mtest() 
   out = ones(1e3,1e3,1e3); % Caution, 8Gb
   out=permute(out,[3 1 2]);
end 

致电:

profile -memory on
a=mtest;
profreport

注意,其8Gb数据.

推荐答案

您的参数存在缺陷,因为MATLAB内存分析器没有告诉您真相!

Your argument is flawed because the MATLAB memory profiler is not telling you the truth!

permute方法实际上确实对矩阵进行了第二次复制,并对其进行了排列,并返回了数据...

The permute method in fact does create a second copy of the matrix with the data permuted and returns it...

我自己尝试过:

  • 在Windows 10中,我在性能"选项卡上打开了任务管理器",并显示了内存"图.我还将更新速度"设置为高"以获得更好的时间分辨率.

  • In Windows 10, I opened the "task manager" on the "performance" tab with the "memory" graphs in view. I also set the "update speed" to "high" to get a finer time resolution.

我的笔记本电脑上只有8 GB的RAM,为避免崩溃,我将您的功能修改为:

I only have 8 gigs of RAM on my laptop, so to avoid thrashing I modified your function as:

function out = mtest()
    out = rand([1e3,5e2,5e2]);  % about 1.8 GB = 1e3*5e2*5e2*8/2^30
    out = permute(out, [3 2 1]);
end

  • 然后我简单地运行该函数:

  • I then ran the function simply as:

    %clear a
    a = mtest();
    

    并查看内存使用情况;它从使用的1.8演出开始,上升到5.2,然后迅速下降到3.6演出.这确认已创建副本.

    and watched the memory usage; It went from 1.8 gigs in use, and rose to 5.2 then quickly down to 3.6 gigs. This confirms that a copy was created.

    我还重复了perfmon.exe下的测试,结果显示出相同的模式:

    I also repeated the test under perfmon.exe which showed the same pattern:

    您可以看到函数在其峰值时达到的内存使用量是返回时的两倍.

    you can see how at its peak the function reached twice as much memory usage as when it returned.

    虽然这不是分析内存使用情况的最佳方法(最好使用适当的内存分析器,例如Intel Inspector XE),但它确实在某种程度上表明permute确实不能就地工作.

    While this is not exactly the best way to profile memory usage (better use a proper memory profiler, something like Intel Inspector XE), it does show to some degree that permute is indeed not working in-place.

    这篇关于为什么MATLAB的置换不需要额外的内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆