Maatlab crash with .mex function
3 views (last 30 days)
Show older comments
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "mat.h"
#include "mex.h"
#include "ComplexCal.h"
#include "IntrinsicUtils.h"
void mexFunction(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[])
{
uint16_t dim0, dim1, dim2;
uint32_t NumOfEle = 1;
int len_A, len_B, len_C, jj; /*can be changed to mwSize? */
int k = 1;
float *pA, *pB, *pC; /*can be changed to doube? */
mm_ps_Matrix2D_Info_t A;
mm_ps_Matrix2D_Info_t B;
mm_ps_Matrix2D_Info_t C;
pA = NULL;
pB = NULL;
pC = NULL;
pA = (float *) mxGetPr(prhs[0]);
pB = (float *) mxGetPr(prhs[1]);
dim0 = (uint16_t)(* (mxGetPr(prhs[2])));
dim1 = (uint16_t)(* (mxGetPr(prhs[3])));
dim2 = (uint16_t)(* (mxGetPr(prhs[4])));
len_A = mxGetNumberOfElements(prhs[0]);
len_B = mxGetNumberOfElements(prhs[1]);
len_C = dim0 * dim2 * 2 * NumOfEle;
if ((len_A != 2*dim0*dim1*NumOfEle) || (len_B != 2*dim1*dim2*NumOfEle))
{
mexErrMsgTxt("Input and output matrix dimension should be aligned!");
}
plhs[0] = mxCreateNumericMatrix(1, len_C , mxSINGLE_CLASS, mxREAL);
pC = mxGetPr(plhs[0]);
A.ppMatrix = (float ** )mxCalloc( dim0*dim1, sizeof(float *));
B.ppMatrix = (float ** )mxCalloc( dim1*dim2, sizeof(float *));
C.ppMatrix = (float ** )mxCalloc( dim0*dim2, sizeof(float *));
A.DimSize0 = dim0;
A.DimSize1 = dim1;
for (jj = 0; jj < dim0*dim1; jj++)
{
A.ppMatrix[jj] = &pA[jj*2*NumOfEle];
}
B.DimSize0 = dim1;
B.DimSize1 = dim2;
for (jj = 0; jj < dim1*dim2; jj++)
{
B.ppMatrix[jj] = &pB[jj*2*NumOfEle];
}
C.DimSize0 = dim0;
C.DimSize1 = dim2;
for (jj = 0; jj < dim0*dim2; jj++)
{
C.ppMatrix[jj] = &pC[jj*2*NumOfEle];
}
astri_mm_ps_matrix_cmul_2d(&A, &B, &C, NumOfEle);
} */
mxFree( (void ** ) A.ppMatrix);
mxFree( (void ** ) B.ppMatrix);
mxFree( (void ** ) C.ppMatrix);
}
5 Comments
James Tursa
on 22 Apr 2021
Edited: James Tursa
on 22 Apr 2021
Which version of MATLAB are you using? If it is R2018a or later note that MATLAB already stores interleaved complex data so you wouldn't need to make that adjustment.
Also, I can't find any online documentation for these routines, so we may have to do a bit of trial and error to figure this out.
Answers (1)
James Tursa
on 22 Apr 2021
Edited: James Tursa
on 23 Apr 2021
I had to make some assumptions about how the matrix data areas are supposed to be loaded. We can try this first to see if these assumptions are correct. This is untested, so you may have to fix some typos.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "mat.h"
#include "mex.h"
#include "ComplexCal.h"
#include "IntrinsicUtils.h"
void mexFunction(int nlhs, mxArray * plhs[], int nrhs, const mxArray * prhs[])
{
uint16_t dim0, dim1, dim2;
uint32_t NumOfEle = 1;
int len_A, len_B, len_C, jj; /*can be changed to mwSize? */
int k = 1;
float *pA, *pB, *pC; /*can be changed to doube? */
mm_ps_Matrix2D_Info_t A;
mm_ps_Matrix2D_Info_t B;
mm_ps_Matrix2D_Info_t C;
mwSize i;
/* Argument checks*/
if (nrhs != 5) mexErrMsgTxt("Need five inputs");
if (nlhs > 1) mexErrMsgTxt("Too many outputs");
if (!mxIsSingle(prhs[0]) || mxGetNumberOfDimensions(prhs[0]) != 2 ||
!mxIsSingle(prhs[1]) || mxGetNumberOfDimensions(prhs[1]) != 2)
mexErrMsgTxt("Inputs must be single 2D matrix");
if (!mxIsComplex(prhs[0]) || !mxIsComplex(prhs[1]))
mexErrMsgTxt("Inputs must be complex");
if (!mxIsNumeric(prhs[2]) || !mxIsNumeric(prhs[3]) || !mxIsNumeric(prhs[4]) ||
mxIsComplex(prhs[2]) || mxIsComplex(prhs[3]) || mxIsComplex(prhs[4]) ||
mxGetNumberOfElements(prhs[2]) != 1 || mxGetNumberOfElements(prhs[3]) != 1) ||
mxGetNumberOfElements(prhs[4]) != 1 )
mexErrMsgTxt("Dimensions must be real numeric scalars");
len_A = mxGetNumberOfElements(prhs[0]);
len_B = mxGetNumberOfElements(prhs[1]);
dim0 = mxGetScalar(prhs[2]);
dim1 = mxGetScalar(prhs[3]);
dim2 = mxGetScalar(prhs[4]);
len_C = dim0 * dim2 * 2 * NumOfEle;
if ((len_A != 2 * dim0*dim1*NumOfEle) || (len_B != 2 * dim1*dim2*NumOfEle))
{
mexErrMsgTxt("Input and output matrix dimension should be aligned!");
}
/* Load matrix headers */
/* ASSUMTION: matrix is stored in row order and ppMatrix are the row pointers */
A.DimSize0 = mxGetScalar(prhs[2]);
A.DimSize1 = mxGetScalar(prhs[3]);
A.ppMatrix = (float **)mxCalloc(A.DimSize0, sizeof(float *));
A.ppMatrix[0] = (float *)mxGetData(prhs[0]);
for (i = 1; i < A.DimSize0; i++){
A.ppMatrix[i] = A.ppMatrix[i - 1] + 2*A.DimSize1;
}
B.DimSize0 = mxGetScalar(prhs[3]);
B.DimSize1 = mxGetScalar(prhs[4]);
B.ppMatrix = (float **)mxCalloc(B.DimSize0, sizeof(float *));
B.ppMatrix[0] = (float *)mxGetData(prhs[1]);
for (i = 1; i < B.DimSize0; i++){
B.ppMatrix[i] = B.ppMatrix[i - 1] + 2*B.DimSize1;
}
plhs[0] = mxCreateNumericMatrix(1, mxGetScalar(prhs[2]) * mxGetScalar(prhs[4]) * 2 * NumOfEle, mxSINGLE_CLASS, mxREAL);
C.DimSize0 = mxGetScalar(prhs[2]);
C.DimSize1 = mxGetScalar(prhs[4]);
C.ppMatrix = (float **)mxCalloc(C.DimSize0, sizeof(float *));
C.ppMatrix[0] = (float *)mxGetData(plhs[0]);
for (i = 1; i < C.DimSize0; i++){
C.ppMatrix[i] = C.ppMatrix[i - 1] + 2*C.DimSize1;
}
/* Do the 2D complex matrix multiply */
astri_mm_ps_matrix_cmul_2d(&A, &B, &C, NumOfEle);
/* Free dynamic memory */
mxFree(A.ppMatrix);
mxFree(B.ppMatrix);
mxFree(C.ppMatrix);
}
3 Comments
James Tursa
on 23 Apr 2021
Are you sure the struct definition has float** ppMatrix; and not float* ppMatrix; ?
See Also
Categories
Find more on Matrix Indexing 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!