/*
 * Copyright (c) 2023 Frank Fischer <frank-fischer@shadow-soft.de>
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see  <http://www.gnu.org/licenses/>
 */

mod problem;
pub use problem::Problem;

mod worker;
pub use worker::Worker;

mod msg;

pub use crate::mpi::problem::DistributedFirstOrderProblem;
use log::{debug, log_enabled};
use mpi::traits::*;
use mpi::Threading;
use procfs::process::Process;
use serde::Serialize;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
    #[error("mpi initialization could not create a universe.")]
    NoUniverse,
}

/// An MPI task, either the main process or a worker process.
pub enum Task<P: DistributedFirstOrderProblem + 'static> {
    Main(Problem<P>),
    Worker(Worker<P>),
}

/// Create a first order problem for usage with MPI.
///
/// The function returns either [`Problem`] or a [`Worker`] depending
/// on whether the current task is being run on the root node or some
/// worker node.
pub fn new_problem<P: DistributedFirstOrderProblem>(problem: P) -> Result<Task<P>, Error>
where
    P::Minorant: Serialize,
    P::Err: Serialize,
{
    let (universe, thr) = mpi::initialize_with_threading(Threading::Multiple).ok_or(Error::NoUniverse)?;
    let world = universe.world();
    let rank = world.rank();

    debug!(
        "Init universe [{}]: {:?} {:?}",
        rank,
        thr,
        mpi::environment::threading_support()
    );

    if rank == 0 {
        // root node, the main process
        Ok(Task::Main(Problem::new(universe, problem)))
    } else {
        Ok(Task::Worker(Worker::new(universe, problem)))
    }
}

/// Helper function to show the memory usage of the current process.
fn show_mem_info(rank: i32) {
    if log_enabled!(log::Level::Debug) {
        match Process::myself().and_then(|p| p.stat()) {
            Ok(s) => {
                let page_size = procfs::page_size();
                debug!(
                    "  Memory (rank:{}) v:{:.2}G r:{:.2}G",
                    rank,
                    (s.vsize * page_size) as f64 / (1000.0 * 1000.0 * 1000.0),
                    (s.rss * page_size) as f64 / (1000.0 * 1000.0 * 1000.0)
                );
            }
            Err(err) => debug!("  No memory info (rank:{}): {}", rank, err),
        }
    }
}
