How does one have arrays in a Matlab share the same memory
3 次查看(过去 30 天)
显示 更早的评论
Greetings, Is there a simple means in Matlab for two arrays to share the same data space? In other words letting A be a N by M array of type int32 and B being a NxMX4 type int8. In C I would do this with pointers. Thank you
0 个评论
采纳的回答
Stephen23
2016-7-6
编辑:Stephen23
2016-7-6
myData = int8(1:10)';
fileID = fopen('records.dat','w');
fwrite(fileID, myData,'int8');
fclose(fileID);
m1 = memmapfile('records.dat','Writable',true,'Format','int8');
m2 = memmapfile('records.dat','Writable',true,'Format','int32');
and checking the original data:
>> m1.Data
ans =
1
2
3
4
5
6
7
8
9
10
>> m2.Data
ans =
67305985
134678021
Now we change one of the int32 values:
>> m2.Data(2) = m2.Data(2) + 1;
and check what happened to the int8 data:
>> m1.Data
ans =
1
2
3
4
6
6
7
8
9
10
So, the 5 has changed into a 6, proving that memmapfile can be used to achieve your goal. You will need to play around with byte order, dimensions, and whatnot, but it seems to be possible to make this happen.
4 个评论
James Tursa
2016-7-15
As Stephen points out, this could be done with a mex routine but with a couple of caveats:
1) You would need to make the shared data copy of a different class using a mex routine that hacks into the variable mxArray structure since there are no existing official MATLAB or mex API functions that will do this. E.g., my typecastx function from the FEX can do this. MATLAB's typecast function can't be used for this since it produces a deep data copy.
2) ALL of the data element changes/updates would have to be done at the mex level so that you could modify values in-place without causing an unsharing data copy to take place. I.e., any data element change that you would make at the m-file level will see the shared variable status and invoke the copy-on-write mechanism and first unshare the variables ... which is precisely what you are trying to avoid.
更多回答(3 个)
Philip Borghesani
2016-7-7
A libpointer can be (mis)used to do this but I am not sure it will help in the long run due to the overall performance of and copying out (accessing ptr.value) needed with libpointers.
pi8=libpointer('int8Ptr',1:16)
pi16=libpointer('voidPtr',pi8)
pi16.setdatatype('int16Ptr',1,8)
pi16.value(3)=100;
pi8.value
ans =
1×16 int8 row vector
1 2 3 4 100 0 7 8 9 10 11 12 13 14 15 16
Patrick Ford
2016-7-13
1 个评论
James Tursa
2016-7-15
编辑:James Tursa
2016-7-15
In your libpointer example, the value that libpointer creates is a deep data copy of "a". There would be no way to use this libpointer, even in a mex routine, to alter any values in "a". E.g.,
>> a = zeros(1,5)
a =
Structure address = 72ca8b0
m = 1
n = 5
pr = 24545770
pi = 0
0 0 0 0 0
>> px = libpointer('voidPtr', a)
px =
libpointer
>> px.value
ans =
Structure address = 72ca178
m = 1
n = 5
pr = 206aee80
pi = 0
0 0 0 0 0
You can see that the pr data pointers for a and px.value are different, which means that they are not shared data copies of each other.
To have variables of different classes sharing memory and being able to alter one without automatic unsharing, see my mex comment above. You would probably need to employ something like my typecastx FEX submission to accomplish it. Or maybe just pass in the int16 or int32 and manipulate the data elements in-place in the mex routine as int8 without ever creating a shared int8 version of the variable.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!