File Exchange

image thumbnail

InplaceArray: a semi-pointer package for Matlab

version 1.5 (10.9 KB) by

Manipulate (multi-dimensional) arrays in the in-place form

8 Downloads

Updated

View License

This package helps to create "children" arrays that share the same data as the mother array. The children array can be shaped any customized size for later manipulation.

In short it provides some sort of equivalent C-"pointer" where Matlab lacks.

Extreme cautious when using this package, otherwise encountered crashes and memory leaks warranty. For "advanced" users only.

SPARSE, STRUCTURE are currently not supported

ATTENTION: Serious bug in testinplace.m in the first submission. Before the new fixed version is submitted please do not run this function (the rest is OK)

Comments and Ratings (9)

Binlin Wu

Josh, thank you for your emails. The code is working now for vectors. It's not critical if it works for scalar or not.

This is a great program and will be very helpful.

Andrew

Hi Bruno. Can I suggest adding the line:

((Internal_mxArray*)(BMAT))->reserved3 = 1; /*give it a reference count*/

Around about line 194 of inplacearray.c. This will tell Matlab not to try to free the memory pointer so:

A = eye(3);
B = inplacecolumn(A, 2);
clear B;

will not cause Matlab to crash (R2009A anyway)
--Cheers, Andrew

Joshua Dillon

Joshua Dillon (view profile)

Hi Bruno. I confirm this problem for my sharedmatrix submission. It would seem Mathworks has dropped support for array_access_inlining. Sadly, this is probably the beginning of the end for our hacks. Please let me know if you find a method to automatically reverse engineer future implementations of the mxArray object (and I will do the same). --Cheers, Josh

Bruno Luong

Bruno Luong (view profile)

Edit: save the following in Internal_mxArray.h before building the mex file:

/*
Internal_mxArray.h
Matlab version: 2010B
*/

typedef struct {
    void *reserved;
    int reserved1[2];
    void *reserved2;
    size_t number_of_dims;
    unsigned int reserved3;
    struct {
        unsigned int flag0 : 1;
        unsigned int flag1 : 1;
        unsigned int flag2 : 1;
        unsigned int flag3 : 1;
        unsigned int flag4 : 1;
        unsigned int flag5 : 1;
        unsigned int flag6 : 1;
        unsigned int flag7 : 1;
        unsigned int flag7a: 1;
        unsigned int flag8 : 1;
        unsigned int flag9 : 1;
        unsigned int flag10 : 1;
        unsigned int flag11 : 4;
        unsigned int flag12 : 8;
        unsigned int flag13 : 8;
    } flags;
    size_t reserved4[2];
    union {
        struct {
            void *pdata;
            void *pimag_data;
            void *reserved5;
            size_t reserved6[3];
        } number_array;
    } data;
} Internal_mxArray;

Bruno Luong

Bruno Luong (view profile)

There is a compilation issue with 2010B, and I still not able to find a good fix. A workaround is compile by hand the mex files:

% On 32-bit platform
mex inplacecolumnmex.c
mex releaseinplace.c

or on 64-bit platform
mex -largeArrayDims inplacecolumnmex.c
mex -largeArrayDims releaseinplace.c

Joshua Dillon

Joshua Dillon (view profile)

I have figured out how to work with sparse matrices. Contact me if interested.

Joshua Dillon

Joshua Dillon (view profile)

Wow this really is a gem!

Any ideas how to handle sparse matrices? ...nothing in matrix.h sadly.

Bruno Luong

Bruno Luong (view profile)

Hi Christophe,
Thank you for report this. It is expected that PACK would mess up with memory pointers.
Bruno

Nice, however, very tricky.
For instance PACK messes up the reference and I have the feeling more things can go wrong when you use memory-related function calls, or am I being too pessimistic?

>> A=1
>> B=inPlaceArray(A);
>> B=2;
>> pack
>> A
A =
  2.1220e-314
>>

Updates

1.5

Improve the installation

1.4

Upload testinplace.m file with modifications reported previously (submission 2 & 3 upload wrong file, sorry)

1.3

Important Comment

1.1

Correct a serious bug in testinplace.m (could cause computer crash)

MATLAB Release
MATLAB 7.8 (R2009a)
Acknowledgements

Inspired: sharedmatrix, MxArrayDefinition

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video