dvcompute_mpi 3.0.0

Discrete event simulation library (the module of integration with MPI for distributed simulation)
Documentation
// Copyright (c) 2020-2022  David Sorokin <davsor@mail.ru>, based in Yoshkar-Ola, Russia
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use std::convert::TryInto;

use dvcompute_network::network::*;

use libc::*;

#[cfg_attr(windows, link(name = "dvcompute_mpi.dll"))]
#[cfg_attr(not(windows), link(name = "dvcompute_mpi"))]
extern {

    /// Initialize the MPI support.
    #[doc(hidden)]
    pub fn init_extern_mpi_network_support();

    /// Finalize the MPI support.
    #[doc(hidden)]
    pub fn finalize_extern_mpi_network_support();

    /// Get the MPI support.
    #[doc(hidden)]
    pub fn create_extern_mpi_network_support(create_buf: unsafe extern "C" fn(size: c_ulong) -> NetworkBuffer) -> NetworkSupport;

    /// Get the MPI support by the specified run index.
    #[doc(hidden)]
    pub fn create_extern_mpi_network_support_by_index(create_buf: unsafe extern "C" fn(size: c_ulong) -> NetworkBuffer, run_index: c_int) -> NetworkSupport;

    /// Support of version 3.
    #[doc(hidden)]
    pub fn extern_mpi_network_support_v3();
}

/// Initialize the MPI support.
pub fn init_mpi_network_support() {
    unsafe {
        init_extern_mpi_network_support();
        extern_mpi_network_support_v3();
    }
}

/// Finalize the MPI support.
pub fn finalize_mpi_network_support() {
    unsafe {
        finalize_extern_mpi_network_support()
    }
}

/// Get the MPI support.
pub fn create_mpi_network_support() -> NetworkSupport {
    unsafe {
        create_extern_mpi_network_support(create_buf)
    }
}

/// Get the MPI support by the specified run index.
pub fn create_mpi_network_support_by_index(run_index: usize) -> NetworkSupport {
    let run_index: c_int = run_index.try_into().expect("Expected the integer run index.");
    unsafe {
        create_extern_mpi_network_support_by_index(create_buf, run_index)
    }
}

/// Create a network buffer by the specified size.
unsafe extern "C" fn create_buf(size: c_ulong) -> NetworkBuffer {
    let size = size as usize;
    let mut vec = Vec::with_capacity(size);
    vec.resize(size, 0);
    NetworkBuffer::new(vec)
}