1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-11-10 07:55:35 +00:00

Coordinate handling GPU ready avoid malloc

This commit is contained in:
paboyle 2018-02-24 22:25:39 +00:00
parent 1c16ffa1c1
commit ff7b19a71b
5 changed files with 152 additions and 29 deletions

108
lib/util/Coordinate.h Normal file
View File

@ -0,0 +1,108 @@
/*************************************************************************************
Grid physics library, www.github.com/paboyle/Grid
Source file: ./lib/cartesian/Coordinate.h
Copyright (C) 2018
Author: Peter Boyle <paboyle@ph.ed.ac.uk>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
See the full license in the file "LICENSE" in the top level distribution directory
*************************************************************************************/
/* END LEGAL */
#pragma once
////////////////////////////////////////////////////////////////////////////
// Provide a stack resident container for coordinates, or extents
////////////////////////////////////////////////////////////////////////////
NAMESPACE_BEGIN(Grid);
template<class _T,int MaxEntries>
class AcceleratorVector {
public:
typedef _T value;
typedef int size_type;
typedef value & reference;
typedef const value & const_reference;
typedef value * pointer;
typedef const value * const_pointer;
private:
value _data[MaxEntries];
size_type _size;
public:
accelerator_inline reference operator[](size_type __n) { return _data[__n];}
accelerator_inline const_reference operator[](size_type __n) const { return _data[__n];}
accelerator_inline size_type size(void) const { return _size; };
accelerator_inline void clear(void) { resize(0);}
accelerator_inline void resize(size_type sz) {
assert(sz>=0);
assert(sz<MaxEntries);
_size = sz;
}
accelerator_inline void resize(size_type sz,const value &val) {
assert(sz>=0);
assert(sz<MaxEntries);
_size = sz;
for(int s=0;s<sz;s++) _data[s]=val;
}
accelerator_inline pointer begin(void) { return _data; }
accelerator_inline pointer end (void) { return &_data[_size]; }
accelerator_inline void push_back(const value &val) { resize(_size+1); _data[_size-1] = val;}
accelerator_inline AcceleratorVector() { _size = 0; }
accelerator_inline AcceleratorVector(size_type sz) { resize(sz); }
accelerator_inline AcceleratorVector(size_type sz,const value &val) { resize(sz,val); }
AcceleratorVector(const std::vector<value> &copyme) {
resize(copyme.size());
for(int s=0;s<_size;s++){
_data[s] = copyme[s];
}
}
std::vector<value> toVector(void) const {
auto clone =*this;
std::vector<value> ret;
std::copy(clone.begin(),clone.end(),std::back_inserter(ret));
return ret;
}
};
////////////////////////////////////////////////////////////////
// Coordinate class, maxdims = 8 for now.
////////////////////////////////////////////////////////////////
#define GRID_MAX_LATTICE_DIMENSION (8)
#define GRID_MAX_SIMD (16)
static constexpr int MaxDims = GRID_MAX_LATTICE_DIMENSION;
typedef AcceleratorVector<int,MaxDims> Coordinate;
inline std::ostream & operator<<(std::ostream &os, const Coordinate &v)
{
os << "[";
for(int s=0;s<v.size();s++) {
os << v[s] << " ";
}
if (v.size() > 0) {
os << "\b";
}
os << "]";
return os;
}
NAMESPACE_END(Grid);

View File

