Could exponent of an array be inproved in speed ? Maybe mex file with exp function (reduced accuracy) for windows 64 bit.

1 view (last 30 days)
Could this line of code be improved in speed ? MM is an array 512x512^2 elements.
MM =ones(512^2,512); tic; exp(1i*(MM)); toc
Elapsed time is 3.824361 seconds.
I am looking for mex file for 64bit windows of exp function with reduced accuracy ?
code from Exp function with reduced accuracy But I can not compile it.
#include <stddef.h>
#include <math.h>
#include "mex.h"
#ifndef mwSize
#define mwSize int
#endif
/* these 2 #define lines help make the later code more
readable */
/* Input Arguments */
#define PARAMETER_IN prhs[0]
/* Output Arguments */
#define RESULT_OUT plhs[0]
#define LITTLE_ENDIAN 1
static union
{
double d;
struct {
#ifdef LITTLE_ENDIAN
int j,i;
#else
int i,j;
#endif
} n;
} _eco;
#define EXP_A (1048576/0.69314718055994530942)
#define EXP_C 60801
#define EXP(y) (_eco.n.i = EXP_A*(y) + (1072693248 - EXP_C),_eco.d)
void mexexp(double*y, double*yp, mwSize m) {
while(m--) {
*yp++ = EXP(*y++);
}
}
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray*prhs[] )
{
mwSize numel;
mwSize ndim;
mwSize *dims;
/* Check for proper number of arguments */
if (nrhs != 1) {
mexErrMsgTxt("One input argument required.");
} else if (nlhs > 1) {
mexErrMsgTxt("Too many output arguments.");
} else if ( !mxIsDouble(PARAMETER_IN) ) {
mexErrMsgTxt("Input must be double.");
} else if ( mxIsComplex(PARAMETER_IN) ) {
mexErrMsgTxt("Input cannot be complex.");
}
ndim = mxGetNumberOfDimensions(PARAMETER_IN);
dims = mxGetDimensions(PARAMETER_IN);
numel = mxGetNumberOfElements(PARAMETER_IN);
/* Create a matrix for the return argument */
RESULT_OUT = mxCreateNumericArray(ndim, dims,
mxDOUBLE_CLASS, mxREAL);
/* Do the actual computation*/
mexexp(mxGetPr(PARAMETER_IN),mxGetPr(RESULT_OUT),numel);
return;
}

Answers (1)

James Tursa
James Tursa on 14 Mar 2015
Edited: James Tursa on 14 Mar 2015
For your particular posed example all the elements are the same, so only compute the exp calculation once and don't do all of those multiplies either. E.g.,
NN = complex(zeros(512^2,512)); tic; NN(:) = exp(1i); toc
Is this the real problem, or is MM not really all 1's in your actual problem?
  2 Comments
Ole
Ole on 14 Mar 2015
Edited: Ole on 14 Mar 2015
Thank you. The matrix MM is ones just for simplicity. I would like to take inverse dft of an irregularly sampled complex function in k-space, and MM is the kernel of a dft. I cannot find available function. It is exp(kx*X+ky*Y).
Jan
Jan on 14 Mar 2015
@Ole: Usually it is impossible to give suggestions for speed improvements, if only a roughly simplified version of the code is posted. So please post the relevant part of the code.

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!