Blitz logo

Blitz Support :

From: Julian Cummings (cummings_at_[hidden])
Date: 2003-05-14 12:55:38


Actually, you can do this without looping over the data and copying it.
All you have to do is grab the data pointer and hand it to the appropriate
Blitz Array constructor along with the shape of the Array and the desired
policy for pre-existing memory. To do this, you do need to know the
storage order of the data Blitz is being handed. I believe that Matlab
arrays use C-style storage (column-major order), which is what Blitz Arrays
use by default. So the code would look something like this:

void mexFunction(inbt nlhs, mxArray *plhs[], int nrhs, const mxArray
*prhs[]) {
    double *pr;
    int m, n;

    // matlab --> blitz
    m = mxGetM(prhs[0]);
    n = mxGetN(prhs[0]);
    pr = (double *)mxGetPr(prhs[0]);

    Array<double,2> A(pr, shape(m,n), neverDeleteData);

    // perform computations ...

    // blitz --> matlab
    // nothing more to do!
}

The Blitz Arrays can operate on data in place within the pre-existing
data buffer,
so you don't have to copy data in or back out. Of course, you can copy
the data
if you want to leave the original buffer unchanged or if it is volatile
in some way.
To do this, replace the policy "neverDeleteData" with "duplicateData".
 Then the
Blitz Array will work with its own copy of the data. In that case, you
will have
to loop over data elements and copy them back into the Matlab mxArray
buffer.
Please see Section 2.3.7 of the Blitz documentation for more information.

Regards, Julian C.

 

Sam Kaplan wrote:

>Hello,
>
>Perhaps this isn't the answer you're looking for, but here is a quick
>and dirty solution:
>
>void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray
>*prhs[])
>{
>Array<double,2> A;
>double *pr;
>int i,j,m,n;
>
>// matlab --> blitz
>m = mxGetM(prhs[0]);
>n = mxGetN(prhs[0]);
>
>A.resize(m,n);
>
>pr = (double *)mxGetPr(prhs[0]);
>
>for (j = 0; j < n; j++){
> for (i = 0; i < m; i++)
> A(i,j) = pr[i + m*j];
>}
>...
>
>// blitz --> matlab
>plhs[0] = mxCreateDoubleMatrix(m,n,mxREAL);
>pr = (double *)mxGetPr(plhs[0]);
>for (j = 0; j < n; j++){
> for (i = 0; i < m; i++)
> pr[i + j*m] = A(i,j);
>}
>pr = 0;
>}
>
>I hope this wasn't totally useless.
>
>Sam
>
>On Wed, 2003-05-14 at 05:29, Oliver Lange wrote:
>
>
>>Hi,
>>
>>has anyone written a conversion routines from
>>Blitz Array's to the Matlab mxArray Structure?
>>
>>I want to use
>>Blitz-driven Routines as mexFunctions
>>that means i get input from Matlab as mxArray have
>>to convert to Blitz++, do calculations and convert
>>back into mxArray - Format before passing back the results to
>>matlab.
>>
>>Olli
>>
>>
>>
>>
>
>_______________________________________________
>Blitz-support mailing list
>Blitz-support_at_[hidden]
>http://www.oonumerics.org/mailman/listinfo.cgi/blitz-support
>
>

-- 
Dr. Julian C. Cummings                       E-mail: cummings_at_[hidden]
California Institute of Technology           Phone:  626-395-2543
1200 E. California Blvd., Mail Code 158-79   Fax:    626-584-5917
Pasadena, CA 91125