<commit_before>//
// Created by salmon on 17-9-18.
//
#include "MPIUpdater.h"
#include <mpi.h>
#include "MPIComm.h"
#include "simpla/SIMPLA_config.h"
#include "simpla/utilities/macro.h"

namespace simpla {
namespace parallel {

struct MPIUpdater::pimpl_s {
    bool m_is_setup_ = false;
    MPI_Comm comm;
    int ndims = 3;
    int mpi_topology_ndims = 0;
    int mpi_dims[3], mpi_periods[3], mpi_coords[3];
    index_box_type local_box_;
    index_box_type send_box[6];
    index_box_type recv_box[6];
    index_tuple m_gw_{2, 2, 2};

    MPI_Datatype ele_type;
    int tag;
    void *send_buffer[6];
    void *recv_buffer[6];
    int send_size[6];
    int recv_size[6];
};
MPIUpdater::MPIUpdater() : m_pimpl_(new pimpl_s) {
    if (GLOBAL_COMM.comm() == MPI_COMM_NULL) { return; }

    m_pimpl_->comm = GLOBAL_COMM.comm();
    if (GLOBAL_COMM.comm() == MPI_COMM_NULL) { return; }
    int tope_type = MPI_CART;
    MPI_CALL(MPI_Topo_test(GLOBAL_COMM.comm(), &tope_type));
    if (tope_type != MPI_CART) { return; }
    int tag = 0;
    MPI_CALL(MPI_Cartdim_get(GLOBAL_COMM.comm(), &m_pimpl_->mpi_topology_ndims));
};
MPIUpdater::~MPIUpdater() { TearDown(); };
void MPIUpdater::SetGhostWidth(index_tuple const &gw) { m_pimpl_->m_gw_ = gw; }
index_tuple MPIUpdater::GetGhostWidth() const { return m_pimpl_->m_gw_; }
void MPIUpdater::SetIndexBox(index_box_type const &idx_box) { m_pimpl_->local_box_ = idx_box; }
index_box_type MPIUpdater::GetIndexBox() const { return m_pimpl_->local_box_; }

bool MPIUpdater::isSetUp() const { return m_pimpl_->m_is_setup_; }
bool MPIUpdater::isEnable() const { return m_pimpl_->comm != MPI_COMM_NULL && GLOBAL_COMM.size() > 1; }

void MPIUpdater::SetUp() {
    m_pimpl_->m_is_setup_ = true;
    int topo_type = MPI_CART;

    MPI_CALL(MPI_Topo_test(m_pimpl_->comm, &topo_type));

    ASSERT(topo_type == MPI_CART);

    MPI_CALL(MPI_Cartdim_get(m_pimpl_->comm, &m_pimpl_->mpi_topology_ndims));

    ASSERT(m_pimpl_->mpi_topology_ndims <= m_pimpl_->ndims);

    MPI_CALL(MPI_Cart_get(m_pimpl_->comm, 3, m_pimpl_->mpi_dims, m_pimpl_->mpi_periods, m_pimpl_->mpi_coords));
    for (int i = 0; i < 6; ++i) {
        m_pimpl_->send_box[i] = m_pimpl_->local_box_;
        m_pimpl_->recv_box[i] = m_pimpl_->local_box_;
    }

    for (int d = 0; d < m_pimpl_->mpi_topology_ndims; ++d) {
        if (m_pimpl_->mpi_dims[d] == 1) {
            std::get<0>(m_pimpl_->send_box[2 * d + 0]) = 0;
            std::get<0>(m_pimpl_->send_box[2 * d + 1]) = 0;
            std::get<0>(m_pimpl_->recv_box[2 * d + 0]) = 0;
            std::get<0>(m_pimpl_->recv_box[2 * d + 1]) = 0;
            std::get<1>(m_pimpl_->send_box[2 * d + 0]) = 0;
            std::get<1>(m_pimpl_->send_box[2 * d + 1]) = 0;
            std::get<1>(m_pimpl_->recv_box[2 * d + 0]) = 0;
            std::get<1>(m_pimpl_->recv_box[2 * d + 1]) = 0;
            continue;
        }

        std::get<0>(m_pimpl_->send_box[2 * d + 0])[d] = std::get<0>(m_pimpl_->local_box_)[d];
        std::get<1>(m_pimpl_->send_box[2 * d + 0])[d] = std::get<0>(m_pimpl_->local_box_)[d] + m_pimpl_->m_gw_[d];

        std::get<0>(m_pimpl_->recv_box[2 * d + 0])[d] = std::get<0>(m_pimpl_->local_box_)[d] - m_pimpl_->m_gw_[d];
        std::get<1>(m_pimpl_->recv_box[2 * d + 0])[d] = std::get<0>(m_pimpl_->local_box_)[d];

        std::get<0>(m_pimpl_->send_box[2 * d + 1])[d] = std::get<1>(m_pimpl_->local_box_)[d] - m_pimpl_->m_gw_[d];
        std::get<1>(m_pimpl_->send_box[2 * d + 1])[d] = std::get<1>(m_pimpl_->local_box_)[d];

        std::get<0>(m_pimpl_->recv_box[2 * d + 1])[d] = std::get<1>(m_pimpl_->local_box_)[d];
        std::get<1>(m_pimpl_->recv_box[2 * d + 1])[d] = std::get<1>(m_pimpl_->local_box_)[d] + m_pimpl_->m_gw_[d];

        //        for (int i = 0; i < d; ++i) {
        //            std::get<0>(m_pimpl_->send_box[2 * d + 0])[i] = std::get<0>(m_pimpl_->local_box_)[i] -
        //            m_pimpl_->m_gw_[i];
        //            std::get<1>(m_pimpl_->send_box[2 * d + 0])[i] = std::get<0>(m_pimpl_->local_box_)[i] +
        //            m_pimpl_->m_gw_[i];
        //
        //            std::get<0>(m_pimpl_->recv_box[2 * d + 0])[i] = std::get<0>(m_pimpl_->local_box_)[i] -
        //            m_pimpl_->m_gw_[i];
        //            std::get<1>(m_pimpl_->recv_box[2 * d + 0])[i] = std::get<0>(m_pimpl_->local_box_)[i] +
        //            m_pimpl_->m_gw_[i];
        //
        //            std::get<0>(m_pimpl_->send_box[2 * d + 1])[i] = std::get<1>(m_pimpl_->local_box_)[i] -
        //            m_pimpl_->m_gw_[i];
        //            std::get<1>(m_pimpl_->send_box[2 * d + 1])[i] = std::get<1>(m_pimpl_->local_box_)[i] +
        //            m_pimpl_->m_gw_[i];
        //
        //            std::get<0>(m_pimpl_->recv_box[2 * d + 1])[i] = std::get<1>(m_pimpl_->local_box_)[i] -
        //            m_pimpl_->m_gw_[i];
        //            std::get<1>(m_pimpl_->recv_box[2 * d + 1])[i] = std::get<1>(m_pimpl_->local_box_)[i] +
        //            m_pimpl_->m_gw_[i];
        //        }
    }

    size_type ele_size = 0;
    if (value_type_info() == typeid(int)) {
        ele_size = sizeof(int);
        m_pimpl_->ele_type = MPI_INT;
    } else if (value_type_info() == typeid(double)) {
        ele_size = sizeof(double);
        m_pimpl_->ele_type = MPI_DOUBLE;
    } else if (value_type_info() == typeid(float)) {
        ele_size = sizeof(float);
        m_pimpl_->ele_type = MPI_FLOAT;<commit_msg>MPIUpdater<commit_after>//
// Created by salmon on 17-9-18.
//
#include "MPIUpdater.h"
#include <mpi.h>
#include "MPIComm.h"
#include "simpla/SIMPLA_config.h"
#include "simpla/utilities/macro.h"

namespace simpla {
namespace parallel {

struct MPIUpdater::pimpl_s {
    bool m_is_setup_ = false;
    MPI_Comm comm;
    int ndims = 3;
    int mpi_topology_ndims = 0;
    int mpi_dims[3], mpi_periods[3], mpi_coords[3];
    index_box_type local_box_;
    index_box_type send_box[6];
    index_box_type recv_box[6];
    index_tuple m_gw_{2, 2, 2};

    MPI_Datatype ele_type;
    int tag;
    void *send_buffer[6];
    void *recv_buffer[6];
    int send_size[6];
    int recv_size[6];
};
MPIUpdater::MPIUpdater() : m_pimpl_(new pimpl_s) {
    if (GLOBAL_COMM.comm() == MPI_COMM_NULL) { return; }

    m_pimpl_->comm = GLOBAL_COMM.comm();
    if (GLOBAL_COMM.comm() == MPI_COMM_NULL) { return; }
    int tope_type = MPI_CART;
    MPI_CALL(MPI_Topo_test(GLOBAL_COMM.comm(), &tope_type));
    if (tope_type != MPI_CART) { return; }
    int tag = 0;
    MPI_CALL(MPI_Cartdim_get(GLOBAL_COMM.comm(), &m_pimpl_->mpi_topology_ndims));
};
MPIUpdater::~MPIUpdater() { TearDown(); };
void MPIUpdater::SetGhostWidth(index_tuple const &gw) { m_pimpl_->m_gw_ = gw; }
index_tuple MPIUpdater::GetGhostWidth() const { return m_pimpl_->m_gw_; }
void MPIUpdater::SetIndexBox(index_box_type const &idx_box) { m_pimpl_->local_box_ = idx_box; }
index_box_type MPIUpdater::GetIndexBox() const { return m_pimpl_->local_box_; }

bool MPIUpdater::isSetUp() const { return m_pimpl_->m_is_setup_; }
bool MPIUpdater::isEnable() const { return m_pimpl_->comm != MPI_COMM_NULL && GLOBAL_COMM.size() > 1; }

void MPIUpdater::SetUp() {
    m_pimpl_->m_is_setup_ = true;
    int topo_type = MPI_CART;

    MPI_CALL(MPI_Topo_test(m_pimpl_->comm, &topo_type));

    ASSERT(topo_type == MPI_CART);

    MPI_CALL(MPI_Cartdim_get(m_pimpl_->comm, &m_pimpl_->mpi_topology_ndims));

    ASSERT(m_pimpl_->mpi_topology_ndims <= m_pimpl_->ndims);

    MPI_CALL(MPI_Cart_get(m_pimpl_->comm, 3, m_pimpl_->mpi_dims, m_pimpl_->mpi_periods, m_pimpl_->mpi_coords));
    for (int i = 0; i < 6; ++i) {
        m_pimpl_->send_box[i] = m_pimpl_->local_box_;
        m_pimpl_->recv_box[i] = m_pimpl_->local_box_;
    }

    for (int d = 0; d < m_pimpl_->mpi_topology_ndims; ++d) {
        if (m_pimpl_->mpi_dims[d] == 1) {
            std::get<0>(m_pimpl_->send_box[2 * d + 0]) = 0;
            std::get<0>(m_pimpl_->send_box[2 * d + 1]) = 0;
            std::get<0>(m_pimpl_->recv_box[2 * d + 0]) = 0;
            std::get<0>(m_pimpl_->recv_box[2 * d + 1]) = 0;
            std::get<1>(m_pimpl_->send_box[2 * d + 0]) = 0;
            std::get<1>(m_pimpl_->send_box[2 * d + 1]) = 0;
            std::get<1>(m_pimpl_->recv_box[2 * d + 0]) = 0;
            std::get<1>(m_pimpl_->recv_box[2 * d + 1]) = 0;
            continue;
        }

        std::get<0>(m_pimpl_->send_box[2 * d + 0])[d] = std::get<0>(m_pimpl_->local_box_)[d];
        std::get<1>(m_pimpl_->send_box[2 * d + 0])[d] = std::get<0>(m_pimpl_->local_box_)[d] + m_pimpl_->m_gw_[d];

        std::get<0>(m_pimpl_->recv_box[2 * d + 0])[d] = std::get<0>(m_pimpl_->local_box_)[d] - m_pimpl_->m_gw_[d];
        std::get<1>(m_pimpl_->recv_box[2 * d + 0])[d] = std::get<0>(m_pimpl_->local_box_)[d];

        std::get<0>(m_pimpl_->send_box[2 * d + 1])[d] = std::get<1>(m_pimpl_->local_box_)[d] - m_pimpl_->m_gw_[d];
        std::get<1>(m_pimpl_->send_box[2 * d + 1])[d] = std::get<1>(m_pimpl_->local_box_)[d];

        std::get<0>(m_pimpl_->recv_box[2 * d + 1])[d] = std::get<1>(m_pimpl_->local_box_)[d];
        std::get<1>(m_pimpl_->recv_box[2 * d + 1])[d] = std::get<1>(m_pimpl_->local_box_)[d] + m_pimpl_->m_gw_[d];

        for (int i = 0; i < d; ++i) {
            std::get<0>(m_pimpl_->send_box[2 * d + 0])[i] = std::get<0>(m_pimpl_->local_box_)[i] - m_pimpl_->m_gw_[i];
            std::get<1>(m_pimpl_->send_box[2 * d + 0])[i] = std::get<1>(m_pimpl_->local_box_)[i] + m_pimpl_->m_gw_[i];

            std::get<0>(m_pimpl_->recv_box[2 * d + 0])[i] = std::get<0>(m_pimpl_->local_box_)[i] - m_pimpl_->m_gw_[i];
            std::get<1>(m_pimpl_->recv_box[2 * d + 0])[i] = std::get<1>(m_pimpl_->local_box_)[i] + m_pimpl_->m_gw_[i];

            std::get<0>(m_pimpl_->send_box[2 * d + 1])[i] = std::get<0>(m_pimpl_->local_box_)[i] - m_pimpl_->m_gw_[i];
            std::get<1>(m_pimpl_->send_box[2 * d + 1])[i] = std::get<1>(m_pimpl_->local_box_)[i] + m_pimpl_->m_gw_[i];

            std::get<0>(m_pimpl_->recv_box[2 * d + 1])[i] = std::get<0>(m_pimpl_->local_box_)[i] - m_pimpl_->m_gw_[i];
            std::get<1>(m_pimpl_->recv_box[2 * d + 1])[i] = std::get<1>(m_pimpl_->local_box_)[i] + m_pimpl_->m_gw_[i];
        }
    }

    size_type ele_size = 0;
    if (value_type_info() == typeid(int)) {
        ele_size = sizeof(int);
        m_pimpl_->ele_type = MPI_INT;
    } else if (value_type_info() == typeid(double)) {
        ele_size = sizeof(double);
        m_pimpl_->ele_type = MPI_DOUBLE;
    } else if (value_type_info() == typeid(float)) {
        ele_size = sizeof(float);
        m_pimpl_->ele_type = MPI_FLOAT;
{'fraction_non_alphanumeric': 0.19278477416911277, 'fraction_numerical': 0.02348262475144399, 'mean_word_length': 3.1664694280078893, 'pattern_counts': {'":': 0, '<': 93, '<?xml version=': 0, '>': 245, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5697024', 'n_tokens_mistral': 4722, 'n_tokens_neox': 4582, 'n_words': 611}