Grid 0.7.0
Lattice_matrix_reduction.h
Go to the documentation of this file.
1/*************************************************************************************
2 Grid physics library, www.github.com/paboyle/Grid
3 Source file: ./lib/lattice/Lattice_reduction.h
4 Copyright (C) 2015
5Author: Azusa Yamaguchi <ayamaguc@staffmail.ed.ac.uk>
6Author: Peter Boyle <paboyle@ph.ed.ac.uk>
7Author: paboyle <paboyle@ph.ed.ac.uk>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 See the full license in the file "LICENSE" in the top level distribution directory
20*************************************************************************************/
21/* END LEGAL */
22#pragma once
24
25#ifdef GRID_WARN_SUBOPTIMAL
26#warning "Optimisation alert all these reduction loops are NOT threaded "
27#endif
28
30
31template<class vobj>
32static void sliceMaddMatrix (Lattice<vobj> &R,Eigen::MatrixXcd &aa,const Lattice<vobj> &X,const Lattice<vobj> &Y,int Orthog,RealD scale=1.0)
33{
34 typedef typename vobj::scalar_object sobj;
35 typedef typename vobj::vector_type vector_type;
36
37 int Nblock = X.Grid()->GlobalDimensions()[Orthog];
38
39 GridBase *FullGrid = X.Grid();
40 // GridBase *SliceGrid = makeSubSliceGrid(FullGrid,Orthog);
41
42 // Lattice<vobj> Xslice(SliceGrid);
43 // Lattice<vobj> Rslice(SliceGrid);
44
45 assert( FullGrid->_simd_layout[Orthog]==1);
46
47 //FIXME package in a convenient iterator
48 //Should loop over a plane orthogonal to direction "Orthog"
49 int stride=FullGrid->_slice_stride[Orthog];
50 int block =FullGrid->_slice_block [Orthog];
51 int nblock=FullGrid->_slice_nblock[Orthog];
52 int ostride=FullGrid->_ostride[Orthog];
53 autoView( X_v , X, CpuRead);
54 autoView( Y_v , Y, CpuRead);
55 autoView( R_v , R, CpuWrite);
57 {
58 std::vector<vobj> s_x(Nblock);
59
60 thread_loop_collapse2( (int n=0;n<nblock;n++),{
61 for(int b=0;b<block;b++){
62 int o = n*stride + b;
63
64 for(int i=0;i<Nblock;i++){
65 s_x[i] = X_v[o+i*ostride];
66 }
67
68 vobj dot;
69 for(int i=0;i<Nblock;i++){
70 dot = Y_v[o+i*ostride];
71 for(int j=0;j<Nblock;j++){
72 dot = dot + s_x[j]*(scale*aa(j,i));
73 }
74 R_v[o+i*ostride]=dot;
75 }
76 }});
77 }
78};
79
80template<class vobj>
81static void sliceMulMatrix (Lattice<vobj> &R,Eigen::MatrixXcd &aa,const Lattice<vobj> &X,int Orthog,RealD scale=1.0)
82{
83 typedef typename vobj::scalar_object sobj;
84 typedef typename vobj::vector_type vector_type;
85
86 int Nblock = X.Grid()->GlobalDimensions()[Orthog];
87
88 GridBase *FullGrid = X.Grid();
89 assert( FullGrid->_simd_layout[Orthog]==1);
90
91 //FIXME package in a convenient iterator
92 //Should loop over a plane orthogonal to direction "Orthog"
93 int stride=FullGrid->_slice_stride[Orthog];
94 int block =FullGrid->_slice_block [Orthog];
95 int nblock=FullGrid->_slice_nblock[Orthog];
96 int ostride=FullGrid->_ostride[Orthog];
97
98 autoView( X_v , X, CpuRead);
99 autoView( R_v , R, CpuWrite);
100
102 {
103 std::vector<vobj> s_x(Nblock);
104
105 thread_loop_collapse2( (int n=0;n<nblock;n++),{
106 for(int b=0;b<block;b++){
107 int o = n*stride + b;
108
109 for(int i=0;i<Nblock;i++){
110 s_x[i] = X_v[o+i*ostride];
111 }
112
113 vobj dot;
114 for(int i=0;i<Nblock;i++){
115 dot = s_x[0]*(scale*aa(0,i));
116 for(int j=1;j<Nblock;j++){
117 dot = dot + s_x[j]*(scale*aa(j,i));
118 }
119 R_v[o+i*ostride]=dot;
120 }
121 }});
122 }
123
124};
125
126
127template<class vobj>
128static void sliceInnerProductMatrix( Eigen::MatrixXcd &mat, const Lattice<vobj> &lhs,const Lattice<vobj> &rhs,int Orthog)
129{
130 typedef typename vobj::scalar_object sobj;
131 typedef typename vobj::vector_type vector_type;
132
133 GridBase *FullGrid = lhs.Grid();
134 // GridBase *SliceGrid = makeSubSliceGrid(FullGrid,Orthog);
135
136 int Nblock = FullGrid->GlobalDimensions()[Orthog];
137
138 // Lattice<vobj> Lslice(SliceGrid);
139 // Lattice<vobj> Rslice(SliceGrid);
140
141 mat = Eigen::MatrixXcd::Zero(Nblock,Nblock);
142
143 assert( FullGrid->_simd_layout[Orthog]==1);
144 // int nh = FullGrid->_ndimension;
145 // int nl = SliceGrid->_ndimension;
146 // int nl = nh-1;
147
148 //FIXME package in a convenient iterator
149 //Should loop over a plane orthogonal to direction "Orthog"
150 int stride=FullGrid->_slice_stride[Orthog];
151 int block =FullGrid->_slice_block [Orthog];
152 int nblock=FullGrid->_slice_nblock[Orthog];
153 int ostride=FullGrid->_ostride[Orthog];
154
155 typedef typename vobj::vector_typeD vector_typeD;
156 autoView( lhs_v , lhs, CpuRead);
157 autoView( rhs_v , rhs, CpuRead);
159 std::vector<vobj> Left(Nblock);
160 std::vector<vobj> Right(Nblock);
161 Eigen::MatrixXcd mat_thread = Eigen::MatrixXcd::Zero(Nblock,Nblock);
162
163 thread_loop_collapse2((int n=0;n<nblock;n++),{
164 for(int b=0;b<block;b++){
165
166 int o = n*stride + b;
167
168 for(int i=0;i<Nblock;i++){
169 Left [i] = lhs_v[o+i*ostride];
170 Right[i] = rhs_v[o+i*ostride];
171 }
172
173 for(int i=0;i<Nblock;i++){
174 for(int j=0;j<Nblock;j++){
175 auto tmp = innerProduct(Left[i],Right[j]);
176 auto rtmp = TensorRemove(tmp);
177 ComplexD z = Reduce(rtmp);
178 mat_thread(i,j) += std::complex<double>(real(z),imag(z));
179 }}
180 }});
182 mat += mat_thread;
183 }
184 }
185
186 for(int i=0;i<Nblock;i++){
187 for(int j=0;j<Nblock;j++){
188 ComplexD sum = mat(i,j);
189 FullGrid->GlobalSum(sum);
190 mat(i,j)=sum;
191 }}
192
193 return;
194}
195
197
198
199
static void sliceMulMatrix(Lattice< vobj > &R, Eigen::MatrixXcd &aa, const Lattice< vobj > &X, int Orthog, RealD scale=1.0)
static void sliceMaddMatrix(Lattice< vobj > &R, Eigen::MatrixXcd &aa, const Lattice< vobj > &X, const Lattice< vobj > &Y, int Orthog, RealD scale=1.0)
static void sliceInnerProductMatrix(Eigen::MatrixXcd &mat, const Lattice< vobj > &lhs, const Lattice< vobj > &rhs, int Orthog)
Lattice< vobj > real(const Lattice< vobj > &lhs)
Lattice< vobj > imag(const Lattice< vobj > &lhs)
ComplexD innerProduct(const Lattice< vobj > &left, const Lattice< vobj > &right)
vobj::scalar_object sum(const vobj *arg, Integer osites)
#define autoView(l_v, l, mode)
@ CpuRead
@ CpuWrite
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
std::complex< RealD > ComplexD
Definition Simd.h:79
double RealD
Definition Simd.h:61
accelerator_inline ComplexD Reduce(const ComplexD &r)
Definition Simd.h:129
accelerator_inline std::enable_if<!isGridTensor< T >::value, T >::type TensorRemove(T arg)
#define thread_critical
Definition Threads.h:73
#define thread_region
Definition Threads.h:72
const Coordinate & GlobalDimensions(void)
Coordinate _slice_stride
Coordinate _slice_nblock
Coordinate _slice_block
Coordinate _simd_layout
Coordinate _ostride
GridBase * Grid(void) const