-
Notifications
You must be signed in to change notification settings - Fork 33
Add wasi p2 example using rustc native p2 target #344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ludfjig
wants to merge
1
commit into
main
Choose a base branch
from
native_p2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/hyperlight_wasm/examples/monte_carlo_example/main.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /* | ||
| Copyright 2024 The Hyperlight Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| //! Monte Carlo Pi Estimator - Host Example | ||
| //! | ||
| //! Demonstrates running a WASI P2 component built with native `wasm32-wasip2` | ||
| //! target and `wit-bindgen`. The host provides a random number generator | ||
| //! that the guest component imports to estimate Pi. | ||
| //! | ||
| //! Build the guest first: `just build-monte-carlo-example` | ||
| //! Run: `just examples-components` | ||
|
|
||
| extern crate alloc; | ||
|
|
||
| use std::env; | ||
| use std::path::Path; | ||
|
|
||
| use rand::rngs::StdRng; | ||
| use rand::{Rng, SeedableRng}; | ||
|
|
||
| mod bindings { | ||
| hyperlight_component_macro::host_bindgen!("../wasip2_guest/monte-carlo-world.wasm"); | ||
| } | ||
|
|
||
| /// Host-provided RNG state for the guest component. | ||
| /// | ||
| /// The guest cannot generate randomness in `no_std` mode, so the host | ||
| /// provides random numbers via the imported `random` interface. | ||
| struct State { | ||
| rng: StdRng, | ||
| } | ||
|
|
||
| impl State { | ||
| fn new() -> Self { | ||
| State { | ||
| rng: StdRng::from_entropy(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl bindings::my::monte_carlo::Random for State { | ||
| fn random(&mut self) -> f32 { | ||
| self.rng.r#gen::<f32>() | ||
| } | ||
| } | ||
|
|
||
| #[allow(refining_impl_trait)] | ||
| impl bindings::my::monte_carlo::EstimatorImports for State { | ||
| type Random = State; | ||
|
|
||
| fn random(&mut self) -> &mut Self::Random { | ||
| self | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| let state = State::new(); | ||
|
|
||
| let mut sb: hyperlight_wasm::ProtoWasmSandbox = hyperlight_wasm::SandboxBuilder::new() | ||
| .with_guest_input_buffer_size(1000000) | ||
| .with_guest_heap_size(10000000) | ||
| .with_guest_stack_size(1000000) | ||
| .build() | ||
| .unwrap(); | ||
|
|
||
| let rt = bindings::register_host_functions(&mut sb, state); | ||
|
|
||
| let sb = sb.load_runtime().unwrap(); | ||
|
|
||
| // Always look in x64/release since the guest is always built in release mode | ||
| // (debug mode pulls in WASI dependencies that we don't support) | ||
| let proj_dir = env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set"); | ||
| let mod_path = Path::new(&proj_dir) | ||
| .join("../../x64/release/monte_carlo.aot") | ||
| .canonicalize() | ||
| .expect("Failed to find monte_carlo.aot - run 'just build-monte-carlo-example' first"); | ||
| let sb = sb.load_module(mod_path).unwrap(); | ||
|
|
||
| let mut wrapped = bindings::EstimatorSandbox { sb, rt }; | ||
|
|
||
| // Estimate pi with different sample sizes | ||
| for samples in [100, 1000, 10000] { | ||
| let pi_estimate = | ||
| bindings::my::monte_carlo::EstimatorExports::estimate_pi(&mut wrapped, samples); | ||
| println!( | ||
| "Pi estimate with {} samples: {:.6} (error: {:.6})", | ||
| samples, | ||
| pi_estimate, | ||
| (pi_estimate - std::f32::consts::PI).abs() | ||
| ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we update the existing to use wasip2 target?
hyperlight-wasm/Justfile
Lines 47 to 49 in 192a036
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently it only works in release mode, so I would say let's wait a bit longer. in debug profile, it still does bring in entire wasi