|
| 1 | +/* |
| 2 | +Copyright 2024 The Hyperlight Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +//! Monte Carlo Pi Estimator - Host Example |
| 18 | +//! |
| 19 | +//! Demonstrates running a WASI P2 component built with native `wasm32-wasip2` |
| 20 | +//! target and `wit-bindgen`. The host provides a random number generator |
| 21 | +//! that the guest component imports to estimate Pi. |
| 22 | +//! |
| 23 | +//! Build the guest first: `just build-monte-carlo-example` |
| 24 | +//! Run: `just examples-components` |
| 25 | +
|
| 26 | +extern crate alloc; |
| 27 | + |
| 28 | +use std::env; |
| 29 | +use std::path::Path; |
| 30 | + |
| 31 | +use rand::rngs::StdRng; |
| 32 | +use rand::{Rng, SeedableRng}; |
| 33 | + |
| 34 | +mod bindings { |
| 35 | + hyperlight_component_macro::host_bindgen!("../wasip2_guest/monte-carlo-world.wasm"); |
| 36 | +} |
| 37 | + |
| 38 | +/// Host-provided RNG state for the guest component. |
| 39 | +/// |
| 40 | +/// The guest cannot generate randomness in `no_std` mode, so the host |
| 41 | +/// provides random numbers via the imported `random` interface. |
| 42 | +struct State { |
| 43 | + rng: StdRng, |
| 44 | +} |
| 45 | + |
| 46 | +impl State { |
| 47 | + fn new() -> Self { |
| 48 | + State { |
| 49 | + rng: StdRng::from_entropy(), |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl bindings::my::monte_carlo::Random for State { |
| 55 | + fn random(&mut self) -> f32 { |
| 56 | + self.rng.r#gen::<f32>() |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +#[allow(refining_impl_trait)] |
| 61 | +impl bindings::my::monte_carlo::EstimatorImports for State { |
| 62 | + type Random = State; |
| 63 | + |
| 64 | + fn random(&mut self) -> &mut Self::Random { |
| 65 | + self |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +fn main() { |
| 70 | + let state = State::new(); |
| 71 | + |
| 72 | + let mut sb: hyperlight_wasm::ProtoWasmSandbox = hyperlight_wasm::SandboxBuilder::new() |
| 73 | + .with_guest_input_buffer_size(1000000) |
| 74 | + .with_guest_heap_size(10000000) |
| 75 | + .with_guest_stack_size(1000000) |
| 76 | + .build() |
| 77 | + .unwrap(); |
| 78 | + |
| 79 | + let rt = bindings::register_host_functions(&mut sb, state); |
| 80 | + |
| 81 | + let sb = sb.load_runtime().unwrap(); |
| 82 | + |
| 83 | + // Always look in x64/release since the guest is always built in release mode |
| 84 | + // (debug mode pulls in WASI dependencies that we don't support) |
| 85 | + let proj_dir = env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set"); |
| 86 | + let mod_path = Path::new(&proj_dir) |
| 87 | + .join("../../x64/release/monte_carlo.aot") |
| 88 | + .canonicalize() |
| 89 | + .expect("Failed to find monte_carlo.aot - run 'just build-monte-carlo-example' first"); |
| 90 | + let sb = sb.load_module(mod_path).unwrap(); |
| 91 | + |
| 92 | + let mut wrapped = bindings::EstimatorSandbox { sb, rt }; |
| 93 | + |
| 94 | + // Estimate pi with different sample sizes |
| 95 | + for samples in [100, 1000, 10000] { |
| 96 | + let pi_estimate = |
| 97 | + bindings::my::monte_carlo::EstimatorExports::estimate_pi(&mut wrapped, samples); |
| 98 | + println!( |
| 99 | + "Pi estimate with {} samples: {:.6} (error: {:.6})", |
| 100 | + samples, |
| 101 | + pi_estimate, |
| 102 | + (pi_estimate - std::f32::consts::PI).abs() |
| 103 | + ); |
| 104 | + } |
| 105 | +} |
0 commit comments