Skip to content

Commit

Permalink
Merge pull request #238 from ye-luo/update-containers
Browse files Browse the repository at this point in the history
Update containers as QMCPACK
  • Loading branch information
ye-luo authored Jun 7, 2019
2 parents de8572c + 7cd3929 commit 80555fb
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 74 deletions.
66 changes: 26 additions & 40 deletions src/Numerics/OhmmsPETE/OhmmsMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@

namespace qmcplusplus
{
template<class T, typename Alloc = std::allocator<T>, unsigned MemType = MemorySpace::HOST>
template<class T, typename Alloc = std::allocator<T>>
class Matrix
{
public:
typedef T Type_t;
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef Vector<T, Alloc, MemType> Container_t;
typedef Vector<T, Alloc> Container_t;
typedef typename Container_t::size_type size_type;
typedef typename Container_t::iterator iterator;
typedef Matrix<T, Alloc, MemType> This_t;
typedef Matrix<T, Alloc> This_t;

Matrix() : D1(0), D2(0), TotSize(0) {} // Default Constructor initializes to zero.

Expand All @@ -56,7 +56,7 @@ class Matrix
Matrix(const This_t& rhs)
{
resize(rhs.D1, rhs.D2);
if (MemType == MemorySpace::HOST)
if (allocator_traits<Alloc>::is_host_accessible)
assign(*this, rhs);
}

Expand Down Expand Up @@ -107,38 +107,32 @@ class Matrix
X.attachReference(ref, TotSize);
}

template<typename Allocator = Alloc, typename = IsHostSafe<Allocator>>
inline void add(size_type n) // you can add rows: adding columns are forbidden
{
static_assert(MemType == MemorySpace::HOST, "Matrix::add MemType must be MemorySpace::HOST");
X.insert(X.end(), n * D2, T());
D1 += n;
}

template<typename Allocator = Alloc, typename = IsHostSafe<Allocator>>
inline void copy(const This_t& rhs)
{
static_assert(MemType == MemorySpace::HOST, "Matrix::copy MemType must be MemorySpace::HOST");
resize(rhs.D1, rhs.D2);
assign(*this, rhs);
}

// Assignment Operators
inline This_t& operator=(const This_t& rhs)
{
static_assert(MemType == MemorySpace::HOST, "Matrix::operator= MemType must be MemorySpace::HOST");
resize(rhs.D1, rhs.D2);
return assign(*this, rhs);
}

inline const This_t& operator=(const This_t& rhs) const
{
static_assert(MemType == MemorySpace::HOST, "Matrix::operator= MemType must be MemorySpace::HOST");
return assign(*this, rhs);
if (allocator_traits<Alloc>::is_host_accessible)
assign(*this, rhs);
return *this;
}

template<class RHS>
template<class RHS, typename Allocator = Alloc, typename = IsHostSafe<Allocator>>
This_t& operator=(const RHS& rhs)
{
static_assert(MemType == MemorySpace::HOST, "Matrix::operator= MemType must be MemorySpace::HOST");
return assign(*this, rhs);
}

Expand Down Expand Up @@ -172,35 +166,36 @@ class Matrix
/// returns a pointer of i-th row, g++ iterator problem
inline Type_t* operator[](size_type i) { return X.data() + i * D2; }

template<typename Allocator = Alloc, typename = IsHostSafe<Allocator>>
inline Type_t& operator()(size_type i)
{
static_assert(MemType == MemorySpace::HOST, "Matrix::operator() MemType must be MemorySpace::HOST");
return X[i];
}

// returns the i-th value in D1*D2 vector
template<typename Allocator = Alloc, typename = IsHostSafe<Allocator>>
inline Type_t operator()(size_type i) const
{
static_assert(MemType == MemorySpace::HOST, "Matrix::operator() MemType must be MemorySpace::HOST");
return X[i];
}

// returns val(i,j)
template<typename Allocator = Alloc, typename = IsHostSafe<Allocator>>
inline Type_t& operator()(size_type i, size_type j)
{
static_assert(MemType == MemorySpace::HOST, "Matrix::operator() MemType must be MemorySpace::HOST");
return X[i * D2 + j];
}

// returns val(i,j)
template<typename Allocator = Alloc, typename = IsHostSafe<Allocator>>
inline const Type_t& operator()(size_type i, size_type j) const
{
static_assert(MemType == MemorySpace::HOST, "Matrix::operator() MemType must be MemorySpace::HOST");
return X[i * D2 + j];
}

template<typename Allocator = Alloc, typename = IsHostSafe<Allocator>>
inline void swap_rows(int r1, int r2)
{
static_assert(MemType == MemorySpace::HOST, "Matrix::operator() MemType must be MemorySpace::HOST");
for (int col = 0; col < D2; col++)
{
Type_t tmp = (*this)(r1, col);
Expand All @@ -209,9 +204,9 @@ class Matrix
}
}

template<typename Allocator = Alloc, typename = IsHostSafe<Allocator>>
inline void swap_cols(int c1, int c2)
{
static_assert(MemType == MemorySpace::HOST, "Matrix::operator() MemType must be MemorySpace::HOST");
for (int row = 0; row < D1; row++)
{
Type_t tmp = (*this)(row, c1);
Expand All @@ -220,27 +215,23 @@ class Matrix
}
}


template<class IT>
template<class IT, typename Allocator = Alloc, typename = IsHostSafe<Allocator>>
inline void replaceRow(IT first, size_type i)
{
static_assert(MemType == MemorySpace::HOST, "Matrix::operator() MemType must be MemorySpace::HOST");
std::copy(first, first + D2, X.begin() + i * D2);
}

template<class IT>
template<class IT, typename Allocator = Alloc, typename = IsHostSafe<Allocator>>
inline void replaceColumn(IT first, size_type j)
{
static_assert(MemType == MemorySpace::HOST, "Matrix::operator() MemType must be MemorySpace::HOST");
typename Container_t::iterator ii(X.begin() + j);
for (int i = 0; i < D1; i++, ii += D2)
*ii = *first++;
}

template<class IT>
template<class IT, typename Allocator = Alloc, typename = IsHostSafe<Allocator>>
inline void add2Column(IT first, size_type j)
{
static_assert(MemType == MemorySpace::HOST, "Matrix::operator() MemType must be MemorySpace::HOST");
typename Container_t::iterator ii(X.begin() + j);
for (int i = 0; i < D1; i++, ii += D2)
*ii += *first++;
Expand All @@ -253,10 +244,9 @@ class Matrix
* \param i0 starting row where the copying is done
* \param j0 starting column where the copying is done
*/
template<class T1>
template<class T1, typename Allocator = Alloc, typename = IsHostSafe<Allocator>>
inline void add(const T1* sub, size_type d1, size_type d2, size_type i0, size_type j0)
{
static_assert(MemType == MemorySpace::HOST, "Matrix::operator() MemType must be MemorySpace::HOST");
int ii = 0;
for (int i = 0; i < d1; i++)
{
Expand All @@ -268,10 +258,9 @@ class Matrix
}
}

template<class T1>
template<class T1, typename Allocator = Alloc, typename = IsHostSafe<Allocator>>
inline void add(const T1* sub, size_type d1, size_type d2, size_type i0, size_type j0, const T& phi)
{
static_assert(MemType == MemorySpace::HOST, "Matrix::operator() MemType must be MemorySpace::HOST");
size_type ii = 0;
for (size_type i = 0; i < d1; i++)
{
Expand All @@ -283,10 +272,9 @@ class Matrix
}
}

template<class SubMat_t>
template<class SubMat_t, typename Allocator = Alloc, typename = IsHostSafe<Allocator>>
inline void add(const SubMat_t& sub, unsigned int i0, unsigned int j0)
{
static_assert(MemType == MemorySpace::HOST, "Matrix::operator() MemType must be MemorySpace::HOST");
size_type ii = 0;
for (size_type i = 0; i < sub.rows(); i++)
{
Expand All @@ -298,9 +286,9 @@ class Matrix
}
}

template<typename Allocator = Alloc, typename = IsHostSafe<Allocator>>
inline void add(const This_t& sub, unsigned int i0, unsigned int j0)
{
static_assert(MemType == MemorySpace::HOST, "Matrix::operator() MemType must be MemorySpace::HOST");
size_type ii = 0;
for (size_type i = 0; i < sub.rows(); i++)
{
Expand All @@ -312,18 +300,16 @@ class Matrix
}
}

template<class Msg>
template<class Msg, typename Allocator = Alloc, typename = IsHostSafe<Allocator>>
inline Msg& putMessage(Msg& m)
{
static_assert(MemType == MemorySpace::HOST, "Matrix::operator() MemType must be MemorySpace::HOST");
m.Pack(X.data(), D1 * D2);
return m;
}

template<class Msg>
template<class Msg, typename Allocator = Alloc, typename = IsHostSafe<Allocator>>
inline Msg& getMessage(Msg& m)
{
static_assert(MemType == MemorySpace::HOST, "Matrix::operator() MemType must be MemorySpace::HOST");
m.Unpack(X.data(), D1 * D2);
return m;
}
Expand Down
47 changes: 25 additions & 22 deletions src/Numerics/OhmmsPETE/OhmmsVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
#include <type_traits>
#include <stdexcept>
#include "Numerics/PETE/PETE.h"
#include "Utilities/SIMD/MemorySpace.hpp"
#include "Utilities/SIMD/allocator_traits.hpp"

namespace qmcplusplus
{
template<class T, typename Alloc = std::allocator<T>, unsigned MemType = MemorySpace::HOST>
template<class T, typename Alloc = std::allocator<T>>
class Vector
{
public:
Expand All @@ -48,7 +48,7 @@ class Vector
if (n)
{
resize_impl(n);
if (MemType == MemorySpace::HOST)
if (allocator_traits<Alloc>::is_host_accessible)
std::fill_n(X, n, val);
}
}
Expand All @@ -60,28 +60,26 @@ class Vector
Vector(const Vector& rhs) : nLocal(rhs.nLocal), nAllocated(0), X(nullptr)
{
resize_impl(rhs.nLocal);
if (MemType == MemorySpace::HOST)
if (allocator_traits<Alloc>::is_host_accessible)
std::copy_n(rhs.data(), nLocal, X);
}

// default assignment operator
inline Vector& operator=(const Vector& rhs)
{
static_assert(MemType == MemorySpace::HOST, "Vector::operator= MemType must be MemorySpace::HOST");
if (this == &rhs)
return *this;
if (nLocal != rhs.nLocal)
resize(rhs.nLocal);
std::copy_n(rhs.data(), nLocal, X);
if (allocator_traits<Alloc>::is_host_accessible)
std::copy_n(rhs.data(), nLocal, X);
return *this;
}

// assignment operator from anther Vector class
template<typename T1, typename C1>
template<typename T1, typename C1, typename Allocator = Alloc, typename = IsHostSafe<Allocator>>
inline Vector& operator=(const Vector<T1, C1>& rhs)
{
static_assert(MemType == MemorySpace::HOST,
"Vector::operator= the MemType of both sides must be MemorySpace::HOST");
if (std::is_convertible<T1, T>::value)
{
if (nLocal != rhs.nLocal)
Expand All @@ -92,10 +90,9 @@ class Vector
}

// assigment operator to enable PETE
template<class RHS>
template<class RHS, typename Allocator = Alloc, typename = IsHostSafe<Allocator>>
inline Vector& operator=(const RHS& rhs)
{
static_assert(MemType == MemorySpace::HOST, "Vector::operator= MemType must be MemorySpace::HOST");
assign(*this, rhs);
return *this;
}
Expand Down Expand Up @@ -128,20 +125,26 @@ class Vector
static_assert(std::is_same<value_type, typename Alloc::value_type>::value, "Vector and Alloc data types must agree!");
if (nLocal > nAllocated)
throw std::runtime_error("Resize not allowed on Vector constructed by initialized memory.");
if (n > nAllocated)
if(allocator_traits<Alloc>::is_host_accessible)
{
resize_impl(n);
if (MemType == MemorySpace::HOST)
if (n > nAllocated)
{
resize_impl(n);
std::fill_n(X, n, val);
}
else
{
if (n > nLocal) std::fill_n(X + nLocal, n - nLocal, val);
nLocal = n;
}
}
else if (n > nLocal)
else
{
if (MemType == MemorySpace::HOST)
std::fill_n(X + nLocal, n - nLocal, val);
nLocal = n;
if (n > nAllocated)
resize_impl(n);
else
nLocal = n;
}
else
nLocal = n;
return;
}

Expand All @@ -161,15 +164,15 @@ class Vector
}

// Get and Set Operations
template<typename Allocator = Alloc, typename = IsHostSafe<Allocator>>
inline Type_t& operator[](size_t i)
{
static_assert(MemType == MemorySpace::HOST, "Vector::operator[] MemType must be MemorySpace::HOST");
return X[i];
}

template<typename Allocator = Alloc, typename = IsHostSafe<Allocator>>
inline const Type_t& operator[](size_t i) const
{
static_assert(MemType == MemorySpace::HOST, "Vector::operator[] MemType must be MemorySpace::HOST");
return X[i];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,27 @@
//
// File created by: Ye Luo, [email protected], Argonne National Laboratory
//////////////////////////////////////////////////////////////////////////////////////
// -*- C++ -*-
/** @file MemorySpace.hpp
*/
#ifndef QMCPLUSPLUS_MEMORYSPACE_H
#define QMCPLUSPLUS_MEMORYSPACE_H


#ifndef QMCPLUSPLUS_ACCESS_TRAITS_H
#define QMCPLUSPLUS_ACCESS_TRAITS_H

namespace qmcplusplus
{
struct MemorySpace
/** template class defines whether the memory allocated by the allocator is host accessible
*/
template<class Allocator>
struct allocator_traits
{
enum
{
HOST = 0,
CUDA
};
const static bool is_host_accessible = true;
};

template<class Allocator>
using IsHostSafe = typename std::enable_if<allocator_traits<Allocator>::is_host_accessible>::type;

template<class Allocator>
using IsNotHostSafe = typename std::enable_if<!allocator_traits<Allocator>::is_host_accessible>::type;

} // namespace qmcplusplus

#endif
#endif // QMCPLUSPLUS_ACCESS_TRAITS_H

0 comments on commit 80555fb

Please sign in to comment.