Mentions légales du service

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

avoid memory leak due to std::vector<Faust::MatGeneric*>, solution: create...

avoid memory leak due to std::vector<Faust::MatGeneric*>, solution: create virtual destructor for abstract class Faust::MatGeneric
parent e6e6c657
Branches
No related tags found
No related merge requests found
......@@ -99,7 +99,25 @@ if(BUILD_WRAPPER_MATLAB)
endif()
####### UNIT TEST ########
foreach(TEST_FPP float double)
configure_file(${FAUST_SRC_TEST_SRC_DIR}/unit_test_faust_mult.cpp.in ${FAUST_BIN_TEST_SRC_DIR}/unit_test_faust_mult_${TEST_FPP}.cpp @ONLY)
# Generation of the binary files in double and float precision
add_executable(unit_test_faust_mult_${TEST_FPP} ${FAUST_BIN_TEST_SRC_DIR}/unit_test_faust_mult_${TEST_FPP}.cpp ${FAUST_BIN_TEST_SRC_DIR}/ )
target_link_libraries(unit_test_faust_mult_${TEST_FPP} ${FAUST_TARGET} ${MATIO_LIB_FILE} ${HDF5_LIB_FILE} ${OPENBLAS_LIB_FILE})
add_test(NAME UNIT_MULT_FAUST_${TEST_FPP} COMMAND ${FAUST_BIN_TEST_BIN_DIR}/unit_test_faust_mult_${TEST_FPP})
endforeach()
####### FIN UNIT TEST #####
......
......@@ -129,11 +129,14 @@ int main(int argc, char* argv[])
exit(EXIT_FAILURE);
}
}
int nbMultiplication = 10000;
/**** MULTIPLICATION ******/
//int nbMultiplication = 10000;
int nbMultiplication = 100;
float percentage = 0;
//multiplication
Faust::Timer t_dense;
std::cout<<std::endl<<" multiplication "<<std::endl;
std::vector<Faust::Timer> t_faust;
......@@ -141,6 +144,11 @@ int main(int argc, char* argv[])
for (int i=0;i<nbMultiplication;i++)
{
if (float(i)/float(nbMultiplication) > percentage)
{
percentage+=0.1;
std::cout<<"% "<<percentage*100<<std::endl;
}
//std::cout<<i+1<<"/"<<nbMultiplication<<std::endl;
Faust::Vect<FPP,Cpu> x(nbCol);
Faust::Vect<FPP,Cpu> y;
......
......@@ -196,6 +196,9 @@ int main(int argc, char* argv[])
Faust::Transform<FPP,Cpu> hierFactCore(list_fact_generic);
for (int i=0;i<list_fact_generic.size();i++)
delete list_fact_generic[i];
char nomFichier[100];
string outputFile="@FAUST_BIN_TEST_OUTPUT_DIR@/hier_fact_factorisation.dat";
......
/****************************************************************************/
/* Description: */
/* For more information on the FAuST Project, please visit the website */
/* of the project : <http://faust.gforge.inria.fr> */
/* */
/* License: */
/* Copyright (2016): 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> */
/****************************************************************************/
#include "faust_MatSparse.h"
#include "faust_HierarchicalFact.h"
#include "faust_Timer.h"
#include "faust_Transform.h"
#include "faust_init_from_matio_params.h"
#include "faust_init_from_matio_core.h"
#include <string>
#include <sstream>
#include "faust_BlasHandle.h"
#include "faust_SpBlasHandle.h"
#include <iostream>
#include <iomanip>
/** \brief unitary test for testing multiplication by faust
*/
typedef @TEST_FPP@ FPP;
//using namespace Faust;
int main(int argc, char* argv[])
{
if (typeid(FPP) == typeid(double))
{
cout<<"floating point precision == double"<<endl;
}
if (typeid(FPP) == typeid(float))
{
cout<<"floating point precision == float"<<endl;
}
int dim1 = 10;
int dim2 = 5;
int dim3 = 2;
int nb_factor = 3;
Faust::MatDense<FPP,Cpu> fact1(dim1,dim2);
for (int j=0;j < dim2; j ++)
{
fact1[j*dim1+j]= (FPP) 2.0;
};
vector<Faust::MatGeneric<FPP,Cpu> *> list_fact;
list_fact.resize(nb_factor);
Faust::MatDense<FPP,Cpu> fact2(dim2,dim2);
for (int i=0;i< dim2; i++)
{
for (int j=0;j< dim2; j++)
{
fact2[i*dim2+j]=(FPP) (i+1);
}
}
Faust::MatDense<FPP,Cpu> fact3(dim2,dim2);
for (int i=0;i< dim2; i++)
{
for (int j=0;j< dim2; j++)
{
fact3[i*dim2+j]=(FPP) (i+2);
}
}
std::cout<<"fact1 value"<<std::endl;
fact1.Display();
std::cout<<"fact2 value"<<std::endl;
fact2.Display();
std::cout<<"fact3 value"<<std::endl;
fact3.Display();
list_fact[0]=fact1.Clone();
list_fact[1]=fact2.Clone();
list_fact[2]=fact3.Clone();
Faust::Transform<FPP,Cpu> F(list_fact);
std::cout<<"delete list_fact"<<std::endl;
for (int i=0;i<list_fact.size();i++)
{
delete list_fact[i];
}
/*
std::cout<<"prod mult"<<std::endl;
Faust::MatDense<FPP,Cpu> F_prod=F.get_product();
/*
std::cout<<"F value "<<std::endl;
F_prod.Display();
Faust::MatDense<FPP,Cpu> X(dim2,dim3);
for (int j=0;j < dim3; j ++)
{
for (int i=0;i<dim2;i++)
X[j*dim2+i]= (FPP) (j*dim2+i);
};
std::cout<<"X value "<<std::endl;
X.Display();
Faust::MatDense<FPP,Cpu> Y;
Y = F * X;
Faust::MatDense<FPP,Cpu> Y_prod=X;
F_prod.multiply(Y_prod,'N');
std::cout<<"Y value "<<std::endl;
Y.Display();
std::cout<<"Y_prod value "<<std::endl;
Y_prod.Display();
*/
return 0;
}
......@@ -300,6 +300,11 @@ void Faust::HierarchicalFact<FPP,DEVICE>::compute_errors()
Transform_facts[i] = sp_facts[i].Clone();
Faust::Transform<FPP,DEVICE> faust_Transform_tmp(Transform_facts, get_lambda());
// delete all the dynamic memory allocated by the Clone method
for (int i=0; i < nb_factor;i++)
delete Transform_facts[i];
const Faust::MatDense<FPP,DEVICE> estimate_mat = faust_Transform_tmp.get_product(cublas_handle, cusparse_handle);
Faust::MatDense<FPP,DEVICE> data(palm_global.get_data());
......
......@@ -252,7 +252,7 @@ namespace Faust
void multiply(Faust::MatDense<FPP,Cpu> & M, char opThis) const;
//! Destructor
~MatSparse(){}
~MatSparse(){std::cout<<"destructor MatSparse"<<std::endl;/*this->mat.resize(0,0);*/}
private:
void update_dim(){this->dim1=mat.rows();this->dim2=mat.cols();nnz=mat.nonZeros();}
......
......@@ -143,7 +143,7 @@ namespace Faust
void multiplyLeft(const Transform<FPP,Cpu> & A);
void scalarMultiply(const FPP scalar);
FPP spectralNorm(const int nbr_iter_max, FPP threshold, int &flag) const;
~Transform(){}
~Transform(){for (int i=0;i<data.size();i++) delete data[i];}
/*!
......
......@@ -94,7 +94,8 @@ namespace Faust
which means dynamic type of the copy could be different from the original one <br>
-False, the return copy is simple, the dynamic type isn't changed <br> (default value is False) <br>
//! \return a pointer of MatGeneric
// \warning the dynamic type of the copy can be different from the original object
// \warning the dynamic type of the copy can be different from the original object,
// \warning dynamic allocation of the return pointer, must be delete by hand
*/
virtual MatGeneric<FPP,DEVICE>* Clone(const bool isOptimize=false) const=0;
......@@ -128,10 +129,14 @@ namespace Faust
//! \brief get the dynamic type of the matrix (SPARSE or DENSE)
virtual MatType getType() const=0;
//! \brief multply a matrix by the given scalar alpha
//! \brief multiply a matrix by the given scalar alpha
// \param alpha : multplicative scalar
virtual void operator*=(const FPP alpha)=0;
//! \brief
//! \warning : declare a virtual destructor is mandatory for an abstract class
//! in order to allow descendant class destructor to clean up in case of pointer to the abstract class
virtual ~ MatGeneric()=0;
......
......@@ -120,3 +120,25 @@ Faust::MatGeneric<FPP,Cpu>* Faust::optimize(Faust::MatDense<FPP,Cpu> const & M,F
}
template<typename FPP,Device DEVICE>
Faust::MatGeneric<FPP,DEVICE>::~MatGeneric()
{
}
template<typename FPP,Device DEVICE>
void Faust::MatGeneric<FPP,DEVICE>::multiply(Faust::Vect<FPP,DEVICE> & vec, char opThis) const
{
handleError("Faust::MatGeneric::","multiply (Vect): this function should not be called");
}
template<typename FPP,Device DEVICE>
void Faust::MatGeneric<FPP,DEVICE>::multiply(Faust::MatDense<FPP,DEVICE> & vec, char opThis) const
{
handleError("Faust::MatGeneric::","multiply (MatDense): this function should not be called");
}
......@@ -113,7 +113,8 @@ void init_faust_core_from_matvar(Faust::Transform<FPP,DEVICE>& core, matvar_t* c
{
current_spmat_var = Mat_VarGetCell(cell_var, j);
init_spmat_from_matvar(data_spmat, current_spmat_var);
core.push_back(data_spmat.Clone());
Faust::MatGeneric<FPP,DEVICE> * ptr_data_spmat = &data_spmat;
core.push_back(ptr_data_spmat);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment