简体   繁体   English

MATLAB MEX导致GPU上的内存丢失

[英]MATLAB MEX causes memory loss on GPU

I wrote a mex function and noticed that every time I run it, more and more memory disappears from my GPU, even though I overwrite the previous results every time. 我编写了一个mex函数,并注意到,即使我每次覆盖以前的结果,每次运行它时,GPU上都会有越来越多的内存消失。 In my attempts to find the source of the problem, I wrote the following code (the file is called MexMemoryTrack ): 在寻找问题根源的过程中,我编写了以下代码(该文件称为MexMemoryTrack ):

#include "mex.h"
#include "gpu/mxGPUArray.h"

void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, mxArray const *prhs[])
{
    mxInitGPU();

    const mxGPUArray * inp=mxGPUCreateFromMxArray(prhs[0]);
    const mxGPUArray * ms=mxGPUCreateFromMxArray(prhs[1]);
    const double * inpPtr=(const double*) mxGPUGetDataReadOnly(inp);
    const double * masksPtr=(const double*) mxGPUGetDataReadOnly(ms);
    mxGPUArray * out=mxGPUCopyFromMxArray(prhs[2]);
    double * outPtr=(double* ) mxGPUGetData(out);
    plhs[0] = mxGPUCreateMxArrayOnGPU(out);
    mxGPUDestroyGPUArray(inp);
    mxGPUDestroyGPUArray(ms);
    mxGPUDestroyGPUArray(out);
}

I run it using: 我使用以下命令运行它:

foo=gpuArray.zeros([3 3 10000 18]);
foo2=gpuArray.randn([7 7 10000 20]);
foo3=gpuArray.randn([5 5 18 20]);
dumdum=MexMemoryTrack(foo2,foo3,foo);

If I put this code in a loop, all my memory ends up disappearing and I get an "out of memory" error. 如果我将此代码放入循环中,我的所有内存最终都会消失,并且会出现“内存不足”错误。 It's very simple. 非常简单 I allocate the memory, I destroy the memory I created except plhs[0] = mxGPUCreateMxArrayOnGPU(out); 我分配内存,销毁我创建的内存, 除了 plhs[0] = mxGPUCreateMxArrayOnGPU(out); which isn't, and shouldn't be destroyed. 不是,也不应该被摧毁。 Since I'm overwriting dumdum (the only lhs argument), I assume Matlab is smart enough to either overwrite the values, or free them and re-allocate space for dumdum . 由于我要覆盖dumdum (唯一的lhs参数),因此我假设Matlab足够聪明,可以覆盖值,或者释放它们并为dumdum重新分配空间。 Using clear dumdum doesn't work either (if that was the solution, I'd be worried about how to keep the information returned...). 使用clear dumdum也不起作用(如果这是解决方案,那么我将担心如何保留返回的信息...)。

Am I missing something? 我想念什么吗?

Could the compiler be the source of the problem (Visual Studio 2010)? 编译器可能是问题的根源(Visual Studio 2010)? Maybe it doesn't work well with Matlab (I'm using Matlab 2013a)? 也许不适用于Matlab(我正在使用Matlab 2013a)?

After many google searches, I eventually reached a (possible) solution, found here 经过许多Google搜索,我最终找到了一个(可能的)解决方案,在这里找到

according to this, lhs is not overwritten when, so when calling the code more than once, copies of previous results remain in the memory - so maybe lhs[0] should be destroyed in the beginning of the code (though first we need to check if there is anything to destroy, as it may not have been allocated yet) 据此,lhs不会在何时被覆盖,因此当多次调用该代码时,先前结果的副本仍保留在内存中-因此lhs[0]可能应在代码开头处销毁(尽管首先我们需要检查一下如果有什么东西要销毁,因为可能尚未分配)

I am unable to verify that this will indeed solve the problem, as I am currently without access to a computer with MATLAB and a graphics card. 我无法验证这是否确实可以解决问题,因为我目前无法使用带有MATLAB和图形卡的计算机。 Basically, the (possible) problem is that the lhs isn't overwritten, but re-allocated (I am actually not surprised, but I wouldn't have guessed that, since I have a poor understanding of exactly how data is handled by MATLAB). 基本上,(可能的)问题是lhs没有被覆盖,而是被重新分配了(实际上我并不感到惊讶,但是我不会猜到,因为我对MATLAB究竟如何处理数据了解得很少。 )。

If anyone happens to test out this theory, please let me know what you find out. 如果有人碰巧验证了这一理论,请告诉我您发现了什么。

In a couple of posts 在几篇文章中

Memory leak while using gpuArray in parallel computing toolbox 2013a 在并行计算工具箱2013a中使用gpuArray时发生内存泄漏

Matlab 2013a GPU memory leak Matlab 2013a GPU内存泄漏

it was recognized that: 人们认识到:

When calling functions using gpuArray data in MATLAB R2013a , MATLAB's memory usage can increase. MATLAB R2013a使用gpuArray数据调用函数时,MATLAB的内存使用量会增加。 A very large number of GPU function calls might exhaust the available memory, causing an Out of Memory or Java heap space error . 大量的GPU函数调用可能会耗尽可用内存,从而导致Out of MemoryJava heap space error

There is a patch published at this bug report page capable to overcome the problem. 在此错误报告页面上发布了一个补丁,可以解决该问题。 This bug has been fixed as of MATLAB R2013b . MATLAB R2013b起已修复此错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM