Can I call mex function in other mex functions

4 views (last 30 days)
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

Accepted Answer

Jan
Jan on 20 Feb 2021
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.

More Answers (0)

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!