Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve proxy management for data using COW #81

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 6 additions & 0 deletions src/cpp/AutoWIG.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
def controller(asg):
for node in ['class ::std::ctype',
'class ::std::ios_base']:
try:
asg[node].pybind11_export = False
except Exception as e:
print(e)
from scons_tools.site_autowig.controller.statiskit_stl import controller as stl_controller
return stl_controller(asg, library=False)
95 changes: 25 additions & 70 deletions src/cpp/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,40 @@
#include <memory>

#include "base.h"
#include "sample_space.h"

namespace statiskit
{
namespace __impl
{
double reldiff(const double& prev, const double& curr)
{ return fabs(curr-prev) / fabs(prev); }
{
return fabs(curr-prev) / fabs(prev);
}

double reldiff(const Eigen::VectorXd& prev, const Eigen::VectorXd& curr)
{
double norm = 0;
for(Index i = 0, max_index = prev.size(); i < max_index; ++i)
{
if(prev[i] == 0)
{ norm += pow(curr[i], 2); }
else
{ norm += pow((prev[i]-curr[i])/prev[i], 2); }
for (Index i = 0, max_index = prev.size(); i < max_index; ++i) {
if (prev[i] == 0) {
norm += pow(curr[i], 2);
} else {
norm += pow((prev[i]-curr[i])/prev[i], 2);
}
}
return sqrt(norm);
}

double reldiff(const Eigen::MatrixXd& prev, const Eigen::MatrixXd& curr)
{
double norm = 0;
for(Index i = 0, max_index = prev.rows(); i < max_index; ++i)
{
for(Index j = 0, max_index = prev.cols(); j < max_index; ++j)
{
if(prev(i,j) == 0)
{ norm += pow(curr(i,j), 2); }
else
{ norm += pow((prev(i,j)-curr(i,j))/prev(i,j), 2); }
for (Index i = 0, max_index = prev.rows(); i < max_index; ++i) {
for (Index j = 0, max_index = prev.cols(); j < max_index; ++j) {
if (prev(i,j) == 0) {
norm += pow(curr(i,j), 2);
} else {
norm += pow((prev(i,j)-curr(i,j))/prev(i,j), 2);
}
}
}
return sqrt(norm);
Expand All @@ -44,48 +46,22 @@ namespace statiskit
boost::mt19937 _random_generator = boost::mt19937(0);

boost::mt19937& get_random_generator()
{ return _random_generator; }

std::unordered_map< uintptr_t, unsigned int > iterations = std::unordered_map< uintptr_t, unsigned int >();

unsigned int get_maxits(const uintptr_t& ptr, const unsigned int& maxits)
{
unsigned int _maxits;
std::unordered_map< uintptr_t, unsigned int >::iterator it = iterations.find(ptr);
if(it == iterations.end())
{ _maxits = maxits; }
else
{ _maxits = it->second; }
return _maxits;
}

void set_maxits(const uintptr_t& ptr, const unsigned int& maxits)
{
std::unordered_map< uintptr_t, unsigned int >::iterator it = iterations.find(ptr);
if(it == iterations.end())
{ iterations[ptr] = maxits; }
else
{ it->second = maxits; }
}

void unset_maxits(const uintptr_t& ptr)
{
std::unordered_map< uintptr_t, unsigned int >::iterator it = iterations.find(ptr);
if(it != iterations.end())
{ iterations.erase(it); }
return _random_generator;
}
}

void set_seed()
{ __impl::_random_generator.seed(); }
{
__impl::_random_generator.seed();
}

void set_seed(const Index& seed)
{ __impl::_random_generator.seed(seed); }

not_implemented_error::not_implemented_error(const std::string& function) : std::runtime_error("'" + function + "' is not yet implemented")
{}
{
__impl::_random_generator.seed(seed);
}

proxy_connection_error::proxy_connection_error() : std::exception()
not_implemented_error::not_implemented_error(const std::string& function, const std::string& file, const unsigned int& line) : std::runtime_error("'" + function + "' in file '" + file + "' at line " + __impl::to_string(line) + " is not implemented")
{}

parameter_error::parameter_error(const std::string& parameter, const std::string& error) : std::runtime_error("'" + parameter + "' parameter: " + error)
Expand All @@ -99,25 +75,4 @@ namespace statiskit

nullptr_error::nullptr_error(const std::string& parameter) : parameter_error(parameter, "cannot be set to nullptr")
{}

Schedule::~Schedule()
{}

ExponentialSchedule::ExponentialSchedule(const double& theta)
{ _theta = theta; }

ExponentialSchedule::ExponentialSchedule(const ExponentialSchedule& schedule)
{ _theta = schedule._theta; }

ExponentialSchedule::~ExponentialSchedule()
{}

double ExponentialSchedule::operator() (const double& stage) const
{ return exp(- stage / _theta); }

const double& ExponentialSchedule::get_theta() const
{ return _theta; }

void ExponentialSchedule::set_theta(const double& theta)
{ _theta = theta; }
}
160 changes: 45 additions & 115 deletions src/cpp/base.h
Original file line number Diff line number Diff line change
@@ -1,59 +1,64 @@
#ifndef STATISKIT_CORE_BASE_H
#define STATISKIT_CORE_BASE_H
#pragma once

#include <statiskit/linalg/Eigen.h>
#include <statiskit/stl/STL.h>

#include <boost/random/mersenne_twister.hpp>
#include <boost/math/constants/constants.hpp>
#include <boost/random/uniform_01.hpp>
#include <type_traits>
#include <map>
#include <cassert>
#include <exception>
#include <assert.h>
#include <iostream>
#include <map>
#include <memory>
#include <type_traits>
#include <unordered_map>

#include <boost/math/constants/constants.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_01.hpp>

#include <statiskit/linalg/Eigen.h>
#include <statiskit/stl/STL.h>

#if defined WIN32 || defined _WIN32 || defined __CYGWIN__
#ifdef LIBSTATISKIT_CORE
#ifdef __GNUC__
#define STATISKIT_CORE_API __attribute__ ((dllexport))
#define __PRETTY_FUNCTION__ __FUNCSIG__
#ifdef LIBSTATISKIT_CORE
#ifdef __GNUC__
#define STATISKIT_CORE_API __attribute__ ((dllexport))
#else
#define STATISKIT_CORE_API __declspec(dllexport)
#endif
#else
#define STATISKIT_CORE_API __declspec(dllexport)
#ifdef __GNUC__
#define STATISKIT_CORE_API __attribute__ ((dllimport))
#else
#define STATISKIT_CORE_API __declspec(dllimport)
#endif
#endif
#else
#ifdef __GNUC__
#define STATISKIT_CORE_API __attribute__ ((dllimport))
#else
#if __GNUC__ >= 4
#define STATISKIT_CORE_API __attribute__ ((visibility ("default")))
#else
#define STATISKIT_CORE_API __declspec(dllimport)
#define STATISKIT_CORE_API
#endif
#endif
#else
#if __GNUC__ >= 4
#define STATISKIT_CORE_API __attribute__ ((visibility ("default")))
#else
#define STATISKIT_CORE_API
#endif
#endif

#define NOT_IMPLEMENTED() _Pragma("message \"not implemented function\""); throw not_implemented_error(__PRETTY_FUNCTION__, __FILE__, __LINE__)

#define INFOPOINT() std::cout << __PRETTY_FUNCTION__ << "in file '" << __FILE__ << "' at line " << __LINE__ << std::endl

#ifdef NDEBUG
#define BREAKPOINT __pragma(message("BREAKPOINT found in file '" __FILE__ "' at line " STR(__LINE__)) " but not used");
#define BREAKPOINT() _Pragma("message \"breakpoint ignored\"")
#else
#include <csignal>
#define BREAKPOINT std::raise(SIGINT);
#define BREAKPOINT() std::raise(SIGINT)
#endif

namespace statiskit
{

template<class T, class D, class B=T> struct PolymorphicCopy : public B
template<class D, class B=D> struct PolymorphicCopy : public B
{
PolymorphicCopy();
PolymorphicCopy(const PolymorphicCopy<T, D, B>& other);
using B::B;

virtual ~PolymorphicCopy() = default;

virtual std::unique_ptr< T > copy() const;
virtual std::unique_ptr< typename B::copy_type > copy() const;
};

namespace __impl
Expand Down Expand Up @@ -82,16 +87,19 @@ namespace statiskit
template<class T> void merge(std::unordered_set< T >& lhs, const std::unordered_set< T >& rhs);

template<class U, class V> std::set< U > keys(const std::map< U, V >& map);

template<class T> void delete_vector(std::vector<T>& v);
template<class T> void delete_vector(std::vector<T*>& v);

template<class T> std::vector<T> copy_vector(const std::vector<T>& v);
template<class T> std::vector<T*> copy_vector(const std::vector<T*>& v);
}

STATISKIT_CORE_API void set_seed();
STATISKIT_CORE_API void set_seed(const Index& seed);

struct STATISKIT_CORE_API not_implemented_error : std::runtime_error
{ not_implemented_error(const std::string& function); };

struct STATISKIT_CORE_API proxy_connection_error : std::exception
{ proxy_connection_error(); };
{ not_implemented_error(const std::string& function, const std::string& file, const unsigned int& line); };

struct STATISKIT_CORE_API parameter_error : std::runtime_error
{ parameter_error(const std::string& parameter, const std::string& error); };
Expand Down Expand Up @@ -127,84 +135,6 @@ namespace statiskit

struct STATISKIT_CORE_API duplicated_value_error : parameter_error
{ template<typename T> duplicated_value_error(const std::string& parameter, const T& value); };

template<class T>
class Optimization : public T
{
public:
Optimization();
Optimization(const Optimization< T >& optimization);
virtual ~Optimization();

const double& get_mindiff() const;
void set_mindiff(const double& mindiff);

unsigned int get_minits() const;
void set_minits(const unsigned int& maxits);

unsigned int get_maxits() const;
void set_maxits(const unsigned int& maxits);

protected:
double _mindiff;
unsigned int _minits;
unsigned int _maxits;

bool run(const unsigned int& its, const double& delta) const;
};

struct STATISKIT_CORE_API Schedule
{
virtual ~Schedule() = 0;

virtual double operator() (const double& stage) const = 0;

virtual std::unique_ptr< Schedule > copy() const = 0;
};

class STATISKIT_CORE_API ExponentialSchedule : public PolymorphicCopy< Schedule, ExponentialSchedule >
{
public:
ExponentialSchedule(const double& theta);
ExponentialSchedule(const ExponentialSchedule& schedule);
virtual ~ExponentialSchedule();

virtual double operator() (const double& stage) const;

const double& get_theta() const;
void set_theta(const double& theta);

protected:
double _theta;
};

template<class T>
class SimulatedAnnealing : public T
{
public:
SimulatedAnnealing();
SimulatedAnnealing(const SimulatedAnnealing< T >& simulated_annealing);
virtual ~SimulatedAnnealing();

const Schedule* get_schedule() const;
void set_schedule(const Schedule& schedule);

unsigned int get_minits() const;
void set_minits(const unsigned int& maxits);

unsigned int get_maxits() const;
void set_maxits(const unsigned int& maxits);

protected:
Schedule* _schedule;
unsigned int _minits;
unsigned int _maxits;

bool accept(const unsigned int& its, const double& delta) const;
};
}

#ifndef AUTOWIG
#include "base.hpp"
#endif
#endif
#include "base.hpp"
Loading