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!