Skip to content

Commit

Permalink
added hpx::fill but it only works for 1D vector
Browse files Browse the repository at this point in the history
Signed-off-by: Dikshant <[email protected]>
  • Loading branch information
pingu-73 committed Jul 28, 2024
1 parent 177333f commit 1f3cb7e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
12 changes: 12 additions & 0 deletions hpx-sys/include/wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,15 @@ inline bool hpx_equal(rust::Slice<const int32_t> src, rust::Slice<const int32_t>
dest.begin(), dest.end()
);
}

inline void hpx_fill(rust::Vec<int32_t>& src, int32_t value) {
std::vector<int32_t> 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);
}
}
26 changes: 26 additions & 0 deletions hpx-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ pub mod ffi {
fn hpx_count_if(vec: &Vec<i32>, 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<i32>, value: i32);
}
}

Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 1f3cb7e

Please sign in to comment.