mex file crashes in loop

2 次查看(过去 30 天)
ttopal
ttopal 2018-8-2
回答: OCDER 2018-8-2
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 个评论
OCDER
OCDER 2018-8-2
"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
ttopal
ttopal 2018-8-2
Yes i am calling it many times, by calling another function in a loop which receives/calculates parameters from different functions to call mex function.

请先登录,再进行评论。

采纳的回答

OCDER
OCDER 2018-8-2
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 个)

类别

Help CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by