2018-01-13 00:31:02 +00:00
|
|
|
/*************************************************************************************
|
2016-01-02 14:51:32 +00:00
|
|
|
|
|
|
|
Grid physics library, www.github.com/paboyle/Grid
|
|
|
|
|
|
|
|
Source file: ./lib/tensors/Tensor_extract_merge.h
|
|
|
|
|
|
|
|
Copyright (C) 2015
|
|
|
|
|
|
|
|
Author: Azusa Yamaguchi <ayamaguc@staffmail.ed.ac.uk>
|
|
|
|
Author: Peter Boyle <paboyle@ph.ed.ac.uk>
|
|
|
|
Author: neo <cossu@post.kek.jp>
|
|
|
|
Author: paboyle <paboyle@ph.ed.ac.uk>
|
2016-07-06 20:57:04 +01:00
|
|
|
Author: Christopher Kelly <ckelly@phys.columbia.edu>
|
2016-01-02 14:51:32 +00:00
|
|
|
|
|
|
|
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
|
2018-01-13 00:31:02 +00:00
|
|
|
*************************************************************************************/
|
|
|
|
/* END LEGAL */
|
2018-02-24 22:21:08 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-03-07 15:27:26 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
//#pragma GCC optimize("no-strict-aliasing")
|
|
|
|
|
2018-02-24 22:21:08 +00:00
|
|
|
NAMESPACE_BEGIN(Grid);
|
|
|
|
|
2015-04-28 08:11:59 +01:00
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
// Generic extract/merge/permute
|
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-02-24 22:21:08 +00:00
|
|
|
template<class __T> using ExtractPointerArray = AcceleratorVector<__T *,GRID_MAX_SIMD>;
|
|
|
|
template<class __T> using ExtractBuffer = AcceleratorVector<__T ,GRID_MAX_SIMD>;
|
|
|
|
|
|
|
|
//void extract(const vobj &vec,ExtractBuffer<typename vobj::scalar_object> &extracted);
|
|
|
|
//void extract(const vobj &vec,ExtractPointerArray<sobj> &extracted, int offset);
|
|
|
|
//void merge(vobj &vec,ExtractBuffer<typename vobj::scalar_object> &extracted)
|
|
|
|
//void merge(vobj &vec,ExtractPointerArray<typename vobj::scalar_object> &extracted)
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Extract to contiguous array scalar object
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
2018-03-05 11:39:52 +00:00
|
|
|
template<class vobj,class sobj> accelerator
|
2018-02-24 22:21:08 +00:00
|
|
|
void extract(const vobj &vec,ExtractBuffer<sobj> &extracted)
|
|
|
|
{
|
2018-03-05 11:39:52 +00:00
|
|
|
typedef typename GridTypeMapper<sobj>::scalar_type sobj_scalar_type;
|
2018-02-24 22:21:08 +00:00
|
|
|
typedef typename GridTypeMapper<vobj>::scalar_type scalar_type;
|
|
|
|
typedef typename GridTypeMapper<vobj>::vector_type vector_type;
|
|
|
|
|
|
|
|
const int words=sizeof(vobj)/sizeof(vector_type);
|
|
|
|
const int Nsimd=vector_type::Nsimd();
|
|
|
|
const int Nextr=extracted.size();
|
|
|
|
const int s=Nsimd/Nextr;
|
2018-03-07 15:27:26 +00:00
|
|
|
sobj_scalar_type *sp = (sobj_scalar_type *) &extracted[0];
|
2018-02-24 22:21:08 +00:00
|
|
|
scalar_type *vp = (scalar_type *)&vec;
|
2018-03-07 15:27:26 +00:00
|
|
|
scalar_type vtmp;
|
|
|
|
sobj_scalar_type stmp;
|
2018-02-24 22:21:08 +00:00
|
|
|
for(int w=0;w<words;w++){
|
2018-01-13 00:31:02 +00:00
|
|
|
for(int i=0;i<Nextr;i++){
|
2018-03-07 15:27:26 +00:00
|
|
|
memcpy((char *)&vtmp,(char *)&vp[w*Nsimd+i*s],sizeof(vtmp));
|
|
|
|
stmp = vtmp;
|
|
|
|
memcpy((char *)&sp[i*words+w],(char *)&stmp,sizeof(stmp));
|
2015-04-28 08:11:59 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-05 11:39:52 +00:00
|
|
|
return;
|
2018-02-24 22:21:08 +00:00
|
|
|
}
|
2018-01-13 00:31:02 +00:00
|
|
|
|
2018-02-24 22:21:08 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Merge a contiguous array of scalar objects
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
2018-03-05 11:39:52 +00:00
|
|
|
template<class vobj,class sobj> accelerator
|
2018-02-24 22:21:08 +00:00
|
|
|
void merge(vobj &vec,ExtractBuffer<sobj> &extracted)
|
|
|
|
{
|
2018-03-05 11:39:52 +00:00
|
|
|
typedef typename GridTypeMapper<sobj>::scalar_type sobj_scalar_type;
|
2018-02-24 22:21:08 +00:00
|
|
|
typedef typename GridTypeMapper<vobj>::scalar_type scalar_type;
|
|
|
|
typedef typename GridTypeMapper<vobj>::vector_type vector_type;
|
|
|
|
|
|
|
|
const int words=sizeof(vobj)/sizeof(vector_type);
|
|
|
|
const int Nsimd=vector_type::Nsimd();
|
|
|
|
const int Nextr = extracted.size();
|
|
|
|
const int s=Nsimd/Nextr;
|
|
|
|
|
2018-03-05 11:39:52 +00:00
|
|
|
sobj_scalar_type *sp = (sobj_scalar_type *)&extracted[0];
|
2018-02-24 22:21:08 +00:00
|
|
|
scalar_type *vp = (scalar_type *)&vec;
|
2018-03-07 15:27:26 +00:00
|
|
|
scalar_type vtmp;
|
|
|
|
sobj_scalar_type stmp;
|
2018-02-24 22:21:08 +00:00
|
|
|
for(int w=0;w<words;w++){
|
|
|
|
for(int i=0;i<Nextr;i++){
|
2018-03-04 15:53:04 +00:00
|
|
|
for(int ii=0;ii<s;ii++){
|
2018-03-07 15:27:26 +00:00
|
|
|
memcpy((char *)&stmp,(char *)&sp[i*words+w],sizeof(stmp));
|
|
|
|
vtmp = stmp;
|
|
|
|
memcpy((char *)&vp[w*Nsimd+i*s+ii],(char *)&vtmp,sizeof(vtmp));
|
2018-03-04 15:53:04 +00:00
|
|
|
}
|
2015-11-06 11:26:20 +00:00
|
|
|
}
|
2015-04-28 08:11:59 +01:00
|
|
|
}
|
2018-02-24 22:21:08 +00:00
|
|
|
}
|
2015-04-28 08:11:59 +01:00
|
|
|
|
2018-05-12 22:59:35 +01:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Extract/Insert a single lane
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
template<class vobj> accelerator_inline
|
|
|
|
typename vobj::scalar_object extractLane(int lane, const vobj & __restrict__ vec)
|
|
|
|
{
|
|
|
|
typedef typename vobj::scalar_object scalar_object;
|
|
|
|
typedef typename vobj::scalar_type scalar_type;
|
|
|
|
typedef typename vobj::vector_type vector_type;
|
|
|
|
|
|
|
|
constexpr int words=sizeof(vobj)/sizeof(vector_type);
|
|
|
|
constexpr int Nsimd=vector_type::Nsimd();
|
|
|
|
|
|
|
|
scalar_object extracted;
|
|
|
|
scalar_type * __restrict__ sp = (scalar_type *)&extracted; // Type pun
|
|
|
|
scalar_type * __restrict__ vp = (scalar_type *)&vec;
|
|
|
|
for(int w=0;w<words;w++){
|
|
|
|
sp[w]=vp[w*Nsimd+lane];
|
|
|
|
}
|
|
|
|
return extracted;
|
|
|
|
}
|
|
|
|
template<class vobj> accelerator_inline
|
|
|
|
void insertLane(int lane, vobj & __restrict__ vec,const typename vobj::scalar_object & __restrict__ extracted)
|
|
|
|
{
|
|
|
|
typedef typename vobj::scalar_type scalar_type;
|
|
|
|
typedef typename vobj::vector_type vector_type;
|
|
|
|
|
|
|
|
constexpr int words=sizeof(vobj)/sizeof(vector_type);
|
|
|
|
constexpr int Nsimd=vector_type::Nsimd();
|
|
|
|
|
|
|
|
scalar_type * __restrict__ sp = (scalar_type *)&extracted;
|
|
|
|
scalar_type * __restrict__ vp = (scalar_type *)&vec;
|
|
|
|
for(int w=0;w<words;w++){
|
|
|
|
vp[w*Nsimd+lane]=sp[w];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-24 22:21:08 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Extract to a bunch of scalar object pointers of different scalar type, with offset. Useful for precision change
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
2018-03-05 11:39:52 +00:00
|
|
|
template<class vobj, class sobj> accelerator
|
2018-02-24 22:21:08 +00:00
|
|
|
void extract(const vobj &vec,ExtractPointerArray<sobj> &extracted, int offset)
|
|
|
|
{
|
2018-03-04 15:53:04 +00:00
|
|
|
typedef typename GridTypeMapper<sobj>::scalar_type sobj_scalar_type;
|
2018-02-24 22:21:08 +00:00
|
|
|
typedef typename GridTypeMapper<vobj>::scalar_type scalar_type;
|
|
|
|
typedef typename GridTypeMapper<vobj>::vector_type vector_type;
|
2016-07-06 20:57:04 +01:00
|
|
|
|
2018-02-24 22:21:08 +00:00
|
|
|
const int words=sizeof(vobj)/sizeof(vector_type);
|
|
|
|
const int Nsimd=vector_type::Nsimd();
|
|
|
|
const int Nextr=extracted.size();
|
|
|
|
const int s = Nsimd/Nextr;
|
2015-04-28 08:11:59 +01:00
|
|
|
|
2018-02-24 22:21:08 +00:00
|
|
|
scalar_type * vp = (scalar_type *)&vec;
|
2018-03-07 15:27:26 +00:00
|
|
|
scalar_type vtmp;
|
|
|
|
sobj_scalar_type stmp;
|
2018-02-24 22:21:08 +00:00
|
|
|
for(int w=0;w<words;w++){
|
|
|
|
for(int i=0;i<Nextr;i++){
|
2018-03-04 15:53:04 +00:00
|
|
|
sobj_scalar_type * pointer = (sobj_scalar_type *)& extracted[i][offset];
|
2018-03-07 15:27:26 +00:00
|
|
|
memcpy((char *)&vtmp,(char *)&vp[w*Nsimd+i*s],sizeof(vtmp));
|
|
|
|
stmp = vtmp;
|
|
|
|
memcpy((char *)&pointer[w],(char *)&stmp,sizeof(stmp)); // may do a precision conversion
|
2018-01-13 00:31:02 +00:00
|
|
|
}
|
2015-04-28 08:11:59 +01:00
|
|
|
}
|
2018-02-24 22:21:08 +00:00
|
|
|
}
|
2015-04-28 08:11:59 +01:00
|
|
|
|
2018-02-24 22:21:08 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Merge bunch of scalar object pointers of different scalar type, with offset. Useful for precision change
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
2018-03-05 11:39:52 +00:00
|
|
|
template<class vobj, class sobj> accelerator
|
2018-02-24 22:21:08 +00:00
|
|
|
void merge(vobj &vec,ExtractPointerArray<sobj> &extracted, int offset)
|
|
|
|
{
|
2018-03-05 11:39:52 +00:00
|
|
|
typedef typename GridTypeMapper<sobj>::scalar_type sobj_scalar_type;
|
2018-02-24 22:21:08 +00:00
|
|
|
typedef typename GridTypeMapper<vobj>::scalar_type scalar_type;
|
|
|
|
typedef typename GridTypeMapper<vobj>::vector_type vector_type;
|
2015-04-28 08:11:59 +01:00
|
|
|
|
2018-02-24 22:21:08 +00:00
|
|
|
const int words=sizeof(vobj)/sizeof(vector_type);
|
|
|
|
const int Nsimd=vector_type::Nsimd();
|
|
|
|
const int Nextr=extracted.size();
|
|
|
|
const int s = Nsimd/Nextr;
|
2015-06-14 00:55:21 +01:00
|
|
|
|
2018-02-24 22:21:08 +00:00
|
|
|
scalar_type * vp = (scalar_type *)&vec;
|
2018-03-07 15:27:26 +00:00
|
|
|
scalar_type vtmp;
|
|
|
|
sobj_scalar_type stmp;
|
2018-02-24 22:21:08 +00:00
|
|
|
for(int w=0;w<words;w++){
|
|
|
|
for(int i=0;i<Nextr;i++){
|
2018-03-05 11:39:52 +00:00
|
|
|
sobj_scalar_type * pointer = (sobj_scalar_type *)& extracted[i][offset];
|
2018-03-04 15:53:04 +00:00
|
|
|
for(int ii=0;ii<s;ii++){
|
2018-03-07 15:27:26 +00:00
|
|
|
memcpy((char *)&stmp,(char *)&pointer[w],sizeof(stmp));
|
|
|
|
vtmp=stmp;
|
|
|
|
memcpy((char *)&vp[w*Nsimd+i*s+ii],(char *)&vtmp,sizeof(vtmp));
|
2018-03-04 15:53:04 +00:00
|
|
|
}
|
2015-11-06 11:26:20 +00:00
|
|
|
}
|
2015-04-28 08:11:59 +01:00
|
|
|
}
|
2018-02-24 22:21:08 +00:00
|
|
|
}
|
2016-01-11 14:34:22 +00:00
|
|
|
|
|
|
|
|
2018-02-24 22:21:08 +00:00
|
|
|
NAMESPACE_END(Grid);
|
2016-01-11 14:34:22 +00:00
|
|
|
|