Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 98a8800d authored by Nicolas Bellot's avatar Nicolas Bellot Committed by hhakim
Browse files

wrapper/matlab nettoyage et documentation

parent d95f9170
Branches
Tags
No related merge requests found
ROOT_DIR=pwd;
PALM_DIR = [ROOT_DIR filesep '..' filesep 'palm4MSA'];
MAT_DIR = [ROOT_DIR filesep '..' filesep 'matrix'];
FAUST_CORE_DIR = [ROOT_DIR filesep '..' filesep 'faust_core'];
EIGEN_DIR = getenv('EIGEN_ROOT_DIR');
TOOLS_DIR = 'tools';
PALM_FILE = dir([PALM_DIR filesep '*.cpp']);
MAT_FILE = dir([MAT_DIR filesep '*.cpp']);
FAUST_CORE_FILE = dir([FAUST_CORE_DIR filesep '*.cpp']);
TOOLS_FILE = dir([TOOLS_DIR filesep '*.cpp']);
PALM_SRC= cell(0,0);
MAT_SRC = cell(1,length(MAT_FILE)-1);
FAUST_CORE_SRC = cell(0,0);
for i=1:length(PALM_FILE)
PALM_SRC = [PALM_SRC , [PALM_DIR filesep PALM_FILE(i).name]];
end
cpt = 1;
for i=1:length(MAT_FILE)
if (strcmp(MAT_FILE(i).name,'faust_timer.cpp'))
else
MAT_SRC{cpt} = [MAT_DIR filesep MAT_FILE(i).name];
cpt = cpt + 1;
end
end
for i=1:length(FAUST_CORE_FILE)
FAUST_CORE_SRC = [FAUST_CORE_SRC , [FAUST_CORE_DIR filesep FAUST_CORE_FILE(i).name]];
end
for i=1:length(TOOLS_FILE)
TOOLS_SRC{i} = [TOOLS_DIR filesep TOOLS_FILE(i).name];
end
S1 = ['mex_functions' filesep 'mexHierarchical_fact.cpp'];
SRC_FILE = [S1,PALM_SRC,MAT_SRC,FAUST_CORE_SRC,TOOLS_SRC];
INCLUDE = {['-I' EIGEN_DIR],['-I' MAT_DIR],['-I' FAUST_CORE_DIR],['-I' PALM_DIR],['-I' TOOLS_DIR]};
OTHER_OPT = {'-v'};
INPUT_MEX = [INCLUDE,OTHER_OPT,SRC_FILE];
mex(INPUT_MEX{:});
......@@ -43,27 +43,15 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
if(nlhs!=1)
mexErrMsgTxt("1 output is expected.");
if(nrhs!=2)
mexErrMsgTxt("2 inputs are expected.");
if((nrhs<2) || (nrhs>3))
mexErrMsgTxt("1 or 2 inputs are expected.");
if(!mxIsCell(prhs[1]))
mexErrMsgTxt("input must be a cell-array");
mexErrMsgTxt("1st arg input must be a cell-array");
std::vector<Faust::MatSparse<FFPP,Cpu> > vec_spmat;
mwSize nb_element = mxGetNumberOfElements(prhs[1]);
/*if (nb_element == 0)
mexWarnMsgTxt("Empty cell array.");
else if (!mxIsSparse(mxGetCell(prhs[1],0)))
{
//mexPrintf("Dense\n");
loadDenseFaust(prhs[1],vec_spmat);
}
else
{
//mexPrintf("Sparse\n");
loadSpFaust(prhs[1],vec_spmat);
}*/
if (nb_element == 0)
mexWarnMsgTxt("Empty cell array.");
else
......@@ -75,9 +63,12 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
addSpmat<FFPP>(mxMat,vec_spmat);
}
}
FFPP lambda = 1.0;
if (nrhs > 2)
lambda = (FFPP) mxGetScalar(prhs[2]);
Faust::Transform<FFPP,Cpu>* F = new Faust::Transform<FFPP,Cpu>(vec_spmat);
Faust::Transform<FFPP,Cpu>* F = new Faust::Transform<FFPP,Cpu>(vec_spmat,lambda);
plhs[0]=convertPtr2Mat<Faust::Transform<FFPP,Cpu> >(F);
return;
......
#include "mex.h"
#include <vector>
#include <string>
#include <algorithm>
#include "faust_MatDense.h"
#include "faust_MatSparse.h"
#include "faust_Transform.h"
#include "faust_constant.h"
#include "tools_mex.h"
#include "class_handle.hpp"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
/* int nnzMax = mxGetNzmax(prhs[0]);
int nbCol = mxGetN(prhs[0]);
int nbRow = mxGetM(prhs[0]);
mexPrintf("DIM (%d,%d) NNZMAX : %d\n",nbRow,nbCol,nnzMax);
size_t* jc,*ir;
double* pr;
jc = (size_t *) mxCalloc(nbCol+1,sizeof(size_t));
jc = (size_t *)mxGetJc(prhs[0]);
ir = (size_t *) mxCalloc(nnzMax,sizeof(size_t));
ir = (size_t *) mxGetIr(prhs[0]);
pr = (double *) mxCalloc(nnzMax,sizeof(double));
pr = (double *) mxGetPr(prhs[0]);
for (int i=0;i<nnzMax;i++)
{
mexPrintf("Id_row : %d Value %f\n",ir[i],pr[i]);
}
for (int i=0;i<(nbCol+1);i++)
{
mexPrintf("Col_ptr: %d\n",jc[i]);
}
Faust::MatSparse S(nnzMax,nbRow,nbCol,(double *)pr,(int *)ir,(int *)jc); */
//mexPrintf("abc\n");
if(nlhs!=1)
mexErrMsgTxt("mexLoadFaust must have 1 output.");
if(nrhs!=1)
mexErrMsgTxt("mexLoadFaust must have 1 input.");
//int nbRow,nbCol;
if(!mxIsCell(prhs[0]))
{
mexErrMsgTxt("input must be a cell-array");
}
std::vector<Faust::MatSparse<FFPP,Cpu> > vec_spmat;
mwSize nb_element = mxGetNumberOfElements(prhs[0]);
if (nb_element == 0)
mexWarnMsgTxt("Empty cell array.");
else
{
mxArray * mxMat;
for (mwSize i=0;i<nb_element;i++)
{
mxMat=mxGetCell(prhs[0],i);
addSpmat<FFPP>(mxMat,vec_spmat);
}
}
Faust::Transform<FFPP,Cpu>* F = new Faust::Transform<FFPP,Cpu>(vec_spmat);
plhs[0]=convertPtr2Mat<Faust::Transform<FFPP,Cpu> >(F);
}
clear all;
close all;
addpath('../build/mex')
% cd '../../Code_Luc';
% set_path;
% cd '../devcpp/interface_matlab/';
DIM1 = 5;
DIM2 = 2;
data = zeros(DIM1,DIM2);
data(:)=1:DIM1*DIM2;
params.data=data;
params.nfacts = 3;
params.cons{1,1} = {'sp',3,DIM1,DIM1};%;%{'splin',P_REP_tab(i),N_SAMP,DIM};%params.cons{1,1} = %
params.cons{2,1} = {'splin',1,DIM1,DIM2};
params.cons{1,2} = {'spcol',3,DIM1,DIM2};%;%{'splin',P_REP_tab(i),N_SAMP,DIM};%params.cons{1,1} = %
params.cons{2,2} = {'normcol',6,DIM2,DIM2};
params.niter1 = 100;
params.niter2 = 751;
[mexlambda,mexfact]=mexHierarchical_fact(params);
disp(['MEX LAMBDA ' num2str(mexlambda)]);
[lambda,fact]=hierarchical_fact(params);
disp([' LAMBDA ' num2str(lambda) ' MEX_LAMBDA ' num2str(mexlambda)]);
sum_coeff = 0;
threshold = 0.00001;
disp('\n\n COMPARAISON ');
for i=1:params.nfacts
comp=abs(mexfact{i} - fact{i})> threshold;
nb_coeff_diff = sum(comp(:));
disp([int2str(i) ' nb_coeff diff : ' int2str(nb_coeff_diff)]);
sum_coeff = nb_coeff_diff + sum_coeff;
end
disp([' nb coeff different ' int2str(sum_coeff)]);
mex_error = norm(params.data- lambda*dvp(fact));
error = norm(params.data - mexlambda*dvp(mexfact));
\ No newline at end of file
clear all;
close all;
addpath('../build/mex')
cd '../../Code_Luc'
set_path;
cd '../devcpp/interface_matlab/';
DIM1 = 5;
DIM2 = 2;
data = zeros(DIM1,DIM2);
data(:)=1:DIM1*DIM2;
params.data=data;
params.nfacts = 3;
params.cons{1,1} = {'sp',3,DIM1,DIM1};
params.cons{1,2} = {'sp',4,DIM1,DIM2};
params.cons{1,3} = {'sp',5,DIM2,DIM2};
params.niter = 400;
init_facts=cell(1,params.nfacts);
for i=1:params.nfacts
init_facts{i}=eye(params.cons{i}{3},params.cons{i}{4});
end
params.init_facts=init_facts
[mexlambda,mexfact]=mexPalm4MSA(params);
disp(['MEX LAMBDA ' num2str(mexlambda)]);
[lambda,fact]=palm4MSA(params);
disp([' LAMBDA ' num2str(lambda)]);
sum_coeff = 0;
threshold = 0.0000001;
for i=1:params.nfacts
comp=abs(mexfact{i} - fact{i})> threshold;
nb_coeff_diff = sum(comp(:));
sum_coeff = nb_coeff_diff + sum_coeff;
end
disp([' nb coeff different ' int2str(sum_coeff)]);
\ No newline at end of file
% nbRow = 5;
% nbCol = 10;
% density = 0.3;
clear all;
close all;
%
% S=sprand(nbRow,nbCol,density);
%
addpath('../build/mex');
addpath('tools/');
% getenv('LD_LIBRARY_PATH')
%setenv('LD_LIBRARY_PATH',[getenv('')])
S{1}=2*eye(3,5);
S{2}=3*eye(5,7);
S{3}=randint(7,7);
% Sparrow = zeros(7,3);
% Sparrow(2,1) = 1;
% Sparrow(5,1) = 1;
% Sparrow(3,2) = 1;
% Sparrow(2,3) = 2;
% Sparrow(5,3) = 1;
% Sparrow(6,3) = 1;
% Sparrow = sparse(Sparrow);
% S=Sparrow;
% S=sparse(S);
fc=matlab_faust(S);
PROD=S{1};
for i=1:length(S)
S{i}=sparse(S{i});
end
for i=2:length(S)
PROD=PROD*S{i};
end
disp('test vectoriel');
x=ones(size(S{end},2),1);
y=fc*x;
y
ybis=PROD*x;
ybis
disp('test matriciel');
x=ones(size(S{end},2),5);
y=fc*x;
y
ybis=PROD*x;
ybis
% Si k>1, DIMS(k) correspond au nombre de colonnes de facts{k-1}
% Si k<length(DIMS), DIMS(k) correspond au nombre de lignes de facts{k}
%DIMS = [8193 204 204 204 204 204 204 ]; % param MEG
DIMS = [500 32 32 32 32 ];
% DENSITY(k) correspond a la densite de facts{k}
%DENSITY = [0.0735 0.0392 0.0392 0.0392 0.0392 0.5734]; % param MEG
%DENSITY = [0.1562 0.0938 0.0938 0.3552];
DENSITY = [0.5 0.5 0.5 0.5];
%addpath('/home/tgautrai/faust/trunk/devcpp/output/mex')
addpath('../build/mex')
addpath('../build/testing/matlab_scripts');
if(length(DENSITY) ~= length(DIMS)-1)
error('Lengths of array DIMS or DENSITY don''t match');
end
%cd ([getenv('FAUST_ROOT_DIR') '/trunk/Code_Luc']);
%set_path;
%cd ([getenv('FAUST_ROOT_DIR') '/trunk/devcpp/interface_matlab'] );
%fid=fopen('temps_mult_mat.dat','w');
NB_RUN = 500000;
t=zeros(NB_RUN,1);
for l=1:NB_RUN
facts_sparse = cell(1,(length(DENSITY)));
facts = cell(size(facts_sparse));
for k=1:length(facts_sparse)
facts_sparse{k} = sprand(DIMS(k), DIMS(k+1),DENSITY(k));
facts{k} = full(facts_sparse{k});
end
%for k=1:length(facts),facts{k}=single(facts{k});end
v=rand(size(facts{end},2),1);
tic;
%objectHandle = mexLoadFaust(facts);
fc = matlab_faust(facts_sparse);
temps_creation_faust = toc;
%fprintf('temps creation objet faust : %g s\n\n',temps_creation_faust);
tic;
%w_faust_mex=faust_mex('multiply', objectHandle, v);
w_faust_mex = fc*v; % surcharge de l'operateur * de la cltopass matlab_faust
t_faust_mex = toc;
%fprintf('temps multiplication avec faust mex : %g s\n',t_faust_mex);
%fprintf(fid,'%e ',t_faust_mex);
for k=1:length(facts),facts_sparse{k}=sparse(facts_sparse{k});end
w_faust_matlab=v;
tic;
for k=length(facts):-1:1, w_faust_matlab=facts_sparse{k}*w_faust_matlab;end
t_faust_matlab = toc;
%fprintf('temps multiplication avec faust matlab : %g s\n',t_faust_matlab);
%fprintf(fid,'%e\n',t_faust_matlab);
% M = dvp(facts);
% tic
% w_dense_matlab=M*v;
% t_dense_matlab = toc;
% fprintf('temps multiplication avec dense matlab : %g s\n\n',t_dense_matlab);
delete(fc);
clear v;
%fprintf('erreur relative max faust_mex - faust_matlab : %g \n',max(max(abs((w_faust_matlab-w_faust_mex)./w_faust_matlab))));
%fprintf('erreur relative max faust_mex - dense_matlab : %g \n\n',max(max(abs((w_dense_matlab-w_faust_mex)./w_dense_matlab))));
%fprintf('rapport temps faust matlab / temps faust mex : %g \n\n',t_faust_matlab/t_faust_mex);
%fprintf('rapport temps dense matlab / temps faust mex : %g \n',t_dense_matlab/t_faust_mex);
t(l)=t_faust_matlab/t_faust_mex;
end
%fclose(fid);
hise=1;
// file useful from wrapping an underlying C++ class into matlab
// file under BSD License downloaded from
// https://www.mathworks.com/matlabcentral/fileexchange/38964-example-matlab-class-wrapper-for-a-c++-class
#ifndef __CLASS_HANDLE_HPP__
#define __CLASS_HANDLE_HPP__
#include "mex.h"
#include <string>
#include <cstring>
#include <typeinfo>
//#include <stdint.h>
#ifndef __MACH__
......
%CLASS_INTERFACE Example MATLAB class wrapper to an underlying C++ class
%class MATLAB_FAUST
% representing a given dense matrix by a product of sparse matrix (i.e faust)
% in order to speed-up multiplication by this matrix,
% matlab wrapper class implemented in C++
%
% For more information on the FAuST Project, please visit the website of
% the project : <http://faust.gforge.inria.fr>
classdef matlab_faust < handle
properties (SetAccess = private, Hidden = true)
objectHandle; % Handle to the underlying C++ class instance
......@@ -6,27 +13,33 @@ classdef matlab_faust < handle
methods
%% Constructor - Create a new C++ class instance
function this = matlab_faust(varargin)
% Constructor - build a faust from a cell array of matrix and a scalar (optional)
% 1st input : 1D cell array of matrix (sparse or dense)
% 2nd input : (optional) multiplicative scalar
this.objectHandle = mexFaust('new',varargin{:});
%this.objectHandle = mexLoadFaust(varargin{:});
end
%% Destructor - Destroy the C++ class instance
function delete(this)
% destructor delete the faust
mexFaust('delete', this.objectHandle);
end
%% Multiplication faust-vector or faust-matrix
function varargout = mtimes(this, varargin)
% mtimes - overloading of the matlab multiplication (*) function, compatible with matlab matrix and vector
[varargout{1:nargout}] = mexFaust('multiply', this.objectHandle, varargin{:});
end
%% Evaluate the product of a faust_core
function varargout = get_product(this)
% get_product - compute the dense matrix equivalent to the faust (the product of sparse matrix)
[varargout{1:nargout}]=mexFaust('get_product',this.objectHandle);
end
%% Transpose operator
function trans=transpose(this)
if (nargout == 0)
%transpose - overloading of the matlab transpose operator (')
if (nargout == 0)
mexFaust('transpose',this.objectHandle);
else
trans = matlab_faust({});
......@@ -44,14 +57,10 @@ classdef matlab_faust < handle
end
end
function res=solve(this,varargin)
[varargout{1:nargout}] = mexFaust('solve', this.objectHandle, varargin{:});
end
%% Size
function Size=size(this,varargin);
if (nargin == 1)
%size - overload of the matlab size function
if (nargin == 1)
Size=mexFaust('size',this.objectHandle);
else (nargin == 2)
Size=mexFaust('size',this.objectHandle,varargin);
......@@ -59,4 +68,4 @@ classdef matlab_faust < handle
end
end
end
\ No newline at end of file
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment