Commit f6f86e1c authored by Damien Leroux's avatar Damien Leroux
Browse files

Massive reorganization.

parent 29dcc482
......@@ -387,7 +387,7 @@ struct algebraic_genotype { /* must fit in a QWORD */
* Cross * Cross : boom
*/
algebraic_genotype(int i=0)
algebraic_genotype()
{
genotype = {{0, 0}, {0, 0}};
type = Type::Null;
......@@ -733,7 +733,7 @@ struct algebraic_locus {
return *this;
}
algebraic_locus& operator += (const algebraic_locus& al)
algebraic_locus& operator += (const algebraic_locus&)
{
/* only authorized when perfect equality between *this and al. Does nothing. */
/*if (*this == al) {*/
......@@ -830,20 +830,16 @@ static inline tie_struc<T1, T2> tie(T1& a, T2& b)
struct eval_poly_optim {
std::map<fast_polynom, int> indices;
std::vector<fast_polynom> polynoms;
std::vector<double> values;
Matrix<int, Dynamic, Dynamic> rebuild;
std::vector<double> r_pow, s_pow;
size_t pow_max;
std::unordered_map<double, MatrixXd> cache;
eval_poly_optim()
: indices(), polynoms(), values(), rebuild(), r_pow(), s_pow(), pow_max(0)
: indices(), polynoms(), rebuild(), pow_max(0)
{}
void init(const GenoMatrix& G)
{
indices.clear();
cache.clear();
rebuild.resize(G.innerSize(), G.outerSize());
std::set<fast_polynom> uniq;
int index = 0;
......@@ -860,55 +856,88 @@ struct eval_poly_optim {
}
}
}
values.resize(polynoms.size());
pow_max = polynoms[0].degree() + 1;
r_pow.resize(pow_max);
s_pow.resize(pow_max);
r_pow[0] = 1.;
s_pow[0] = 1.;
/*std::cout << "There are " << uniq.size() << " dictinct polynoms in " << (G.innerSize() * G.outerSize()) << " cells. pow_max=" << pow_max << std::endl;*/
/*std::cout << rebuild << std::endl;*/
}
MatrixXd& eval(double dist)
{
MatrixXd& ret = cache[dist];
struct cache {
const eval_poly_optim* m_epo;
std::unordered_map<double, MatrixXd> m_cache;
std::vector<double> r_pow, s_pow;
std::vector<double> values;
if (ret.innerSize() != 0) {
return ret;
cache(const eval_poly_optim* et)
: m_epo(et), m_cache(), r_pow(), s_pow(), values()
{
values.resize(m_epo->polynoms.size());
r_pow.resize(m_epo->pow_max);
s_pow.resize(m_epo->pow_max);
r_pow[0] = 1.;
s_pow[0] = 1.;
}
double r = prob_recomb(dist);
double s = 1. - r;
double r_tmp = r, s_tmp = s;
cache() : m_epo(0), m_cache(), r_pow(), s_pow(), values() {}
ret.resize(rebuild.innerSize(), rebuild.outerSize());
cache(cache&& tmp)
: m_epo(tmp.m_epo)
, m_cache(std::forward<std::unordered_map<double, MatrixXd>>(tmp.m_cache))
, r_pow(std::forward<std::vector<double>>(tmp.r_pow))
, s_pow(std::forward<std::vector<double>>(tmp.s_pow))
, values(std::forward<std::vector<double>>(tmp.values))
{}
for (size_t i = 1; i < pow_max; ++i) {
r_pow[i] = r_tmp;
s_pow[i] = s_tmp;
r_tmp *= r;
s_tmp *= s;
cache& operator = (cache&& tmp)
{
m_epo = tmp.m_epo;
r_pow.swap(tmp.r_pow);
s_pow.swap(tmp.s_pow);
values.swap(tmp.values);
return *this;
}
for (size_t p = 0; p < polynoms.size(); ++p) {
const impl::f_polynom& poly = polynoms[p];
double accum = r_pow[poly.r_exp] * s_pow[poly.s_exp];
double tmp = poly.P[0];
for (size_t i = 1; i < poly.P.size(); ++i) {
tmp += poly.P[i] * r_pow[i];
MatrixXd& eval(double dist, int order)
{
MatrixXd& ret = m_cache[dist];
if (ret.innerSize() != 0) {
return ret;
}
values[p] = accum * tmp;
}
for (int i = 0; i < rebuild.innerSize(); ++i) {
for (int j = 0; j < rebuild.outerSize(); ++j) {
ret(i, j) = values[rebuild(i, j)];
double r = prob_recomb(dist, order);
double s = 1. - r;
double r_tmp = r, s_tmp = s;
ret.resize(m_epo->rebuild.innerSize(), m_epo->rebuild.outerSize());
for (size_t i = 1; i < m_epo->pow_max; ++i) {
r_pow[i] = r_tmp;
s_pow[i] = s_tmp;
r_tmp *= r;
s_tmp *= s;
}
for (size_t p = 0; p < m_epo->polynoms.size(); ++p) {
const impl::f_polynom& poly = m_epo->polynoms[p];
double accum = r_pow[poly.r_exp] * s_pow[poly.s_exp];
double tmp = poly.P[0];
for (size_t i = 1; i < poly.P.size(); ++i) {
tmp += poly.P[i] * r_pow[i];
}
values[p] = accum * tmp;
}
for (int i = 0; i < m_epo->rebuild.innerSize(); ++i) {
for (int j = 0; j < m_epo->rebuild.outerSize(); ++j) {
ret(i, j) = values[m_epo->rebuild(i, j)];
}
}
return ret;
}
};
return ret;
}
cache new_cache() const { return {this}; }
};
......
#include <x2c/x2c.h>
/*#include <x2c/x2c.h>*/
#error "DO NOT USE THIS HEADER!!!"
#define EIGEN_NO_DEPRECATED_WARNING
#include <cctype>
......@@ -17,6 +19,7 @@
#include "settings.h"
#include "banner.h"
#include "cache2.h"
#include "model.h"
......@@ -37,5 +40,5 @@ std::ostream& operator << (std::ostream& os, const std::map<T1, T2>& m)
return os << '}';
}
#include "computations.h"
/*#include "computations.h"*/
......@@ -36,6 +36,7 @@ namespace cache {
#include "cache/value.h"
#include "cache/cartesian_product.h"
#include "cache/file.h"
#include "cache/async.h"
#include "cache/factory.h"
#endif
......
......@@ -112,6 +112,13 @@ template <typename... ITEMS>
{
return std::make_tuple(*std::get<Indices>(iter)...);
}
template <typename T>
static
void push_to(std::vector<T>& vec, const std::tuple<iterator_context<ITEMS>...>& iter)
{
vec.emplace_back(*std::get<Indices>(iter)...);
}
};
std::tuple<typename item_iterator<ITEMS>::value_type...> operator * () const
......@@ -123,6 +130,15 @@ template <typename... ITEMS>
throw cartesian_product_at_end_exception();
}
template <typename T>
void push_to(std::vector<T>& vec)
{
if (!at_end) {
return _get<indices>::push_to(vec, iter);
}
throw cartesian_product_at_end_exception();
}
struct iterator {
typedef std::tuple<typename item_iterator<ITEMS>::value_type...> value_type;
cartesian_product<ITEMS...> cp;
......
......@@ -20,6 +20,8 @@ template <typename T>
using only_if_derives_from_wof = typename std::enable_if<std::is_base_of<cache::with_output_func, T>::value, T>::type;
namespace std {
template <> struct hash<const double> : hash<double> {};
template <typename... Elem>
struct hash<std::tuple<Elem...>> {
typedef std::tuple<Elem...> TupleType;
......@@ -33,7 +35,7 @@ namespace std {
};
template <int N>
struct hash_elem<N, N> {
size_t operator () (const TupleType& t) const
size_t operator () (const TupleType&) const
{
return 0;
}
......@@ -50,8 +52,6 @@ namespace std {
#define DEFAULT_CACHE_DIR "tmp/"
/*msg_handler_t::lock_type msg_handler_t::mutex;*/
namespace cache {
......@@ -95,52 +95,136 @@ namespace detail {
namespace cache {
template <bool _MultiThread>
struct launch_impl;
template <>
struct launch_impl<true> {
static std::launch& policy()
{
static std::launch _ = std::launch::async | std::launch::deferred;
/*static std::launch _ = std::launch::deferred;*/
return _;
}
template <typename Type>
using computed_type = std::future<Type>;
template <typename Class, typename Type, typename... Dependencies>
struct computed_value : value<Type> {
typedef Type value_type;
typedef std::tuple<Dependencies...> dependencies_type;
typedef async_computed_value<Class, Type, Dependencies...> task_type;
using value<Type>::output;
using value<Type>::val;
task_type* task;
/*computed_value(Dependencies&&... d)*/
/*: value<Type>(), task(task_type::get(d...))*/
/*{}*/
computed_value(const Dependencies&... d)
: value<Type>(), task(task_type::get(d...))
{}
/*computed_value(std::tuple<Dependencies...>&& t)*/
/*: value<Type>(), task(task_type::get(t))*/
/*{}*/
computed_value(const std::tuple<Dependencies...>& t)
: value<Type>(), task(task_type::get(t))
{}
computed_value(const computed_value<Class, Type, Dependencies...>& cv)
: value<Type>(), task(cv.task)
{}
/*computed_value(computed_value<Class, Type, Dependencies...>&& cv)*/
/*: value<Type>(), task(cv.task)*/
/*{}*/
template <typename Type, typename FSig>
static computed_type<Type> run(FSig f)
{
return std::async(policy(), f);
}
template <typename... DerDep>
computed_value(const std::tuple<DerDep...>& ddt)
: value<Type>(), task(task_type::get(std::tuple<Dependencies...>(ddt)))
{}
template <typename Type>
static Type access(computed_type<Type>& ct) { return ct.get(); }
};
template <typename... DerDep>
computed_value(const DerDep&... dd)
: value<Type>(), task(task_type::get(((const Dependencies&)dd)...))
{}
template <>
struct launch_impl<false> {
template <typename Type>
using computed_type = Type;
computed_value() : value<Type>(), task() {}
void __init(const std::tuple<Dependencies...>& d)
{
task = task_type::get(d);
}
Class& operator = (const computed_value<Class, Type, Dependencies...>& c)
{
/*if (task) {*/
/*delete task;*/
/*}*/
task = c.task;
return *static_cast<Class*>(this);
}
#if 1
Class& operator = (computed_value<Class, Type, Dependencies...>&& c)
{
/*if (task) {*/
/*delete task;*/
/*}*/
task = c.task;
return *static_cast<Class*>(this);
}
#endif
operator const Type& () const
{
return *task;
}
template <typename Type, typename FSig>
static computed_type<Type> run(FSig f)
/*Type& v() const { return *task; }*/
const Type& v() const { return *task; }
static const bool composite = false;
virtual
std::ostream&
output(std::ostream& os) const override
{
return os << v();
}
virtual
md5_digest&
to_md5(md5_digest& md5) const override
{
return f();
return task->to_md5(md5);
}
template <typename Type>
static Type access(computed_type<Type>& ct) { return ct; }
};
virtual
size_t hash() const
{
std::hash<dependencies_type> h;
return h(task->dependencies);
}
typedef launch_impl<true> launch;
bool operator == (const Class& v) const
{
return task == v.task;
}
struct iterator {
/*typedef computed_value<Class, Type, Dependencies...> value_type;*/
typedef const Class value_type;
/*computed_value<Class, Type, Dependencies...> val;*/
const Class* val;
bool end;
iterator(const value_type& v) : val(&v) {}
iterator() : val(), end(true) {}
iterator(const iterator& i) : val(i.val) {}
const value_type& operator * () const { return *val; }
iterator& operator ++ () { val = NULL; return *this; }
iterator& operator ++ (int) { val = NULL; return *this; }
bool operator != (const iterator& i) const { return val != i.val; }
bool operator == (const iterator& i) const { return val == i.val; }
iterator& operator = (const iterator& i) { val = i.val; return *this; }
};
iterator begin() const { return {*this}; }
iterator end() const { return {}; }
typedef iterator const_iterator;
template <typename Type>
using computed_type = launch::computed_type<Type>;
/*static const bool skip_cache = false;*/
/*static const bool skip_registry = false;*/
static const bool skip_cache = true;
static const bool skip_registry = true;
};
#if 0
template <typename Class, typename Type, typename... Dependencies>
struct async_computed_value : value<computed_type<Type>> {
/*virtual Type do_compute(const Dependencies&...) = 0;*/
......@@ -199,6 +283,7 @@ struct async_computed_value : value<computed_type<Type>> {
operator const Type& () const { _fetch(); return val_cache; }
operator Type& () { _fetch(); return val_cache; }
const Type* operator -> () const { _fetch(); return &val_cache; }
void move(Type& dest) { _fetch(); dest = val_cache; }
......@@ -253,11 +338,15 @@ private:
void compute_and_save(const std::string& filename, Type& v)
{
std::ofstream ofs(filename);
cache_output ar(ofs);
ar & dependencies_digest();
v = compute_(indices(), dependencies);
ar & v;
if (!Class::skip_cache) {
std::ofstream ofs(filename);
cache_output ar(ofs);
ar & dependencies_digest();
v = compute_(indices(), dependencies);
ar & v;
} else {
v = compute_(indices(), dependencies);
}
}
std::string pretty_name() const
......@@ -270,13 +359,13 @@ private:
return typeid(simple_type).name();
}
Type compute_(const Dependencies&... t)
{
return Class::do_compute(t...);
/*Type compute_(const Dependencies&... t)*/
/*{*/
/*return Class::do_compute(t...);*/
/*Class* tmp = reinterpret_cast<Class*>(this);*/
/*MSG_DEBUG(__FILE__ << ':' << __LINE__ << " compute_@" << tmp << " from @" << this);*/
/*return tmp->do_compute(t...);*/
}
/*}*/
template <int... Indices>
Type compute_(sequence<Indices...>, const std::tuple<Dependencies...>& t)
......@@ -379,127 +468,7 @@ public:
return md5;
}
};
template <typename Class, typename Type, typename... Dependencies>
struct computed_value : value<Type> {
typedef Type value_type;
typedef std::tuple<Dependencies...> dependencies_type;
typedef async_computed_value<Class, Type, Dependencies...> task_type;
using value<Type>::output;
using value<Type>::val;
task_type* task;
computed_value(Dependencies&&... d)
: value<Type>(), task(task_type::get(d...))
{}
computed_value(const Dependencies&... d)
: value<Type>(), task(task_type::get(d...))
{}
computed_value(std::tuple<Dependencies...>&& t)
: value<Type>(), task(task_type::get(t))
{}
computed_value(const std::tuple<Dependencies...>& t)
: value<Type>(), task(task_type::get(t))
{}
computed_value(const computed_value<Class, Type, Dependencies...>& cv)
: value<Type>(cv.val), task(cv.task)
{}
computed_value(computed_value<Class, Type, Dependencies...>&& cv)
: value<Type>(cv.val), task(cv.task)
{}
template <typename... DerDep>
computed_value(const std::tuple<DerDep...>& ddt)
: value<Type>(), task(task_type::get(std::tuple<Dependencies...>(ddt)))
{}
template <typename... DerDep>
computed_value(const DerDep&... dd)
: value<Type>(), task(task_type::get(((const Dependencies&)dd)...))
{}
computed_value() : value<Type>(), task() {}
void __init(const std::tuple<Dependencies...>& d)
{
task = task_type::get(d);
}
Class& operator = (const computed_value<Class, Type, Dependencies...>& c)
{
/*if (task) {*/
/*delete task;*/
/*}*/
task = c.task;
return *static_cast<Class*>(this);
}
Class& operator = (computed_value<Class, Type, Dependencies...>&& c)
{
/*if (task) {*/
/*delete task;*/
/*}*/
task = std::move(c.task);
return *static_cast<Class*>(this);
}
operator const Type& () const
{
return *task;
}
/*Type& v() const { return *task; }*/
const Type& v() const { return *task; }
static const bool composite = false;
virtual
std::ostream&
output(std::ostream& os) const override
{
return os << v();
}
virtual
md5_digest&
to_md5(md5_digest& md5) const override
{
return task->to_md5(md5);
}
virtual
size_t hash() const
{
std::hash<dependencies_type> h;
return h(task->dependencies);
}
bool operator == (const Class& v) const
{
return task == v.task;
}
struct iterator {
/*typedef computed_value<Class, Type, Dependencies...> value_type;*/
typedef const Class value_type;
/*computed_value<Class, Type, Dependencies...> val;*/
const Class* val;
bool end;
iterator(const value_type& v) : val(&v) {}
iterator() : val(), end(true) {}
iterator(const iterator& i) : val(i.val) {}
const value_type& operator * () const { return *val; }
iterator& operator ++ () { val = NULL; return *this; }
iterator& operator ++ (int) { val = NULL; return *this; }
bool operator != (const iterator& i) const { return val != i.val; }
bool operator == (const iterator& i) const { return val == i.val; }
iterator& operator = (const iterator& i) { val = i.val; return *this; }
};
iterator begin() const { return {*this}; }
iterator end() const { return {}; }
typedef iterator const_iterator;
};
#endif
template <typename T>
struct naked_type {
......@@ -544,7 +513,8 @@ struct make_task_helper<true, TASK_CLASS, Elems...> {
do {
/*ret.emplace_back(TASK_CLASS::get(*cp));*/
/*MSG_DEBUG("make_task " << (*cp));*/
ret.emplace_back(*cp);
/*ret.emplace_back(*cp);*/
cp.push_to(ret);
} while (cp.next());
return ret;
}
......@@ -567,25 +537,29 @@ auto make_task(const std::tuple<Elems...>& t)
}
template <typename TASK_CLASS, typename... Elems>
auto make_task(std::tuple<Elems...>&& t)