PISM, A Parallel Ice Sheet Model  stable v2.1-1-g6902d5502 committed by Ed Bueler on 2023-12-20 08:38:27 -0800
NC4_Par.cc
Go to the documentation of this file.
1 // Copyright (C) 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2020, 2023 PISM Authors
2 //
3 // This file is part of PISM.
4 //
5 // PISM is free software; you can redistribute it and/or modify it under the
6 // terms of the GNU General Public License as published by the Free Software
7 // Foundation; either version 3 of the License, or (at your option) any later
8 // version.
9 //
10 // PISM is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with PISM; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 #include "pism/util/io/NC4_Par.hh"
20 #include "pism/util/error_handling.hh"
21 #include "pism/util/io/IO_Flags.hh"
22 
23 // netcdf_par.h has to be included *after* mpi.h and after netcdf.h
24 //
25 // note that we don't need to define MPI_INCLUDED because this code is built *only* if we
26 // have a parallel NetCDF library.
27 extern "C" {
28 #include <netcdf.h>
29 #include <netcdf_par.h>
30 }
31 
32 namespace pism {
33 namespace io {
34 
35 //! \brief Prints an error message; for debugging.
36 static void check(const ErrorLocation &where, int return_code) {
37  if (return_code != NC_NOERR) {
38  throw RuntimeError(where, nc_strerror(return_code));
39  }
40 }
41 
42 void NC4_Par::open_impl(const std::string &fname, io::Mode mode) {
43  MPI_Info info = MPI_INFO_NULL;
44  int stat;
45 
46  int open_mode = mode == io::PISM_READONLY ? NC_NOWRITE : NC_WRITE;
47  open_mode = open_mode | NC_MPIIO;
48 
49  stat = nc_open_par(fname.c_str(), open_mode, m_com, info, &m_file_id);
50 
52 }
53 
54 void NC4_Par::create_impl(const std::string &fname) {
55  MPI_Info info = MPI_INFO_NULL;
56  int stat;
57 
58  stat = nc_create_par(fname.c_str(),
59  NC_NETCDF4 | NC_MPIIO,
60  m_com, info, &m_file_id);
61 
63 }
64 
65 void NC4_Par::set_access_mode(int varid, bool transposed) const {
66  int stat;
67 
68  if (transposed) {
69  // Use independent parallel access mode because it works. It would be
70  // better to use collective mode, but I/O performance is ruined by
71  // the transpose anyway.
72  //
73  // See https://bugtracking.unidata.ucar.edu/browse/NCF-152 for the description of the bug we're
74  // avoiding here.
75  stat = nc_var_par_access(m_file_id, varid, NC_INDEPENDENT); check(PISM_ERROR_LOCATION, stat);
76  } else {
77  // Use collective parallel access mode because it is faster (and because it
78  // works in this case).
79  stat = nc_var_par_access(m_file_id, varid, NC_COLLECTIVE); check(PISM_ERROR_LOCATION, stat);
80  }
81 }
82 
83 void NC4_Par::set_compression_level_impl(int level) const {
84  m_compression_level = level;
85 }
86 
87 
88 } // end of namespace io
89 } // end of namespace pism
unsigned int m_compression_level
Definition: NC4File.hh:113
virtual void set_compression_level_impl(int level) const
Definition: NC4_Par.cc:83
virtual void open_impl(const std::string &filename, io::Mode mode)
Definition: NC4_Par.cc:42
virtual void set_access_mode(int varid, bool mapped) const
Definition: NC4_Par.cc:65
virtual void create_impl(const std::string &filename)
Definition: NC4_Par.cc:54
MPI_Comm m_com
Definition: NCFile.hh:232
#define PISM_ERROR_LOCATION
@ PISM_READONLY
open an existing file for reading only
Definition: IO_Flags.hh:72
static void check(const ErrorLocation &where, int return_code)
Prints an error message; for debugging.
Definition: NC4_Par.cc:36