Extension: Json and NetCDF utilities
#include "dg/file/file.h"
Loading...
Searching...
No Matches
nc_hyperslab.h
Go to the documentation of this file.
1#pragma once
2#include <vector>
3#include <netcdf.h>
4
5namespace dg{
6namespace file{
27{
34 NcHyperslab( size_t start, size_t count = 1)
35 : m_start( 1, start), m_count( 1, count)
36 {
37 }
38
45 NcHyperslab(std::vector<size_t> start, std::vector<size_t> count)
46 : m_start(start), m_count(count)
47 {
48 assert( start.size() == count.size());
49 }
50
58 template<class File>
59 NcHyperslab( const File& file, std::string name)
60 {
61 auto dims = file.get_var_dims(name);
62 m_count = std::vector<size_t>( get_dims_shape( dims));
63 m_start = std::vector<size_t>( dims.size(), 0);
64 }
65
73 template<class ContainerType, std::enable_if_t< dg::is_vector_v<
74 ContainerType, dg::SharedVectorTag>, bool> = true>
75 NcHyperslab( const ContainerType& data)
76 : NcHyperslab( 0, data.size())
77 {
78 }
86 template<class Topology, std::enable_if_t<
87 !dg::is_vector_v<Topology>, bool> = true>
88 NcHyperslab( const Topology& grid)
89 {
90 auto ss = grid.start();
91 auto cc = grid.count();
92 m_start = std::vector<size_t>( ss.begin(), ss.end());
93 m_count = std::vector<size_t>( cc.begin(), cc.end());
94 }
95
97 template<class T>
98 NcHyperslab( size_t start0, const T& param)
99 : NcHyperslab( start0, 1, param)
100 {
101 }
102
112 template<class T>
113 NcHyperslab( size_t start0, size_t count0, const T& param)
114 : NcHyperslab( param)
115 {
116 m_start.insert( m_start.begin(), start0);
117 m_count.insert( m_count.begin(), count0);
118 }
119
121 unsigned ndim() const { return m_start.size();}
122
124 const std::vector<size_t>& start() const { return m_start;}
126 const std::vector<size_t>& count() const { return m_count;}
128 std::vector<size_t>& start() { return m_start;}
130 std::vector<size_t>& count() { return m_count;}
132 const size_t* startp() const { return &m_start[0];}
134 const size_t* countp() const { return &m_count[0];}
135 private:
136 std::vector<size_t> m_start, m_count;
137};
138
139#ifdef MPI_VERSION
151{
152
160 MPINcHyperslab( size_t local_start, size_t local_count, MPI_Comm comm)
161 : m_slab( local_start, local_count), m_comm(comm)
162 {
163 }
164
165
173 MPINcHyperslab(std::vector<size_t> local_start,
174 std::vector<size_t> local_count, MPI_Comm comm)
175 : m_slab( local_start, local_count), m_comm(comm)
176 {
177 }
178
191 template<class ContainerType, std::enable_if_t< dg::is_vector_v<
192 ContainerType, dg::MPIVectorTag>, bool> = true>
193 MPINcHyperslab( const ContainerType& data)
194 : m_slab ( 0) // "default" construct
195 {
196 int count = data.size();
197 MPI_Comm comm = data.communicator();
198 int rank, size;
199 MPI_Comm_rank( comm, &rank);
200 MPI_Comm_size( comm, &size);
201 std::vector<int> counts ( size);
202 MPI_Allgather( &count, 1, MPI_INT, &counts[0], 1, MPI_INT, comm);
203 size_t start = 0;
204 for( int r=0; r<rank; r++)
205 start += counts[r];
206
207 *this = MPINcHyperslab{ start, (size_t)count, comm};
208 }
209
219 template<class MPITopology, std::enable_if_t<!dg::is_vector_v<MPITopology>,
220 bool> = true>
221 MPINcHyperslab( const MPITopology& grid)
222 : m_slab( grid), m_comm(grid.communicator())
223 {
224 }
225
227 template<class T>
228 MPINcHyperslab( size_t start0, const T& param)
229 : MPINcHyperslab( start0, 1, param)
230 {
231 }
244 template<class T>
245 MPINcHyperslab( size_t start0, size_t count0, const T& param)
246 : MPINcHyperslab( param)
247 {
248 m_slab.start().insert( m_slab.start().begin(), start0);
249 m_slab.count().insert( m_slab.count().begin(), count0);
250 }
251
253 unsigned ndim() const { return m_slab.ndim();}
254
256 const std::vector<size_t>& start() const { return m_slab.start();}
258 const std::vector<size_t>& count() const { return m_slab.count();}
260 std::vector<size_t>& start() { return m_slab.start();}
262 std::vector<size_t>& count() { return m_slab.count();}
264 MPI_Comm communicator() const { return m_comm;}
266 const size_t* startp() const { return m_slab.startp();}
268 const size_t* countp() const { return m_slab.countp();}
269 private:
270 NcHyperslab m_slab;
271 MPI_Comm m_comm;
272};
273
275namespace detail
276{
296inline int mpi_comm_global2local_rank( MPI_Comm comm, int global_rank = 0, MPI_Comm global_comm = MPI_COMM_WORLD )
297{
298 MPI_Group local_group, global_group;
299 MPI_Comm_group(comm, &local_group);//local call
300 MPI_Comm_group(MPI_COMM_WORLD, &global_group);//local call
301 int local_root_rank;
302 MPI_Group_translate_ranks(global_group, 1, &global_rank, local_group, &local_root_rank);
303 return local_root_rank;
304}
305} // namespace detail
307#endif
308
309}//namespace file
310}//namespace dg
Definition easy_atts.h:15
A NetCDF Hyperslab for MPINcFile.
Definition nc_hyperslab.h:151
const size_t * countp() const
Definition nc_hyperslab.h:268
MPINcHyperslab(const MPITopology &grid)
grid.start(), grid.count(), grid.communicator()
Definition nc_hyperslab.h:221
MPINcHyperslab(size_t start0, const T &param)
Same as MPINcHyperslab{ start0, 1, grid}
Definition nc_hyperslab.h:228
const size_t * startp() const
Definition nc_hyperslab.h:266
MPINcHyperslab(const ContainerType &data)
{local_start(data) , local_size(data), data.communicator()}
Definition nc_hyperslab.h:193
MPI_Comm communicator() const
Definition nc_hyperslab.h:264
MPINcHyperslab(size_t start0, size_t count0, const T &param)
{start0, MPINcHyperslab( param).start()}, {count0, MPINcHyperslab(param).count(), MPINcHyperslab....
Definition nc_hyperslab.h:245
std::vector< size_t > & count()
Definition nc_hyperslab.h:262
MPINcHyperslab(size_t local_start, size_t local_count, MPI_Comm comm)
{local_start}, {local_count}
Definition nc_hyperslab.h:160
const std::vector< size_t > & count() const
Definition nc_hyperslab.h:258
const std::vector< size_t > & start() const
Definition nc_hyperslab.h:256
unsigned ndim() const
Definition nc_hyperslab.h:253
MPINcHyperslab(std::vector< size_t > local_start, std::vector< size_t > local_count, MPI_Comm comm)
local_start, local_count, comm
Definition nc_hyperslab.h:173
std::vector< size_t > & start()
Definition nc_hyperslab.h:260
A NetCDF Hyperslab for SerialNcFile.
Definition nc_hyperslab.h:27
const size_t * countp() const
Definition nc_hyperslab.h:134
std::vector< size_t > & count()
Definition nc_hyperslab.h:130
NcHyperslab(std::vector< size_t > start, std::vector< size_t > count)
start, count
Definition nc_hyperslab.h:45
std::vector< size_t > & start()
Definition nc_hyperslab.h:128
const std::vector< size_t > & count() const
Definition nc_hyperslab.h:126
const size_t * startp() const
Definition nc_hyperslab.h:132
const std::vector< size_t > & start() const
Definition nc_hyperslab.h:124
unsigned ndim() const
Definition nc_hyperslab.h:121
NcHyperslab(const Topology &grid)
grid.start(), grid.count()
Definition nc_hyperslab.h:88
NcHyperslab(size_t start0, size_t count0, const T &param)
{start0, NcHyperslab( param).start()}, {count0, NcHyperslab(param).count()}
Definition nc_hyperslab.h:113
NcHyperslab(const ContainerType &data)
{0 , data.size()}
Definition nc_hyperslab.h:75
NcHyperslab(size_t start0, const T &param)
Same as NcHyperslab{ start0, 1, param}
Definition nc_hyperslab.h:98
NcHyperslab(const File &file, std::string name)
Definition nc_hyperslab.h:59
NcHyperslab(size_t start, size_t count=1)
{start}, {count}
Definition nc_hyperslab.h:34