Skip to content

Commit

Permalink
Set up clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
jwallwork23 committed Aug 22, 2024
1 parent c7836fa commit 41d22df
Show file tree
Hide file tree
Showing 16 changed files with 255 additions and 284 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ on:
pull_request:
branches: [main]
jobs:
clang-formatter:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v2
- name: clang-format
run: |
sudo apt update
sudo apt install clang-format
clang-format --dry-run -Werror *cpp *hpp
for component in examples test
do
clang-format --dry-run -Werror *cpp *hpp
done
build-on-ubuntu:
runs-on: ubuntu-latest
steps:
Expand Down
53 changes: 23 additions & 30 deletions Grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
#include <netcdf.h>
#include <netcdf_par.h>

static void find_factors(int n, int& factor_a, int& factor_b)
{
static void find_factors(int n, int &factor_a, int &factor_b) {
for (int i = 2; i * i <= n; i += 2) {
if (n % i == 0) {
factor_a = i;
Expand All @@ -24,41 +23,37 @@ static void find_factors(int n, int& factor_a, int& factor_b)
}
}

Grid* Grid::create(MPI_Comm comm, const std::string& filename, bool ignore_mask)
{
Grid *Grid::create(MPI_Comm comm, const std::string &filename,
bool ignore_mask) {
return new Grid(comm, filename, "x", "y", "mask", 1, 1, ignore_mask);
}

Grid* Grid::create(MPI_Comm comm, const std::string& filename, int blk_dim0,
int blk_dim1, bool ignore_mask)
{
Grid *Grid::create(MPI_Comm comm, const std::string &filename, int blk_dim0,
int blk_dim1, bool ignore_mask) {
return new Grid(comm, filename, "x", "y", "mask", blk_dim0, blk_dim1,
ignore_mask);
}

Grid* Grid::create(MPI_Comm comm, const std::string& filename,
Grid *Grid::create(MPI_Comm comm, const std::string &filename,
const std::string dim0_name, const std::string dim1_name,
const std::string mask_name, bool ignore_mask)
{
const std::string mask_name, bool ignore_mask) {
return new Grid(comm, filename, dim0_name, dim1_name, mask_name, 1, 1,
ignore_mask);
}

Grid* Grid::create(MPI_Comm comm, const std::string& filename,
Grid *Grid::create(MPI_Comm comm, const std::string &filename,
const std::string dim0_name, const std::string dim1_name,
const std::string mask_name, int blk_dim0, int blk_dim1,
bool ignore_mask)
{
bool ignore_mask) {
return new Grid(comm, filename, dim0_name, dim1_name, mask_name, blk_dim0,
blk_dim1, ignore_mask);
}

Grid::Grid(MPI_Comm comm, const std::string& filename,
const std::string& dim0_name, const std::string& dim1_name,
const std::string& mask_name, int blk_dim0, int blk_dim1,
Grid::Grid(MPI_Comm comm, const std::string &filename,
const std::string &dim0_name, const std::string &dim1_name,
const std::string &mask_name, int blk_dim0, int blk_dim1,
bool ignore_mask)
: _comm(comm), _blk_factor_0(blk_dim0), _blk_factor_1(blk_dim1)
{
: _comm(comm), _blk_factor_0(blk_dim0), _blk_factor_1(blk_dim1) {
// Use C API for parallel I/O
int nc_id, nc_o_mode;
nc_o_mode = NC_NOWRITE;
Expand Down Expand Up @@ -105,12 +100,12 @@ Grid::Grid(MPI_Comm comm, const std::string& filename,
_global_blk_0 = (_rank / _num_procs_1) * _local_ext_blk_0;
_global_blk_1 = (_rank % _num_procs_1) * _local_ext_blk_1;
if ((_rank / _num_procs_1) == _num_procs_0 - 1) {
_local_ext_blk_0
= _global_ext_blk_0 - (_rank / _num_procs_1) * _local_ext_blk_0;
_local_ext_blk_0 =
_global_ext_blk_0 - (_rank / _num_procs_1) * _local_ext_blk_0;
}
if ((_rank % _num_procs_1) == _num_procs_1 - 1) {
_local_ext_blk_1
= _global_ext_blk_1 - (_rank % _num_procs_1) * _local_ext_blk_1;
_local_ext_blk_1 =
_global_ext_blk_1 - (_rank % _num_procs_1) * _local_ext_blk_1;
}

_local_ext_0 = _local_ext_blk_0 * _blk_factor_0;
Expand Down Expand Up @@ -171,7 +166,7 @@ Grid::Grid(MPI_Comm comm, const std::string& filename,
int index = 0;
for (size_t j = 0; j < count[1]; j++) {
for (size_t i = 0; i < count[0]; i++) {
_land_mask[index] = _land_mask_copy[i*count[1]+j];
_land_mask[index] = _land_mask_copy[i * count[1] + j];
index++;
}
}
Expand Down Expand Up @@ -255,22 +250,20 @@ int Grid::get_num_procs_0() const { return _num_procs_0; }

int Grid::get_num_procs_1() const { return _num_procs_1; }

const int* Grid::get_land_mask() const
{
const int *Grid::get_land_mask() const {
if (_blk_factor_0 == 1 && _blk_factor_1 == 1) {
return _land_mask.data();
} else {
return _land_mask_blk.data();
}
}

const int* Grid::get_sparse_to_dense() const { return _sparse_to_dense.data(); }
const int *Grid::get_sparse_to_dense() const { return _sparse_to_dense.data(); }

const int* Grid::get_nonzero_object_ids() const { return _object_id.data(); }
const int *Grid::get_nonzero_object_ids() const { return _object_id.data(); }

void Grid::get_bounding_box(int& global_0, int& global_1, int& local_ext_0,
int& local_ext_1) const
{
void Grid::get_bounding_box(int &global_0, int &global_1, int &local_ext_0,
int &local_ext_1) const {
global_0 = _global_blk_0;
global_1 = _global_blk_1;
local_ext_0 = _local_ext_blk_0;
Expand Down
35 changes: 17 additions & 18 deletions Grid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@
* global coordinates of the upper left corner and the local extents of the box
* in each dimension.
*/
class LIB_EXPORT Grid
{
class LIB_EXPORT Grid {
// Default grid metadata naming conventions
const std::string data_id = "data";

public:
// Disallow compiler-generated special functions
Grid(const Grid&) = delete;
Grid& operator=(const Grid&) = delete;
Grid(const Grid &) = delete;
Grid &operator=(const Grid &) = delete;

/*!
* @brief Destructor.
Expand Down Expand Up @@ -78,14 +77,14 @@ class LIB_EXPORT Grid
*/
// We are using the named constructor idiom so that objects can only be
// created in the heap to ensure it's dtor is executed before MPI_Finalize()
static Grid* create(MPI_Comm comm, const std::string& filename,
static Grid *create(MPI_Comm comm, const std::string &filename,
bool ignore_mask = false);
static Grid* create(MPI_Comm comm, const std::string& filename, int blk_dim0,
static Grid *create(MPI_Comm comm, const std::string &filename, int blk_dim0,
int blk_dim1, bool ignore_mask = false);
static Grid* create(MPI_Comm comm, const std::string& filename,
static Grid *create(MPI_Comm comm, const std::string &filename,
const std::string dim0_name, const std::string dim1_name,
const std::string mask_name, bool ignore_mask = false);
static Grid* create(MPI_Comm comm, const std::string& filename,
static Grid *create(MPI_Comm comm, const std::string &filename,
const std::string dim0_name, const std::string dim1_name,
const std::string mask_name, int blk_dim0, int blk_dim1,
bool ignore_mask = false);
Expand Down Expand Up @@ -174,7 +173,7 @@ class LIB_EXPORT Grid
*
* @return Global land mask.
*/
const int* get_land_mask() const;
const int *get_land_mask() const;

/*!
* @brief Returns the index mapping of sparse to dense representation, where
Expand All @@ -184,7 +183,7 @@ class LIB_EXPORT Grid
*
* @return Index mapping of sparse to dense representation.
*/
const int* get_sparse_to_dense() const;
const int *get_sparse_to_dense() const;

/*!
* @brief Returns the IDs of the non-land objects in the local domain, where
Expand All @@ -194,7 +193,7 @@ class LIB_EXPORT Grid
*
* @return IDs of the non-land objects in the local domain.
*/
const int* get_nonzero_object_ids() const;
const int *get_nonzero_object_ids() const;

/*!
* @brief Returns the bounding box for this process. If blocking is used, then
Expand All @@ -207,14 +206,14 @@ class LIB_EXPORT Grid
* @param local_ext_0 Local extent in the 1st dimension of the grid.
* @param local_ext_1 Local extent in the 2nd dimension of the grid.
*/
void get_bounding_box(int& global_0, int& global_1, int& local_ext_0,
int& local_ext_1) const;
void get_bounding_box(int &global_0, int &global_1, int &local_ext_0,
int &local_ext_1) const;

private:
// Construct a ditributed grid from a NetCDF file describing the global domain
Grid(MPI_Comm comm, const std::string& filename,
const std::string& dim0_id = "x", const std::string& dim1_id = "y",
const std::string& mask_id = "mask", int blk0 = 1, int blk1 = 1,
Grid(MPI_Comm comm, const std::string &filename,
const std::string &dim0_id = "x", const std::string &dim1_id = "y",
const std::string &mask_id = "mask", int blk0 = 1, int blk1 = 1,
bool ignore_mask = false);

private:
Expand Down Expand Up @@ -245,8 +244,8 @@ class LIB_EXPORT Grid
int _num_nonzero_blks = 0; // Number of non-land grid blocks
std::vector<int> _land_mask = {}; // Land mask values of grid bGpoints
std::vector<int> _land_mask_blk = {}; // Land mask values of grid blocks
std::vector<int> _sparse_to_dense
= {}; // Map from sparse to dense index of blocks
std::vector<int> _sparse_to_dense =
{}; // Map from sparse to dense index of blocks
std::vector<int> _object_id = {}; // Unique non-land grid block IDs
};

Expand Down
Loading

0 comments on commit 41d22df

Please sign in to comment.