mex file crashes in loop
2 views (last 30 days)
Show older comments
Hello,
I have a problem calling my mex file inside a loop. After the first run, it creates myMexFunction.mexw64, and after 5-6 times, the file disappears and I get this error:
mt : general error c101008d: Failed to write the updated manifest to the resource of file "myMexFunction.mexw64".
The system cannot open the device or file specified.
I have searched the forum and suggestions includes adding clear mex, and looking at the variable types, i have tried as much, but could not succeed.
Here is my mex code:
/*=========================================================
* This function replaces nested loops in coefFourier_ll_log.m
* IT requires Matlab2018a or higher to compile.
* When calling from Matlab use explicitly
* mex myMexFunction.cpp -R2018a.
* to do: use templateArray class
* 06/06/2018 TT
*=======================================================*/
#include "mex.h"
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <complex>
using namespace std;
/* computational subroutine */
void myMexFunction(const mxArray * lambda,const mxArray * ll_n, mxArray * output,
int N)
{
mwSignedIndex n,s,r;
mwSignedIndex M = N/2-1;
mwSignedIndex N1 = N-1;
mwSignedIndex c,d,rmin,rmax;
// mexPrintf("M:%d\n", M);
/* get pointers to the arrays */
mxDouble * lam = mxGetDoubles(lambda);
mxComplexDouble * lln = mxGetComplexDoubles(ll_n);
mxComplexDouble * out = mxGetComplexDoubles(output);
/* perform the nestedloop op */
for(s = -M; s <= M; s++){
c = max(-M,s-M);
d = min(M,s+M);
for(n = -M; n <= M; n++){
rmin=max(-M-n,c);
rmax=min(M-n,d);
for(r= rmin; r<=rmax; r++){
out[N1*(s+M)+(n+M)].real =
out[N1*(s+M)+(n+M)].real + (lln[s-r+M].real * lln[n+r+M].real - lln[s-r+M].imag * lln[n+r+M].imag)*lam[r+M];
out[N1*(s+M)+(n+M)].imag =
out[N1*(s+M)+(n+M)].imag + (lln[s-r+M].real * lln[n+r+M].imag + lln[s-r+M].imag * lln[n+r+M].real)*lam[r+M];
}
}
}
}
/* The gateway routine. */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
{
int N = (int) mxGetScalar(prhs[3]);
/* coppy array and set the output pointer to it */
plhs[0] = mxDuplicateArray(prhs[2]);
/* call the C subroutine */
myMexFunction(prhs[0], prhs[1], plhs[0], N);
return;
}
I have tried replacing, only none mex specific type (int) for system dimension N as:
mwIndex N = mxGetScalar(prhs[3]);
which didnt help either.
Finally, i call my mex file in this way:
mex myMexFunction.cpp -R2018a
Lsn=myMexFunction(lambda,ll_n,Lsn,N);
Thanks for your help.
2 Comments
OCDER
on 2 Aug 2018
"After the first run, it creates myMexFunction.mexw64, and after 5-6 times,.."
So are you using mex many times, like this?
for j = 1:100
mex myMexFunction.cpp -R2018a
Lsn=myMexFunction(lambda,ll_n,Lsn,N);
end
Accepted Answer
OCDER
on 2 Aug 2018
You should call mex once only to compile the code once. THEN, you summon the code in your loop. You don't need to call mex right before you want to use your mex function.
%Create your myMexFunction.mexw64 file ONCE. It's like creating a .m file - once.
mex myMexFunction.cpp -R2018a
When you want to use your mex function, just summon it like a normal matlab function. Compiling and Summoning should be separate things.
for j = 1:100
Lsn = myMexFunction(lambda,ll_n,Lsn,N);
end
0 Comments
More Answers (0)
See Also
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!