-
Notifications
You must be signed in to change notification settings - Fork 2
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
Add example for parallel sort #15
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "hpx-examples" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
hpx-sys = { path = "../hpx-sys", version = "0.1.0" } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#![feature(vec_into_raw_parts)] | ||
#![feature(random)] | ||
use core::array::from_fn; | ||
use std::{env, process, random}; | ||
|
||
fn hpx_main(_: Vec<String>) -> i32 { | ||
let numbers: &[i32; 16384] = &from_fn(|_| random::random::<i32>()); | ||
let list: &mut Vec<i32> = &mut Vec::<i32>::from(numbers); | ||
println!("{:#?}", list); | ||
// Sort the array in parallel. | ||
hpx_sys::ffi::hpx_sort_comp(list, |a, b| a < b); | ||
println!("{:#?}", list); | ||
hpx_sys::ffi::finalize(); | ||
return 0; | ||
} | ||
fn main() { | ||
let args = env::args().collect::<Vec<String>>(); | ||
process::exit(hpx_sys::init(hpx_main, args)); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
#![doc(html_root_url = "https://github.com/STEllAR-GROUP/hpx-rs")] | ||
#![allow(bad_style, non_camel_case_types, unused_extern_crates)] | ||
#![allow(dead_code, unused_imports)] | ||
#![feature(vec_into_raw_parts)] | ||
|
||
#[cxx::bridge] | ||
pub mod ffi { | ||
|
@@ -39,9 +40,45 @@ pub mod ffi { | |
// Wrapper for the above Bindings. | ||
// reffer to tests to understand how to use them. [NOTE: Not all bindings have wrapper.] | ||
// ================================================================================================ | ||
use std::ffi::CString; | ||
use std::env::Args; | ||
use std::ffi::{CString, CStr}; | ||
use std::os::raw::c_char; | ||
|
||
static mut FUNC_HOLDER: Option<fn (Vec<String>) -> i32> = None; | ||
|
||
// Convert arguments from *mut *mut c_char to Vec<String> | ||
// and call the saved Rust version of the function. | ||
fn c_init(argc: i32, argv: *mut *mut c_char) -> i32 { | ||
let mut vec_args: Vec<String> = Vec::new(); | ||
|
||
for i in 0..argc { | ||
// SAFETY: We get the argv from a conversion from Vec<String>. | ||
// The conversion, which is safe, perserves the arguments. | ||
// Therefore, no null pointers. | ||
|
||
let c_str: &CStr = unsafe { CStr::from_ptr(*argv.offset(i as isize)) }; | ||
let string = c_str.to_string_lossy().into_owned(); | ||
vec_args.push(string.to_string()); | ||
} | ||
|
||
unsafe { FUNC_HOLDER.unwrap()(vec_args) } | ||
} | ||
|
||
pub fn init(func: fn(Vec<String>) -> i32, func_args: Vec<String>) -> i32 | ||
{ | ||
unsafe { FUNC_HOLDER = Some(func) }; | ||
let str_args: Vec<&str> = func_args | ||
.iter() | ||
.map(|s| s.as_str()) | ||
.collect::<Vec<&str>>(); | ||
let (argc, rust_argv) = create_c_args(&str_args); | ||
let argv = rust_argv.into_raw_parts().0; | ||
|
||
unsafe { | ||
return self::ffi::init(c_init, argc, argv); | ||
} | ||
} | ||
Comment on lines
+47
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need the The need to have a static variable global to this file is more of a burden to verify with the best practices and I intend to just go around it until I know better. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You cannot put the
|
||
|
||
pub fn create_c_args(args: &[&str]) -> (i32, Vec<*mut c_char>) { | ||
let c_args: Vec<CString> = args.iter().map(|s| CString::new(*s).unwrap()).collect(); | ||
let ptrs: Vec<*mut c_char> = c_args.iter().map(|s| s.as_ptr() as *mut c_char).collect(); | ||
|
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.
@Connor-GH can you explain why have you removed
std::ffi::String
?CString
in Rust is a better approach, and it eliminate the need for the c_init function and the static variable.static variable
you can useclosure
like the followingThere 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.
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.
Please explain what you mean by "you can use
closure
". A closure cannot be cast into a function pointer if it takes any arguments.