Mentions légales du service

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

Add faust_WHT.h(pp) to move wht factors computation from faust_TransformHelper (Refactoring).

Issue #39 related.
parent 4d0b7343
Branches
Tags
No related merge requests found
#ifndef FAUST_WHT
#define FAUST_WHT
#include "faust_MatGeneric.h"
#include <vector>
namespace Faust {
/**
* \brief Fast Walsh-Hadamard Transform.
*/
template<typename FPP>
void wht_factors(unsigned int n, vector<MatGeneric<FPP,Cpu>*>& factors);
}
#include "faust_WHT.hpp"
#endif
#include "faust_MatDense.h"
#include "faust_MatSparse.h"
namespace Faust {
template<typename FPP>
void wht_factors(unsigned int n, vector<MatGeneric<FPP,Cpu>*>& factors)
{
if(n == 0)
{
factors.resize(1);
MatDense<FPP,Cpu>* mat = new MatDense<FPP,Cpu>(1,1);
(*mat)[0] = 1;
factors[0] = mat;
}
else
{
factors.resize(n);
unsigned int order = 1ull << n;
vector<int> col_ids(order), row_ids(order);
vector<FPP> vals(order);
unsigned int order_over_2 = order >> 1;
unsigned int order_times_2 = order << 1;
unsigned int i_times_2;
// // init the permutation matrix
for(unsigned int i=0; i < order_over_2; i++)
{
i_times_2 = i << 1;
row_ids[i] = i_times_2;
row_ids[i+order_over_2] = i_times_2+1;
col_ids[i] = i;
col_ids[i+order_over_2] = i + order_over_2;
vals[i] = vals[i+order_over_2] = 1;
}
// cout << row_ids.size() << endl;
// cout << col_ids.size() << endl;
// cout << vals.size() << endl;
MatSparse<FPP,Cpu> P(row_ids, col_ids, vals, order, order);
P.update_dim();
// init the base matrix
int *row_ptr = new int[order+1];
row_ptr[0] = 0;
int *bcol_ids = new int[order_times_2];
FPP *bvalues = new FPP[order_times_2];
// cout << "row_ptr: ";
for(int i = 1; i < order+1;i++)
{
row_ptr[i] = 2+row_ptr[i-1];
// cout << row_ptr[i] << ",";
}
// cout << endl;
bool parity = true; //row parity
int col_id = 0;
// cout << "bvalues: ";
// cout << "bcol_ids: ";
for(unsigned int i=0; i < order_times_2; i+=2)
{
if(parity) //row index is pair
bvalues[i] = bvalues[i+1] = 1;
else
{
bvalues[i+1] = -1;
bvalues[i] = 1;
}
// cout << bvalues[i] << " " << bvalues[i+1];
parity = ! parity;
bcol_ids[i] = col_id;
bcol_ids[i+1] = col_id+1;
// cout << bcol_ids[i] << " " << bcol_ids[i+1] << " ";
if(((i + 1) & 3u) == 3u) col_id+=2; // i+1 mod 4 == 0
}
// cout << endl;
MatSparse<FPP,Cpu> B(order_times_2, order, order, bvalues, row_ptr, bcol_ids, false);
// cout << "Faust::TransformHelper::hadamardFaust(), B:" << endl;
// B.Display();
delete[] bvalues;
delete[] bcol_ids;
delete[] row_ptr;
MatSparse<FPP,Cpu>* factor = new MatSparse<FPP,Cpu>(order,order);
factor->mat = B.mat*P.mat;
factor->update_dim();
factors[0] = factor;
for(int i=1; i < n; i++)
factors[i] = factor->Clone();
}
}
}
......@@ -83,7 +83,8 @@ void Faust::multiply(const Faust::Transform<FPP,Cpu> & A, const Faust::MatDense<
template<typename FPP>
void Faust::spgemm(const Faust::MatSparse<FPP,Cpu> & A,const Faust::MatDense<FPP,Cpu> & B, Faust::MatDense<FPP,Cpu> & C,const FPP & alpha, const FPP & beta, char typeA, char typeB);
template<typename FPP>
void Faust::wht_factors(unsigned int n, vector<MatGeneric<FPP,Cpu>*>& factors);
//! \namespace Faust
//! \brief Faust namespace contains the principal class of the project.
......@@ -102,6 +103,8 @@ namespace Faust
class MatSparse<FPP,Cpu> : public Faust::MatGeneric<FPP,Cpu>
{
friend Faust::TransformHelper<FPP,Cpu>; // TODO: limit to needed member functions only
friend void Faust::wht_factors<>(unsigned int n, vector<MatGeneric<FPP,Cpu>*>& factors);
friend class MatDense<FPP,Cpu>;
//friend void MatDense<FPP,Cpu>::operator+=(const MatSparse<FPP,Cpu>& S);
......@@ -319,7 +322,7 @@ static MatSparse<FPP, Cpu>* randMat(faust_unsigned_int num_rows, faust_unsigned_
//! *this = S * (*this)
friend void Faust::Vect<FPP,Cpu>::multiplyLeft(MatSparse<FPP,Cpu> const& S,const char TransS);
friend double Faust::Transform<FPP,Cpu>::normL1(const bool transpose) const;
friend Faust::TransformHelper<FPP,Cpu>; //TODO: limit to hadamardFaust
/*friend void Faust::MatDense<FPP,Cpu>::multiplyLeft(MatSparse<FPP,Cpu> const& S,const char TransS);*/
// MODIF AL WARNING, ERROR WITH VISUAL STUDIO 2013 compiler
......
......@@ -40,6 +40,7 @@
/****************************************************************************/
#include "faust_FFT.h"
#include "faust_WHT.h"
namespace Faust {
......@@ -938,94 +939,14 @@ namespace Faust {
return randFaust;
}
template<typename FPP>
TransformHelper<FPP,Cpu>* TransformHelper<FPP,Cpu>::hadamardFaust(unsigned int n)
{
if(n == 0) {
MatDense<FPP,Cpu>* mat = new MatDense<FPP,Cpu>(1,1);
(*mat)[0] = 1;
vector<MatGeneric<FPP,Cpu>*> factors(1);
factors[0] = mat;
return new TransformHelper<FPP, Cpu>(factors,1.0,false);
}
TransformHelper<FPP,Cpu>* hadamardFaust = nullptr;
vector<MatGeneric<FPP,Cpu>*> factors;
try {
unsigned int order = 1ull << n;
vector<int> col_ids(order), row_ids(order);
vector<FPP> vals(order);
unsigned int order_over_2 = order >> 1;
unsigned int order_times_2 = order << 1;
unsigned int i_times_2;
// // init the permutation matrix
for(unsigned int i=0; i < order_over_2; i++)
{
i_times_2 = i << 1;
row_ids[i] = i_times_2;
row_ids[i+order_over_2] = i_times_2+1;
col_ids[i] = i;
col_ids[i+order_over_2] = i + order_over_2;
vals[i] = vals[i+order_over_2] = 1;
}
// cout << row_ids.size() << endl;
// cout << col_ids.size() << endl;
// cout << vals.size() << endl;
MatSparse<FPP,Cpu> P(row_ids, col_ids, vals, order, order);
P.update_dim();
// init the base matrix
int *row_ptr = new int[order+1];
row_ptr[0] = 0;
int *bcol_ids = new int[order_times_2];
FPP *bvalues = new FPP[order_times_2];
// cout << "row_ptr: ";
for(int i = 1; i < order+1;i++)
{
row_ptr[i] = 2+row_ptr[i-1];
// cout << row_ptr[i] << ",";
}
// cout << endl;
bool parity = true; //row parity
int col_id = 0;
// cout << "bvalues: ";
// cout << "bcol_ids: ";
for(unsigned int i=0; i < order_times_2; i+=2)
{
if(parity) //row index is pair
bvalues[i] = bvalues[i+1] = 1;
else
{
bvalues[i+1] = -1;
bvalues[i] = 1;
}
// cout << bvalues[i] << " " << bvalues[i+1];
parity = ! parity;
bcol_ids[i] = col_id;
bcol_ids[i+1] = col_id+1;
// cout << bcol_ids[i] << " " << bcol_ids[i+1] << " ";
if(((i + 1) & 3u) == 3u) col_id+=2; // i+1 mod 4 == 0
}
// cout << endl;
MatSparse<FPP,Cpu> B(order_times_2, order, order, bvalues, row_ptr, bcol_ids, false);
// cout << "Faust::TransformHelper::hadamardFaust(), B:" << endl;
// B.Display();
delete[] bvalues;
delete[] bcol_ids;
delete[] row_ptr;
std::vector<MatGeneric<FPP,Cpu>*> factors((size_t) n);
MatSparse<FPP,Cpu>* factor = new MatSparse<FPP,Cpu>(order,order);
factor->mat = B.mat*P.mat;
factor->update_dim();
factors[0] = factor;
for(int i=1; i < n; i++)
factors[i] = factor->Clone();
wht_factors(n, factors);
hadamardFaust = new TransformHelper<FPP, Cpu>(factors, 1.0, false, false);
}
catch(std::bad_alloc)
......@@ -1033,7 +954,6 @@ namespace Faust {
// nothing to do, out of memory return nullptr
}
return hadamardFaust;
}
template<typename FPP>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment