sComplex simd, also commit coarse space as lex lattice tests

This commit is contained in:
Peter Boyle
2026-08-19 19:54:18 -04:00
parent 6a4071cdc2
commit cc54c90ddc
5 changed files with 1055 additions and 12 deletions
+180 -12
View File
@@ -560,29 +560,198 @@ int main (int argc, char ** argv)
auto latt_size = GridDefaultLatt();
auto simd_layout = GridDefaultSimd(4,vComplex::Nsimd());
auto mpi_layout = GridDefaultMpi();
Coordinate simd1_layout({1,1,1,1});
{
std::cout << " Constructing Test({1,2,3,4,5,6}) " << std::endl;
Coordinate Test({1,2,3,4,5,6});
std::cout << " Test({1,2,3,4,5,6}) = " << Test <<std::endl;
}
/*
{
Coordinate Test = {1,2,3,4} ;
std::cout << " Test = {1,2,3,4} " << Test <<std::endl;
}
{
Coordinate Test {1,2,3,4};
std::cout << " Test {1,2,3,4} " << Test <<std::endl;
}
*/
GridCartesian Grid(latt_size,simd_layout,mpi_layout);
std::vector<int> seeds({1,2,3,4});
// Insist that operations on random scalars gives
// identical results to on vectors.
std::cout << GridLogMessage <<"==================================="<< std::endl;
std::cout << GridLogMessage <<"Testing sRealF "<<std::endl;
std::cout << GridLogMessage <<"==================================="<< std::endl;
Tester<RealF,sRealF>(funcPlus());
Tester<RealF,sRealF>(funcMinus());
Tester<RealF,sRealF>(funcTimes());
Tester<RealF,sRealF>(funcDivide());
Tester<RealF,sRealF>(funcAdj());
Tester<RealF,sRealF>(funcConj());
Tester<RealF,sRealF>(funcInnerProduct());
ReductionTester<RealF,RealF,sRealF>(funcReduce());
std::cout<<GridLogMessage << "==================================="<< std::endl;
std::cout<<GridLogMessage << "Testing sRealF permutes (there are none) "<<std::endl;
std::cout<<GridLogMessage << "==================================="<< std::endl;
// Log2 iteration
for(int i=0;(1<<i)< sRealF::Nsimd();i++){
PermTester<RealF,sRealF>(funcPermute(i));
}
std::cout<<GridLogMessage << "==================================="<< std::endl;
std::cout<<GridLogMessage << "Testing sRealF exchanges "<<std::endl;
std::cout<<GridLogMessage << "==================================="<< std::endl;
// Log2 iteration
for(int i=0;(1<<i)< sRealF::Nsimd();i++){
ExchangeTester<RealF,sRealF>(funcExchange(i));
}
std::cout<<GridLogMessage << "==================================="<< std::endl;
std::cout<<GridLogMessage << "Testing sRealF rotate "<<std::endl;
std::cout<<GridLogMessage << "==================================="<< std::endl;
for(int r=0;r<sRealF::Nsimd();r++){
PermTester<RealF,sRealF>(funcRotate(r));
}
std::cout << GridLogMessage <<"==================================="<< std::endl;
std::cout << GridLogMessage <<"Testing sRealD "<<std::endl;
std::cout << GridLogMessage <<"==================================="<< std::endl;
Tester<RealD,sRealD>(funcPlus());
Tester<RealD,sRealD>(funcMinus());
Tester<RealD,sRealD>(funcTimes());
Tester<RealD,sRealD>(funcDivide());
Tester<RealD,sRealD>(funcAdj());
Tester<RealD,sRealD>(funcConj());
Tester<RealD,sRealD>(funcInnerProduct());
ReductionTester<RealD,RealD,sRealD>(funcReduce());
std::cout<<GridLogMessage << "==================================="<< std::endl;
std::cout<<GridLogMessage << "Testing sRealD permutes "<<std::endl;
std::cout<<GridLogMessage << "==================================="<< std::endl;
// Log2 iteration
for(int i=0;(1<<i)< sRealD::Nsimd();i++){
PermTester<RealD,sRealD>(funcPermute(i));
}
std::cout<<GridLogMessage << "==================================="<< std::endl;
std::cout<<GridLogMessage << "Testing sRealD exchanges "<<std::endl;
std::cout<<GridLogMessage << "==================================="<< std::endl;
// Log2 iteration
for(int i=0;(1<<i)< sRealD::Nsimd();i++){
ExchangeTester<RealD,sRealD>(funcExchange(i));
}
std::cout<<GridLogMessage << "==================================="<< std::endl;
std::cout<<GridLogMessage << "Testing sRealD rotate "<<std::endl;
std::cout<<GridLogMessage << "==================================="<< std::endl;
for(int r=0;r<sRealD::Nsimd();r++){
PermTester<RealD,sRealD>(funcRotate(r));
}
std::cout << GridLogMessage <<"==================================="<< std::endl;
std::cout << GridLogMessage <<"Testing sComplexF "<<std::endl;
std::cout << GridLogMessage <<"==================================="<< std::endl;
Tester<ComplexF,sComplexF>(funcTimesI());
Tester<ComplexF,sComplexF>(funcTimesMinusI());
Tester<ComplexF,sComplexF>(funcPlus());
Tester<ComplexF,sComplexF>(funcMinus());
Tester<ComplexF,sComplexF>(funcTimes());
Tester<ComplexF,sComplexF>(funcConj());
Tester<ComplexF,sComplexF>(funcAdj());
Tester<ComplexF,sComplexF>(funcReal());
Tester<ComplexF,sComplexF>(funcImag());
Tester<ComplexF,sComplexF>(funcInnerProduct());
ReductionTester<ComplexF,ComplexF,sComplexF>(funcReduce());
std::cout<<GridLogMessage << "==================================="<< std::endl;
std::cout<<GridLogMessage << "Testing sComplexF permutes "<<std::endl;
std::cout<<GridLogMessage << "==================================="<< std::endl;
// Log2 iteration
for(int i=0;(1<<i)< sComplexF::Nsimd();i++){
PermTester<ComplexF,sComplexF>(funcPermute(i));
}
std::cout<<GridLogMessage << "==================================="<< std::endl;
std::cout<<GridLogMessage << "Testing sComplexF exchanges "<<std::endl;
std::cout<<GridLogMessage << "==================================="<< std::endl;
// Log2 iteration
for(int i=0;(1<<i)< sComplexF::Nsimd();i++){
ExchangeTester<ComplexF,sComplexF>(funcExchange(i));
}
std::cout<<GridLogMessage << "==================================="<< std::endl;
std::cout<<GridLogMessage << "Testing sComplexF rotate "<<std::endl;
std::cout<<GridLogMessage << "==================================="<< std::endl;
for(int r=0;r<sComplexF::Nsimd();r++){
PermTester<ComplexF,sComplexF>(funcRotate(r));
}
std::cout<<GridLogMessage << "==================================="<< std::endl;
std::cout<<GridLogMessage << "Testing sComplexD "<<std::endl;
std::cout<<GridLogMessage << "==================================="<< std::endl;
Tester<ComplexD,sComplexD>(funcTimesI());
Tester<ComplexD,sComplexD>(funcTimesMinusI());
Tester<ComplexD,sComplexD>(funcPlus());
Tester<ComplexD,sComplexD>(funcMinus());
Tester<ComplexD,sComplexD>(funcTimes());
Tester<ComplexD,sComplexD>(funcConj());
Tester<ComplexD,sComplexD>(funcAdj());
Tester<ComplexD, sComplexD>(funcReal());
Tester<ComplexD, sComplexD>(funcImag());
Tester<ComplexD, sComplexD>(funcInnerProduct());
ReductionTester<ComplexD, ComplexD, sComplexD>(funcReduce());
std::cout << GridLogMessage
<< "===================================" << std::endl;
std::cout << GridLogMessage << "Testing sComplexD permutes " << std::endl;
std::cout << GridLogMessage
<< "===================================" << std::endl;
// Log2 iteration
for (int i = 0; (1 << i) < sComplexD::Nsimd(); i++) {
PermTester<ComplexD, sComplexD>(funcPermute(i));
}
std::cout<<GridLogMessage << "==================================="<< std::endl;
std::cout<<GridLogMessage << "Testing sComplexD exchanges "<<std::endl;
std::cout<<GridLogMessage << "==================================="<< std::endl;
// Log2 iteration
for(int i=0;(1<<i)< sComplexD::Nsimd();i++){
ExchangeTester<ComplexD,sComplexD>(funcExchange(i));
}
std::cout<<GridLogMessage << "==================================="<< std::endl;
std::cout<<GridLogMessage << "Testing sComplexD rotate "<<std::endl;
std::cout<<GridLogMessage << "==================================="<< std::endl;
for(int r=0;r<sComplexD::Nsimd();r++){
PermTester<ComplexD,sComplexD>(funcRotate(r));
}
std::cout<<GridLogMessage << "==================================="<< std::endl;
std::cout<<GridLogMessage << "Testing sInteger "<< std::endl;
std::cout<<GridLogMessage << "==================================="<< std::endl;
IntTester(funcPlus());
IntTester(funcMinus());
IntTester(funcTimes());
IntReductionTester<Integer, Integer, sInteger>(funcReduce());
std::cout << GridLogMessage <<"==================================="<< std::endl;
std::cout << GridLogMessage <<"Testing vRealF "<<std::endl;
std::cout << GridLogMessage <<"==================================="<< std::endl;
@@ -623,7 +792,6 @@ int main (int argc, char ** argv)
PermTester<RealF,vRealF>(funcRotate(r));
}
std::cout << GridLogMessage <<"==================================="<< std::endl;
std::cout << GridLogMessage <<"Testing vRealD "<<std::endl;
std::cout << GridLogMessage <<"==================================="<< std::endl;
+252
View File
@@ -0,0 +1,252 @@
/*************************************************************************************
Grid physics library, www.github.com/paboyle/Grid
Source file: ./tests/debug/Test_coarse_v2.cc
Copyright (C) 2026
Author: Peter Boyle <pboyle@bnl.gov>
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 */
//
// MultiGeneralCoarsenedOperatorV2 against the existing mrhs coarse operator.
//
// V1 is constructed on the D+1 grid as now; V2 on the D dimensional grid,
// with SetGrid() adopting the caller owned D+1 grid and building its padded
// cell and neighbour table from the D dimensional stencil, with the Nrhs
// factor multiplied in.
//
// Both are given identical matrix elements, so any difference in the apply is
// the restructured neighbour table. The same geometry object is passed to
// both: V1 adds one to skip for the rhs direction, V2 uses it as is on the D
// dimensional grid, so both describe the same stencil over the D dimensions.
//
#include <Grid/Grid.h>
using namespace Grid;
const int nbasis = 8;
typedef vSpinColourVector FineObj;
typedef sTComplexD CComplexT; // unvectorised coarse space
typedef MultiGeneralCoarsenedMatrix <FineObj,CComplexT,nbasis> MrhsV1;
typedef MultiGeneralCoarsenedOperatorV2<FineObj,CComplexT,nbasis> MrhsV2;
////////////////////////////////////////////////////////////////////////
// Identical random matrix elements into both operators
////////////////////////////////////////////////////////////////////////
template<class OpA,class OpB>
void SeedMatrixElements(OpA &A,OpB &B,int npoint,GridSerialRNG &sRNG)
{
typedef typename OpA::calcMatrix calcMatrix;
for(int p=0;p<npoint;p++){
GRID_ASSERT(A.BLAS_A[p].size() == B.BLAS_A[p].size());
int64_t sites = A.BLAS_A[p].size();
std::vector<calcMatrix> host(sites);
ComplexD *w = (ComplexD *)&host[0];
int64_t words = sites*sizeof(calcMatrix)/sizeof(ComplexD);
for(int64_t i=0;i<words;i++){
RealD re,im;
random(sRNG,re);
random(sRNG,im);
w[i] = ComplexD(re-0.5,im-0.5);
}
acceleratorCopyToDevice(&host[0],&A.BLAS_A[p][0],sites*sizeof(calcMatrix));
acceleratorCopyToDevice(&host[0],&B.BLAS_A[p][0],sites*sizeof(calcMatrix));
}
}
template<class Op>
RealD MatrixChecksum(Op &O,int npoint)
{
typedef typename Op::calcMatrix calcMatrix;
RealD sum=0.0;
for(int p=0;p<npoint;p++){
int64_t sites = O.BLAS_A[p].size();
std::vector<calcMatrix> host(sites);
acceleratorCopyFromDevice(&O.BLAS_A[p][0],&host[0],sites*sizeof(calcMatrix));
ComplexD *w = (ComplexD *)&host[0];
int64_t words = sites*sizeof(calcMatrix)/sizeof(ComplexD);
for(int64_t i=0;i<words;i++) sum += real(w[i])*real(w[i]) + imag(w[i])*imag(w[i]);
}
return sum;
}
int main (int argc, char ** argv)
{
Grid_init(&argc,&argv);
const int nrhs = 4;
Coordinate clatt = GridDefaultLatt();
Coordinate csimd(Nd,1); // the coarse space is unvectorised
Coordinate cmpi = GridDefaultMpi();
////////////////////////////////////////////////
// D dimensional coarse grid, and D+1 for V1
////////////////////////////////////////////////
GridCartesian *CoarseD = new GridCartesian(clatt,csimd,cmpi);
std::cout << GridLogMessage << "coarse D grid "; for(int d=0;d<Nd;d++) std::cout<<clatt[d]<<" ";
std::cout << " Nsimd " << CoarseD->Nsimd() << std::endl;
////////////////////////////////////////////////
// One geometry object for both, and one D+1 grid
// owned here and shared by both operators: fields
// conform only across a shared grid object.
////////////////////////////////////////////////
NextToNearestStencilGeometry4D geom(CoarseD);
Coordinate mlatt(1,nrhs), msimd(1,1), mmpi(1,1);
for(int d=0;d<Nd;d++){
mlatt.push_back(clatt[d]);
msimd.push_back(csimd[d]);
mmpi .push_back(cmpi[d]);
}
GridCartesian *CoarseMulti = new GridCartesian(mlatt,msimd,mmpi);
MrhsV2 OpV2(geom,CoarseD);
MrhsV1 OpV1(geom,CoarseMulti);
OpV2.SetGrid(CoarseMulti);
std::cout << GridLogMessage << "coarse D+1 grid nrhs " << nrhs
<< " Nsimd " << CoarseMulti->Nsimd() << std::endl;
std::cout << GridLogMessage << "npoint V1 " << OpV1.geom.npoint
<< " npoint V2 " << OpV2.geom.npoint << std::endl;
GRID_ASSERT(OpV1.geom.npoint == OpV2.geom.npoint);
int npoint = OpV1.geom.npoint;
////////////////////////////////////////////////
// Identical matrix elements
////////////////////////////////////////////////
GridSerialRNG sRNG; sRNG.SeedFixedIntegers(std::vector<int>({7,8,9,10}));
SeedMatrixElements(OpV1,OpV2,npoint,sRNG);
RealD ckV1 = MatrixChecksum(OpV1,npoint);
RealD ckV2 = MatrixChecksum(OpV2,npoint);
std::cout << GridLogMessage << "matrix element checksum V1 " << ckV1
<< " V2 " << ckV2 << std::endl;
GRID_ASSERT( ckV1 == ckV2 );
////////////////////////////////////////////////
// Same input, compare the applies
////////////////////////////////////////////////
typedef MrhsV1::CoarseVector CoarseVector;
// RNG on the D dimensional grid fills any D+1 field: the rhs direction is
// undistributed and divides cleanly. One RNG serves every Nrhs.
GridParallelRNG pRNG(CoarseD); pRNG.SeedFixedIntegers(std::vector<int>({1,2,3,4}));
CoarseVector in (CoarseMulti); random(pRNG,in);
CoarseVector out1(CoarseMulti);
CoarseVector out2(CoarseMulti);
CoarseVector err (CoarseMulti);
OpV1.M(in,out1);
OpV2.M(in,out2);
err = out1 - out2;
std::cout << GridLogMessage << "|V1 out|^2 = " << norm2(out1)
<< " |V2 out|^2 = " << norm2(out2) << std::endl;
std::cout << GridLogMessage << "|V1 - V2|^2 = " << norm2(err) << std::endl;
GRID_ASSERT( norm2(out1) > 0.0 );
GRID_ASSERT( norm2(err) == 0.0 );
////////////////////////////////////////////////
// SetGrid is idempotent on pointer identity
////////////////////////////////////////////////
OpV2.SetGrid(CoarseMulti);
GRID_ASSERT( MatrixChecksum(OpV2,npoint) == ckV2 );
OpV2.M(in,out2);
err = out1 - out2;
GRID_ASSERT( norm2(err) == 0.0 );
std::cout << GridLogMessage << "SetGrid idempotent on identity" << std::endl;
////////////////////////////////////////////////
// Move to a different Nrhs and back. The matrix
// elements are Nrhs independent and must survive
// both the release and the rebuild.
////////////////////////////////////////////////
// Nrhs 1 is the single RHS case through the multiRHS path, and each slice
// of the Nrhs 4 apply must come back unchanged.
OpV2.M(in,out2);
for(int nr=2;nr>=1;nr--){
Coordinate latt2(1,nr), simd2(1,1), mpi2(1,1);
for(int d=0;d<Nd;d++){
latt2.push_back(clatt[d]);
simd2.push_back(csimd[d]);
mpi2 .push_back(cmpi[d]);
}
GridCartesian *CoarseMulti2 = new GridCartesian(latt2,simd2,mpi2);
OpV2.SetGrid(CoarseMulti2);
GRID_ASSERT( OpV2.Nrhs() == nr );
GRID_ASSERT( MatrixChecksum(OpV2,npoint) == ckV2 );
CoarseVector in2 (CoarseMulti2);
CoarseVector out(CoarseMulti2);
for(int r=0;r<nr;r++){
CoarseVector slice(CoarseD);
ExtractSliceFast(slice,in,r,0);
InsertSliceFast(slice,in2,r,0);
}
OpV2.M(in2,out);
RealD sdiff=0.0;
for(int r=0;r<nr;r++){
CoarseVector a(CoarseD),b(CoarseD),e(CoarseD);
ExtractSliceFast(a,out ,r,0);
ExtractSliceFast(b,out2,r,0);
e = a-b;
sdiff += norm2(e);
}
// Not bit exact: a different Nrhs is a different GEMM shape
std::cout << GridLogMessage << "Nrhs " << nr << " slices agree with Nrhs "
<< nrhs << " : |diff|^2/|out|^2 = " << sdiff/norm2(out) << std::endl;
GRID_ASSERT( norm2(out) > 0.0 );
GRID_ASSERT( sdiff/norm2(out) < 1.0e-20 );
OpV2.ReleaseGrid();
GRID_ASSERT( MatrixChecksum(OpV2,npoint) == ckV2 ); // survives release
delete CoarseMulti2;
}
OpV2.SetGrid(CoarseMulti);
GRID_ASSERT( OpV2.Nrhs() == nrhs );
OpV2.M(in,out2);
err = out1 - out2;
std::cout << GridLogMessage << "after Nrhs 4 -> 2 -> 1 -> release -> 4, |V1 - V2|^2 = "
<< norm2(err) << std::endl;
GRID_ASSERT( norm2(err) == 0.0 );
std::cout << GridLogMessage << "Test_coarse_v2: ALL PASS" << std::endl;
Grid_finalize();
}
+242
View File
@@ -0,0 +1,242 @@
/*************************************************************************************
Grid physics library, www.github.com/paboyle/Grid
Source file: ./tests/debug/Test_coarse_v2_coarsen.cc
Copyright (C) 2026
Author: Peter Boyle <pboyle@bnl.gov>
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 */
//
// Coarsen the same fine operator two ways and compare the matrix elements.
//
// V1 : existing mrhs CoarsenOperator, vectorised coarse space matched to
// the fine SIMD layout, single RHS fine applications
// V2 : D+1 CoarsenOperator, unvectorised sComplexD coarse space, the batch
// of phased basis vectors carried in the rhs direction and applied
// through MrhsPromotedOperator
//
// BLAS_A is written by GridtoBLAS in lSite order, which does not depend on
// the SIMD layout, so the two are directly comparable.
//
#include <Grid/Grid.h>
using namespace Grid;
const int nbasis = 8;
const int batch = 9;
typedef vSpinColourVector FineObj;
typedef vTComplex CComplexV; // vectorised coarse space, V1 reference
typedef sTComplexD CComplexS; // unvectorised coarse space, V2
typedef MultiGeneralCoarsenedMatrix <FineObj,CComplexV,nbasis> MrhsV1;
typedef MultiGeneralCoarsenedOperatorV2<FineObj,CComplexS,nbasis> MrhsV2;
template<class Op>
void ReadMatrix(Op &O,int npoint,std::vector<std::vector<typename Op::calcMatrix> > &host)
{
typedef typename Op::calcMatrix calcMatrix;
host.resize(npoint);
for(int p=0;p<npoint;p++){
int64_t sites = O.BLAS_A[p].size();
host[p].resize(sites);
acceleratorCopyFromDevice(&O.BLAS_A[p][0],&host[p][0],sites*sizeof(calcMatrix));
}
}
int main (int argc, char ** argv)
{
Grid_init(&argc,&argv);
Coordinate flatt = GridDefaultLatt();
Coordinate fsimd = GridDefaultSimd(Nd,vComplexD::Nsimd());
Coordinate fmpi = GridDefaultMpi();
Coordinate block({2,2,2,2});
Coordinate clatt(Nd);
for(int d=0;d<Nd;d++){
GRID_ASSERT(flatt[d]%block[d]==0);
clatt[d] = flatt[d]/block[d];
}
GridCartesian *FineGrid = new GridCartesian(flatt,fsimd,fmpi);
GridRedBlackCartesian *FrbGrid = SpaceTimeGrid::makeFourDimRedBlackGrid(FineGrid);
////////////////////////////////////////////////
// V1 coarse space: SIMD layout matched to the fine
////////////////////////////////////////////////
Coordinate cvsimd = GridDefaultSimd(Nd,CComplexV::Nsimd());
GridCartesian *CoarseV = new GridCartesian(clatt,cvsimd,fmpi);
// V1 puts all of the SIMD in the rhs direction. CoarsenOperator does not
// use the multiRHS grid; it only sizes BLAS_A, so one lane of rhs suffices.
int nrhs_v1 = CComplexV::Nsimd();
Coordinate v1latt(1,nrhs_v1),v1simd(1,CComplexV::Nsimd()),v1mpi(1,1);
for(int d=0;d<Nd;d++){
v1latt.push_back(clatt[d]);
v1simd.push_back(1);
v1mpi .push_back(fmpi[d]);
}
GridCartesian *CoarseVMulti = new GridCartesian(v1latt,v1simd,v1mpi);
////////////////////////////////////////////////
// V2 coarse space: unvectorised
////////////////////////////////////////////////
Coordinate cssimd(Nd,1);
GridCartesian *CoarseS = new GridCartesian(clatt,cssimd,fmpi);
Coordinate cmlatt(1,batch),cmsimd(1,1),cmmpi(1,1);
for(int d=0;d<Nd;d++){
cmlatt.push_back(clatt[d]);
cmsimd.push_back(1);
cmmpi .push_back(fmpi[d]);
}
GridCartesian *CoarseSMulti = new GridCartesian(cmlatt,cmsimd,cmmpi);
// D+1 fine grid carrying the batch
Coordinate fmlatt(1,batch), fmsimd(1,1), fmmpi(1,1);
for(int d=0;d<Nd;d++){
fmlatt.push_back(flatt[d]);
fmsimd.push_back(fsimd[d]);
fmmpi .push_back(fmpi[d]);
}
GridCartesian *FineGridMulti = new GridCartesian(fmlatt,fmsimd,fmmpi);
std::cout << GridLogMessage << "fine "<<flatt<<" coarse "<<clatt<<" batch "<<batch<<std::endl;
std::cout << GridLogMessage << "Nsimd fine "<<FineGrid->Nsimd()
<< " coarse V1 "<<CoarseV->Nsimd()
<< " coarse V2 "<<CoarseS->Nsimd()<<std::endl;
////////////////////////////////////////////////
// Fine operator
////////////////////////////////////////////////
GridParallelRNG pRNG(FineGrid); pRNG.SeedFixedIntegers(std::vector<int>({1,2,3,4}));
LatticeGaugeFieldD Umu(FineGrid); SU<Nc>::HotConfiguration(pRNG,Umu);
RealD mass = 0.1;
WilsonFermionD Dw(Umu,*FineGrid,*FrbGrid,mass);
MdagMLinearOperator<WilsonFermionD,LatticeFermionD> HermOp(Dw);
////////////////////////////////////////////////
// One random subspace, two copies: CoarsenOperator orthogonalises in place
////////////////////////////////////////////////
Aggregation<FineObj,CComplexV,nbasis> Subspace(CoarseV,FineGrid,0);
std::vector<LatticeFermionD> subspace(nbasis,FineGrid);
for(int i=0;i<nbasis;i++){
random(pRNG,Subspace.subspace[i]);
subspace[i] = Subspace.subspace[i];
}
NextToNearestStencilGeometry4D geomV(CoarseV);
NextToNearestStencilGeometry4D geomS(CoarseS);
////////////////////////////////////////////////
// V1 coarsening, matched layouts
////////////////////////////////////////////////
MrhsV1 OpV1(geomV,CoarseVMulti);
std::cout << GridLogMessage << "V1 CoarsenOperator" << std::endl;
OpV1.CoarsenOperator(HermOp,Subspace,CoarseV);
////////////////////////////////////////////////
// V2 coarsening, D+1 fine applications, unvectorised coarse
////////////////////////////////////////////////
MrhsV2 OpV2(geomS,CoarseS);
OpV2.SetGrid(CoarseSMulti);
MrhsPromotedOperator<LatticeFermionD> MrhsHermOp(HermOp,FineGrid,batch);
std::cout << GridLogMessage << "V2 CoarsenOperator (D+1)" << std::endl;
OpV2.CoarsenOperator(MrhsHermOp,FineGridMulti,subspace,CoarseS);
////////////////////////////////////////////////
// Compare matrix elements
////////////////////////////////////////////////
int npoint = OpV1.geom.npoint;
GRID_ASSERT(npoint == OpV2.geom.npoint);
typedef MrhsV1::calcMatrix calcMatrix;
std::vector<std::vector<calcMatrix> > A1,A2;
ReadMatrix(OpV1,npoint,A1);
ReadMatrix(OpV2,npoint,A2);
RealD num=0.0, den=0.0;
for(int p=0;p<npoint;p++){
GRID_ASSERT(A1[p].size()==A2[p].size());
ComplexD *w1 = (ComplexD *)&A1[p][0];
ComplexD *w2 = (ComplexD *)&A2[p][0];
int64_t words = A1[p].size()*sizeof(calcMatrix)/sizeof(ComplexD);
for(int64_t i=0;i<words;i++){
ComplexD d = w1[i]-w2[i];
num += real(d)*real(d)+imag(d)*imag(d);
den += real(w1[i])*real(w1[i])+imag(w1[i])*imag(w1[i]);
}
}
std::cout << GridLogMessage << "|A_V1|^2 = " << den << std::endl;
std::cout << GridLogMessage << "|A_V1 - A_V2|^2 / |A_V1|^2 = " << num/den << std::endl;
GRID_ASSERT( den > 0.0 );
GRID_ASSERT( num/den < 1.0e-20 );
////////////////////////////////////////////////
// Same coarsening through the single RHS variant: no mrhs packing, the
// batch assembled on the coarse side by the mixed blockProject. Block
// Gram-Schmidt is idempotent so the subspace may be reused in place.
////////////////////////////////////////////////
MrhsV2 OpV2s(geomS,CoarseS);
OpV2s.SetGrid(CoarseSMulti);
std::cout << GridLogMessage << "V2 CoarsenOperator (single RHS fine op)" << std::endl;
OpV2s.CoarsenOperator(HermOp,subspace,CoarseS,batch);
std::vector<std::vector<calcMatrix> > A3;
ReadMatrix(OpV2s,npoint,A3);
RealD nums=0.0;
for(int p=0;p<npoint;p++){
GRID_ASSERT(A1[p].size()==A3[p].size());
ComplexD *w1 = (ComplexD *)&A1[p][0];
ComplexD *w3 = (ComplexD *)&A3[p][0];
int64_t words = A1[p].size()*sizeof(calcMatrix)/sizeof(ComplexD);
for(int64_t i=0;i<words;i++){
ComplexD d = w1[i]-w3[i];
nums += real(d)*real(d)+imag(d)*imag(d);
}
}
std::cout << GridLogMessage << "|A_V1 - A_V2srhs|^2 / |A_V1|^2 = " << nums/den << std::endl;
GRID_ASSERT( nums/den < 1.0e-20 );
////////////////////////////////////////////////
// and V2 applies the matrix it just built
////////////////////////////////////////////////
typedef MrhsV2::CoarseVector CoarseVectorS;
GridParallelRNG cRNG(CoarseS); cRNG.SeedFixedIntegers(std::vector<int>({5,6,7,8}));
CoarseVectorS in(CoarseSMulti); random(cRNG,in);
CoarseVectorS out(CoarseSMulti);
OpV2.M(in,out);
std::cout << GridLogMessage << "|in|^2 = " << norm2(in)
<< " |M_V2 in|^2 = " << norm2(out) << std::endl;
GRID_ASSERT( norm2(out) > 0.0 );
std::cout << GridLogMessage << "Test_coarse_v2_coarsen: ALL PASS" << std::endl;
Grid_finalize();
}
@@ -0,0 +1,224 @@
/*************************************************************************************
Grid physics library, www.github.com/paboyle/Grid
Source file: ./tests/debug/Test_mrhs_blockproject_import.cc
Copyright (C) 2026
Author: Peter Boyle <pboyle@bnl.gov>
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 */
//
// ImportFineGridMrhsVectors / ExportCoarseGridMrhsVectors against the vector
// of single RHS routines they replace. The BLAS buffers must come out
// identical, so the mrhs path is a pure removal of the unpack and repack.
//
#include <Grid/Grid.h>
using namespace Grid;
const int nbasis = 8;
const int nrhs = 4;
typedef LatticeFermionD FineField;
typedef iVector<sTComplexD,nbasis> CoarseSiteObj; // unvectorised coarse space
typedef Lattice<CoarseSiteObj> CoarseField;
template<class T>
RealD BufDiff(deviceVector<T> &a,deviceVector<T> &b)
{
GRID_ASSERT(a.size()==b.size());
std::vector<T> ha(a.size()),hb(b.size());
acceleratorCopyFromDevice(&a[0],&ha[0],a.size()*sizeof(T));
acceleratorCopyFromDevice(&b[0],&hb[0],b.size()*sizeof(T));
RealD num=0.0;
for(int64_t i=0;i<a.size();i++){
T d = ha[i]-hb[i];
num += real(d)*real(d)+imag(d)*imag(d);
}
return num;
}
template<class T>
RealD BufNorm(deviceVector<T> &a)
{
std::vector<T> ha(a.size());
acceleratorCopyFromDevice(&a[0],&ha[0],a.size()*sizeof(T));
RealD num=0.0;
for(int64_t i=0;i<a.size();i++) num += real(ha[i])*real(ha[i])+imag(ha[i])*imag(ha[i]);
return num;
}
int main (int argc, char ** argv)
{
Grid_init(&argc,&argv);
Coordinate flatt = GridDefaultLatt();
Coordinate fsimd = GridDefaultSimd(Nd,vComplexD::Nsimd());
Coordinate fmpi = GridDefaultMpi();
Coordinate block({2,2,2,2});
Coordinate clatt(Nd);
for(int d=0;d<Nd;d++) clatt[d]=flatt[d]/block[d];
GridCartesian *FineGrid = new GridCartesian(flatt,fsimd,fmpi);
Coordinate csimd(Nd,1); // coarse space is unvectorised
GridCartesian *CoarseGrid = new GridCartesian(clatt,csimd,fmpi);
// D+1 grids, rhs innermost and unvectorised
Coordinate fml(1,nrhs),fms(1,1),fmm(1,1);
Coordinate cml(1,nrhs),cms(1,1),cmm(1,1);
for(int d=0;d<Nd;d++){
fml.push_back(flatt[d]); fms.push_back(fsimd[d]); fmm.push_back(fmpi[d]);
cml.push_back(clatt[d]); cms.push_back(csimd[d]); cmm.push_back(fmpi[d]);
}
GridCartesian *FineMulti = new GridCartesian(fml,fms,fmm);
GridCartesian *CoarseMulti = new GridCartesian(cml,cms,cmm);
std::cout << GridLogMessage << "fine "<<flatt<<" coarse "<<clatt<<" nrhs "<<nrhs
<< " fine Nsimd "<<FineGrid->Nsimd()<<" coarse Nsimd "<<CoarseGrid->Nsimd()<<std::endl;
GridParallelRNG pRNG(FineGrid); pRNG.SeedFixedIntegers(std::vector<int>({1,2,3,4}));
MultiRHSBlockProject<FineField> Projector;
Projector.Allocate(nbasis,FineGrid,CoarseGrid);
///////////////////////////////////////////////////////
// Same data as nrhs single RHS fields and as one D+1
///////////////////////////////////////////////////////
std::vector<FineField> vecs(nrhs,FineGrid);
FineField mrhs(FineMulti);
for(int r=0;r<nrhs;r++){
random(pRNG,vecs[r]);
InsertSliceFast(vecs[r],mrhs,r,0);
}
int64_t fsz = FineGrid->lSites()*Projector.words*nrhs;
deviceVector<ComplexD> Fa(fsz), Fb(fsz);
Projector.ImportFineGridVectors (vecs,Fa);
Projector.ImportFineGridMrhsVectors(mrhs,Fb);
RealD fn = BufNorm(Fa);
RealD fd = BufDiff(Fa,Fb);
std::cout << GridLogMessage << "fine import |vector|^2 = " << fn
<< " |vector - mrhs|^2 = " << fd << std::endl;
GRID_ASSERT( fn > 0.0 );
GRID_ASSERT( fd == 0.0 );
///////////////////////////////////////////////////////
// Coarse export: fill the BLAS buffer, read it back
// both ways, and compare the resulting fields
///////////////////////////////////////////////////////
int64_t csz = CoarseGrid->lSites()*nbasis*nrhs;
deviceVector<ComplexD> Cbuf(csz);
{
std::vector<ComplexD> host(csz);
GridSerialRNG sRNG; sRNG.SeedFixedIntegers(std::vector<int>({9,8,7,6}));
for(int64_t i=0;i<csz;i++){
RealD re,im; random(sRNG,re); random(sRNG,im);
host[i]=ComplexD(re-0.5,im-0.5);
}
acceleratorCopyToDevice(&host[0],&Cbuf[0],csz*sizeof(ComplexD));
}
std::vector<CoarseField> cvecs(nrhs,CoarseGrid);
CoarseField cmrhs(CoarseMulti);
Projector.ExportCoarseGridVectors (cvecs,Cbuf);
Projector.ExportCoarseGridMrhsVectors(cmrhs,Cbuf);
RealD cn=0.0, cd=0.0;
for(int r=0;r<nrhs;r++){
CoarseField slice(CoarseGrid);
ExtractSliceFast(slice,cmrhs,r,0);
CoarseField e(CoarseGrid);
e = slice - cvecs[r];
cn += norm2(cvecs[r]);
cd += norm2(e);
}
std::cout << GridLogMessage << "coarse export |vector|^2 = " << cn
<< " |vector - mrhs|^2 = " << cd << std::endl;
GRID_ASSERT( cn > 0.0 );
GRID_ASSERT( cd == 0.0 );
///////////////////////////////////////////////////////
// All four blockProject orderings must agree
///////////////////////////////////////////////////////
{
std::vector<FineField> basis(nbasis,FineGrid);
for(int b=0;b<nbasis;b++) random(pRNG,basis[b]);
Projector.ImportBasis(basis);
std::vector<CoarseField> cvv(nrhs,CoarseGrid); // vector fine -> vector coarse
std::vector<CoarseField> cmv(nrhs,CoarseGrid); // mrhs fine -> vector coarse
CoarseField cvm(CoarseMulti); // vector fine -> mrhs coarse
CoarseField cmm(CoarseMulti); // mrhs fine -> mrhs coarse
Projector.blockProject(vecs,cvv);
Projector.blockProject(mrhs,cmv);
Projector.blockProject(vecs,cvm);
Projector.blockProject(mrhs,cmm);
RealD ref=0.0, d_mv=0.0, d_vm=0.0, d_mm=0.0;
for(int r=0;r<nrhs;r++){
CoarseField svm(CoarseGrid),smm(CoarseGrid),e(CoarseGrid);
ExtractSliceFast(svm,cvm,r,0);
ExtractSliceFast(smm,cmm,r,0);
ref += norm2(cvv[r]);
e = cmv[r]-cvv[r]; d_mv += norm2(e);
e = svm -cvv[r]; d_vm += norm2(e);
e = smm -cvv[r]; d_mm += norm2(e);
}
std::cout << GridLogMessage << "blockProject |vec->vec|^2 = " << ref << std::endl;
std::cout << GridLogMessage << " mrhs->vec diff " << d_mv
<< " vec->mrhs diff " << d_vm
<< " mrhs->mrhs diff " << d_mm << std::endl;
GRID_ASSERT( ref > 0.0 );
GRID_ASSERT( d_mv == 0.0 );
GRID_ASSERT( d_vm == 0.0 );
GRID_ASSERT( d_mm == 0.0 );
///////////////////////////////////////////////////////
// Promote must agree across orderings too
///////////////////////////////////////////////////////
std::vector<FineField> fvv(nrhs,FineGrid);
FineField fmm(FineMulti);
Projector.blockPromote(fvv,cvv);
Projector.blockPromote(fmm,cmm);
RealD pref=0.0, pdiff=0.0;
for(int r=0;r<nrhs;r++){
FineField s(FineGrid),e(FineGrid);
ExtractSliceFast(s,fmm,r,0);
e = s - fvv[r];
pref += norm2(fvv[r]);
pdiff += norm2(e);
}
std::cout << GridLogMessage << "blockPromote |vec|^2 = " << pref
<< " mrhs diff " << pdiff << std::endl;
GRID_ASSERT( pref > 0.0 );
GRID_ASSERT( pdiff == 0.0 );
}
std::cout << GridLogMessage << "Test_mrhs_blockproject_import: ALL PASS" << std::endl;
Grid_finalize();
}
+157
View File
@@ -0,0 +1,157 @@
/*************************************************************************************
Grid physics library, www.github.com/paboyle/Grid
Source file: ./tests/debug/Test_mrhs_promotion.cc
Copyright (C) 2026
Author: Peter Boyle <pboyle@bnl.gov>
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 */
//
// MrhsPromotedOperator: a D dimensional operator presented as D+1 with Nrhs.
//
// Each output slice must equal the D dimensional operator applied to the
// corresponding input slice, bit for bit -- the promotion moves data, it does
// not change arithmetic. Slices are independent random fields so a mistake in
// the slice indexing cannot pass.
//
#include <Grid/Grid.h>
using namespace Grid;
int main (int argc, char ** argv)
{
Grid_init(&argc,&argv);
const int Nrhs = 4;
Coordinate latt = GridDefaultLatt();
Coordinate simd = GridDefaultSimd(Nd,vComplexD::Nsimd());
Coordinate mpi = GridDefaultMpi();
GridCartesian *UGrid = new GridCartesian(latt,simd,mpi);
GridRedBlackCartesian *UrbGrid= SpaceTimeGrid::makeFourDimRedBlackGrid(UGrid);
// D+1 grid with Nrhs in dimension 0
GridCartesian *RGrid = SpaceTimeGrid::makeFiveDimGrid(Nrhs,UGrid);
std::cout << GridLogMessage << "Nrhs = " << Nrhs
<< " low grid " << UGrid->_ndimension << "d"
<< " high grid " << RGrid->_ndimension << "d" << std::endl;
GridParallelRNG pRNG(UGrid); pRNG.SeedFixedIntegers(std::vector<int>({1,2,3,4}));
LatticeGaugeFieldD Umu(UGrid); SU<Nc>::HotConfiguration(pRNG,Umu);
RealD mass = 0.1;
WilsonFermionD Dw(Umu,*UGrid,*UrbGrid,mass);
MdagMLinearOperator<WilsonFermionD,LatticeFermionD> HermOp(Dw);
MrhsPromotedOperator<LatticeFermionD> MrhsOp(HermOp,UGrid,Nrhs);
//////////////////////////////////////////////////
// Independent random source on each slice
//////////////////////////////////////////////////
LatticeFermionD hi_in (RGrid);
LatticeFermionD hi_out(RGrid);
std::vector<LatticeFermionD> lo_in (Nrhs,UGrid);
std::vector<LatticeFermionD> lo_ref(Nrhs,UGrid);
for(int r=0;r<Nrhs;r++){
random(pRNG,lo_in[r]);
InsertSliceFast(lo_in[r],hi_in,r,0);
}
//////////////////////////////////////////////////
// Promoted application, and the reference
//////////////////////////////////////////////////
MrhsOp.Op(hi_in,hi_out);
for(int r=0;r<Nrhs;r++){
HermOp.Op(lo_in[r],lo_ref[r]);
}
RealD worst = 0.0;
for(int r=0;r<Nrhs;r++){
LatticeFermionD slice(UGrid);
LatticeFermionD err(UGrid);
ExtractSliceFast(slice,hi_out,r,0);
err = slice - lo_ref[r];
RealD n = norm2(err);
std::cout << GridLogMessage << "rhs["<<r<<"] |promoted - reference|^2 = " << n
<< " |reference|^2 = " << norm2(lo_ref[r]) << std::endl;
worst = std::max(worst,n);
GRID_ASSERT( n == 0.0 );
}
std::cout << GridLogMessage << "worst slice difference " << worst << " (bit exact required)" << std::endl;
//////////////////////////////////////////////////
// Slices must be independent: perturbing one must
// change only that output slice
//////////////////////////////////////////////////
{
LatticeFermionD pert(UGrid);
random(pRNG,pert);
InsertSliceFast(pert,hi_in,1,0);
MrhsOp.Op(hi_in,hi_out);
for(int r=0;r<Nrhs;r++){
LatticeFermionD slice(UGrid);
LatticeFermionD err(UGrid);
ExtractSliceFast(slice,hi_out,r,0);
err = slice - lo_ref[r];
RealD n = norm2(err);
if ( r == 1 ) {
std::cout << GridLogMessage << "perturbed rhs[1] changed by " << n << std::endl;
GRID_ASSERT( n > 0.0 );
} else {
std::cout << GridLogMessage << "untouched rhs["<<r<<"] moved by " << n << std::endl;
GRID_ASSERT( n == 0.0 );
}
}
}
//////////////////////////////////////////////////
// AdjOp routes through the same slicing
//////////////////////////////////////////////////
{
for(int r=0;r<Nrhs;r++){
random(pRNG,lo_in[r]);
InsertSliceFast(lo_in[r],hi_in,r,0);
HermOp.AdjOp(lo_in[r],lo_ref[r]);
}
MrhsOp.AdjOp(hi_in,hi_out);
for(int r=0;r<Nrhs;r++){
LatticeFermionD slice(UGrid);
LatticeFermionD err(UGrid);
ExtractSliceFast(slice,hi_out,r,0);
err = slice - lo_ref[r];
GRID_ASSERT( norm2(err) == 0.0 );
}
std::cout << GridLogMessage << "AdjOp slices bit exact" << std::endl;
}
std::cout << GridLogMessage << "Test_mrhs_promotion: ALL PASS" << std::endl;
Grid_finalize();
}