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

Create a pool wrapper of DynamicSparseNumberArray #29

Closed
wants to merge 5 commits into from
Closed
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ include_HEADERS += numerics/include/metaphysicl/dualsparsenumberstruct.h
include_HEADERS += numerics/include/metaphysicl/dualsparsenumbervector.h
include_HEADERS += numerics/include/metaphysicl/dynamicsparsenumberarray.h
include_HEADERS += numerics/include/metaphysicl/dynamicsparsenumberarray_decl.h
include_HEADERS += numerics/include/metaphysicl/pool_dynamicsparsenumberarray.h
include_HEADERS += numerics/include/metaphysicl/pool_dynamicsparsenumberarray_decl.h
include_HEADERS += numerics/include/metaphysicl/dual_pool_dynamicsparsenumberarray.h
include_HEADERS += numerics/include/metaphysicl/dynamicsparsenumberbase.h
include_HEADERS += numerics/include/metaphysicl/dynamicsparsenumberbase_decl.h
include_HEADERS += numerics/include/metaphysicl/dynamicsparsenumbervector.h
Expand Down Expand Up @@ -89,6 +92,7 @@ include_HEADERS += utilities/include/metaphysicl/metaprogramming.h
include_HEADERS += utilities/include/metaphysicl/testable.h
include_HEADERS += utilities/include/metaphysicl/dynamic_std_array_wrapper.h
include_HEADERS += utilities/include/metaphysicl/parallel_dynamic_std_array_wrapper.h
include_HEADERS += utilities/include/metaphysicl/shared_pool.h

# Needs to be builddir since this is generated by configure
include_HEADERS += $(top_builddir)/src/utilities/include/metaphysicl/metaphysicl_version.h
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//-----------------------------------------------------------------------bl-
//--------------------------------------------------------------------------
//
// MetaPhysicL - A metaprogramming library for physics calculations
//
// Copyright (C) 2013 The PECOS Development Team
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the Version 2.1 GNU Lesser General
// Public License as published by the Free Software Foundation.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc. 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301 USA
//
//-----------------------------------------------------------------------el-

#ifndef METAPHYSICL_DUAL_POOLDYNAMICSPARSENUMBERARRAY_H
#define METAPHYSICL_DUAL_POOLDYNAMICSPARSENUMBERARRAY_H

#include "metaphysicl/dualnumber.h"
#include "metaphysicl/pool_dynamicsparsenumberarray.h"

namespace MetaPhysicL
{

template <typename T, typename I>
struct DerivativeType<PoolDynamicSparseNumberArray<T, I>>
{
typedef PoolDynamicSparseNumberArray<typename DerivativeType<T>::type, I> type;
};

template <typename T, typename I>
struct DerivativesType<PoolDynamicSparseNumberArray<T, I>>
{
typedef PoolDynamicSparseNumberArray<typename DerivativesType<T>::type, I> type;
};

template <typename T, typename I>
inline typename DerivativeType<PoolDynamicSparseNumberArray<T, I>>::type
derivative(const PoolDynamicSparseNumberArray<T, I> & a, unsigned int derivativeindex)
{
typename DerivativeType<PoolDynamicSparseNumberArray<T, I>>::type returnval;
*returnval.dsna_ptr = derivative(*a.dsna_ptr, derivativeindex);
return returnval;
}

template <typename T, typename I>
inline typename DerivativesType<PoolDynamicSparseNumberArray<T, I>>::type
derivatives(const PoolDynamicSparseNumberArray<T, I> & a)
{
typename DerivativeType<PoolDynamicSparseNumberArray<T, I>>::type returnval;
*returnval.dsna_ptr = derivatives(*a.dsna_ptr);
return returnval;
}

template <typename T, typename I, unsigned int derivativeindex>
struct DerivativeOf<PoolDynamicSparseNumberArray<T, I>, derivativeindex>
{
static typename DerivativeType<PoolDynamicSparseNumberArray<T, I>>::type
derivative(const PoolDynamicSparseNumberArray<T, I> & a);
};

// DualNumber is subordinate to PoolDynamicSparseNumberArray

#define DualPoolDynamicSparseNumberArray_comparisons(templatename) \
template <typename T, \
typename T2, \
typename D, \
typename I, \
bool asd, \
bool reverseorder> \
struct templatename<PoolDynamicSparseNumberArray<T2, I>, DualNumber<T, D, asd>, reverseorder> \
{ \
typedef PoolDynamicSparseNumberArray< \
typename Symmetric##templatename<T2, DualNumber<T, D, asd>, reverseorder>::supertype, \
I> \
supertype; \
}

DualPoolDynamicSparseNumberArray_comparisons(CompareTypes);
DualPoolDynamicSparseNumberArray_comparisons(PlusType);
DualPoolDynamicSparseNumberArray_comparisons(MinusType);
DualPoolDynamicSparseNumberArray_comparisons(MultipliesType);
DualPoolDynamicSparseNumberArray_comparisons(DividesType);
DualPoolDynamicSparseNumberArray_comparisons(AndType);
DualPoolDynamicSparseNumberArray_comparisons(OrType);

} // namespace MetaPhysicL

#endif // METAPHYSICL_DUAL_POOLDYNAMICSPARSENUMBERARRAY_H
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ using MetaPhysicL::DynamicSparseNumberArray;
template <typename T, typename I>
class numeric_limits<DynamicSparseNumberArray<T, I> > :
public MetaPhysicL::raw_numeric_limits<DynamicSparseNumberArray<T, I>, T> {};

} // namespace std


