| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception |
| 3 |
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 4 |
// vi: set et ts=4 sw=2 sts=2: |
| 5 |
|
| 6 |
#include <iterator> |
| 7 |
|
| 8 |
#include <dune/common/fmatrix.hh> |
| 9 |
#include <dune/common/fvector.hh> |
| 10 |
#include <dune/common/float_cmp.hh> |
| 11 |
#include <dune/common/gmpfield.hh> |
| 12 |
#include <dune/common/quadmath.hh> |
| 13 |
#include <dune/common/simd/loop.hh> |
| 14 |
|
| 15 |
#include <dune/istl/matrixmarket.hh> |
| 16 |
#include <dune/istl/io.hh> |
| 17 |
#include <dune/istl/bvector.hh> |
| 18 |
|
| 19 |
#if HAVE_MPI |
| 20 |
#include <dune/istl/paamg/test/anisotropic.hh> |
| 21 |
#include "mpi.h" |
| 22 |
#include <dune/istl/schwarz.hh> |
| 23 |
#else |
| 24 |
#include <dune/istl/operators.hh> |
| 25 |
#include "laplacian.hh" |
| 26 |
#endif |
| 27 |
|
| 28 |
#if HAVE_GMP |
| 29 |
template<unsigned int prec> |
| 30 |
struct Dune::FloatCmp::EpsilonType<Dune::GMPField<prec>> { |
| 31 |
typedef float Type; |
| 32 |
}; |
| 33 |
#endif |
| 34 |
|
| 35 |
template <class Matrix, class Vector> |
| 36 |
int testMatrixMarket(int N) |
| 37 |
{ |
| 38 |
#if HAVE_MPI |
| 39 |
typedef int GlobalId; |
| 40 |
typedef Dune::OwnerOverlapCopyCommunication<GlobalId> Communication; |
| 41 |
Communication comm(MPI_COMM_WORLD); |
| 42 |
std::cout << comm.communicator().rank() << " " << comm.communicator().size() << std::endl; |
| 43 |
int n; |
| 44 |
Matrix mat = setupAnisotropic2d<typename Matrix::block_type>(N, comm.indexSet(), comm.communicator(), &n, .011); |
| 45 |
#else |
| 46 |
Matrix mat; |
| 47 |
setupLaplacian(mat, N); |
| 48 |
#endif |
| 49 |
|
| 50 |
Vector bv(mat.N()), cv(mat.N()); |
| 51 |
|
| 52 |
int i=0; |
| 53 |
for(auto&& block : bv) |
| 54 |
for(auto&& entry : Dune::Impl::asVector(block)) |
| 55 |
entry = (i++); |
| 56 |
|
| 57 |
using R = typename Dune::FieldTraits<Vector>::real_type; |
| 58 |
int prec = std::numeric_limits<R>::is_specialized |
| 59 |
? std::numeric_limits<R>::max_digits10 |
| 60 |
: std::numeric_limits<long double>::max_digits10; |
| 61 |
R eps = std::numeric_limits<R>::is_specialized |
| 62 |
? std::numeric_limits<R>::epsilon() |
| 63 |
: R(std::numeric_limits<float>::epsilon()) ; |
| 64 |
|
| 65 |
#if HAVE_MPI |
| 66 |
comm.remoteIndices().rebuild<false>(); |
| 67 |
comm.copyOwnerToAll(bv,bv); |
| 68 |
|
| 69 |
Dune::OverlappingSchwarzOperator<Matrix,Vector,Vector,Communication> op(mat, comm); |
| 70 |
op.apply(bv, cv); |
| 71 |
storeMatrixMarket(mat, std::string("testmat"), comm, true, prec); |
| 72 |
storeMatrixMarket(bv, std::string("testvec"), comm, false, prec); |
| 73 |
#else |
| 74 |
typedef Dune::MatrixAdapter<Matrix,Vector,Vector> Operator; |
| 75 |
Operator op(mat); |
| 76 |
op.apply(bv, cv); |
| 77 |
|
| 78 |
storeMatrixMarket(mat, std::string("testmat"), prec); |
| 79 |
storeMatrixMarket(bv, std::string("testvec"), prec); |
| 80 |
#endif |
| 81 |
|
| 82 |
Matrix mat1; |
| 83 |
Vector bv1,cv1; |
| 84 |
|
| 85 |
#if HAVE_MPI |
| 86 |
Communication comm1(MPI_COMM_WORLD); |
| 87 |
|
| 88 |
loadMatrixMarket(mat1, std::string("testmat"), comm1); |
| 89 |
loadMatrixMarket(bv1, std::string("testvec"), comm1, false); |
| 90 |
#else |
| 91 |
loadMatrixMarket(mat1, std::string("testmat")); |
| 92 |
loadMatrixMarket(bv1, std::string("testvec")); |
| 93 |
#endif |
| 94 |
|
| 95 |
int ret=0; |
| 96 |
if(mat.N()!=mat1.N() || mat.M()!=mat1.M()) |
| 97 |
{ |
| 98 |
++ret; |
| 99 |
std::cerr<<"matrix sizes do not match"<<std::endl; |
| 100 |
} |
| 101 |
|
| 102 |
for (auto row=mat.begin(), row1=mat1.begin(); row!=mat.end(); ++row, ++row1) |
| 103 |
for (auto col=row->begin(), col1=row1->begin(); col!= row->end(); ++col, ++col1) |
| 104 |
{ |
| 105 |
if(col.index()!=col1.index()) { |
| 106 |
std::cerr <<"Column indices do not match"<<std::endl; |
| 107 |
++ret; |
| 108 |
} |
| 109 |
if(!Dune::FloatCmp::eq(*col, *col1)) { |
| 110 |
using std::abs; |
| 111 |
std::cerr <<"Matrix entries do not match: " << abs(*col - *col1) << std::endl; |
| 112 |
++ret; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
for (auto entry=bv.begin(), entry1=bv1.begin(); bv.end() != entry; ++entry, ++entry1) |
| 117 |
if (Dune::Simd::anyTrue(abs(*entry - *entry1) > eps)) |
| 118 |
{ |
| 119 |
std::cerr<<"written and read vector do not match"<<std::endl; |
| 120 |
++ret; |
| 121 |
} |
| 122 |
|
| 123 |
cv1.resize(mat1.M()); |
| 124 |
|
| 125 |
#if HAVE_MPI |
| 126 |
Dune::OverlappingSchwarzOperator<Matrix,Vector,Vector,Communication> op1(mat1, comm1); |
| 127 |
op1.apply(bv1, cv1); |
| 128 |
|
| 129 |
if(comm1.indexSet()!=comm.indexSet()) |
| 130 |
{ |
| 131 |
std::cerr<<"written and read idxset do not match"<<std::endl; |
| 132 |
++ret; |
| 133 |
} |
| 134 |
#else |
| 135 |
typedef Dune::MatrixAdapter<Matrix,Vector,Vector> Operator; |
| 136 |
Operator op1(mat1); |
| 137 |
op1.apply(bv1, cv1); |
| 138 |
#endif |
| 139 |
|
| 140 |
for (auto entry=cv.begin(), entry1=cv1.begin(); cv.end() != entry; ++entry, ++entry1) |
| 141 |
if (Dune::Simd::anyTrue(abs(*entry - *entry1) > eps)) |
| 142 |
{ |
| 143 |
std::cerr<<"computed vectors do not match: " << *entry << " != " << *entry1 <<std::endl; |
| 144 |
++ret; |
| 145 |
} |
| 146 |
|
| 147 |
return ret; |
| 148 |
} |
| 149 |
|
| 150 |
int main(int argc, char** argv) |
| 151 |
{ |
| 152 |
#if HAVE_MPI |
| 153 |
MPI_Init(&argc, &argv); |
| 154 |
int size; |
| 155 |
MPI_Comm_size(MPI_COMM_WORLD, &size); |
| 156 |
#endif |
| 157 |
const int BS=1; |
| 158 |
int N=2; |
| 159 |
|
| 160 |
if(argc>1) |
| 161 |
N = atoi(argv[1]); |
| 162 |
std::cout << "testing for N=" << N << " BS=" << BS << std::endl; |
| 163 |
|
| 164 |
// Test scalar matrices and vectors |
| 165 |
int ret = testMatrixMarket<Dune::BCRSMatrix<double>, Dune::BlockVector<double> >(N); |
| 166 |
|
| 167 |
#if HAVE_MPI |
| 168 |
if(ret!=0) |
| 169 |
MPI_Abort(MPI_COMM_WORLD, ret); |
| 170 |
#endif |
| 171 |
|
| 172 |
// Test block matrices and vectors with trivial blocks |
| 173 |
typedef Dune::FieldMatrix<double,BS,BS> MatrixBlock; |
| 174 |
typedef Dune::BCRSMatrix<MatrixBlock> BCRSMat; |
| 175 |
typedef Dune::FieldVector<double,BS> VectorBlock; |
| 176 |
typedef Dune::BlockVector<VectorBlock> BVector; |
| 177 |
|
| 178 |
ret |= testMatrixMarket<BCRSMat, BVector>(N); |
| 179 |
|
| 180 |
// test for vector with multiple lanes |
| 181 |
typedef Dune::BlockVector<Dune::LoopSIMD<double, 4>> BVectorSIMD; |
| 182 |
|
| 183 |
ret |= testMatrixMarket<Dune::BCRSMatrix<double>, BVectorSIMD>(N); |
| 184 |
|
| 185 |
// Test other field types |
| 186 |
#if HAVE_QUADMATH |
| 187 |
std::cout << "Test Float128" << std::endl; |
| 188 |
ret |= testMatrixMarket<Dune::BCRSMatrix<Dune::Float128>, Dune::BlockVector<Dune::Float128>>(N); |
| 189 |
#endif |
| 190 |
|
| 191 |
#if HAVE_GMP |
| 192 |
std::cout << "Test GMPField" << std::endl; |
| 193 |
ret |= testMatrixMarket<Dune::BCRSMatrix<Dune::GMPField<128>>, Dune::BlockVector<Dune::GMPField<128>>>(N); |
| 194 |
#endif |
| 195 |
|
| 196 |
#if HAVE_MPI |
| 197 |
if(ret!=0) |
| 198 |
MPI_Abort(MPI_COMM_WORLD, ret); |
| 199 |
#endif |
| 200 |
|
| 201 |
|
| 202 |
#if HAVE_MPI |
| 203 |
MPI_Finalize(); |
| 204 |
#endif |
| 205 |
|
| 206 |
return ret; |
| 207 |
} |