@ -79,18 +79,18 @@ NAMESPACE_BEGIN(Grid);
// Convenience functions to access stadard command line arg
// driven parallelism controls
//////////////////////////////////////////////////////
static std::vector<int> Grid_default_latt;
static std::vector<int> Grid_default_mpi;
static Coordinate Grid_default_latt;
static Coordinate Grid_default_mpi;
int GridThread::_threads =1;
int GridThread::_hyperthreads=1;
int GridThread::_cores=1;
const std::vector<int> &GridDefaultLatt(void) {return Grid_default_latt;};
const std::vector<int> &GridDefaultMpi(void) {return Grid_default_mpi;};
const std::vector<int> GridDefaultSimd(int dims,int nsimd)
const Coordinate &GridDefaultLatt(void) {return Grid_default_latt;};
const Coordinate &GridDefaultMpi(void) {return Grid_default_mpi;};
const Coordinate GridDefaultSimd(int dims,int nsimd)
{
std::vector<int> layout(dims);
Coordinate layout(dims);
int nn=nsimd;
for(int d=dims-1;d>=0;d--){
if ( nn>=2) {
@ -138,7 +138,8 @@ void GridCmdOptionCSL(std::string str,std::vector<std::string> & vec)
return;
}
void GridCmdOptionIntVector(std::string &str,std::vector<int> & vec)
template<class VectorInt>
void GridCmdOptionIntVector(std::string &str,VectorInt & vec)
{
vec.resize(0);
std::stringstream ss(str);
@ -160,11 +161,11 @@ void GridCmdOptionInt(std::string &str,int & val)
void GridParseLayout(char **argv,int argc,
std::vector<int> &latt,
std::vector<int> &mpi)
Coordinate &latt_c,
Coordinate &mpi_c)
{
mpi =std::vector<int>({1,1,1,1});
latt=std::vector<int>({8,8,8,8});
auto mpi =std::vector<int>({1,1,1,1});
auto latt=std::vector<int>({8,8,8,8});
GridThread::SetMaxThreads();
@ -194,9 +195,22 @@ void GridParseLayout(char **argv,int argc,
GridCmdOptionInt(arg,cores);
GridThread::SetCores(cores);
}
// Copy back into coordinate format
int nd = mpi.size();
assert(latt.size()==nd);
latt_c.resize(nd);
mpi_c.resize(nd);
for(int d=0;d<nd;d++){
latt_c[d] = latt[d];
mpi_c[d] = mpi[d];
}
}
std::string GridCmdVectorIntToString(const std::vector<int> & vec){
template<class VectorInt>
std::string GridCmdVectorIntToString(const VectorInt & vec_in){
int sz = vec_in.size();
std::vector<int> vec(sz);
for(int s=0;s<sz;s++) vec[s] = vec_in[s];
std::ostringstream oss;
std::copy(vec.begin(), vec.end(),std::ostream_iterator<int>(oss, " "));
return oss.str();

View File

@ -40,26 +40,22 @@ void Grid_debug_handler_init(void);
void Grid_quiesce_nodes(void);
void Grid_unquiesce_nodes(void);
const std::vector<int> GridDefaultSimd(int dims,int nsimd);
const std::vector<int> &GridDefaultLatt(void);
const std::vector<int> &GridDefaultMpi(void);
const int &GridThreads(void) ;
void GridSetThreads(int t) ;
const Coordinate GridDefaultSimd(int dims,int nsimd);
const Coordinate &GridDefaultLatt(void);
const Coordinate &GridDefaultMpi(void);
const int &GridThreads(void) ;
void GridSetThreads(int t) ;
void GridLogTimestamp(int);
void GridLogLayout();
// Common parsing chores
std::string GridCmdOptionPayload(char ** begin, char ** end, const std::string & option);
bool GridCmdOptionExists(char** begin, char** end, const std::string& option);
std::string GridCmdVectorIntToString(const std::vector<int> & vec);
template<class VectorInt>
std::string GridCmdVectorIntToString(const VectorInt & vec);
void GridCmdOptionCSL(std::string str,std::vector<std::string> & vec);
void GridCmdOptionIntVector(std::string &str,std::vector<int> & vec);
void GridParseLayout(char **argv,int argc,
std::vector<int> &latt,
std::vector<int> &simd,
std::vector<int> &mpi);
template<class VectorInt>
void GridCmdOptionIntVector(std::string &str,VectorInt & vec);
NAMESPACE_END(Grid);

View File

@ -7,7 +7,8 @@ namespace Grid{
class Lexicographic {
public:
static inline void CoorFromIndex (std::vector<int>& coor,int index,const std::vector<int> &dims){
template<class coor_t>
static inline void CoorFromIndex (coor_t& coor,int index,const coor_t &dims){
int nd= dims.size();
coor.resize(nd);
for(int d=0;d<nd;d++){
@ -16,7 +17,8 @@ namespace Grid{
}
}
static inline void IndexFromCoor (const std::vector<int>& coor,int &index,const std::vector<int> &dims){
template<class coor_t>
static inline void IndexFromCoor (const coor_t& coor,int &index,const coor_t &dims){
int nd=dims.size();
int stride=1;
index=0;
@ -26,7 +28,8 @@ namespace Grid{
}
}
static inline void IndexFromCoorReversed (const std::vector<int>& coor,int &index,const std::vector<int> &dims){
template<class coor_t>
static inline void IndexFromCoorReversed (const coor_t& coor,int &index,const coor_t &dims){
int nd=dims.size();
int stride=1;
index=0;
@ -35,7 +38,8 @@ namespace Grid{
stride=stride*dims[d];
}
}
static inline void CoorFromIndexReversed (std::vector<int>& coor,int index,const std::vector<int> &dims){
template<class coor_t>
static inline void CoorFromIndexReversed (coor_t& coor,int index,const coor_t &dims){
int nd= dims.size();
coor.resize(nd);
for(int d=nd-1;d>=0;d--){

View File

@ -1,5 +1,6 @@
#ifndef GRID_UTIL_H
#define GRID_UTIL_H
#include <Grid/util/Coordinate.h>
#include <Grid/util/Lexicographic.h>
#include <Grid/util/Init.h>
#endif