Mentions légales du service

Skip to content
Snippets Groups Projects
Commit c779ccfe authored by hhakim's avatar hhakim
Browse files

Refactor mexFaust new cmd into separate module new_faust.h(pp) and optimize...

Refactor mexFaust new cmd into separate module new_faust.h(pp) and optimize the Faust creation (one useless copy per factor removed).
parent 96a44290
Branches
Tags
No related merge requests found
......@@ -63,6 +63,7 @@
#include "faust_Timer.h"
#include <complex>
#include <memory>
#include "new_faust.h"
typedef @FAUST_SCALAR@ SCALAR;
typedef @FAUST_FPP@ FPP;
......@@ -107,7 +108,8 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
std::cout<<"SCALAR == double"<<std::endl;
}
#endif
try{
try
{
// Get the command string
char cmd[256];
if (nrhs < 1 || mxGetString(prhs[0], cmd, sizeof(cmd)))
......@@ -345,66 +347,10 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
// New
if (!strcmp("new", cmd))
{
if(nlhs!=1)
mexErrMsgTxt("1 output is expected.");
if((nrhs<2) || (nrhs>4))
mexErrMsgTxt("between 1 and 3 inputs are expected.");
if(!mxIsCell(prhs[1]))
mexErrMsgTxt("1st arg input must be a cell-array");
// v1105 : Faust::Transform<SCALAR,Cpu> is no longer made of Faust::MatSparse<SCALAR,Cpu> but of pointer of abstract class Faust::MatGeneric<SCALAR,Cpu>
//old version : std::vector<Faust::MatSparse<SCALAR,Cpu> > vec_spmat;
std::vector<Faust::MatGeneric<SCALAR,Cpu>*> list_factor;
mwSize nb_element = mxGetNumberOfElements(prhs[1]);
if (nb_element != 0)
{
mxArray * mxMat;
for (int i=0; i < nb_element; i++)
{
mxMat=mxGetCell(prhs[1],i);
concatMatGeneric<SCALAR>(mxMat,list_factor);
}
}
// 2nd input (optional) : multiplicative scalar
SCALAR lambda = 1.0;
if (nrhs > 2)
lambda = (SCALAR) mxGetScalar(prhs[2]);
// 3rd input (optional) : boolean to determine the type of copy
bool optimizedCopy = false;
if (nrhs > 3)
{
double optimizedCopy_inter = mxGetScalar(prhs[3]);
if ((optimizedCopy_inter != 1.0) && (optimizedCopy_inter != 0.0))
mexErrMsgTxt("invalid boolean argument.");
optimizedCopy = (bool) optimizedCopy_inter;
}
Faust::TransformHelper<SCALAR,Cpu>* F = new Faust::TransformHelper<SCALAR,Cpu>(list_factor, lambda, optimizedCopy);
for (int i=0;i<list_factor.size();i++)
delete list_factor[i];
plhs[0]=convertPtr2Mat<Faust::TransformHelper<SCALAR,Cpu> >(F);
new_faust<SCALAR,Cpu>(prhs, nrhs, plhs, nlhs);
return;
}
// Delete
if (!strcmp("delete", cmd))
{
......@@ -910,7 +856,8 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
// Got here, so command not recognized
mexErrMsgTxt("Command not recognized.");
}catch (const std::exception& e)
}
catch (const std::exception& e)
{
mexErrMsgTxt(e.what());
}
......
/****************************************************************************/
/* Description: */
/* file where the C++ class Faust::TransformHelper is interfaced */
/* with Matlab */
/* */
/* For more information on the FAuST Project, please visit the website */
/* of the project : <http://faust.inria.fr> */
/* */
/* License: */
/* Copyright (2021): Hakim HADJ-DJILANI */
/* Nicolas Bellot, Adrien Leman, Thomas Gautrais, */
/* Luc Le Magoarou, Remi Gribonval */
/* INRIA Rennes, FRANCE */
/* http://www.inria.fr/ */
/* */
/* The FAuST Toolbox is distributed under the terms of the GNU Affero */
/* General Public License. */
/* This program is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU Affero General Public License as */
/* published by the Free Software Foundation. */
/* */
/* This program is distributed in the hope that it will be useful, but */
/* WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */
/* See the GNU Affero General Public License for more details. */
/* */
/* You should have received a copy of the GNU Affero General Public */
/* License along with this program. */
/* If not, see <http://www.gnu.org/licenses/>. */
/* */
/* Contacts: */
/* Nicolas Bellot : nicolas.bellot@inria.fr */
/* Adrien Leman : adrien.leman@inria.fr */
/* Thomas Gautrais : thomas.gautrais@inria.fr */
/* Luc Le Magoarou : luc.le-magoarou@inria.fr */
/* Remi Gribonval : remi.gribonval@inria.fr */
/* */
/* References: */
/* [1] Le Magoarou L. and Gribonval R., "Flexible multi-layer sparse */
/* approximations of matrices and applications", Journal of Selected */
/* Topics in Signal Processing, 2016. */
/* <https://hal.archives-ouvertes.fr/hal-01167948v1> */
#ifndef __MEX_NEW_FAUST__
#define __MEX_NEW_FAUST__
template <typename SCALAR, FDevice DEV>
void new_faust(const mxArray **prhs, const int nrhs, mxArray **plhs, const int nlhs);
#include "new_faust.hpp"
#endif
template <typename SCALAR, FDevice DEV>
void new_faust(const mxArray **prhs, const int nrhs, mxArray **plhs, const int nlhs)
{
if(nlhs!=1)
mexErrMsgTxt("1 output is expected.");
if((nrhs<2) || (nrhs>4))
mexErrMsgTxt("between 1 and 3 inputs are expected.");
if(!mxIsCell(prhs[1]))
mexErrMsgTxt("1st arg input must be a cell-array");
std::vector<Faust::MatGeneric<SCALAR,Cpu>*> list_factor;
mwSize nb_element = mxGetNumberOfElements(prhs[1]);
if (nb_element != 0)
{
mxArray * mxMat;
for (int i=0; i < nb_element; i++)
{
mxMat=mxGetCell(prhs[1],i);
concatMatGeneric<SCALAR>(mxMat,list_factor);
}
}
// 2nd input (optional) : multiplicative scalar
SCALAR lambda = 1.0;
if (nrhs > 2)
lambda = (SCALAR) mxGetScalar(prhs[2]);
// 3rd input (optional) : boolean to determine the type of copy
bool optimizedCopy = false;
if (nrhs > 3)
{
double optimizedCopy_inter = mxGetScalar(prhs[3]);
if ((optimizedCopy_inter != 1.0) && (optimizedCopy_inter != 0.0))
mexErrMsgTxt("invalid boolean argument.");
optimizedCopy = (bool) optimizedCopy_inter;
}
Faust::TransformHelper<SCALAR,DEV>* F = new Faust::TransformHelper<SCALAR,DEV>(list_factor, lambda, optimizedCopy, /* cloning */ false);
plhs[0]=convertPtr2Mat<Faust::TransformHelper<SCALAR,Cpu> >(F);
return;
}
......@@ -158,7 +158,7 @@ template<typename FPP>
void mxArray2FaustMat(const mxArray* Mat_array,Faust::MatDense<FPP,Cpu> & Mat)
{
int nbRow,nbCol;
if (mxIsEmpty(Mat_array))
if (mxIsEmpty(Mat_array))
{
mexErrMsgTxt("tools_mex.h:mxArray2FaustMat :input matrix is empty.");
}
......@@ -167,31 +167,26 @@ void mxArray2FaustMat(const mxArray* Mat_array,Faust::MatDense<FPP,Cpu> & Mat)
{
mexErrMsgTxt("tools_mex.h:mxArray2FaustMat :input matrix must be a 2D array.");
}
const mwSize *dimsMat;
dimsMat = mxGetDimensions(Mat_array);
const mwSize *dimsMat;
dimsMat = mxGetDimensions(Mat_array);
nbRow = (int) dimsMat[0];
nbCol = (int) dimsMat[1];
if ((nbRow == 0) || (nbCol == 0))
mexErrMsgIdAndTxt("tools_mex.h:mxArray2FaustMat", "empty matrix");
if (mxIsSparse(Mat_array))
{
//mexErrMsgTxt("sparse matrix entry instead of dense matrix");
mexErrMsgIdAndTxt("a","a sparse matrix entry instead of dense matrix");
}
Mat.resize(nbRow,nbCol);
FPP* MatPtr;
mxArray2Ptr(Mat_array,MatPtr);
memcpy(Mat.getData(),MatPtr,nbRow*nbCol*sizeof(FPP));
if(MatPtr) {delete [] MatPtr ; MatPtr = NULL;}
nbCol = (int) dimsMat[1];
if ((nbRow == 0) || (nbCol == 0))
mexErrMsgIdAndTxt("tools_mex.h:mxArray2FaustMat", "empty matrix");
if (mxIsSparse(Mat_array))
{
//mexErrMsgTxt("sparse matrix entry instead of dense matrix");
mexErrMsgIdAndTxt("a","a sparse matrix entry instead of dense matrix");
}
Mat.resize(nbRow,nbCol);
FPP* MatPtr;
mxArray2Ptr(Mat_array,MatPtr);
memcpy(Mat.getData(),MatPtr,nbRow*nbCol*sizeof(FPP));
if(MatPtr) {delete [] MatPtr ; MatPtr = NULL;}
}
......@@ -433,32 +428,29 @@ void concatMatGeneric(const mxArray * mxMat,std::vector<Faust::MatGeneric<FPP,Cp
{
if (mxMat == NULL)
mexErrMsgTxt("concatMatGeneric : empty matlab matrix");
Faust::MatGeneric<FPP,Cpu> * M;
mexErrMsgTxt("concatMatGeneric : empty matlab matrix");
Faust::MatGeneric<FPP,Cpu> * M = nullptr;
Faust::MatDense<FPP,Cpu> * dM = nullptr;
Faust::MatSparse<FPP,Cpu> * sM = nullptr;
if (!mxIsSparse(mxMat))
{
Faust::MatDense<FPP,Cpu> denseM;
mxArray2FaustMat(mxMat,denseM);
M=denseM.Clone();
}else
{
M = dM = new Faust::MatDense<FPP,Cpu>();
mxArray2FaustMat(mxMat, *dM);
}
else
{
Faust::MatSparse<FPP,Cpu> spM;
mxArray2FaustspMat(mxMat,spM);
M=spM.Clone();
M = sM = new Faust::MatSparse<FPP,Cpu>();
mxArray2FaustspMat(mxMat, *sM);
}
list_mat.push_back(M);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment