Mentions légales du service

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

Finish PALM4MSA 2020 GPU2 wrapper for matfaust (tested on a CBP's GPU).

Updating the CPU version to fix a missing cast for ConstraintMat.
parent 9d1bb8f9
Branches
Tags
No related merge requests found
......@@ -39,6 +39,9 @@
/* <https://hal.archives-ouvertes.fr/hal-01167948v1> */
/****************************************************************************/
#include "faust_prox.h"
#ifdef USE_GPU_MOD
#include "faust_prox_gpu.h"
#endif
#include "faust_ConstraintInt.h"
#include <iostream>
#include <cstdlib>
......
......@@ -26,6 +26,9 @@ classdef ParamsPalm4MSA < matfaust.factparams.ParamsFact
%> @param 'norm2_max_iter', real (optional) maximum number of iterations of power iteration algorithm. Used for computing 2-norm.
%> @param 'norm2_threshold', real (optional) power iteration algorithm threshold (default to 1e-6). Used for computing 2-norm.
%> @param 'grad_calc_opt_mode', int (optional) the mode used for computing the PALM gradient. It can be one value among matfaust.factparams.ParamsFact.EXTERNAL_OPT, matfaust.factparams.ParamsFact.INTERNAL_OPT or matfaust.factparams.ParamsFact.DISABLED_OPT. This
%> @param 'use_csr', bool true (by default) to prefer compressed sparse row matrix format when updating factors (only available with 2020 backend of matfaust.fact.palm4msa).
%> @param 'packing_RL', bool true (by default) to pre-compute R and L products (only available with 2020 backend of pyfaust.fact.palm4msa).
%>
%>
% =========================================================
function p = ParamsPalm4MSA(constraints, stop_crit, varargin)
......
......@@ -124,6 +124,8 @@ foreach(SCALAR_AND_FSUFFIX double:Real std::complex<double>:Cplx) # TODO: float
if(USE_GPU_MOD)
configure_file(${FAUST_MATLAB_MEX_SRC_DIR}/mexPALM4MSA2020_gpu2.cpp.in ${FAUST_MATLAB_MEX_SRC_DIR}/mexPALM4MSA2020_gpu2${FSUFFIX}.cpp @ONLY)
configure_file(${FAUST_MATLAB_DOC_SRC_DIR}/mexPALM4MSA2020_gpu2.m.in ${FAUST_MATLAB_DOC_SRC_DIR}/mexPALM4MSA2020_gpu2${FSUFFIX}.m @ONLY)
else()
file(REMOVE ${FAUST_MATLAB_MEX_SRC_DIR}/mexPALM4MSA2020_gpu2${FSUFFIX}.cpp) # delete a possible previous file generated with USE_GPU_MOD = ON (because otherwise it fails the building)
endif()
endif()
# copy the *.m for factorization now, because we have the FSUFFIX in hands
......
......@@ -104,21 +104,21 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
TransformHelper<SCALAR, Cpu>* F = new TransformHelper<SCALAR, Cpu>(), *F_lambda = nullptr;
//TODO: use_csr, packing_RL to add to ParamsPalm and parsing of matlab_params
bool packing_RL = true;
bool use_csr = true;
//TODO: the constness should be kept
std::vector<Faust::ConstraintGeneric*> noconst_cons;
for(auto cons: params->cons)
{
noconst_cons.push_back(const_cast<Faust::ConstraintGeneric*>(cons));
if(cons->is_constraint_parameter_int<SCALAR,Cpu>())
(dynamic_cast<Faust::ConstraintInt<SCALAR,Cpu>*>(*(noconst_cons.end()-1)))->Display();
else if(cons->is_constraint_parameter_real<SCALAR,Cpu>())
(dynamic_cast<Faust::ConstraintFPP<SCALAR,Cpu>*>(*(noconst_cons.end()-1)))->Display();
// if(cons->is_constraint_parameter_int<SCALAR,Cpu>())
// (dynamic_cast<Faust::ConstraintInt<SCALAR,Cpu>*>(*(noconst_cons.end()-1)))->Display();
// else if(cons->is_constraint_parameter_real<SCALAR,Cpu>())
// (dynamic_cast<Faust::ConstraintFPP<SCALAR,Cpu>*>(*(noconst_cons.end()-1)))->Display();
// else if(cons->is_constraint_parameter_mat<SCALAR,Cpu>())
// (dynamic_cast<Faust::ConstraintMat<SCALAR,Cpu>*>(*(noconst_cons.end()-1)))->Display();
//
}
palm4msa2(params->data, noconst_cons, *F, lambda, params->stop_crit, params->isUpdateWayR2L,
use_csr, packing_RL, /* compute_2norm_on_array */ false, params->norm2_threshold,
params->use_csr, params->packing_RL, /* compute_2norm_on_array */ false, params->norm2_threshold,
params->norm2_max_iter, params->isConstantStepSize, params->step_size);
Faust::MatDense<FPP2,Cpu> mat1x1Lambda = Faust::MatDense<FPP2, Cpu>(&lambda, 1, 1);
plhs[0] = FaustMat2mxArray(mat1x1Lambda);
......
......@@ -59,6 +59,7 @@
#include "faust_ConstraintGeneric.h"
#include "faust_ConstraintFPP.h"
using namespace Faust;
using namespace std;
void testCoherencePalm4MSA(const mxArray* params,std::vector<bool> & presentFields);
......@@ -67,7 +68,7 @@ typedef @FACT_FPP@ FPP2;
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
//Faust::enable_gpu_mod();
Faust::enable_gpu_mod();
#ifdef FAUST_VERBOSE
if (typeid(SCALAR) == typeid(float))
{
......@@ -98,39 +99,53 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
// ///////////// PALM4MSA LAUNCH ///////////////
try
{
// // creation des parametres
auto params = mxArray2FaustParamsPALM4MSA<SCALAR,FPP2>(prhs[0], presentFields);
// creation of parameters
auto params = mxArray2FaustParamsPALM4MSA<SCALAR,FPP2>(prhs[0], presentFields); //Cpu world
if(params->isVerbose) params->Display();
SCALAR lambda = params->init_lambda;
TransformHelper<SCALAR, GPU2>* F = new TransformHelper<SCALAR, GPU2>(), *F_lambda = nullptr;
//TODO: use_csr, packing_RL to add to ParamsPalm and parsing of matlab_params
bool packing_RL = true;
bool use_csr = true;
//TODO: the constness should be kept
std::vector<Faust::ConstraintGeneric*> noconst_cons;
for(auto cons: params->cons)
{
noconst_cons.push_back(const_cast<Faust::ConstraintGeneric*>(cons));
if(cons->is_constraint_parameter_int<SCALAR,GPU2>())
(dynamic_cast<Faust::ConstraintInt<SCALAR,GPU2>*>(*(noconst_cons.end()-1)))->Display();
{
auto c = dynamic_cast<const Faust::ConstraintInt<SCALAR,Cpu>*>(cons);
// c->Display();
noconst_cons.push_back(new Faust::ConstraintInt<SCALAR, GPU2>(c->get_constraint_type(), c->get_parameter(), c->get_rows(), c->get_cols()));
// noconst_cons[0]->Display();
}
else if(cons->is_constraint_parameter_real<SCALAR,GPU2>())
(dynamic_cast<Faust::ConstraintFPP<SCALAR,GPU2>*>(*(noconst_cons.end()-1)))->Display();
{
auto c = dynamic_cast<const Faust::ConstraintFPP<SCALAR,Cpu>*>(cons);
// c->Display();
noconst_cons.push_back(new Faust::ConstraintFPP<SCALAR, GPU2>(c->get_constraint_type(), c->get_parameter(), c->get_rows(), c->get_cols()));
// noconst_cons[0]->Display();
}
else
{
auto c = dynamic_cast<const Faust::ConstraintMat<SCALAR,Cpu>*>(cons);
// c->Display();
noconst_cons.push_back(new Faust::ConstraintMat<SCALAR, GPU2>(c->get_constraint_type(), c->get_parameter(), c->get_rows(), c->get_cols()));
// noconst_cons[0]->Display();
}
}
palm4msa2<SCALAR,GPU2>(params->data, noconst_cons, *F, lambda, params->stop_crit, params->isUpdateWayR2L,
use_csr, packing_RL, /* compute_2norm_on_array */ false, params->norm2_threshold,
auto inMat = Faust::MatDense<SCALAR,GPU2>(params->data);
Faust::palm4msa2<SCALAR,GPU2>(inMat, noconst_cons, *F, lambda, params->stop_crit, params->isUpdateWayR2L,
params->use_csr, params->packing_RL, /* compute_2norm_on_array */ false, params->norm2_threshold,
params->norm2_max_iter, params->isConstantStepSize, params->step_size);
Faust::MatDense<FPP2,GPU2> mat1x1Lambda = Faust::MatDense<FPP2, GPU2>(1, 1, &lambda, /* no_alloc*/ false, /*dev_id*/ -1, /*stream*/ nullptr);
Faust::MatDense<FPP2,Cpu> cpu_mat1x1Lambda;
plhs[0] = FaustMat2mxArray(cpu_mat1x1Lambda);
Faust::MatDense<FPP2,Cpu> mat1x1Lambda = Faust::MatDense<FPP2, Cpu>(1, 1, &lambda);
plhs[0] = FaustMat2mxArray(mat1x1Lambda);
F_lambda = F->multiply(lambda);
delete F;
F = F_lambda;
plhs[1] = convertPtr2Mat<Faust::TransformHelper<SCALAR, GPU2>>(F);
auto cpuF = F->tocpu();
plhs[1] = convertPtr2Mat<Faust::TransformHelper<SCALAR, Cpu>>(cpuF);
delete params;
}
catch (const std::exception& e)
......
......@@ -723,9 +723,7 @@ const Params<SCALAR, Cpu, FPP2>* mxArray2FaustParams(const mxArray* matlab_param
if (presentFields[INIT_FACTS])
{
mxCurrentField = mxGetField(matlab_params,0,mat_field_type2str(INIT_FACTS).c_str());
// std::cout<<"PASSERbeforeInitFact"<<std::endl;
setVectorFaustMat(init_facts,mxCurrentField);
// std::cout<<"PASSERafterInitFact"<<std::endl;
}
//verbosity
......@@ -914,7 +912,6 @@ const ParamsPalm<SCALAR,Cpu,FPP2>* mxArray2FaustParamsPALM4MSA(const mxArray* ma
{
mexErrMsgTxt("params.cons must be specified");
}
// std::cout<<"FINI_CONS"<<std::endl;
//niter1
// Faust::StoppingCriterion<SCALAR> crit1;
// if (presentFields[3])
......@@ -957,9 +954,7 @@ const ParamsPalm<SCALAR,Cpu,FPP2>* mxArray2FaustParamsPALM4MSA(const mxArray* ma
if (presentFields[4])
{
mxCurrentField = mxGetField(matlab_params,0,"init_facts");
// std::cout<<"PASSERbeforeInitFact"<<std::endl;
setVectorFaustMat(init_facts,mxCurrentField);
// std::cout<<"PASSERafterInitFact"<<std::endl;
}else
{
......@@ -1030,13 +1025,11 @@ const ParamsPalm<SCALAR,Cpu,FPP2>* mxArray2FaustParamsPALM4MSA(const mxArray* ma
{
mxCurrentField = mxGetField(matlab_params, 0, "use_csr");
use_csr = (bool) mxGetScalar(mxCurrentField);
std::cout << "mx2Faust use_csr:" << use_csr << std::endl;
}
if(presentFields[17])
{
mxCurrentField = mxGetField(matlab_params, 0, "packing_RL");
packing_RL = (bool) mxGetScalar(mxCurrentField);
std::cout << "mx2Faust packing_RL:" << packing_RL << std::endl;
}
//compute_lambda
// bool compute_lambda = true;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment