jsmpi 0.1.0

A browser-oriented MPI compatibility layer for Rust/WASM using Web Workers
Documentation
//! rsmpi-style gather example.
//! Root rank gathers one integer from each rank.

#[cfg(target_arch = "wasm32")]
use jsmpi as mpi;

#[cfg(not(target_arch = "wasm32"))]
use mpi::traits::*;

#[cfg(target_arch = "wasm32")]
use std::time::Duration;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_futures::spawn_local;

#[cfg(not(target_arch = "wasm32"))]
fn run() {
    let universe = mpi::initialize().expect("failed to initialize MPI runtime");
    let world = universe.world();

    let rank = world.rank();
    let root = 0;
    let local = rank * 10;

    let mut gathered: Vec<i32> = Vec::new();
    world
        .process_at_rank(root)
        .gather_into_root(&local, &mut gathered);

    if rank == root {
        log::info!("[gather] root collected values={gathered:?}");
    }

    world.barrier();
}

#[cfg(target_arch = "wasm32")]
async fn run_async() {
    let runtime = mpi::runtime::Runtime::detect().expect("failed to initialize MPI runtime");
    let rank = runtime.rank();
    let size = runtime.size();
    let root = 0;
    let local = rank * 10;
    let tag = 21;

    if rank == root {
        let mut gathered = vec![local];
        for src in 1..size {
            let (value, _status) = runtime
                .receive_with_timeout_async::<i32>(Some(src), Some(tag), Duration::from_secs(15))
                .await
                .expect("gather receive failed");
            gathered.push(value);
        }
        log::info!("[gather] root collected values={gathered:?}");
    } else {
        runtime
            .send(rank, root, tag, &local)
            .expect("gather send failed");
    }

    runtime.barrier_async().await.expect("barrier failed");
}

fn main() {
    #[cfg(not(target_arch = "wasm32"))]
    env_logger::init();

    #[cfg(not(target_arch = "wasm32"))]
    run();
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn jsmpi_main() {
    let _ = console_log::init();
    spawn_local(async {
        run_async().await;
        mpi::runtime::mark_finished().expect("failed to mark worker as finished");
    });
}