Mentions légales du service

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

Change the mat product optimization boolean of the previous commit to a int in...

Change the mat product optimization boolean of the previous commit to a int in order to be able to choose one of the 3 optimization methods available and rename these methods.
parent 97bf52a1
No related branches found
No related tags found
No related merge requests found
......@@ -201,7 +201,7 @@ template<typename FPP,Device DEVICE,typename FPP2>
void Faust::Palm4MSA<FPP,DEVICE,FPP2>::compute_grad_over_c_ext_opt()
{
//#define mul_3_facts multiply_order_opt
#define mul_3_facts multiply_order_opt_ext // this one only optimize the product on factor ends but for three factors it doesn't change anything comparing to multiply_order_opt
#define mul_3_facts multiply_order_opt_all_ends// this one only optimize the product on factor ends but for three factors it doesn't change anything comparing to multiply_order_opt
// compute error = m_lambda*L*S*R-data
error = data;
std::vector<MatDense<FPP,DEVICE>*> facts;
......
......@@ -73,7 +73,7 @@ namespace Faust {
bool is_sliced;
Slice slices[2];
bool is_fancy_indexed;
bool enable_mul_order_opt;
int mul_order_opt_mode;
faust_unsigned_int * fancy_indices[2];
faust_unsigned_int fancy_num_rows;
faust_unsigned_int fancy_num_cols;
......@@ -94,7 +94,7 @@ namespace Faust {
Vect<FPP,Cpu> multiply(const Vect<FPP,Cpu> x, const bool transpose=false, const bool conjugate=false);
// MatDense<FPP,Cpu> multiply(const MatDense<FPP,Cpu> A) const;
MatDense<FPP, Cpu> multiply(const MatDense<FPP,Cpu> A, const bool transpose=false, const bool conjugate=false);
void set_enable_mul_order_opt(const bool enable_mul_order_opt);
void set_mul_order_opt_mode(const int mul_order_opt_mode);
MatDense<FPP, Cpu> multiply(const MatSparse<FPP,Cpu> A, const bool transpose=false, const bool conjugate=false);
TransformHelper<FPP, Cpu>* multiply(TransformHelper<FPP, Cpu>*);
......
......@@ -70,7 +70,7 @@ namespace Faust {
}
template<typename FPP>
TransformHelper<FPP,Cpu>::TransformHelper() : is_transposed(false), is_conjugate(false), is_sliced(false), is_fancy_indexed(false), enable_mul_order_opt(false)
TransformHelper<FPP,Cpu>::TransformHelper() : is_transposed(false), is_conjugate(false), is_sliced(false), is_fancy_indexed(false), mul_order_opt_mode(0)
{
this->transform = make_shared<Transform<FPP,Cpu>>();
}
......@@ -102,7 +102,7 @@ namespace Faust {
if(th->is_sliced)
copy_slices(th);
this->is_conjugate = conjugate?!th->is_conjugate:th->is_conjugate;
this->enable_mul_order_opt = th->enable_mul_order_opt;
this->mul_order_opt_mode = th->mul_order_opt_mode;
}
template<typename FPP>
......@@ -114,7 +114,7 @@ namespace Faust {
this->is_sliced = th->is_sliced;
if(th->is_sliced)
copy_slices(th);
this->enable_mul_order_opt = th->enable_mul_order_opt;
this->mul_order_opt_mode = th->mul_order_opt_mode;
}
template<typename FPP>
......@@ -130,7 +130,7 @@ namespace Faust {
this->slices[1] = s[1];
this->is_sliced = true;
eval_sliced_Transform();
this->enable_mul_order_opt = th->enable_mul_order_opt;
this->mul_order_opt_mode = th->mul_order_opt_mode;
}
template<typename FPP>
......@@ -160,7 +160,7 @@ namespace Faust {
eval_fancy_idx_Transform();
delete[] this->fancy_indices[0];
delete[] this->fancy_indices[1];
this->enable_mul_order_opt = th->enable_mul_order_opt;
this->mul_order_opt_mode = th->mul_order_opt_mode;
}
template<typename FPP>
......@@ -193,10 +193,10 @@ namespace Faust {
is_transposed ^= transpose;
is_conjugate ^= conjugate;
Faust::MatDense<FPP,Cpu> M;
if(this->enable_mul_order_opt)
if(this->mul_order_opt_mode)
{
this->transform->data.push_back(&A);
Faust::multiply_order_opt(this->transform->data, M);
Faust::multiply_order_opt(mul_order_opt_mode, this->transform->data, M);
this->transform->data.erase(this->transform->data.end()-1);
}
else
......@@ -207,10 +207,10 @@ namespace Faust {
}
template<typename FPP>
void TransformHelper<FPP,Cpu>::set_enable_mul_order_opt(const bool enable_mul_order_opt)
void TransformHelper<FPP,Cpu>::set_mul_order_opt_mode(const int mul_order_opt_mode)
{
this->enable_mul_order_opt = enable_mul_order_opt;
std::cout << "mul order opt enabled: " << this->enable_mul_order_opt << std::endl;
this->mul_order_opt_mode = mul_order_opt_mode;
std::cout << "mul order opt mode (0 when disabled): " << this->mul_order_opt_mode << std::endl;
}
template<typename FPP>
......
......@@ -168,11 +168,13 @@ namespace Faust
template<typename FPP>
FPP fabs(std::complex<FPP> c);
template<typename FPP, Device DEVICE>
void multiply_order_opt_ext(std::vector<Faust::MatDense<FPP,DEVICE>*>& facts, Faust::MatDense<FPP,DEVICE>& out, FPP alpha=1.0, FPP beta_out=.0, std::vector<char> transconj_flags = std::vector<char>({'N'}));
void multiply_order_opt_all_ends(std::vector<Faust::MatDense<FPP,DEVICE>*>& facts, Faust::MatDense<FPP,DEVICE>& out, FPP alpha=1.0, FPP beta_out=.0, std::vector<char> transconj_flags = std::vector<char>({'N'}));
template<typename FPP, Device DEVICE>
void multiply_order_opt(std::vector<Faust::MatDense<FPP,DEVICE>*>& facts, Faust::MatDense<FPP,DEVICE>& out, FPP alpha=1.0, FPP beta_out=.0, std::vector<char> transconj_flags = std::vector<char>({'N'}));
void multiply_order_opt_all_best(std::vector<Faust::MatDense<FPP,DEVICE>*>& facts, Faust::MatDense<FPP,DEVICE>& out, FPP alpha=1.0, FPP beta_out=.0, std::vector<char> transconj_flags = std::vector<char>({'N'}));
template<typename FPP, Device DEVICE>
void multiply_order_opt(std::vector<Faust::MatGeneric<FPP,DEVICE>*>& facts, Faust::MatDense<FPP,DEVICE>& out, FPP alpha=1.0, FPP beta_out=.0, std::vector<char> transconj_flags = std::vector<char>({'N'}));
void multiply_order_opt_first_best(std::vector<Faust::MatDense<FPP,DEVICE>*>& facts, Faust::MatDense<FPP,DEVICE>& out, FPP alpha=1.0, FPP beta_out=.0, std::vector<char> transconj_flags = std::vector<char>({'N'}));
template<typename FPP, Device DEVICE>
void multiply_order_opt(const int mode, std::vector<Faust::MatGeneric<FPP,DEVICE>*>& facts, Faust::MatDense<FPP,DEVICE>& out, FPP alpha=1.0, FPP beta_out=.0, std::vector<char> transconj_flags = std::vector<char>({'N'}));
}
......
......@@ -42,7 +42,7 @@
#define __FAUST_LINALGEBRA_HPP
#include <iostream>
#include <stdexcept>
#include "faust_LinearOperator.h"
#include "faust_MatGeneric.h"
#include "faust_MatDense.h"
......@@ -733,7 +733,7 @@ FPP Faust::fabs(std::complex<FPP> c)
* \note: the std::vector facts is altered after the call! Don't reuse it.
*/
template<typename FPP, Device DEVICE>
void Faust::multiply_order_opt_ext(std::vector<Faust::MatDense<FPP,DEVICE>*>& facts, Faust::MatDense<FPP,DEVICE>& out, FPP alpha/* =1.0*/, FPP beta_out/*=.0*/, std::vector<char> transconj_flags /* = {'N'}*/)
void Faust::multiply_order_opt_all_ends(std::vector<Faust::MatDense<FPP,DEVICE>*>& facts, Faust::MatDense<FPP,DEVICE>& out, FPP alpha/* =1.0*/, FPP beta_out/*=.0*/, std::vector<char> transconj_flags /* = {'N'}*/)
{
Faust::MatDense<FPP, DEVICE> tmpr, tmpl;
int nfacts = facts.size();
......@@ -772,77 +772,77 @@ void Faust::multiply_order_opt_ext(std::vector<Faust::MatDense<FPP,DEVICE>*>& fa
facts.erase(facts.begin(), facts.end());
}
//template<typename FPP, Device DEVICE>
//void Faust::multiply_order_opt(std::vector<Faust::MatDense<FPP,DEVICE>*>& facts, Faust::MatDense<FPP,DEVICE>& out, FPP alpha/* =1.0*/, FPP beta_out/*=.0*/, std::vector<char> transconj_flags /* = {'N'}*/)
//{
// std::vector<Faust::MatDense<FPP,DEVICE>*> tmp_facts; //temporary product results
// Faust::MatDense<FPP, DEVICE>* tmp;
// int nfacts = facts.size();
// Faust::MatDense<FPP,DEVICE> *Si, *Sj;
// std::vector<int> complexity(nfacts-1);
// for(int i = 0; i <nfacts-1; i++)
// {
// Si = facts[i];
// Sj = facts[i+1];
// complexity[i] = Si->getNbRow() * Si->getNbCol() * Sj->getNbCol();
// }
// int idx; // marks the factor to update with a product of contiguous factors
// bool multiplying_tmp_factor = false; // allows to avoid to allocate uselessly a tmp factor if Si or Sj are already tmp factors
// while(facts.size() > 2)
// {
// // find the least complex product facts[idx]*facts[idx+1]
// idx = distance(complexity.begin(), min_element(complexity.begin(), complexity.end()));
// Si = facts[idx];
// Sj = facts[idx+1];
// for(auto Tit = tmp_facts.begin(); Tit != tmp_facts.end(); Tit++)
// {
// if(Sj == *Tit)
// {// Sj is original fact
// multiplying_tmp_factor = true;
// tmp = Sj;
// break;
// }
// else if(Si == *Tit)
// {
// multiplying_tmp_factor = true;
// tmp = Si;
// break;
// }
// }
// if(! multiplying_tmp_factor)
// {
// tmp = new Faust::MatDense<FPP, DEVICE>();
// tmp_facts.push_back(tmp);
// }
// //else no need to instantiate a new tmp, erasing Sj which is a tmp
// gemm(*Si, *Sj, *tmp, (FPP)1.0, (FPP)0.0, transconj_flags[transconj_flags.size()>idx?idx:0], transconj_flags[transconj_flags.size()>idx+1?idx+1:0]);
// facts.erase(facts.begin()+idx+1);
// complexity.erase(complexity.begin()+idx); //complexity size == facts size - 1
// facts[idx] = tmp;
// if(transconj_flags.size() > idx)
// transconj_flags[idx] = 'N';
// // update complexity around the new factor
// if(facts.size() > 2)
// {
// if(idx > 0)
// complexity[idx-1] = facts[idx-1]->getNbRow() * facts[idx-1]->getNbCol() * facts[idx]->getNbCol();
// if(idx < facts.size()-1)
// complexity[idx] = facts[idx]->getNbRow() * facts[idx]->getNbCol() * facts[idx+1]->getNbCol();
// }
// multiplying_tmp_factor = false;
// }
// // last mul
// gemm(*facts[0], *facts[1], out, alpha, beta_out, transconj_flags[0], transconj_flags.size()>1?transconj_flags[1]:'N');
// facts.erase(facts.begin(), facts.end());
// // delete all tmp facts
// for(auto Tit = tmp_facts.begin(); Tit != tmp_facts.end(); Tit++)
// {
// delete *Tit;
// }
//}
template<typename FPP, Device DEVICE>
void Faust::multiply_order_opt_all_best(std::vector<Faust::MatDense<FPP,DEVICE>*>& facts, Faust::MatDense<FPP,DEVICE>& out, FPP alpha/* =1.0*/, FPP beta_out/*=.0*/, std::vector<char> transconj_flags /* = {'N'}*/)
{
std::vector<Faust::MatDense<FPP,DEVICE>*> tmp_facts; //temporary product results
Faust::MatDense<FPP, DEVICE>* tmp;
int nfacts = facts.size();
Faust::MatDense<FPP,DEVICE> *Si, *Sj;
std::vector<int> complexity(nfacts-1);
for(int i = 0; i <nfacts-1; i++)
{
Si = facts[i];
Sj = facts[i+1];
complexity[i] = Si->getNbRow() * Si->getNbCol() * Sj->getNbCol();
}
int idx; // marks the factor to update with a product of contiguous factors
bool multiplying_tmp_factor = false; // allows to avoid to allocate uselessly a tmp factor if Si or Sj are already tmp factors
while(facts.size() > 2)
{
// find the least complex product facts[idx]*facts[idx+1]
idx = distance(complexity.begin(), min_element(complexity.begin(), complexity.end()));
Si = facts[idx];
Sj = facts[idx+1];
for(auto Tit = tmp_facts.begin(); Tit != tmp_facts.end(); Tit++)
{
if(Sj == *Tit)
{// Sj is original fact
multiplying_tmp_factor = true;
tmp = Sj;
break;
}
else if(Si == *Tit)
{
multiplying_tmp_factor = true;
tmp = Si;
break;
}
}
if(! multiplying_tmp_factor)
{
tmp = new Faust::MatDense<FPP, DEVICE>();
tmp_facts.push_back(tmp);
}
//else no need to instantiate a new tmp, erasing Sj which is a tmp
gemm(*Si, *Sj, *tmp, (FPP)1.0, (FPP)0.0, transconj_flags[transconj_flags.size()>idx?idx:0], transconj_flags[transconj_flags.size()>idx+1?idx+1:0]);
facts.erase(facts.begin()+idx+1);
complexity.erase(complexity.begin()+idx); //complexity size == facts size - 1
facts[idx] = tmp;
if(transconj_flags.size() > idx)
transconj_flags[idx] = 'N';
// update complexity around the new factor
if(facts.size() > 2)
{
if(idx > 0)
complexity[idx-1] = facts[idx-1]->getNbRow() * facts[idx-1]->getNbCol() * facts[idx]->getNbCol();
if(idx < facts.size()-1)
complexity[idx] = facts[idx]->getNbRow() * facts[idx]->getNbCol() * facts[idx+1]->getNbCol();
}
multiplying_tmp_factor = false;
}
// last mul
gemm(*facts[0], *facts[1], out, alpha, beta_out, transconj_flags[0], transconj_flags.size()>1?transconj_flags[1]:'N');
facts.erase(facts.begin(), facts.end());
// delete all tmp facts
for(auto Tit = tmp_facts.begin(); Tit != tmp_facts.end(); Tit++)
{
delete *Tit;
}
}
template<typename FPP, Device DEVICE>
void Faust::multiply_order_opt(std::vector<Faust::MatDense<FPP,DEVICE>*>& facts, Faust::MatDense<FPP,DEVICE>& out, FPP alpha/* =1.0*/, FPP beta_out/*=.0*/, std::vector<char> transconj_flags /* = {'N'}*/)
void Faust::multiply_order_opt_first_best(std::vector<Faust::MatDense<FPP,DEVICE>*>& facts, Faust::MatDense<FPP,DEVICE>& out, FPP alpha/* =1.0*/, FPP beta_out/*=.0*/, std::vector<char> transconj_flags /* = {'N'}*/)
{
int nfacts = facts.size();
Faust::MatDense<FPP,DEVICE> *Si, *Sj, tmp;
......@@ -885,7 +885,7 @@ void Faust::multiply_order_opt(std::vector<Faust::MatDense<FPP,DEVICE>*>& facts,
}
// find the least complex product facts[idx]*facts[idx+1]
// idx = distance(complexity.begin(), min_element(complexity.begin(), complexity.end()));
std::cout << "idx: " << idx << std::endl;
// std::cout << "idx: " << idx << std::endl;
Si = facts[idx];
Sj = facts[idx+1];
gemm(*Si, *Sj, tmp, (FPP)1.0, (FPP)0.0, transconj_flags[transconj_flags.size()>idx?idx:0], transconj_flags[transconj_flags.size()>idx+1?idx+1:0]);
......@@ -914,7 +914,7 @@ void Faust::multiply_order_opt(std::vector<Faust::MatDense<FPP,DEVICE>*>& facts,
}
template<typename FPP, Device DEVICE>
void Faust::multiply_order_opt(std::vector<Faust::MatGeneric<FPP,DEVICE>*>& facts, Faust::MatDense<FPP,DEVICE>& out, FPP alpha/* =1.0*/, FPP beta_out/*=.0*/, std::vector<char> transconj_flags /* = {'N'}*/)
void Faust::multiply_order_opt(const int mode, std::vector<Faust::MatGeneric<FPP,DEVICE>*>& facts, Faust::MatDense<FPP,DEVICE>& out, FPP alpha/* =1.0*/, FPP beta_out/*=.0*/, std::vector<char> transconj_flags /* = {'N'}*/)
{
std::vector<Faust::MatDense<FPP,DEVICE>*> dfacts;
std::vector<Faust::MatDense<FPP,DEVICE>*> dfacts_to_del;
......@@ -929,7 +929,20 @@ void Faust::multiply_order_opt(std::vector<Faust::MatGeneric<FPP,DEVICE>*>& fact
}
dfacts.push_back(df);
}
multiply_order_opt(dfacts, out, alpha, beta_out, transconj_flags);
switch(mode)
{
case 1:
Faust::multiply_order_opt_all_ends(dfacts, out, alpha, beta_out, transconj_flags);
break;
case 2:
Faust::multiply_order_opt_first_best(dfacts, out, alpha, beta_out, transconj_flags);
break;
case 3:
Faust::multiply_order_opt_all_best(dfacts, out, alpha, beta_out, transconj_flags);
break;
default:
throw std::out_of_range("optimization asked not known");
}
// free dense copies
for(auto df: dfacts_to_del)
delete df;
......
......@@ -65,7 +65,7 @@ class FaustCoreCpp
unsigned int getNbCol() const;
void get_product(FPP* y_data, int y_nrows, int y_ncols);
void multiply(FPP* y_data, int y_nrows, int y_ncols, FPP* x_data, int* x_row_ptr, int* x_id_col, int x_nnz, int x_nrows, int x_ncols);
void set_enable_mul_order_opt(const bool enable);
void set_mul_order_opt_mode(const int enable);
void multiply(FPP* value_y,int nbrow_y,int nbcol_y,FPP* value_x,int nbrow_x,int nbcol_x/*,bool isTranspose*/)const;
FaustCoreCpp<FPP>* mul_faust(FaustCoreCpp<FPP>* right);
FaustCoreCpp<FPP>* vertcat(FaustCoreCpp<FPP>* right);
......
......@@ -164,9 +164,9 @@ void FaustCoreCpp<FPP>::multiply(FPP* value_y,int nbrow_y,int nbcol_y,FPP* value
}
template<typename FPP>
void FaustCoreCpp<FPP>::set_enable_mul_order_opt(const bool enable)
void FaustCoreCpp<FPP>::set_mul_order_opt_mode(const int mode)
{
this->transform->set_enable_mul_order_opt(enable);
this->transform->set_mul_order_opt_mode(mode);
}
template<typename FPP>
......
......@@ -51,7 +51,7 @@ cdef extern from "FaustCoreCpp.h" :
void get_product(FPP* data, int nbrow, int nbcol);
void multiply(FPP* value_y,int nbrow_y,int nbcol_y,FPP* value_x,
int nbrow_x, int nbcol_x);#,bool isTranspose*/);
void set_enable_mul_order_opt(const bool enable);
void set_mul_order_opt_mode(const int mode);
# Faust-by-csr product -> dense mat
void multiply(FPP* y_data, int y_nrows, int y_ncols, FPP* x_data, int* x_row_ptr, int* x_id_col, int x_nnz, int x_nrows, int x_ncols);
unsigned int getNbRow() const
......
......@@ -472,11 +472,11 @@ cdef class FaustCore:
return y
def set_enable_mul_order_opt(self, enable):
def set_mul_order_opt_mode(self, mode):
if(self._isReal):
self.core_faust_dbl.set_enable_mul_order_opt(enable)
self.core_faust_dbl.set_mul_order_opt_mode(mode)
else:
self.core_faust_cplx.set_enable_mul_order_opt(enable)
self.core_faust_cplx.set_mul_order_opt_mode(mode)
# print information about the faust (size, number of factor, type of factor (dense/sparse) ...)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment