// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- // vi: set et ts=4 sw=2 sts=2: #include #include #include #include #include #include #include #include #include "mpi.h" enum GridFlags { owner, overlap, border }; void testIndices(MPI_Comm comm) { //using namespace Dune; // The global grid size const int Nx = 20; const int Ny = 2; // Process configuration int procs, rank, master=0; MPI_Comm_size(comm, &procs); MPI_Comm_rank(comm, &rank); // shift the ranks //rank = (rank + 1) % procs; //master= (master+1) %procs; // The local grid int nx = Nx/procs; // distributed indexset // typedef ParallelLocalIndex LocalIndexType; typedef Dune::ParallelIndexSet,45> ParallelIndexSet; ParallelIndexSet distIndexSet; // global indexset ParallelIndexSet globalIndexSet; // Set up the indexsets. int start = std::max(rank*nx-1,0); int end = std::min((rank + 1) * nx+1, Nx); distIndexSet.beginResize(); int localIndex=0; int size = Ny*(end-start); typedef Dune::FieldVector Vector; typedef std::vector Array; Array distArray(size); Array* globalArray; int index=0; for(int j=0; j=end-2); GridFlags flag = owner; if((i==start && i!=0)||(i==end-1 && i!=Nx-1)) { distArray[index++]=-(i+j*Nx+rank*Nx*Ny); flag = overlap; }else distArray[index++]=i+j*Nx+rank*Nx*Ny; distIndexSet.add(i+j*Nx, Dune::ParallelLocalIndex (localIndex++,flag,isPublic)); } distIndexSet.endResize(); if(rank==master) { // build global indexset on first process globalIndexSet.beginResize(); globalArray=new Array(Nx*Ny); for(int j=0; j (i+j*Nx,owner,false)); globalArray->operator[](i+j*Nx)=-(i+j*Nx); } globalIndexSet.endResize(); }else globalArray=new Array(1); // Size one is needed for CommPolicy typedef Dune::RemoteIndices RemoteIndices; RemoteIndices accuIndices(distIndexSet, globalIndexSet, comm); RemoteIndices overlapIndices(distIndexSet, distIndexSet, comm); accuIndices.rebuild(); overlapIndices.rebuild(); Dune::DatatypeCommunicator accumulator, overlapExchanger; Dune::EnumItem sourceFlags; Dune::Combine,Dune::EnumItem,GridFlags> destFlags; accumulator.build(accuIndices, sourceFlags, distArray, destFlags, *globalArray); overlapExchanger.build(overlapIndices, Dune::EnumItem(), distArray, Dune::EnumItem(), distArray); std::cout<< rank<<": before forward distArray="; std::copy(distArray.begin(), distArray.end(), std::ostream_iterator(std::cout, " ")); // Exchange the overlap overlapExchanger.forward(); std::cout<begin(), globalArray->end(), std::ostream_iterator(std::cout, " ")); struct Twice { void operator()(Vector& v) { v*=2; } }; std::for_each(globalArray->begin(), globalArray->end(), Twice()); std::cout<<" Multiplied by two: globalArray="; std::copy(globalArray->begin(), globalArray->end(), std::ostream_iterator(std::cout, " ")); } accumulator.backward(); std::cout<< rank<<": after backward distArray="; std::copy(distArray.begin(), distArray.end(), std::ostream_iterator(std::cout, " ")); // Exchange the overlap overlapExchanger.forward(); std::cout<