|
"James Tursa" wrote in message <jdp3od$esi$1@newscl01ah.mathworks.com>...
> "Sira" wrote in message <jdmt8n$qho$1@newscl01ah.mathworks.com>...
> > "James Tursa" wrote in message <jdlas8$5fm$1@newscl01ah.mathworks.com>...
> > > "Sira" wrote in message <jdl8h8$rg8$1@newscl01ah.mathworks.com>...
> > > > "James Tursa" wrote in message <jdl0eg$1fn$1@newscl01ah.mathworks.com>...
> > > > >
> > > > > Q: Why are you doing this? Are you trying to gain some speed advantage for some calculations, or what? Maybe there is another way to go about your particular problem that accesses the 2D pages of your variable without actually forming a separate mxArray from them.
> > > >
> > > > The reason to do this is that the matrix in prhs[1] has size nxnxM and I would like to access to these 2D matrices of size nxn without copying the whole matrix, and this is why the position of :
> > > > SIPR+ 1*n*n
> > > > should correspond to the position S(:,:,2). So, if there is any other way of doing this it would be perfect to me.
> > >
> > > You didn't answer my question. You simply told me again *what* you were trying to do. I already knew that. I am asking *why* you want to do this. Since there is no way to legally do what you are attempting to do in a mex routine, I would like to know why you are trying to do it in the first place.
> > >Why can't you operate on the 2D pages directly in your mex routine without making mxArrays out of them? Why do you need separate mxArrays of the 2D pages to work with? Are you trying to pass them back into the MATLAB workspace? Are you trying to call a MATLAB function with them as input(s)? Or what?
> > >
> > > James Tursa
> >
> > Sorry, I didn't understand your question. I need to implement the following formula:
> > B = sum_i (A*S(:,:,i)*A)^0.5
> >
> > where all matrices A,B,S are complex. Because I will need to iterate it, I would like it to be as fast as possible, so I thought the best option was to use the BLAS method ZHEMM (the matrices are also Hermitian), but to do that I need to isolate the 2D pages....
>
> You will NOT be able to call the BLAS ZHEMM routine without making a copy of your data. That is because all of the complex BLAS (and LAPACK) routines expect the complex data to be interleaved with the real data ala Fortran. MATLAB does not store real and imaginary data in interleaved fashion, but has separate memory blocks for the real and imaginary parts. So either you will need to copy your data into new memory blocks and call the complex BLAS routines, or perhaps call the real BLAS routines with the various real and imaginary pieces and combine the results into the complex result manually. E.g., you could do this:
>
> mtimesx(mtimesx(A,S),A)
>
> which would compute the 2D slice results via BLAS routines (it calls the real BLAS routines multiple times with the real and imaginary pieces and combines the results into the complex result). The mtimesx routine can be found on the FEX here:
>
> http://www.mathworks.com/matlabcentral/fileexchange/25977-mtimesx-fast-matrix-multiply-with-multi-dimensional-support
>
> As to the rest of the calculation, may I ask what the dimensions of all of your variables are? And could you be explicit as to whether the sum is inside the sqrt or ourside of the sqrt?
>
> In any event, there is absolutely no reason to construct mxArrays of the 2D slices in order to calculate your desired result. You can do everything inside the mex routine without making data copies. I can help you with that once I know the dimensions involved and the order of the calculations.
>
> James Tursa
Ok, understood, I will use mtimesx.
Regarding the formula B = sum_i [ (A*S(:,:,i)*A)^0.5 ], the square root is inside the sum and the dimensions of A are nxn, where n can be 1 or 3 and S is nxnxM, where the value of M depends but it is normally between 2 and 15. So, the space is not as important as the time it takes to do the computations, given that I will need to iterate this formula 20000 times, more specifically, I want to implement:
for j=1:20000
B = sum_i [ (A*S(:,:,i)*A)^0.5 ];
A = B^0.5;
end
For the pow(XX,0.5), is there a fast way of executing it in mex? I was planning to call the mexcallMATLAB, something like:
mxArray * squareRoot( mxArray *Input){
//inputs
mxArray* params[2];
params[0] = Input;
params[1] = mxCreateDoubleScalar(0.5); //power=0.5
mwSize n = mxGetM(Input);
//outputs
mxArray *Output[1];
Output[0] = mxCreateDoubleMatrix(n, n + n - 1, mxCOMPLEX);
/* get the square root */
mexCallMATLAB(1, Output, 2, params, "mpower");
return(Output[0]);
}
and then sum along "i" in the formula and every position using a regular C loop.
|