15 inline void ncmpiwrap(
int ierr ,
int line ) {
16 if (ierr != NC_NOERR) {
17 std::cerr <<
"PNetCDF ERROR at line: " << line << std::endl;
18 throw std::runtime_error(ncmpi_strerror(ierr));
27 void pnetcdf_put_var(
int ncid ,
int varid ,
signed char const *data) {
28 ncmpiwrap( ncmpi_put_var_schar( ncid , varid , data ) , __LINE__ );
31 void pnetcdf_put_var(
int ncid ,
int varid ,
unsigned char const *data) {
32 ncmpiwrap( ncmpi_put_var_uchar( ncid , varid , data ) , __LINE__ );
35 void pnetcdf_put_var(
int ncid ,
int varid ,
short const *data) {
36 ncmpiwrap( ncmpi_put_var_short( ncid , varid , data ) , __LINE__ );
39 void pnetcdf_put_var(
int ncid ,
int varid ,
unsigned short const *data) {
40 ncmpiwrap( ncmpi_put_var_ushort( ncid , varid , data ) , __LINE__ );
43 void pnetcdf_put_var(
int ncid ,
int varid ,
int const *data) {
44 ncmpiwrap( ncmpi_put_var_int( ncid , varid , data ) , __LINE__ );
47 void pnetcdf_put_var(
int ncid ,
int varid ,
unsigned int const *data) {
48 ncmpiwrap( ncmpi_put_var_uint( ncid , varid , data ) , __LINE__ );
51 void pnetcdf_put_var(
int ncid ,
int varid ,
long long const *data) {
52 ncmpiwrap( ncmpi_put_var_longlong( ncid , varid , data ) , __LINE__ );
55 void pnetcdf_put_var(
int ncid ,
int varid ,
unsigned long long const *data) {
56 ncmpiwrap( ncmpi_put_var_ulonglong( ncid , varid , data ) , __LINE__ );
59 void pnetcdf_put_var(
int ncid ,
int varid ,
float const *data) {
60 ncmpiwrap( ncmpi_put_var_float( ncid , varid , data ) , __LINE__ );
63 void pnetcdf_put_var(
int ncid ,
int varid ,
double const *data) {
64 ncmpiwrap( ncmpi_put_var_double( ncid , varid , data ) , __LINE__ );
72 void pnetcdf_put_var1(
int ncid ,
int varid ,
signed char const *data) {
73 ncmpiwrap( ncmpi_put_var1_schar( ncid , varid , 0 , data ) , __LINE__ );
76 void pnetcdf_put_var1(
int ncid ,
int varid ,
unsigned char const *data) {
77 ncmpiwrap( ncmpi_put_var1_uchar( ncid , varid , 0 , data ) , __LINE__ );
80 void pnetcdf_put_var1(
int ncid ,
int varid ,
short const *data) {
81 ncmpiwrap( ncmpi_put_var1_short( ncid , varid , 0 , data ) , __LINE__ );
84 void pnetcdf_put_var1(
int ncid ,
int varid ,
unsigned short const *data) {
85 ncmpiwrap( ncmpi_put_var1_ushort( ncid , varid , 0 , data ) , __LINE__ );
88 void pnetcdf_put_var1(
int ncid ,
int varid ,
int const *data) {
89 ncmpiwrap( ncmpi_put_var1_int( ncid , varid , 0 , data ) , __LINE__ );
92 void pnetcdf_put_var1(
int ncid ,
int varid ,
unsigned int const *data) {
93 ncmpiwrap( ncmpi_put_var1_uint( ncid , varid , 0 , data ) , __LINE__ );
96 void pnetcdf_put_var1(
int ncid ,
int varid ,
long long const *data) {
97 ncmpiwrap( ncmpi_put_var1_longlong( ncid , varid , 0 , data ) , __LINE__ );
100 void pnetcdf_put_var1(
int ncid ,
int varid ,
unsigned long long const *data) {
101 ncmpiwrap( ncmpi_put_var1_ulonglong( ncid , varid , 0 , data ) , __LINE__ );
104 void pnetcdf_put_var1(
int ncid ,
int varid ,
float const *data) {
105 ncmpiwrap( ncmpi_put_var1_float( ncid , varid , 0 , data ) , __LINE__ );
108 void pnetcdf_put_var1(
int ncid ,
int varid ,
double const *data) {
109 ncmpiwrap( ncmpi_put_var1_double( ncid , varid , 0 , data ) , __LINE__ );
117 void pnetcdf_put_vara(
int ncid ,
int varid , MPI_Offset
const start[] , MPI_Offset
const count[] ,
signed char const *data) {
118 ncmpiwrap( ncmpi_put_vara_schar( ncid , varid , start ,
count , data ) , __LINE__ );
121 void pnetcdf_put_vara(
int ncid ,
int varid , MPI_Offset
const start[] , MPI_Offset
const count[] ,
unsigned char const *data) {
122 ncmpiwrap( ncmpi_put_vara_uchar( ncid , varid , start ,
count , data ) , __LINE__ );
125 void pnetcdf_put_vara(
int ncid ,
int varid , MPI_Offset
const start[] , MPI_Offset
const count[] ,
short const *data) {
126 ncmpiwrap( ncmpi_put_vara_short( ncid , varid , start ,
count , data ) , __LINE__ );
129 void pnetcdf_put_vara(
int ncid ,
int varid , MPI_Offset
const start[] , MPI_Offset
const count[] ,
unsigned short const *data) {
130 ncmpiwrap( ncmpi_put_vara_ushort( ncid , varid , start ,
count , data ) , __LINE__ );
133 void pnetcdf_put_vara(
int ncid ,
int varid , MPI_Offset
const start[] , MPI_Offset
const count[] ,
int const *data) {
134 ncmpiwrap( ncmpi_put_vara_int( ncid , varid , start ,
count , data ) , __LINE__ );
137 void pnetcdf_put_vara(
int ncid ,
int varid , MPI_Offset
const start[] , MPI_Offset
const count[] ,
unsigned int const *data) {
138 ncmpiwrap( ncmpi_put_vara_uint( ncid , varid , start ,
count , data ) , __LINE__ );
141 void pnetcdf_put_vara(
int ncid ,
int varid , MPI_Offset
const start[] , MPI_Offset
const count[] ,
long long const *data) {
142 ncmpiwrap( ncmpi_put_vara_longlong( ncid , varid , start ,
count , data ) , __LINE__ );
145 void pnetcdf_put_vara(
int ncid ,
int varid , MPI_Offset
const start[] , MPI_Offset
const count[] ,
unsigned long long const *data) {
146 ncmpiwrap( ncmpi_put_vara_ulonglong( ncid , varid , start ,
count , data ) , __LINE__ );
149 void pnetcdf_put_vara(
int ncid ,
int varid , MPI_Offset
const start[] , MPI_Offset
const count[] ,
float const *data) {
150 ncmpiwrap( ncmpi_put_vara_float( ncid , varid , start ,
count , data ) , __LINE__ );
153 void pnetcdf_put_vara(
int ncid ,
int varid , MPI_Offset
const start[] , MPI_Offset
const count[] ,
double const *data) {
154 ncmpiwrap( ncmpi_put_vara_double( ncid , varid , start ,
count , data ) , __LINE__ );
162 void pnetcdf_put_vara_all(
int ncid ,
int varid , MPI_Offset
const start[] , MPI_Offset
const count[] ,
signed char const *data) {
163 ncmpiwrap( ncmpi_put_vara_schar_all( ncid , varid , start ,
count , data ) , __LINE__ );
166 void pnetcdf_put_vara_all(
int ncid ,
int varid , MPI_Offset
const start[] , MPI_Offset
const count[] ,
unsigned char const *data) {
167 ncmpiwrap( ncmpi_put_vara_uchar_all( ncid , varid , start ,
count , data ) , __LINE__ );
170 void pnetcdf_put_vara_all(
int ncid ,
int varid , MPI_Offset
const start[] , MPI_Offset
const count[] ,
short const *data) {
171 ncmpiwrap( ncmpi_put_vara_short_all( ncid , varid , start ,
count , data ) , __LINE__ );
174 void pnetcdf_put_vara_all(
int ncid ,
int varid , MPI_Offset
const start[] , MPI_Offset
const count[] ,
unsigned short const *data) {
175 ncmpiwrap( ncmpi_put_vara_ushort_all( ncid , varid , start ,
count , data ) , __LINE__ );
178 void pnetcdf_put_vara_all(
int ncid ,
int varid , MPI_Offset
const start[] , MPI_Offset
const count[] ,
int const *data) {
179 ncmpiwrap( ncmpi_put_vara_int_all( ncid , varid , start ,
count , data ) , __LINE__ );
182 void pnetcdf_put_vara_all(
int ncid ,
int varid , MPI_Offset
const start[] , MPI_Offset
const count[] ,
unsigned int const *data) {
183 ncmpiwrap( ncmpi_put_vara_uint_all( ncid , varid , start ,
count , data ) , __LINE__ );
186 void pnetcdf_put_vara_all(
int ncid ,
int varid , MPI_Offset
const start[] , MPI_Offset
const count[] ,
long long const *data) {
187 ncmpiwrap( ncmpi_put_vara_longlong_all( ncid , varid , start ,
count , data ) , __LINE__ );
190 void pnetcdf_put_vara_all(
int ncid ,
int varid , MPI_Offset
const start[] , MPI_Offset
const count[] ,
unsigned long long const *data) {
191 ncmpiwrap( ncmpi_put_vara_ulonglong_all( ncid , varid , start ,
count , data ) , __LINE__ );
194 void pnetcdf_put_vara_all(
int ncid ,
int varid , MPI_Offset
const start[] , MPI_Offset
const count[] ,
float const *data) {
195 ncmpiwrap( ncmpi_put_vara_float_all( ncid , varid , start ,
count , data ) , __LINE__ );
198 void pnetcdf_put_vara_all(
int ncid ,
int varid , MPI_Offset
const start[] , MPI_Offset
const count[] ,
double const *data) {
199 ncmpiwrap( ncmpi_put_vara_double_all( ncid , varid , start ,
count , data ) , __LINE__ );
235 ncmpiwrap( ncmpi_open( MPI_COMM_WORLD , fname.c_str() , NC_WRITE , MPI_INFO_NULL , &ncid ) , __LINE__ );
240 void create(std::string fname ,
int flag = -1 ) {
243 ncmpiwrap( ncmpi_create( MPI_COMM_WORLD , fname.c_str() , flag , MPI_INFO_NULL , &ncid ) , __LINE__ );
245 ncmpiwrap( ncmpi_create( MPI_COMM_WORLD , fname.c_str() , NC_CLOBBER , MPI_INFO_NULL , &ncid ) , __LINE__ );
253 ncmpiwrap( ncmpi_close(ncid) , __LINE__ );
262 ncmpiwrap( ncmpi_inq_dimid( ncid , dimName.c_str() , &dimid) , __LINE__ );
270 ncmpiwrap( ncmpi_inq_varid( ncid , varName.c_str() , &varid) , __LINE__ );
278 int ierr = ncmpi_inq_varid( ncid , varName.c_str() , &varid);
279 if (ierr == NC_NOERR) {
290 int ierr = ncmpi_inq_dimid( ncid , dimName.c_str() , &dimid);
291 if (ierr == NC_NOERR) {
303 ncmpiwrap( ncmpi_inq_dimid ( ncid , dimName.c_str() , &dimid) , __LINE__ );
304 ncmpiwrap( ncmpi_inq_dimlen( ncid , dimid , &dimlen ) , __LINE__ );
311 void create_var( std::string varName , std::vector<std::string> dnames ) {
312 int ndims = dnames.size();
313 std::vector<int> dimids(ndims);
314 for (
int i=0; i < ndims; i++) {
317 nc_type xtype = getType<T>();
319 ncmpiwrap( ncmpi_def_var( ncid , varName.c_str() , xtype , ndims , dimids.data() , &varid ) , __LINE__ );
326 ncmpiwrap( ncmpi_def_dim( ncid , dimName.c_str() , len , &dimid ) , __LINE__ );
333 ncmpiwrap( ncmpi_def_dim( ncid , dimName.c_str() , NC_UNLIMITED , &dimid ) , __LINE__ );
339 ncmpiwrap( ncmpi_enddef(ncid) , __LINE__ );
345 ncmpiwrap( ncmpi_begin_indep_data(ncid) , __LINE__ );
351 ncmpiwrap( ncmpi_end_indep_data(ncid) , __LINE__ );
356 template <
class T,
int rank,
int myMem,
int myStyle>
359 pnetcdf_put_var( ncid , varid , arr.data() );
364 template <
class T,
int rank,
int myMem,
int myStyle>
366 if (rank != start .
size()) {
yakl_throw(
"start.size() != Array's rank"); }
367 std::vector<MPI_Offset>
count(rank);
368 for (
int i=0; i < rank; i++) {
369 count[i] = arr.extent(i);
375 pnetcdf_put_vara_all( ncid , varid , start.data() ,
count.data() , arr.createHostCopy().data() );
377 pnetcdf_put_vara_all( ncid , varid , start.data() ,
count.data() , arr.data() );
383 template <class T, typename std::enable_if<std::is_arithmetic<T>::value,
int>::type = 0 >
384 void write1(T val , std::string varName ,
int ind , std::string ulDimName=
"unlim" ) {
387 std::vector<MPI_Offset> start(1);
388 std::vector<MPI_Offset>
count(1);
392 pnetcdf_put_vara( ncid , varid , start.data() ,
count.data() , &val );
397 template <
class T,
int rank,
int myMem,
int myStyle>
399 int ind , std::string ulDimName=
"unlim" ) {
400 std::vector<MPI_Offset> start(rank+1);
401 std::vector<MPI_Offset>
count(rank+1);
404 for (
int i=1; i < rank+1; i++) {
406 count[i] = arr.extent(i-1);
412 pnetcdf_put_vara( ncid , varid , start.data() ,
count.data() , arr.createHostCopy().data() );
414 pnetcdf_put_vara( ncid , varid , start.data() ,
count.data() , arr.data() );
420 template <
class T,
int rank,
int myMem,
int myStyle>
422 int ind , std::vector<MPI_Offset> start_in , std::string ulDimName=
"unlim" ) {
423 if (rank != start_in.size()) {
yakl_throw(
"start_in.size() != Array's rank"); }
424 std::vector<MPI_Offset> start(rank+1);
425 std::vector<MPI_Offset>
count(rank+1);
428 for (
int i=1; i < rank+1; i++) {
429 start[i] = start_in[i-1];
430 count[i] = arr.extent(i-1);
436 pnetcdf_put_vara_all( ncid , varid , start.data() ,
count.data() , arr.createHostCopy().data() );
438 pnetcdf_put_vara_all( ncid , varid , start.data() ,
count.data() , arr.data() );
509 template <
class T> nc_type getType()
const {
510 if ( std::is_same<T, char>::value ) {
return NC_CHAR; }
511 else if ( std::is_same<T,unsigned char>::value ) {
return NC_UBYTE; }
512 else if ( std::is_same<T, short>::value ) {
return NC_SHORT; }
513 else if ( std::is_same<T,unsigned short>::value ) {
return NC_USHORT; }
514 else if ( std::is_same<T, int>::value ) {
return NC_INT; }
515 else if ( std::is_same<T,unsigned int>::value ) {
return NC_UINT; }
516 else if ( std::is_same<T, long>::value ) {
return NC_INT64; }
517 else if ( std::is_same<T,unsigned long>::value ) {
return NC_UINT64; }
518 else if ( std::is_same<T, float>::value ) {
return NC_FLOAT; }
519 else if ( std::is_same<T, double>::value ) {
return NC_DOUBLE; }