diff --git a/hpx-sys/include/wrapper.h b/hpx-sys/include/wrapper.h index 38f15a9..cbe8b71 100644 --- a/hpx-sys/include/wrapper.h +++ b/hpx-sys/include/wrapper.h @@ -110,3 +110,15 @@ inline bool hpx_equal(rust::Slice src, rust::Slice dest.begin(), dest.end() ); } + +inline void hpx_fill(rust::Vec& src, int32_t value) { + std::vector cpp_vec(src.begin(), src.end()); + + hpx::fill(hpx::execution::par, cpp_vec.begin(), cpp_vec.end(), value); + + src.clear(); + src.reserve(cpp_vec.size()); + for (const auto& item : cpp_vec) { + src.push_back(item); + } +} diff --git a/hpx-sys/src/lib.rs b/hpx-sys/src/lib.rs index b88d769..bf84335 100644 --- a/hpx-sys/src/lib.rs +++ b/hpx-sys/src/lib.rs @@ -25,6 +25,9 @@ pub mod ffi { fn hpx_count_if(vec: &Vec, pred: fn(i32) -> bool) -> i64; fn hpx_ends_with(src: &[i32], dest: &[i32]) -> bool; fn hpx_equal(slice1: &[i32], slice2: &[i32]) -> bool; + + // will only work for 1D vectors + fn hpx_fill(src: &mut Vec, value: i32); } } @@ -286,4 +289,27 @@ mod tests { assert_eq!(result, 0); } } + + #[test] + #[serial] + fn test_hpx_fill() { + let (argc, mut argv) = create_c_args(&["test_hpx_fill"]); + + let hpx_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 { + let mut v = vec![0; 10]; + ffi::hpx_fill(&mut v, 42); + assert!(v.iter().all(|&x| x == 42)); + + let mut v2 = vec![0; 1_000_000]; // testing on a long vector + ffi::hpx_fill(&mut v2, 7); + assert!(v2.iter().all(|&x| x == 7)); + + ffi::finalize() + }; + + unsafe { + let result = ffi::init(hpx_main, argc, argv.as_mut_ptr()); + assert_eq!(result, 0); + } + } }