Expand Down
78 changes: 71 additions & 7 deletions src/numerics/include/metaphysicl/dynamicsparsenumberbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@

namespace MetaPhysicL {

template <typename Data, typename Indices, template <class...> class SubType, class... SubTypeArgs>
std::stack<Data, std::vector<Data>> DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::_data_stack;

template <typename Data, typename Indices, template <class...> class SubType, class... SubTypeArgs>
std::stack<Indices, std::vector<Indices>> DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::_indices_stack;

template <typename Data, typename Indices, template <class...> class SubType, class... SubTypeArgs>
inline
std::size_t
Expand All @@ -52,27 +58,85 @@ DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::resize(std::siz

template <typename Data, typename Indices, template <class...> class SubType, class... SubTypeArgs>
inline
DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::DynamicSparseNumberBase() {}
void
DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::acquire()
{
if (!_data_stack.empty())
{
_data.swap(_data_stack.top());
_data_stack.pop();
}
if (!_indices_stack.empty())
{
_indices.swap(_indices_stack.top());
_indices_stack.pop();
}
}

template <typename Data, typename Indices, template <class...> class SubType, class... SubTypeArgs>
inline
void
DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::release()
{
_data_stack.push(std::move(_data));
_indices_stack.push(std::move(_indices));
}

template <typename Data, typename Indices, template <class...> class SubType, class... SubTypeArgs>
inline
DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::DynamicSparseNumberBase()
{
this->acquire();
this->resize(0);
}

template <typename Data, typename Indices, template <class...> class SubType, class... SubTypeArgs>
inline
DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::DynamicSparseNumberBase(
const DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...> & src)
{
this->acquire();
this->resize(src.size());
std::copy(src.nude_data().begin(), src.nude_data().end(), _data.begin());
std::copy(src.nude_indices().begin(), src.nude_indices().end(), _indices.begin());
}

template <typename Data, typename Indices, template <class...> class SubType, class... SubTypeArgs>
template <typename Data2, typename Indices2, class... SubTypeArgs2>
inline
DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::
DynamicSparseNumberBase(const DynamicSparseNumberBase<Data2, Indices2, SubType, SubTypeArgs2...> & src)
{ this->resize(src.size());
{
this->acquire();
this->resize(src.size());
std::copy(src.nude_data().begin(), src.nude_data().end(), _data.begin());
std::copy(src.nude_indices().begin(), src.nude_indices().end(), _indices.begin()); }
std::copy(src.nude_indices().begin(), src.nude_indices().end(), _indices.begin());
}

template <typename Data, typename Indices, template <class...> class SubType, class... SubTypeArgs>
inline DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::DynamicSparseNumberBase(
DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...> && src)
{
this->acquire();
_data.swap(src.nude_data());
_indices.swap(src.nude_indices());
}

#ifdef METAPHYSICL_USE_STD_MOVE
template <typename Data, typename Indices, template <class...> class SubType, class... SubTypeArgs>
template <typename Data2, typename Indices2, class... SubTypeArgs2>
inline DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::DynamicSparseNumberBase(
DynamicSparseNumberBase<Data2, Indices2, SubType, SubTypeArgs2...> && src)
{
_data = std::move(src.nude_data());
_indices = std::move(src.nude_indices());
this->acquire();
_data.swap(src.nude_data());
_indices.swap(src.nude_indices());
}

template <typename Data, typename Indices, template <class...> class SubType, class... SubTypeArgs>
inline DynamicSparseNumberBase<Data, Indices, SubType, SubTypeArgs...>::~DynamicSparseNumberBase()
{
this->release();
}
#endif // METAPHYSICL_USE_STD_MOVE

template <typename Data, typename Indices, template <class...> class SubType, class... SubTypeArgs>
inline
Expand Down
15 changes: 11 additions & 4 deletions src/numerics/include/metaphysicl/dynamicsparsenumberbase_decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <functional>
#include <ostream>
#include <stdexcept>
#include <stack>

namespace MetaPhysicL {

Expand All @@ -67,23 +68,23 @@ class DynamicSparseNumberBase

DynamicSparseNumberBase();

#if METAPHYSICL_USE_STD_MOVE
// Move constructors are useful when all your data is on the heap
DynamicSparseNumberBase(DynamicSparseNumberBase && src) = default;
DynamicSparseNumberBase(DynamicSparseNumberBase && src);

// Move assignment avoids heap operations too
DynamicSparseNumberBase& operator= (DynamicSparseNumberBase && src) = default;

// Standard copy operations get implicitly deleted upon move
// constructor definition, so we manually enable them.
DynamicSparseNumberBase(const DynamicSparseNumberBase & src) = default;
DynamicSparseNumberBase(const DynamicSparseNumberBase & src);

DynamicSparseNumberBase& operator= (const DynamicSparseNumberBase & src) = default;

template <typename Data2, typename Indices2, class... SubTypeArgs2>
DynamicSparseNumberBase(
DynamicSparseNumberBase<Data2, Indices2, SubType, SubTypeArgs2...> && src);
#endif

~DynamicSparseNumberBase();

template <typename Data2, typename Indices2, class... SubTypeArgs2>
DynamicSparseNumberBase(
Expand Down Expand Up @@ -178,6 +179,12 @@ class DynamicSparseNumberBase

Data _data;
Indices _indices;

void acquire();
void release();

static std::stack<Data, std::vector<Data>> _data_stack;
static std::stack<Indices, std::vector<Indices>> _indices_stack;
};


Expand Down
Loading