Matlab:避免在mex中分配内存 [英] Matlab: avoiding memory allocation in mex

查看:226
本文介绍了Matlab:避免在mex中分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使我的mex库避免所有内存分配,甚至如此.
到目前为止,mex有了输入,使用mxCreate...()创建了一些矩阵,并返回了此输出.
但是现在我想修改此接口,以便mex本身不做任何分配.
我想到的是,mexFunction将以输入形式获取矩阵以将值填充到该矩阵中,并将该矩阵返回为输出.

I'm trying to make my mex library avoid all memory allocation what so even.
Until now, the mex got an input, created some matrices using mxCreate...() and returned this output.
But now I'd like to modify this interface so that the mex itself would not do any allocations.
What I had in mind is that the mexFunction will get as input the matrix to fill values into and return this very same matrix as an output.

这应该可行吗?

让我思考这是否是我需要做的所有事情的轻微警报是,左手参数以const的形式出现在mexFunction上,而右手参数是非const的.返回输入矩阵作为输出,我需要删除此const.

The slight alarm that got me thinking if this is at all something I need to be doing is that the left hand arguments come to the mexFunction as const and the right hand argument are non-const. to return the input matrix as an output I'll need to remove this const.

推荐答案

有趣的是,前几天我只是在看这个.我发现的最佳信息是线程此处

Funnily enough I was just looking at this the other day. The best info I found was threads here and here and also this.

基本上,在Matlab世界中,这通常被认为是一件非常糟糕的事情……但是,与此同时,没有什么可以阻止您这样做,因此您可以做到这一点-尝试一些简单的示例,您会发现所做的更改已得到推广.只需更改从prhs获得的数据即可(您无需返回任何内容-因为更改了原始数据,原始数据将反映在工作空间中的变量中).

Basically it is generally considered a very bad thing in Matlab world... but at the same time, nothing stops you so you can do it - try some simple examples and you will see that the changes are propogated. Just make changes to the data you get from prhs (you don't need to return anything - since you changed the raw data it will be reflected in the variable in the workspace).

但是,正如链接中指出的那样,由于Matlabs的写时复制语义,这可能会产生奇怪的后果.设置format debug可以帮助您直观了解.如果执行a=b,则将看到a和b具有不同的结构地址"或标头,表示它们是不同的变量,但数据指针pr指向内存中的同一区域.通常,如果在Matlab中更改y,则会写入时复制,并且在更改之前先复制数据区域,因此y之后有一个新的数据指针.当您更改mex中的内容时,这不会发生,因此,如果您更改y,x也会更改.

However as pointed out in the links, this can have strange consequences, because of Matlabs copy-on-write semantics. Setting format debug can help a lot with getting intuition on this. If you do a=b then you will see a and b have different 'structure addresses' or headers, representing the fact that they are different variables, but the data pointer, pr, points to the same area in memory. Normally, if you change y in Matlab, copy-on-write kicks in and the data area is copied before being changed, so after y has a new data pointer. When you change things in mex this doesn't happen, so if you changed y, x would also change.

我认为这样做是可以的-如果您需要处理大型数据集,这将非常有用,但是您需要注意任何奇怪之处-尝试确保输入的数据不会在变量之间共享. struct和cell数组使事情变得更加复杂,因此我将更倾向于避免对它们进行处理.

I think it's OK to do it - it's incredibly useful if you need to handle large datasets, but you need to keep an eye out for any oddness - try to make sure the data your putting in isn't shared among variables. Things get even more complicated with struct and cell arrays so I would be more inclined to avoid doing it to those.

这篇关于Matlab:避免在mex中分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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