필터 지우기
필터 지우기

Can I call mex function in other mex functions

조회 수: 7 (최근 30일)
Chang seok Ma
Chang seok Ma 2021년 2월 20일
답변: Jan 2021년 2월 20일
Hello,
I am trying to call mex function in other mex functions as below.
I need matrix multiplication but I realized I have to define it before I use it inside mexfunction.
Define 'matrixmultiplication.c' first and then use matrix multiplication inside 'calculation.c'
But I don't think this works.
Is there anyway I can call mexfunction in other mex functions? Or should I make a joint mex function of these two?
Any help would be appreciated
Thanks in advance
1. matrixmultiplication.c (I just downloaded from Matlab example file)
#if !defined(_WIN32)
#define dgemm dgemm_
#endif
#include "mex.h"
#include "blas.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double *A, *B, *C; /* pointers to input & output matrices*/
size_t m,n,p; /* matrix dimensions */
/* form of op(A) & op(B) to use in matrix multiplication */
char *chn = "N";
/* scalar values to use in dgemm */
double one = 1.0, zero = 0.0;
A = mxGetPr(prhs[0]); /* first input matrix */
B = mxGetPr(prhs[1]); /* second input matrix */
/* dimensions of input matrices */
m = mxGetM(prhs[0]);
p = mxGetN(prhs[0]);
n = mxGetN(prhs[1]);
if (p != mxGetM(prhs[1])) {
mexErrMsgIdAndTxt("MATLAB:matrixMultiply:matchdims",
"Inner dimensions of matrix multiply do not match.");
}
/* create output matrix C */
plhs[0] = mxCreateDoubleMatrix(m, n, mxREAL);
C = mxGetPr(plhs[0]);
/* Pass arguments to Fortran by reference */
dgemm(chn, chn, &m, &n, &p, &one, A, &m, B, &p, &zero, C, &m);
}
2. calculation.c (Version for practice)
#if !defined(_WIN32)
#define dgemm dgemm_
#endif
#include "mex.h"
#include "blas.h"
void secondfn(double *As, double *Bs, int m, int n, int p, double *Ds, double *Ps){
Ds = matrixmultiplication(As,Bs); // HERE I USED matrixmultiplication.c !!!!!!!
for (size_t i = 0; i < m*n; i++){
Ps[i] = Ds[i];
}
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double *A, *B, *D, *P; /* pointers to input & output matrices*/
size_t m,n,p; /* matrix dimensions */
A = mxGetPr(prhs[0]); /* first input matrix */
B = mxGetPr(prhs[1]); /* second input matrix */
m = mxGetM(prhs[0]); /* dimensions of input matrices */
p = mxGetN(prhs[0]);
n = mxGetN(prhs[1]);
plhs[0] = mxCreateDoubleMatrix(m, n, mxREAL);
P = mxGetPr(plhs[0]);
D = (double *) mxMalloc(m*n*sizeof(*D));
secondfn(A, B, m, n, p, D, P);
mxFree(D);
}
mex matrixmultiplication.c calculation.c -lmwblas

채택된 답변

Jan
Jan 2021년 2월 20일
The easiest and most efficient solution would be to avoid the overhead of calling a MEX function, but to embed the needed code directly in the other mex function.
But it is possible to call another MEX function by mexCallMATLAB. There is an undocumented function called "mexRunMexFile" also, but as usual for undocumented commands: This is fragile.

추가 답변 (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