refactoring

parent 9e4d201b
...@@ -7,10 +7,23 @@ env = Environment( #variables = vars, ...@@ -7,10 +7,23 @@ env = Environment( #variables = vars,
CXX = 'g++', CXX = 'g++',
CXXFLAGS = Split('-I./include -I./src \ CXXFLAGS = Split('-I./include -I./src \
-march=native -mtune=native -std=gnu++14 -fabi-version=6 \ -march=native -mtune=native -std=gnu++14 -fabi-version=6 \
-fpermissive -Wall -Wextra -fopenmp'), -fpermissive -Wall -Wextra -fopenmp'),
LIBPATH = '#lib/', LIBPATH = '#lib/',
LIBS = Split('-lopenblas -lgivaro -lgmpxx -lgmp'), LIBS = Split('-lopenblas -lgivaro -lgmpxx -lgmp'),
LINKFLAGS = '-fopenmp' LINKFLAGS = '-fopenmp',
SRC_CC = ['#src/finite_field_arithmetic/givaro_wrapper.cc',
'#src/logger.cc',
'#src/monomial.cc',
'#src/select_reductors.cc',
'#src/critical_pair.cc',
'#src/polynomial_in_matrix.cc',
'#src/polynomial.cc',
'#src/matrices/io_utils.cc',
'#src/matrices/spmat.cc',
'#src/utils.cc',
'#src/linalg/storage_matrix.cc',
'#src/glob_params.cc',
'#src/matrices/rowmajmat.cc']
) )
#Help(vars.GenerateHelpText(env)) #Help(vars.GenerateHelpText(env))
......
...@@ -12,39 +12,34 @@ namespace tinygb { ...@@ -12,39 +12,34 @@ namespace tinygb {
template <typename T> // CRTP template <typename T> // CRTP
class AbstractField { class AbstractField {
public: public:
std::string description() const { std::string description() const {
return static_cast<T*>(this)->description(); return static_cast<T*>(this)->description();
} }
};
// T is the CRTP parameter
// Field is a concrete field
template <typename Field, typename T>
class AbstractFieldElement {
static_assert(std::is_base_of<AbstractField<Field>, Field>::value,
"Field should be derived from AbstractField");
public: template <typename U> // CRTP
typedef Field Parent; class Element {
public:
void operator+=(const AbstractFieldElement<Field, T>& e) { typedef T Parent;
static_cast<T*>(this)->operator+=(e);
} void operator+=(const Element<U>& e) {
void operator-=(const AbstractFieldElement<Field, T>& e) { static_cast<T*>(this)->operator+=(e);
static_cast<T*>(this)->operator-=(e); }
} void operator-=(const Element<U>& e) {
void operator*=(const AbstractFieldElement<Field, T>& e) { static_cast<T*>(this)->operator-=(e);
static_cast<T*>(this)->operator*=(e); }
} void operator*=(const Element<U>& e) {
void operator/=(const AbstractFieldElement<Field, T>& e) { static_cast<T*>(this)->operator*=(e);
static_cast<T*>(this)->operator/=(e); }
} void operator/=(const Element<U>& e) {
void inv() { static_cast<T*>(this)->operator/=(e);
static_cast<T*>(this)->inv(); }
} void inv() {
bool IsZero() const { static_cast<T*>(this)->inv();
return static_cast<T*>(this)->isZero(); }
} bool IsZero() const {
return static_cast<T*>(this)->isZero();
}
};
}; };
} // namespace tinygb } // namespace tinygb
......
...@@ -10,15 +10,14 @@ namespace tinygb { ...@@ -10,15 +10,14 @@ namespace tinygb {
// implementations of polynomials are implemented in subclasses via static // implementations of polynomials are implemented in subclasses via static
// polymorphism. // polymorphism.
template <class FieldElement, class T> // T is the CRTP parameter template <class Field, class T> // T is the CRTP parameter
class AbstractPolynomial { class AbstractPolynomial {
static_assert(std::is_base_of<AbstractFieldElement<typename FieldElement::Parent, FieldElement>, static_assert(std::is_base_of<AbstractField<Field>, Field>::value,
FieldElement>::value,
"Field should be derived from AbstractField"); "Field should be derived from AbstractField");
public: public:
Monomial LeadingMonomial() const { Monomial LeadingMonomial() const {
return static_cast<T*>(this)->LeadingMonomial();} return static_cast<T*>(this)->LeadingMonomial();}
FieldElement LeadingCoefficient() const { typename Field::Element LeadingCoefficient() const {
return static_cast<T*>(this)->LeadingCoefficient();} return static_cast<T*>(this)->LeadingCoefficient();}
void DivideByLeadingCoefficient() { void DivideByLeadingCoefficient() {
static_cast<T*>(this)->DivideByLeadingCoefficient();} static_cast<T*>(this)->DivideByLeadingCoefficient();}
......
Import('env') Import('env')
source_files = ['addcrit.cc', source_files = env['SRC_CC'] + ['addcrit.cc']
'#src/finite_field_arithmetic/givaro_wrapper.cc',
'#src/logger.cc',
'#src/monomial.cc',
'#src/critical_pair.cc',
'#src/polynomial_in_matrix.cc',
'#src/polynomial.cc',
'#src/matrices/io_utils.cc',
'#src/utils.cc',
'#src/glob_params.cc',
'#src/matrices/rowmajmat.cc']
env.Program('addcrit', source_files) env.Program('addcrit', source_files)
Import('env') Import('env')
source_files = ['buildmats.cc', source_files = env['SRC_CC'] + ['buildmats.cc']
'#src/finite_field_arithmetic/givaro_wrapper.cc',
'#src/logger.cc',
'#src/monomial.cc',
'#src/critical_pair.cc',
'#src/polynomial_in_matrix.cc',
'#src/polynomial.cc',
'#src/matrices/spmat.cc',
'#src/matrices/io_utils.cc',
'#src/utils.cc',
'#src/glob_params.cc',
'#src/matrices/rowmajmat.cc']
env.Program('buildmats', source_files) env.Program('buildmats', source_files)
...@@ -10,53 +10,54 @@ ...@@ -10,53 +10,54 @@
namespace tinygb { namespace tinygb {
class GFp : public AbstractField<GFp> { class GFp_Givaro : public AbstractField<GFp_Givaro> {
public: public:
std::string description() const { std::string description() const {
return "Finite field of prime cardinality p";} return "Finite field of prime cardinality p";}
};
class Element : public AbstractField<GFp_Givaro>::Element<GFp_Givaro::Element> {
public:
typedef Givaro::Integer elt_t;
typedef Givaro::Modular<Givaro::Integer> ring_t;
class GivaroWrapper : public AbstractFieldElement<GFp, GivaroWrapper> { static ring_t k;
public: elt_t v;
typedef Givaro::Integer elt_t;
typedef Givaro::Modular<Givaro::Integer> ring_t;
static ring_t k;
elt_t v;
// Constructors // Constructors
GivaroWrapper() {} Element() {}
GivaroWrapper(const char* n) { k.init(v, n); } Element(const char* n) { k.init(v, n); }
GivaroWrapper(int n) { k.init(v, n); } Element(int n) { k.init(v, n); }
GivaroWrapper(const mpz_class& n) { k.init(v, n);} Element(const mpz_class& n) { k.init(v, n);}
GivaroWrapper(const elt_t& n) { k.init(v, n); } Element(const elt_t& n) { k.init(v, n); }
GivaroWrapper(const std::string& s) { k.init(v, s.c_str()); } Element(const std::string& s) { k.init(v, s.c_str()); }
// Copy constructor // Copy constructor
GivaroWrapper(const GivaroWrapper& x) { k.assign(v, x.v); } Element(const Element& x) { k.assign(v, x.v); }
// TODO rule of 0/3/5 // TODO rule of 0/3/5
GivaroWrapper& operator=(const GivaroWrapper& x) { Element& operator=(const Element& x) {
k.assign(v, x.v); k.assign(v, x.v);
return *this; return *this;
} }
inline bool is_zero() const { return k.isZero(v); } inline bool is_zero() const { return k.isZero(v); }
friend std::ostream& operator<<(std::ostream&, const GivaroWrapper&); friend std::ostream& operator<<(std::ostream&, const Element&);
friend std::istream& operator>>(std::istream&, GivaroWrapper&); friend std::istream& operator>>(std::istream&, Element&);
inline void neg() { v = -v; } inline void neg() { v = -v; }
void operator+=(const GivaroWrapper& a) { k.addin(v, a.v); } void operator+=(const Element& a) { k.addin(v, a.v); }
void operator-=(const GivaroWrapper& a) { k.subin(v, a.v); } void operator-=(const Element& a) { k.subin(v, a.v); }
void operator*=(const GivaroWrapper& a) { k.mulin(v, a.v); } void operator*=(const Element& a) { k.mulin(v, a.v); }
void operator/=(const GivaroWrapper& a) { k.divin(v, a.v); } void operator/=(const Element& a) { k.divin(v, a.v); }
bool operator==(const GivaroWrapper& a) const { return v == a.v; } bool operator==(const Element& a) const { return v == a.v; }
bool operator!=(const GivaroWrapper& a) const { return v != a.v; } bool operator!=(const Element& a) const { return v != a.v; }
void inv() { k.invin(v); } void inv() { k.invin(v); }
mpz_srcptr get_mpz() const { return v.get_mpz(); } mpz_srcptr get_mpz() const { return v.get_mpz(); }
void set_mpz(const mpz_class& a) { k.init(v, elt_t(a)); } void set_mpz(const mpz_class& a) { k.init(v, elt_t(a)); }
};
}; };
typedef GFp_Givaro::Element GivaroWrapper;
std::ostream& operator<<(std::ostream&, const GivaroWrapper &); std::ostream& operator<<(std::ostream&, const GivaroWrapper &);
std::istream& operator>>(std::istream&, GivaroWrapper&); std::istream& operator>>(std::istream&, GivaroWrapper&);
......
Import('env') Import('env')
source_files = ['matmulsub.cc', source_files = env['SRC_CC'] + ['matmulsub.cc']
'#src/monomial.cc',
'#src/logger.cc',
'#src/polynomial.cc',
'#src/critical_pair.cc',
'#src/polynomial_in_matrix.cc',
'#src/matrices/rowmajmat.cc',
'#src/matrices/io_utils.cc',
'#src/utils.cc',
'#src/glob_params.cc',
'#src/finite_field_arithmetic/givaro_wrapper.cc']
env.Program('matmulsub', source_files) env.Program('matmulsub', source_files)
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
namespace tinygb { namespace tinygb {
class Polynomial : public AbstractPolynomial<GivaroWrapper, Polynomial> { class Polynomial : public AbstractPolynomial<GFp_Givaro, Polynomial> {
public: public:
typedef std::pair<Monomial, GivaroWrapper> Term; typedef std::pair<Monomial, GivaroWrapper> Term;
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
namespace tinygb { namespace tinygb {
class PolynomialInMatrix { class PolynomialInMatrix : public AbstractPolynomial<GFp_Givaro, PolynomialInMatrix> {
public: public:
PolynomialInMatrix() : pMatrix_(nullptr), row_(0), nbMonoms_(0) {} PolynomialInMatrix() : pMatrix_(nullptr), row_(0), nbMonoms_(0) {}
......
Import('env') Import('env')
source_files = ['rowech.cc', source_files = env['SRC_CC'] + ['rowech.cc']
'#src/finite_field_arithmetic/givaro_wrapper.cc',
'#src/logger.cc',
'#src/monomial.cc',
'#src/critical_pair.cc',
'#src/polynomial_in_matrix.cc',
'#src/polynomial.cc',
'#src/matrices/io_utils.cc',
'#src/utils.cc',
'#src/glob_params.cc',
'#src/matrices/rowmajmat.cc']
env.Program('rowech', source_files) env.Program('rowech', source_files)
Import('env') Import('env')
source_files = ['selcrit.cc', source_files = env['SRC_CC'] + ['selcrit.cc']
'#src/finite_field_arithmetic/givaro_wrapper.cc',
'#src/logger.cc',
'#src/monomial.cc',
'#src/critical_pair.cc',
'#src/polynomial_in_matrix.cc',
'#src/polynomial.cc',
'#src/matrices/io_utils.cc',
'#src/utils.cc',
'#src/glob_params.cc',
'#src/matrices/rowmajmat.cc']
env.Program('selcrit', source_files) env.Program('selcrit', source_files)
Import('env') Import('env')
source_files = ['selred.cc', source_files = env['SRC_CC'] + ['selred.cc']
'#src/finite_field_arithmetic/givaro_wrapper.cc',
'#src/logger.cc',
'#src/monomial.cc',
'select_reductors.cc',
'#src/critical_pair.cc',
'#src/polynomial_in_matrix.cc',
'#src/polynomial.cc',
'#src/matrices/io_utils.cc',
'#src/utils.cc',
'#src/glob_params.cc',
'#src/matrices/rowmajmat.cc']
env.Program('selred', source_files) env.Program('selred', source_files)
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
#include "./finite_field_arithmetic/givaro_wrapper.h" #include "./finite_field_arithmetic/givaro_wrapper.h"
#include "./common.h" #include "./common.h"
#include "./utils.h" #include "./utils.h"
#include "selred/select_reductors.h" #include "./select_reductors.h"
#include "./macros.h" #include "./macros.h"
#include "./logger.h" #include "./logger.h"
......
Import('env') Import('env')
source_files = ['tinygb_init.cc', source_files = env['SRC_CC'] + ['tinygb_init.cc']
'#src/finite_field_arithmetic/givaro_wrapper.cc',
'#src/monomial.cc',
'#src/logger.cc',
'#src/critical_pair.cc',
'#src/polynomial_in_matrix.cc',
'#src/polynomial.cc',
'#src/matrices/io_utils.cc',
'#src/utils.cc',
'#src/glob_params.cc',
'#src/linalg/storage_matrix.cc',
'#src/matrices/rowmajmat.cc']
env.Program('tinygb_init', source_files) env.Program('tinygb_init', source_files)
Import('env') Import('env')
source_files = ['utsolve.cc', source_files = env['SRC_CC'] + ['utsolve.cc']
'#src/critical_pair.cc',
'#src/monomial.cc',
'#src/polynomial.cc',
'#src/polynomial_in_matrix.cc',
'#src/matrices/spmat.cc',
'#src/matrices/rowmajmat.cc',
'#src/matrices/io_utils.cc',
'#src/utils.cc',
'#src/glob_params.cc',
'#src/finite_field_arithmetic/givaro_wrapper.cc',
'#src/logger.cc']
env.Program('utsolve', source_files) env.Program('utsolve', source_files)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment