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

1 Ansicht (letzte 30 Tage)
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;
}

Antworten (1)

James Tursa
James Tursa am 14 Mär. 2015
Bearbeitet: James Tursa am 14 Mär. 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 Kommentare
Ole
Ole am 14 Mär. 2015
Bearbeitet: Ole am 14 Mär. 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 am 14 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB Compiler finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by