Skip to content
Snippets Groups Projects

added the cpp version of mo_lib_laplace and compiled the code

Open Ali Sedighi requested to merge feature_add_cpp_codes_laplace_A into feature-add-cpp-codes
1 file
+ 171
0
Compare changes
  • Side-by-side
  • Inline
+ 171
0
// ICON
//
// ---------------------------------------------------------------
// Copyright (C) 2004-2024, DWD, MPI-M, DKRZ, KIT, ETH, MeteoSwiss
// Contact information: icon-model.org
//
// See AUTHORS.TXT for a list of authors
// See LICENSES/ for license information
// SPDX-License-Identifier: BSD-3-Clause
// ---------------------------------------------------------------
#include <gtest/gtest.h>
#include <Kokkos_Core.hpp>
#include <vector>
#include "mo_lib_laplace.cpp"
// Free-function helpers for 3D and 4D array sizes (assumed column-major)
template<typename T>
size_t num_elements_3d(int d1, int d2, int d3) {
return static_cast<size_t>(d1) * d2 * d3;
}
template<typename T>
size_t num_elements_4d(int d1, int d2, int d3, int d4) {
return static_cast<size_t>(d1) * d2 * d3 * d4;
}
// Define a helper struct that holds the two types.
template<typename InT, typename OutT>
struct MixedPrecision {
using in_type = InT;
using out_type = OutT;
};
// Define the list of type pairs we want to test.
typedef ::testing::Types< MixedPrecision<double, double>,
MixedPrecision<double, float>,
MixedPrecision<float, float> > MixedTypes;
class Horizonral_dimension
{
public:
static constexpr int nproma = 3; // inner loop length
static constexpr int nlev = 4; // number of vertical levels
static constexpr int nblks_e = 2; // number of edge blocks (for p_e_in)
static constexpr int nblks_v = 2; // number of vertex blocks (for rbf arrays and outputs)
static constexpr int rbf_vec_dim = 6; // fixed dimension for rbf vector (stencil points)
static constexpr int nblks_c = 2;
// Parameter values.
int i_startblk = 0;
int i_endblk = 1; // Test blocks [0, 1]
int i_startidx_in = 0;
int i_endidx_in = nproma - 1; // Full range: 0 .. nproma-1
int i_startblk_e = 0;
int i_endblk_e = 0;
int slev = 0;
int i_startidx_e = 0;
int i_endidx_e = nproma - 1;
int nlev = 0;
int slev = 0;
int elev = nlev - 1; // Full vertical range (0 .. nlev-1)
int cell_type = 0;
bool lacc = false; // Not using ACC-specific behavior.
};
// Define a typed test fixture.
template <typename TypePair>
class MoLibLaplaceMixedTestFixture : public ::testing::Test {
public:
//Arrays for nabla2_scalar_lib
std::vector<T> psi_c; //Dimension (nproma,nlev,nblks_c)
std::vector<int> cell_neighbor_idx; //Dimenaion (nproma,nblks_c,3)
std::vector<int> cell_neighbor_blk; //Dimension (nproma,nblks_c,3)
std::vector<int> edge_cell_idx; //Dimension (nproma,nblks_e,2)
std::vector<int> edge_cell_blk; //Dimension (nproma,nblks_e,2)
std::vector<T> inv_dual_edge_length; //Dimension (nproma,nblks_e)
std::vector<int> cell_edge_idx; //Dimension (nproma,nblks_c,3)
std::vector<int> cell_edge_blk; //Dimension (nproma,nblks_c,3)
std::vector<T> geofac_n2s; // Dimension (nproma,cell_type+1,nblks_c)
std::vector<T> geofac_div; //Dimension (nproma,cell_type,nblks_c)
std::vector<T> nabla2_psi_c; //Dimension (nproma,nlev,nblks_c)
MoLibLaplaceMixedTestFixture() {
//Arrays for nabla2_scalar_lib
psi_c.resize(num_elements_3d<T>(nproma,nlev,nblks_c), static_cast<T>(1));
inv_dual_edge_length.resize(num_elements_2d<T>(nproma,nblks_e), static_cast<T>(1));
geofac_n2s.resize(num_elements_2d<T> (nproma,cell_type+1,nblks_c), static_cast<T>(1));
geofac_div.resize(num_elements_2d<T>(nproma,cell_type,nblks_c), static_cast<T>(1));
nabla2_psi_c.resize(num_elements_2d<T>(nproma,nlev,nblks_c), static_cast<T>(1));
cell_neighbor_idx.resize(num_elements_3d<int>(nproma,nblks_c,3));
cell_neighbor_blk.resize(num_elements_3d<int>(nproma,nblks_e,3));
edge_cell_idx.resize(num_elements_3d<int> (nproma,nblks_e,2));
edge_cell_blk.resize(num_elements_3d<int>(nproma,nlev,nblks_c));
cell_edge_idx.resize(num_elements_2d<int>(nproma,nblks_c,3));
cell_edge_blk.resize(num_elements_2d<int>(nproma,nblks_c,3));
geofac_n2s.resize(num_elements_2d<T>(nproma,cell_type+1,nblks_c), static_cast<T>(1));
geofac_div.resize(num_elements_2d<T>(nproma,cell_type,nblks_c), static_cast<T>(1));
nabla2_psi_c.resize(num_elements_2d<T>(nproma,nlev,nblks_c), static_cast<T>(0));
// Allocate and initialize inputs.
psi_c.resize(num_elements_3d<InType>(nproma, nlev, nblks_e), static_cast<InType>(1));
cell_neighbor_idx.resize(num_elements_3d<int>(rbf_vec_dim, nproma, nblks_v), 1);
cell_neighbor_blk.resize(num_elements_3d<int>(rbf_vec_dim, nproma, nblks_v), 0);
geofac_n2s.resize(num_elements_3d<InType>(rbf_vec_dim, 2, nproma, nblks_v), static_cast<InType>(1));
// Allocate output arrays and initialize to zero.
p_u_out.resize(num_elements_3d<OutType>(nproma, nlev, nblks_v), static_cast<OutType>(0));
p_v_out.resize(num_elements_3d<OutType>(nproma, nlev, nblks_v), static_cast<OutType>(0));
}
};
typedef ::testing::Types<float, double> SingleType;
TYPED_TEST_SUITE(MoLibLaplaceMixedTestFixture, nabla2_scalar_lib);
TYPED_TEST(MoLibLaplaceMixedTestFixture, nabla2_scalar_lib) {
using InType = typename TestFixture::InType;
using OutType = typename TestFixture::OutType;
nabla2_scalar_lib<InType, OutType>(
this->psi_c.data(),
this->cell_neighbor_idx.data(),
this->cell_neighbor_blk.data(),
this->edge_cell_idx.data(),
this->edge_cell_blk.data(),
this->inv_dual_edge_length.data(),
this->cell_edge_idx.data(),
this->cell_edge_blk.data(),
this->geofac_n2s.data(),
this->geofac_div.data(),
this->nabla2_psi_c.data(),
this->i_startblk,
this->i_endblk,
this->i_startidx_in,
this->i_endidx_in,
this->i_startblk_e,
this->i_endblk_e,
this->i_startidx_e,
this->i_endidx_e,
this->nlev,
this->slev,
this->elev,
this->nproma,
this->nblks_e,
this->nblks_c,
this->cell_type,
this->lacc)
for (int block = this->i_startblk; block <= this->i_endblk; ++block) {
for (int level = this->slev ; level < this->elev; ++level) {
for (int i = this->i_startidx ; i < this->i_endidx; ++i) {
size_t idx = i + level * this->nproma + block * this->nproma * this->nlev;
EXPECT_NEAR(this->nabla2_psi_c[idx], static_cast<OutType>(6), static_cast<OutType>(1e-5))
<< "Failure at block " << block << ", level " << level << ", index " << i;
}
}
}
}
Loading