PISM, A Parallel Ice Sheet Model 2.3.0-79cae578d committed by Constantine Khrulev on 2026-03-22
Loading...
Searching...
No Matches
NC4_Par.cc
Go to the documentation of this file.
1// Copyright (C) 2012--2026 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.
27extern "C" {
28#include <netcdf.h>
29#include <netcdf_meta.h>
30
31#if (NC_HAS_PARALLEL4 != 1)
32#error "Selected NetCDF library does not support parallel I/O"
33#endif
34
35#include <netcdf_par.h>
36}
37
38namespace pism {
39namespace io {
40
41//! \brief Prints an error message; for debugging.
42static void check(const ErrorLocation &where, int return_code) {
43 if (return_code != NC_NOERR) {
44 throw RuntimeError(where, nc_strerror(return_code));
45 }
46}
47
48void NC4_Par::open_impl(const std::string &fname, io::Mode mode) {
49 MPI_Info info = MPI_INFO_NULL;
50 int stat;
51
52 int open_mode = mode == io::PISM_READONLY ? NC_NOWRITE : NC_WRITE;
53 open_mode = open_mode | NC_MPIIO;
54
55 stat = nc_open_par(fname.c_str(), open_mode, m_com, info, &m_file_id);
56
58}
59
60void NC4_Par::create_impl(const std::string &fname) {
61 MPI_Info info = MPI_INFO_NULL;
62 int stat;
63
64 stat = nc_create_par(fname.c_str(),
65 NC_NETCDF4 | NC_MPIIO,
66 m_com, info, &m_file_id);
67
69}
70
71void NC4_Par::set_access_mode(int varid) const {
72 int stat;
73
74 // Use collective parallel access mode because it is faster.
75 stat = nc_var_par_access(m_file_id, varid, NC_COLLECTIVE);
77}
78
80 m_compression_level = level;
81}
82
83
84} // end of namespace io
85} // end of namespace pism
unsigned int m_compression_level
Definition NC4File.hh:110
virtual void set_compression_level_impl(int level) const
Definition NC4_Par.cc:79
virtual void open_impl(const std::string &filename, io::Mode mode)
Definition NC4_Par.cc:48
virtual void set_access_mode(int varid) const
Definition NC4_Par.cc:71
virtual void create_impl(const std::string &filename)
Definition NC4_Par.cc:60
MPI_Comm m_com
Definition NCFile.hh:231
#define PISM_ERROR_LOCATION
@ PISM_READONLY
open an existing file for reading only
Definition IO_Flags.hh:69
static void check(const ErrorLocation &where, int return_code)
Prints an error message; for debugging.
Definition NC4_Par.cc:42