Discontinuous Galerkin Library
#include "dg/algorithm.h"
split_and_join.h
Go to the documentation of this file.
1#pragma once
2#include <thrust/host_vector.h>
3#include <thrust/device_vector.h>
4#include "dg/backend/blas1_dispatch_shared.h"
5#include "dg/backend/view.h"
6#include "dg/blas1.h"
7#include "grid.h"
8#ifdef MPI_VERSION
10#include "mpi_grid.h"
11#include "mpi_evaluation.h"
12#endif //MPI_VERSION
13
14namespace dg
15{
16
19
29template<class SharedContainer, class real_type>
30void split( SharedContainer& in, std::vector<View<SharedContainer>>& out, const aRealTopology3d<real_type>& grid)
31{
32 assert( out.size() == grid.nz()*grid.Nz());
33 unsigned size2d=grid.nx()*grid.ny()*grid.Nx()*grid.Ny();
34 for(unsigned i=0; i<grid.nz()*grid.Nz(); i++)
35 out[i].construct( thrust::raw_pointer_cast(in.data()) + i*size2d, size2d);
36}
37
46template<class SharedContainer, class real_type>
47std::vector<View<SharedContainer>> split( SharedContainer& in, const aRealTopology3d<real_type>& grid)
48{
49 std::vector<View<SharedContainer>> out;
50 RealGrid3d<real_type> l( grid);
51 unsigned size2d=l.nx()*l.ny()*l.Nx()*l.Ny();
52 out.resize( l.nz()*l.Nz());
53 for(unsigned i=0; i<l.nz()*l.Nz(); i++)
54 out[i].construct( thrust::raw_pointer_cast(in.data()) + i*size2d, size2d);
55 return out;
56}
57
67template<class Container, class real_type>
68void assign3dfrom2d( const thrust::host_vector<real_type>& in2d, Container&
69 out, const aRealTopology3d<real_type>& grid)
70{
71 thrust::host_vector<real_type> vector( grid.size());
72 std::vector<dg::View< thrust::host_vector<real_type>>> view =
73 dg::split( vector, grid); //3d vector
74 for( unsigned i=0; i<grid.nz()*grid.Nz(); i++)
75 dg::blas1::copy( in2d, view[i]);
76 dg::assign( vector, out);
77}
78
79
80#ifdef MPI_VERSION
81
82template<class MPIContainer>
84 std::conditional_t< std::is_const<MPIContainer>::value,
87
100template<class MPIContainer, class real_type>
101void split( MPIContainer& in, std::vector<get_mpi_view_type<MPIContainer> >&
102 out, const aRealMPITopology3d<real_type>& grid)
103{
104 //local size2d
105 RealGrid3d<real_type> l = grid.local();
106 unsigned size2d=l.nx()*l.ny()*l.Nx()*l.Ny();
107 for(unsigned i=0; i<l.nz()*l.Nz(); i++)
108 {
109 out[i].data().construct( thrust::raw_pointer_cast(in.data().data()) +
110 i*size2d, size2d);
111 }
112}
122template< class MPIContainer, class real_type>
123std::vector<get_mpi_view_type<MPIContainer> > split(
124 MPIContainer& in, const aRealMPITopology3d<real_type>& grid)
125{
126 std::vector<get_mpi_view_type<MPIContainer>> out;
127 int result;
128 MPI_Comm_compare( in.communicator(), grid.communicator(), &result);
129 assert( result == MPI_CONGRUENT || result == MPI_IDENT);
130 MPI_Comm planeComm = grid.get_perp_comm(), comm_mod, comm_mod_reduce;
131 exblas::mpi_reduce_communicator( planeComm, &comm_mod, &comm_mod_reduce);
132 //local size2d
133 RealGrid3d<real_type> l = grid.local();
134 unsigned size2d=l.nx()*l.ny()*l.Nx()*l.Ny();
135 out.resize( l.nz()*l.Nz());
136 for(unsigned i=0; i<l.nz()*l.Nz(); i++)
137 {
138 out[i].data().construct( thrust::raw_pointer_cast(in.data().data())
139 + i*size2d, size2d);
140 out[i].set_communicator( planeComm, comm_mod, comm_mod_reduce);
141 }
142 return out;
143}
144
154template<class LocalContainer, class real_type>
155void assign3dfrom2d( const MPI_Vector<thrust::host_vector<real_type>>& in2d,
158{
160 std::vector<MPI_Vector<dg::View<thrust::host_vector<real_type>>> > view =
161 dg::split( vector, grid); //3d vector
162 for( unsigned i=0; i<grid.nz()*grid.local().Nz(); i++)
163 dg::blas1::copy( in2d, view[i]);
164 dg::assign( vector, out);
165}
166#endif //MPI_VERSION
167
169}//namespace dg
base topology classes
void assign(const from_ContainerType &from, ContainerType &to, Params &&... ps)
Generic way to assign the contents of a from_ContainerType object to a ContainerType object optionall...
Definition: blas1.h:665
static DG_DEVICE double zero(double x)
Definition: functions.h:29
void copy(const ContainerTypeIn &source, ContainerTypeOut &target)
Definition: blas1.h:164
thrust::host_vector< real_type > evaluate(UnaryOp f, const RealGrid1d< real_type > &g)
Evaluate a 1d function on grid coordinates.
Definition: evaluation.h:67
void assign3dfrom2d(const thrust::host_vector< real_type > &in2d, Container &out, const aRealTopology3d< real_type > &grid)
Construct a 3d vector given a 2d host vector.
Definition: split_and_join.h:68
std::conditional_t< std::is_const< MPIContainer >::value, MPI_Vector< View< const typename MPIContainer::container_type > >, MPI_Vector< View< typename MPIContainer::container_type > > > get_mpi_view_type
Definition: split_and_join.h:86
void split(SharedContainer &in, std::vector< View< SharedContainer > > &out, const aRealTopology3d< real_type > &grid)
Split a vector into planes along the last dimension (fast version)
Definition: split_and_join.h:30
Function discretization routines for mpi vectors.
MPI Grid objects.
static void mpi_reduce_communicator(MPI_Comm comm, MPI_Comm *comm_mod, MPI_Comm *comm_mod_reduce)
This is the namespace for all functions and classes defined and used by the discontinuous Galerkin li...
mpi Vector class
Definition: mpi_vector.h:32
The simplest implementation of aRealTopology3d.
Definition: grid.h:844
A vector view class, usable in dg functions.
Definition: view.h:43
3D MPI Grid class
Definition: mpi_grid.h:329
MPI_Comm get_perp_comm() const
MPI Cartesian communicator in the first two dimensions (x and y)
Definition: mpi_grid.h:464
unsigned nz() const
number of polynomial coefficients in z
Definition: mpi_grid.h:418
MPI_Comm communicator() const
Return mpi cartesian communicator that is used in this grid.
Definition: mpi_grid.h:459
const RealGrid3d< real_type > & local() const
Return a non-MPI grid local for the calling process.
Definition: mpi_grid.h:560
An abstract base class for three-dimensional grids.
Definition: grid.h:523
unsigned size() const
The total number of points.
Definition: grid.h:670
unsigned nz() const
number of polynomial coefficients in z
Definition: grid.h:616
unsigned Nx() const
number of points in x
Definition: grid.h:622
unsigned ny() const
number of polynomial coefficients in y
Definition: grid.h:614
unsigned Ny() const
number of points in y
Definition: grid.h:628
unsigned Nz() const
number of points in z
Definition: grid.h:634
unsigned nx() const
number of polynomial coefficients in x
Definition: grid.h:612