From 2850660d77d73ed31f06f88c0e28b0ab2cb3d34d Mon Sep 17 00:00:00 2001 From: Dikshant Date: Wed, 17 Jul 2024 08:14:35 +0530 Subject: [PATCH 01/20] added hpx::copy Signed-off-by: Dikshant --- hpx-sys/include/wrapper.h | 15 +++++++++ hpx-sys/src/lib.rs | 69 ++++++++++++++++++++++++++++++++++----- 2 files changed, 75 insertions(+), 9 deletions(-) diff --git a/hpx-sys/include/wrapper.h b/hpx-sys/include/wrapper.h index 91cf395..dbdcbe2 100644 --- a/hpx-sys/include/wrapper.h +++ b/hpx-sys/include/wrapper.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -45,3 +46,17 @@ inline std::int32_t disconnect_with_timeout(double shutdown_timeout, double loca inline std::int32_t finalize() { return hpx::finalize(); } /*inline std::int32_t stop() { return hpx::stop(); }*/ + + +inline void hpx_copy(const rust::Vec& src, rust::Vec& dest) { + std::vector cpp_src(src.begin(), src.end()); + std::vector cpp_dest(dest.size()); + + hpx::copy(hpx::execution::par, cpp_src.begin(), cpp_src.end(), cpp_dest.begin()); + + dest.clear(); + dest.reserve(cpp_dest.size()); + for (const auto& item : cpp_dest) { + dest.push_back(item); + } +} diff --git a/hpx-sys/src/lib.rs b/hpx-sys/src/lib.rs index 0d2e98a..221d23b 100644 --- a/hpx-sys/src/lib.rs +++ b/hpx-sys/src/lib.rs @@ -20,6 +20,7 @@ pub mod ffi { fn disconnect() -> i32; fn disconnect_with_timeout(shutdown_timeout: f64, localwait: f64) -> i32; //fn stop() -> i32; + fn hpx_copy(src: &Vec, dest: &mut Vec); } } @@ -41,19 +42,69 @@ mod tests { (ptrs.len() as i32, ptrs) } + fn copy_vector(src: &Vec) -> Vec { + let mut dest = vec![0; src.len()]; + ffi::hpx_copy(src, &mut dest); + dest + } + + fn copy_vector_range(src: &Vec, start: usize, end: usize) -> Vec { + let slice = &src[start..end]; + let mut dest = vec![0; slice.len()]; + ffi::hpx_copy(&slice.to_vec(), &mut dest); + dest + } + + //#[test] + //fn test_init_finalize() { + // let (argc, mut argv) = create_c_args(&["testing", "arg1", "arg2"]); + // + // let dummy_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 { + // println!("Dummy fn called"); + // // to exit hpx::init you are required to shutdown hpx runtime + // ffi::finalize(); + // 0 + // }; + // + // unsafe { + // let result = ffi::init(dummy_main, argc, argv.as_mut_ptr()); + // assert_eq!(result, 0); + // } + //} + #[test] - fn test_init_finalize() { - let (argc, mut argv) = create_c_args(&["testing", "arg1", "arg2"]); - - let dummy_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 { - println!("Dummy fn called"); - // to exit hpx::init you are required to shutdown hpx runtime - ffi::finalize(); - 0 + #[serial] + fn test_hpx_copy() { + let (argc, mut argv) = create_c_args(&["test_hpx_copy"]); + + let hpx_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 { + let src = vec![1, 2, 3, 4, 5]; + let result = copy_vector(&src); + assert_eq!(src, result); + ffi::finalize() + }; + + unsafe { + let result = ffi::init(hpx_main, argc, argv.as_mut_ptr()); + assert_eq!(result, 0); + } + } + + // to test hpx::copy with an arbitirage range + #[test] + #[serial] + fn test_hpx_copy_range() { + let (argc, mut argv) = create_c_args(&["test_hpx_copy"]); + + let hpx_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 { + let src = vec![1, 2, 3, 4, 5]; + let result = copy_vector_range(&src, 0, 3); + assert_eq!(&src[0..3], &result); + ffi::finalize() }; unsafe { - let result = ffi::init(dummy_main, argc, argv.as_mut_ptr()); + let result = ffi::init(hpx_main, argc, argv.as_mut_ptr()); assert_eq!(result, 0); } } From 0f9d2c927dbf1f409623497aa63599aaad7495f6 Mon Sep 17 00:00:00 2001 From: Dikshant Date: Wed, 17 Jul 2024 14:54:19 +0530 Subject: [PATCH 02/20] added hpx::copy_n Signed-off-by: Dikshant --- hpx-sys/include/wrapper.h | 13 +++++++++++++ hpx-sys/src/lib.rs | 24 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/hpx-sys/include/wrapper.h b/hpx-sys/include/wrapper.h index dbdcbe2..900d48d 100644 --- a/hpx-sys/include/wrapper.h +++ b/hpx-sys/include/wrapper.h @@ -60,3 +60,16 @@ inline void hpx_copy(const rust::Vec& src, rust::Vec& dest) { dest.push_back(item); } } + +inline void hpx_copy_n(const rust::Vec& src, size_t count, rust::Vec& dest) { + std::vector cpp_src(src.begin(), src.end()); + std::vector cpp_dest(count); + + hpx::copy_n(hpx::execution::par, cpp_src.begin(), count, cpp_dest.begin()); + + dest.clear(); + dest.reserve(cpp_dest.size()); + for (const auto& item : cpp_dest) { + dest.push_back(item); + } +} diff --git a/hpx-sys/src/lib.rs b/hpx-sys/src/lib.rs index 221d23b..cdcae20 100644 --- a/hpx-sys/src/lib.rs +++ b/hpx-sys/src/lib.rs @@ -21,6 +21,7 @@ pub mod ffi { fn disconnect_with_timeout(shutdown_timeout: f64, localwait: f64) -> i32; //fn stop() -> i32; fn hpx_copy(src: &Vec, dest: &mut Vec); + fn hpx_copy_n(src: &Vec, count: usize, dest: &mut Vec); } } @@ -55,6 +56,11 @@ mod tests { dest } + pub fn copy_n(src: &[i32], count: usize) -> Vec { + let mut dest = Vec::with_capacity(count); + ffi::hpx_copy_n(&src.to_vec(), count, &mut dest); + dest + } //#[test] //fn test_init_finalize() { // let (argc, mut argv) = create_c_args(&["testing", "arg1", "arg2"]); @@ -108,4 +114,22 @@ mod tests { assert_eq!(result, 0); } } + + #[test] + #[serial] + fn test_copy_n() { + let (argc, mut argv) = create_c_args(&["test_copy_n"]); + + let test_func = |_argc: i32, _argv: *mut *mut c_char| -> i32 { + let src = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + let result = copy_n(&src, 5); + assert_eq!(result, vec![1, 2, 3, 4, 5]); + ffi::finalize() + }; + + unsafe { + let result = ffi::init(test_func, argc, argv.as_mut_ptr()); + assert_eq!(result, 0); + } + } } From e8f28f26c8f45479701d8e5080ec08d3a092d522 Mon Sep 17 00:00:00 2001 From: Dikshant Date: Thu, 18 Jul 2024 07:57:43 +0530 Subject: [PATCH 03/20] added hpx::copy_if Signed-off-by: Dikshant --- hpx-sys/include/wrapper.h | 19 +++++++++++++++++++ hpx-sys/src/lib.rs | 28 ++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/hpx-sys/include/wrapper.h b/hpx-sys/include/wrapper.h index 900d48d..bd2b829 100644 --- a/hpx-sys/include/wrapper.h +++ b/hpx-sys/include/wrapper.h @@ -73,3 +73,22 @@ inline void hpx_copy_n(const rust::Vec& src, size_t count, rust::Vec& src, rust::Vec& dest, + rust::Fn pred) { + std::vector cpp_src(src.begin(), src.end()); + std::vector cpp_dest(cpp_src.size()); + + auto result = hpx::copy_if(hpx::execution::par, + cpp_src.begin(), cpp_src.end(), + cpp_dest.begin(), + [&](int32_t value) { return pred(value); }); + + cpp_dest.resize(std::distance(cpp_dest.begin(), result)); + + dest.clear(); + dest.reserve(cpp_dest.size()); + for (const auto& item : cpp_dest) { + dest.push_back(item); + } +} diff --git a/hpx-sys/src/lib.rs b/hpx-sys/src/lib.rs index cdcae20..4710386 100644 --- a/hpx-sys/src/lib.rs +++ b/hpx-sys/src/lib.rs @@ -22,6 +22,7 @@ pub mod ffi { //fn stop() -> i32; fn hpx_copy(src: &Vec, dest: &mut Vec); fn hpx_copy_n(src: &Vec, count: usize, dest: &mut Vec); + fn hpx_copy_if(src: &Vec, dest: &mut Vec, pred: fn(i32) -> bool); } } @@ -56,11 +57,17 @@ mod tests { dest } - pub fn copy_n(src: &[i32], count: usize) -> Vec { + fn copy_n(src: &[i32], count: usize) -> Vec { let mut dest = Vec::with_capacity(count); ffi::hpx_copy_n(&src.to_vec(), count, &mut dest); dest } + + fn copy_if_positive(src: &Vec) -> Vec { + let mut dest = Vec::new(); + ffi::hpx_copy_if(src, &mut dest, |x| x % 3 == 0); + dest + } //#[test] //fn test_init_finalize() { // let (argc, mut argv) = create_c_args(&["testing", "arg1", "arg2"]); @@ -96,7 +103,6 @@ mod tests { } } - // to test hpx::copy with an arbitirage range #[test] #[serial] fn test_hpx_copy_range() { @@ -132,4 +138,22 @@ mod tests { assert_eq!(result, 0); } } + + #[test] + #[serial] + fn test_hpx_copy_if() { + let (argc, mut argv) = create_c_args(&["test_hpx_copy_if"]); + + let hpx_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 { + let src = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; + let result = copy_if_positive(&src); + assert_eq!(result, vec![0, 3, 6, 9, 12]); + ffi::finalize() + }; + + unsafe { + let result = ffi::init(hpx_main, argc, argv.as_mut_ptr()); + assert_eq!(result, 0); + } + } } From 5e5b709df371a8b4bebc8f443f82bb41f8b973f9 Mon Sep 17 00:00:00 2001 From: Dikshant Date: Thu, 18 Jul 2024 22:28:52 +0530 Subject: [PATCH 04/20] added hpx::count, hpx::count_if Signed-off-by: Dikshant --- hpx-sys/include/wrapper.h | 30 +++++++------ hpx-sys/src/lib.rs | 92 +++++++++++++++++++++++++++++++-------- 2 files changed, 90 insertions(+), 32 deletions(-) diff --git a/hpx-sys/include/wrapper.h b/hpx-sys/include/wrapper.h index bd2b829..7cf2f6e 100644 --- a/hpx-sys/include/wrapper.h +++ b/hpx-sys/include/wrapper.h @@ -8,17 +8,6 @@ #include "rust/cxx.h" - -/*inline std::int32_t start() { return hpx::start(nullptr, 0, nullptr); }*/ - -/*inline std::int32_t start(rust::Fn rust_fn, int argc, char **argv) {*/ -/* return hpx::start(*/ -/* [&](int argc, char **argv) {*/ -/* return rust_fn(argc, argv);*/ -/* },*/ -/* argc, argv);*/ -/*}*/ - inline std::int32_t init(rust::Fn rust_fn, int argc, char **argv) { return hpx::init( [&](int argc, char **argv) { @@ -45,9 +34,6 @@ inline std::int32_t disconnect_with_timeout(double shutdown_timeout, double loca inline std::int32_t finalize() { return hpx::finalize(); } -/*inline std::int32_t stop() { return hpx::stop(); }*/ - - inline void hpx_copy(const rust::Vec& src, rust::Vec& dest) { std::vector cpp_src(src.begin(), src.end()); std::vector cpp_dest(dest.size()); @@ -92,3 +78,19 @@ inline void hpx_copy_if(const rust::Vec& src, rust::Vec& dest, dest.push_back(item); } } + +inline std::int64_t hpx_count(const rust::Vec& vec, int32_t value) { + return hpx::count(hpx::execution::par, vec.begin(), vec.end(), value); +} + + +inline int64_t hpx_count_if(const rust::Vec& vec, rust::Fn pred) { + std::vector cpp_vec(vec.begin(), vec.end()); + + auto result = hpx::count_if(hpx::execution::par, + cpp_vec.begin(), + cpp_vec.end(), + [&](int32_t value) { return pred(value); }); + + return static_cast(result); +} diff --git a/hpx-sys/src/lib.rs b/hpx-sys/src/lib.rs index 4710386..94b3d97 100644 --- a/hpx-sys/src/lib.rs +++ b/hpx-sys/src/lib.rs @@ -7,7 +7,6 @@ pub mod ffi { unsafe extern "C++" { include!("hpx-sys/include/wrapper.h"); - //fn start() -> i32; unsafe fn init( func: unsafe fn(i32, *mut *mut c_char) -> i32, argc: i32, @@ -19,10 +18,11 @@ pub mod ffi { fn terminate(); fn disconnect() -> i32; fn disconnect_with_timeout(shutdown_timeout: f64, localwait: f64) -> i32; - //fn stop() -> i32; fn hpx_copy(src: &Vec, dest: &mut Vec); fn hpx_copy_n(src: &Vec, count: usize, dest: &mut Vec); fn hpx_copy_if(src: &Vec, dest: &mut Vec, pred: fn(i32) -> bool); + fn hpx_count(vec: &Vec, value: i32) -> i64; + fn hpx_count_if(vec: &Vec, pred: fn(i32) -> bool) -> i64; } } @@ -68,22 +68,28 @@ mod tests { ffi::hpx_copy_if(src, &mut dest, |x| x % 3 == 0); dest } - //#[test] - //fn test_init_finalize() { - // let (argc, mut argv) = create_c_args(&["testing", "arg1", "arg2"]); - // - // let dummy_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 { - // println!("Dummy fn called"); - // // to exit hpx::init you are required to shutdown hpx runtime - // ffi::finalize(); - // 0 - // }; - // - // unsafe { - // let result = ffi::init(dummy_main, argc, argv.as_mut_ptr()); - // assert_eq!(result, 0); - // } - //} + + fn count(vec: &Vec, value: i32) -> i64 { + ffi::hpx_count(vec, value) + } + + #[test] + #[serial] + fn test_init_finalize() { + let (argc, mut argv) = create_c_args(&["testing", "arg1", "arg2"]); + + let dummy_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 { + println!("Dummy fn called"); + // to exit hpx::init you are required to shutdown hpx runtime + ffi::finalize(); + 0 + }; + + unsafe { + let result = ffi::init(dummy_main, argc, argv.as_mut_ptr()); + assert_eq!(result, 0); + } + } #[test] #[serial] @@ -156,4 +162,54 @@ mod tests { assert_eq!(result, 0); } } + + #[test] + #[serial] + fn test_hpx_count() { + let (argc, mut argv) = create_c_args(&["test_hpx_count"]); + + let hpx_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 { + let vec = vec![1, 2, 3, 2, 4, 2, 5, 2]; + let result = count(&vec, 2); + assert_eq!(result, 4); + ffi::finalize() + }; + + unsafe { + let result = ffi::init(hpx_main, argc, argv.as_mut_ptr()); + assert_eq!(result, 0); + } + } + + #[test] + #[serial] + fn test_hpx_count_if() { + let (argc, mut argv) = create_c_args(&["test_hpx_count_if"]); + let hpx_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 { + let vec = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + let result_even = ffi::hpx_count_if(&vec, |x| x % 2 == 0); + assert_eq!(result_even, 5); + let result_greater_than_5 = ffi::hpx_count_if(&vec, |x| x > 5); + assert_eq!(result_greater_than_5, 5); + let is_prime = |n: i32| { + if n <= 1 { + return false; + } + for i in 2..=(n as f64).sqrt() as i32 { + if n % i == 0 { + return false; + } + } + true + }; + let result_prime = ffi::hpx_count_if(&vec, is_prime); + assert_eq!(result_prime, 4); + ffi::finalize() + }; + + unsafe { + let result = ffi::init(hpx_main, argc, argv.as_mut_ptr()); + assert_eq!(result, 0); + } + } } From 78dbbe741fd8386e3c7b827baae718d349b3dccd Mon Sep 17 00:00:00 2001 From: Dikshant Date: Thu, 18 Jul 2024 22:38:52 +0530 Subject: [PATCH 05/20] rerun tests Signed-off-by: Dikshant --- hpx-sys/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/hpx-sys/src/lib.rs b/hpx-sys/src/lib.rs index 94b3d97..ff764cd 100644 --- a/hpx-sys/src/lib.rs +++ b/hpx-sys/src/lib.rs @@ -212,4 +212,5 @@ mod tests { assert_eq!(result, 0); } } + // } From 8988ec270a2049ca33891bfad5295cff1aef9cd5 Mon Sep 17 00:00:00 2001 From: Dikshant Date: Sun, 21 Jul 2024 14:44:28 +0530 Subject: [PATCH 06/20] added hpx::ends_with Signed-off-by: Dikshant --- hpx-sys/include/wrapper.h | 8 ++++++++ hpx-sys/src/lib.rs | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/hpx-sys/include/wrapper.h b/hpx-sys/include/wrapper.h index 7cf2f6e..fb43239 100644 --- a/hpx-sys/include/wrapper.h +++ b/hpx-sys/include/wrapper.h @@ -94,3 +94,11 @@ inline int64_t hpx_count_if(const rust::Vec& vec, rust::Fn(result); } + +inline bool hpx_ends_with(rust::Slice src, + rust::Slice dest) { + return hpx::ends_with(hpx::execution::par, + src.begin(), src.end(), + dest.begin(), dest.end(), + std::equal_to()); +} diff --git a/hpx-sys/src/lib.rs b/hpx-sys/src/lib.rs index ff764cd..c421969 100644 --- a/hpx-sys/src/lib.rs +++ b/hpx-sys/src/lib.rs @@ -23,6 +23,7 @@ pub mod ffi { fn hpx_copy_if(src: &Vec, dest: &mut Vec, pred: fn(i32) -> bool); fn hpx_count(vec: &Vec, value: i32) -> i64; fn hpx_count_if(vec: &Vec, pred: fn(i32) -> bool) -> i64; + fn hpx_ends_with(src: &[i32], dest: &[i32]) -> bool; } } @@ -212,5 +213,41 @@ mod tests { assert_eq!(result, 0); } } - // + + #[test] + #[serial] + fn test_hpx_ends_with() { + let (argc, mut argv) = create_c_args(&["test_hpx_ends_with"]); + + let hpx_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 { + let vec1 = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + let vec2 = vec![8, 9, 10]; + let vec3 = vec![7, 8, 9]; + let vec4 = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + let vec5: Vec = vec![]; + + // checking with complete vectors + assert!(ffi::hpx_ends_with(&vec1, &vec2)); + assert!(!ffi::hpx_ends_with(&vec1, &vec3)); + assert!(ffi::hpx_ends_with(&vec1, &vec4)); + assert!(ffi::hpx_ends_with(&vec1, &vec5)); + assert!(ffi::hpx_ends_with(&vec5, &vec5)); + + // checking for vector slices + assert!(ffi::hpx_ends_with(&vec1[5..], &vec2)); + assert!(ffi::hpx_ends_with(&vec1[..], &vec1[8..])); + assert!(!ffi::hpx_ends_with(&vec1[..5], &vec2)); + assert!(ffi::hpx_ends_with(&vec1[..5], &vec1[3..5])); + + assert!(ffi::hpx_ends_with(&vec1, &[])); + assert!(ffi::hpx_ends_with(&[], &[])); + + ffi::finalize() + }; + + unsafe { + let result = ffi::init(hpx_main, argc, argv.as_mut_ptr()); + assert_eq!(result, 0); + } + } } From 177333f93acecc2830a52238711ad96b1fb17c6e Mon Sep 17 00:00:00 2001 From: Dikshant Date: Sun, 21 Jul 2024 18:22:02 +0530 Subject: [PATCH 07/20] added hpx::equal Signed-off-by: Dikshant --- hpx-sys/include/wrapper.h | 8 +++++ hpx-sys/src/lib.rs | 76 ++++++++++++++++++++++++++++----------- 2 files changed, 64 insertions(+), 20 deletions(-) diff --git a/hpx-sys/include/wrapper.h b/hpx-sys/include/wrapper.h index fb43239..38f15a9 100644 --- a/hpx-sys/include/wrapper.h +++ b/hpx-sys/include/wrapper.h @@ -102,3 +102,11 @@ inline bool hpx_ends_with(rust::Slice src, dest.begin(), dest.end(), std::equal_to()); } + +inline bool hpx_equal(rust::Slice src, rust::Slice dest) { + return hpx::equal( + hpx::execution::par, + src.begin(), src.end(), + dest.begin(), dest.end() + ); +} diff --git a/hpx-sys/src/lib.rs b/hpx-sys/src/lib.rs index c421969..b88d769 100644 --- a/hpx-sys/src/lib.rs +++ b/hpx-sys/src/lib.rs @@ -24,6 +24,7 @@ pub mod ffi { fn hpx_count(vec: &Vec, value: i32) -> i64; 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; } } @@ -220,26 +221,26 @@ mod tests { let (argc, mut argv) = create_c_args(&["test_hpx_ends_with"]); let hpx_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 { - let vec1 = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; - let vec2 = vec![8, 9, 10]; - let vec3 = vec![7, 8, 9]; - let vec4 = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; - let vec5: Vec = vec![]; - - // checking with complete vectors - assert!(ffi::hpx_ends_with(&vec1, &vec2)); - assert!(!ffi::hpx_ends_with(&vec1, &vec3)); - assert!(ffi::hpx_ends_with(&vec1, &vec4)); - assert!(ffi::hpx_ends_with(&vec1, &vec5)); - assert!(ffi::hpx_ends_with(&vec5, &vec5)); - - // checking for vector slices - assert!(ffi::hpx_ends_with(&vec1[5..], &vec2)); - assert!(ffi::hpx_ends_with(&vec1[..], &vec1[8..])); - assert!(!ffi::hpx_ends_with(&vec1[..5], &vec2)); - assert!(ffi::hpx_ends_with(&vec1[..5], &vec1[3..5])); - - assert!(ffi::hpx_ends_with(&vec1, &[])); + let v1 = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + let v2 = vec![8, 9, 10]; + let v3 = vec![7, 8, 9]; + let v4 = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + let v5: Vec = vec![]; + + // passing vectors + assert!(ffi::hpx_ends_with(&v1, &v2)); + assert!(!ffi::hpx_ends_with(&v1, &v3)); + assert!(ffi::hpx_ends_with(&v1, &v4)); + assert!(ffi::hpx_ends_with(&v1, &v5)); + assert!(ffi::hpx_ends_with(&v5, &v5)); + + // passing slices + assert!(ffi::hpx_ends_with(&v1[5..], &v2)); + assert!(ffi::hpx_ends_with(&v1[..], &v1[8..])); + assert!(!ffi::hpx_ends_with(&v1[..5], &v2)); + assert!(ffi::hpx_ends_with(&v1[..5], &v1[3..5])); + + assert!(ffi::hpx_ends_with(&v1, &[])); assert!(ffi::hpx_ends_with(&[], &[])); ffi::finalize() @@ -250,4 +251,39 @@ mod tests { assert_eq!(result, 0); } } + + #[test] + #[serial] + fn test_hpx_equal() { + let (argc, mut argv) = create_c_args(&["test_hpx_equal"]); + + let hpx_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 { + let v1 = vec![1, 2, 3, 4, 5]; + let v2 = vec![1, 2, 3, 4, 5]; + let v3 = vec![1, 2, 3, 4, 6]; + let v4 = vec![1, 2, 3, 4]; + let v5 = vec![0, 1, 2, 3, 4, 5, 6]; + + // passing vectors + assert!(ffi::hpx_equal(&v1, &v2)); + assert!(!ffi::hpx_equal(&v1, &v3)); + assert!(!ffi::hpx_equal(&v1, &v4)); + + // passing slices + assert!(ffi::hpx_equal(&v1[..], &v2[..])); + assert!(ffi::hpx_equal(&v1[1..4], &v2[1..4])); + assert!(ffi::hpx_equal(&v1[..3], &v4[..3])); + assert!(ffi::hpx_equal(&v1[..], &v5[1..6])); + + assert!(ffi::hpx_equal(&v1, &v5[1..6])); + assert!(!ffi::hpx_equal(&v1[..4], &v3)); + + ffi::finalize() + }; + + unsafe { + let result = ffi::init(hpx_main, argc, argv.as_mut_ptr()); + assert_eq!(result, 0); + } + } } From 1f3cb7e128e57f964253dae829f54159b1ae31cf Mon Sep 17 00:00:00 2001 From: Dikshant Date: Sun, 28 Jul 2024 18:42:13 +0530 Subject: [PATCH 08/20] added hpx::fill but it only works for 1D vector Signed-off-by: Dikshant --- hpx-sys/include/wrapper.h | 12 ++++++++++++ hpx-sys/src/lib.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) 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); + } + } } From b646351dfb36fcf491ecbfe7099bbca22ba45d63 Mon Sep 17 00:00:00 2001 From: Dikshant Date: Mon, 29 Jul 2024 16:28:31 +0530 Subject: [PATCH 09/20] added hpx::find Signed-off-by: Dikshant --- hpx-sys/include/wrapper.h | 14 ++++++++++++ hpx-sys/src/lib.rs | 45 ++++++++++++++++++++++++++++++++++----- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/hpx-sys/include/wrapper.h b/hpx-sys/include/wrapper.h index cbe8b71..f8746c5 100644 --- a/hpx-sys/include/wrapper.h +++ b/hpx-sys/include/wrapper.h @@ -122,3 +122,17 @@ inline void hpx_fill(rust::Vec& src, int32_t value) { src.push_back(item); } } + +inline int64_t hpx_find(const rust::Vec& src, int32_t value) { + std::vector cpp_vec(src.begin(), src.end()); + + auto result = hpx::find(hpx::execution::par, + cpp_vec.begin(), + cpp_vec.end(), + value); + + if (result != cpp_vec.end()) { + return static_cast(std::distance(cpp_vec.begin(), result)); + } + return -1; +} diff --git a/hpx-sys/src/lib.rs b/hpx-sys/src/lib.rs index bf84335..1ee4a6a 100644 --- a/hpx-sys/src/lib.rs +++ b/hpx-sys/src/lib.rs @@ -21,13 +21,12 @@ pub mod ffi { fn hpx_copy(src: &Vec, dest: &mut Vec); fn hpx_copy_n(src: &Vec, count: usize, dest: &mut Vec); fn hpx_copy_if(src: &Vec, dest: &mut Vec, pred: fn(i32) -> bool); - fn hpx_count(vec: &Vec, value: i32) -> i64; - fn hpx_count_if(vec: &Vec, pred: fn(i32) -> bool) -> i64; + fn hpx_count(src: &Vec, value: i32) -> i64; + fn hpx_count_if(src: &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); + fn hpx_fill(src: &mut Vec, value: i32); // will only work for linear vectors + fn hpx_find(src: &Vec, value: i32) -> i64; } } @@ -78,6 +77,13 @@ mod tests { ffi::hpx_count(vec, value) } + fn find(vec: &Vec, value: i32) -> Option { + match ffi::hpx_find(vec, value) { + -1 => None, + index => Some(index as usize), + } + } + #[test] #[serial] fn test_init_finalize() { @@ -312,4 +318,33 @@ mod tests { assert_eq!(result, 0); } } + + #[test] + #[serial] + fn test_hpx_find() { + let (argc, mut argv) = create_c_args(&["test_hpx_find"]); + + let hpx_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 { + let v = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + + let result = find(&v, 5); // finding existing value + assert_eq!(result, Some(4)); + + let result = find(&v, 11); // finding non-existing value + assert_eq!(result, None); + + let result = find(&v, 1); + assert_eq!(result, Some(0)); + + let result = find(&v, 10); + assert_eq!(result, Some(9)); + + ffi::finalize() + }; + + unsafe { + let result = ffi::init(hpx_main, argc, argv.as_mut_ptr()); + assert_eq!(result, 0); + } + } } From 354b234d02f35a017d4d28ae133b200a5534db2d Mon Sep 17 00:00:00 2001 From: Dikshant Date: Wed, 31 Jul 2024 11:44:05 +0530 Subject: [PATCH 10/20] added hpx::sort Signed-off-by: Dikshant --- hpx-sys/include/wrapper.h | 12 ++++++++++++ hpx-sys/src/lib.rs | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/hpx-sys/include/wrapper.h b/hpx-sys/include/wrapper.h index f8746c5..abcf994 100644 --- a/hpx-sys/include/wrapper.h +++ b/hpx-sys/include/wrapper.h @@ -136,3 +136,15 @@ inline int64_t hpx_find(const rust::Vec& src, int32_t value) { } return -1; } + +inline void hpx_sort(rust::Vec& src) { + std::vector cpp_vec(src.begin(), src.end()); + + hpx::sort(hpx::execution::par, cpp_vec.begin(), cpp_vec.end()); + + 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 1ee4a6a..5b45748 100644 --- a/hpx-sys/src/lib.rs +++ b/hpx-sys/src/lib.rs @@ -27,6 +27,7 @@ pub mod ffi { fn hpx_equal(slice1: &[i32], slice2: &[i32]) -> bool; fn hpx_fill(src: &mut Vec, value: i32); // will only work for linear vectors fn hpx_find(src: &Vec, value: i32) -> i64; + fn hpx_sort(src: &mut Vec); } } @@ -347,4 +348,22 @@ mod tests { assert_eq!(result, 0); } } + + #[test] + #[serial] + fn test_hpx_sort() { + let (argc, mut argv) = create_c_args(&["test_hpx_sort"]); + + let hpx_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 { + let mut src = vec![5, 2, 8, 1, 9, 3, 7, 6, 4]; + ffi::hpx_sort(&mut src); + assert_eq!(src, vec![1, 2, 3, 4, 5, 6, 7, 8, 9]); + ffi::finalize() + }; + + unsafe { + let result = ffi::init(hpx_main, argc, argv.as_mut_ptr()); + assert_eq!(result, 0); + } + } } From 43ec447a99f36a9fd6b3c6ff0f74b406ad1253ea Mon Sep 17 00:00:00 2001 From: Dikshant Date: Wed, 31 Jul 2024 11:58:14 +0530 Subject: [PATCH 11/20] added hpx::sort along with comparator closure as an argument Signed-off-by: Dikshant --- hpx-sys/include/wrapper.h | 13 +++++++++++++ hpx-sys/src/lib.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/hpx-sys/include/wrapper.h b/hpx-sys/include/wrapper.h index abcf994..8e5c536 100644 --- a/hpx-sys/include/wrapper.h +++ b/hpx-sys/include/wrapper.h @@ -148,3 +148,16 @@ inline void hpx_sort(rust::Vec& src) { src.push_back(item); } } + +inline void hpx_sort_comp(rust::Vec& src, rust::Fn comp) { + std::vector cpp_vec(src.begin(), src.end()); + + hpx::sort(hpx::execution::par, cpp_vec.begin(), cpp_vec.end(), + [&](int32_t a, int32_t b) { return comp(a, b); }); + + 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 5b45748..e8e63e2 100644 --- a/hpx-sys/src/lib.rs +++ b/hpx-sys/src/lib.rs @@ -28,6 +28,7 @@ pub mod ffi { fn hpx_fill(src: &mut Vec, value: i32); // will only work for linear vectors fn hpx_find(src: &Vec, value: i32) -> i64; fn hpx_sort(src: &mut Vec); + fn hpx_sort_comp(src: &mut Vec, comp: fn(i32, i32) -> bool); } } @@ -366,4 +367,37 @@ mod tests { assert_eq!(result, 0); } } + + #[test] + #[serial] + fn test_hpx_sort_comp() { + let (argc, mut argv) = create_c_args(&["test_hpx_sort_comp"]); + + let hpx_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 { + let mut v = vec![5, 2, 8, 1, 9, 3, 7, 6, 4]; + ffi::hpx_sort_comp(&mut v, |a, b| a > b); // sorting in descending order + assert_eq!(v, vec![9, 8, 7, 6, 5, 4, 3, 2, 1]); + + // sorting even numbers before odd numbers + let mut v2 = vec![5, 2, 8, 1, 9, 3, 7, 6, 4]; + ffi::hpx_sort_comp( + &mut v2, + |a, b| { + if a % 2 == b % 2 { + a < b + } else { + a % 2 == 0 + } + }, + ); + assert_eq!(v2, vec![2, 4, 6, 8, 1, 3, 5, 7, 9]); + + ffi::finalize() + }; + + unsafe { + let result = ffi::init(hpx_main, argc, argv.as_mut_ptr()); + assert_eq!(result, 0); + } + } } From 779b4d8621a9dde9c1f0ed039c06a58a6c23c6f9 Mon Sep 17 00:00:00 2001 From: Dikshant Date: Wed, 31 Jul 2024 12:19:55 +0530 Subject: [PATCH 12/20] added hpx::merge Signed-off-by: Dikshant --- hpx-sys/include/wrapper.h | 19 +++++++++++++++++++ hpx-sys/src/lib.rs | 21 +++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/hpx-sys/include/wrapper.h b/hpx-sys/include/wrapper.h index 8e5c536..5aad2ec 100644 --- a/hpx-sys/include/wrapper.h +++ b/hpx-sys/include/wrapper.h @@ -161,3 +161,22 @@ inline void hpx_sort_comp(rust::Vec& src, rust::Fn& src1, + const rust::Vec& src2, + rust::Vec& dest) { + std::vector cpp_src1(src1.begin(), src1.end()); + std::vector cpp_src2(src2.begin(), src2.end()); + std::vector cpp_dest(cpp_src1.size() + cpp_src2.size()); + + hpx::merge(hpx::execution::par, + cpp_src1.begin(), cpp_src1.end(), + cpp_src2.begin(), cpp_src2.end(), + cpp_dest.begin()); + + dest.clear(); + dest.reserve(cpp_dest.size()); + for (const auto& item : cpp_dest) { + dest.push_back(item); + } +} diff --git a/hpx-sys/src/lib.rs b/hpx-sys/src/lib.rs index e8e63e2..badcf7a 100644 --- a/hpx-sys/src/lib.rs +++ b/hpx-sys/src/lib.rs @@ -29,6 +29,7 @@ pub mod ffi { fn hpx_find(src: &Vec, value: i32) -> i64; fn hpx_sort(src: &mut Vec); fn hpx_sort_comp(src: &mut Vec, comp: fn(i32, i32) -> bool); + fn hpx_merge(src1: &Vec, src2: &Vec, dest: &mut Vec); } } @@ -400,4 +401,24 @@ mod tests { assert_eq!(result, 0); } } + + #[test] + #[serial] + fn test_hpx_merge() { + let (argc, mut argv) = create_c_args(&["test_hpx_merge"]); + + let hpx_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 { + let v1 = vec![1, 3, 5, 7, 9, 20, 100]; + let v2 = vec![2, 4, 6, 8, 10, 97]; + let mut dest = Vec::new(); + ffi::hpx_merge(&v1, &v2, &mut dest); + assert_eq!(dest, vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 97, 100]); + ffi::finalize() + }; + + unsafe { + let result = ffi::init(hpx_main, argc, argv.as_mut_ptr()); + assert_eq!(result, 0); + } + } } From 1ea64f28c39f82c190c53e935de921e432bcf62d Mon Sep 17 00:00:00 2001 From: Dikshant Date: Thu, 1 Aug 2024 18:31:03 +0530 Subject: [PATCH 13/20] added hpx::partial_sort Signed-off-by: Dikshant --- hpx-sys/include/wrapper.h | 32 ++++++++++++++++++++ hpx-sys/src/lib.rs | 62 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/hpx-sys/include/wrapper.h b/hpx-sys/include/wrapper.h index 5aad2ec..42e2af1 100644 --- a/hpx-sys/include/wrapper.h +++ b/hpx-sys/include/wrapper.h @@ -180,3 +180,35 @@ inline void hpx_merge(const rust::Vec& src1, dest.push_back(item); } } + +inline void hpx_partial_sort(rust::Vec& src, size_t last) { + std::vector cpp_vec(src.begin(), src.end()); + + hpx::partial_sort(hpx::execution::par, + cpp_vec.begin(), + cpp_vec.begin() + last, + cpp_vec.end()); + + src.clear(); + src.reserve(cpp_vec.size()); + for (const auto& item : cpp_vec) { + src.push_back(item); + } +} + +inline void hpx_partial_sort_comp(rust::Vec& src, size_t last, + rust::Fn comp) { + std::vector cpp_vec(src.begin(), src.end()); + + hpx::partial_sort(hpx::execution::par, + cpp_vec.begin(), + cpp_vec.begin() + last, + cpp_vec.end(), + [&](int32_t a, int32_t b) { return comp(a, b); }); + + 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 badcf7a..c6f4aaa 100644 --- a/hpx-sys/src/lib.rs +++ b/hpx-sys/src/lib.rs @@ -30,6 +30,8 @@ pub mod ffi { fn hpx_sort(src: &mut Vec); fn hpx_sort_comp(src: &mut Vec, comp: fn(i32, i32) -> bool); fn hpx_merge(src1: &Vec, src2: &Vec, dest: &mut Vec); + fn hpx_partial_sort(src: &mut Vec, last: usize); + fn hpx_partial_sort_comp(src: &mut Vec, last: usize, comp: fn(i32, i32) -> bool); } } @@ -421,4 +423,64 @@ mod tests { assert_eq!(result, 0); } } + + #[test] + #[serial] + fn test_hpx_partial_sort() { + let (argc, mut argv) = create_c_args(&["test_hpx_partial_sort"]); + + let hpx_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 { + let mut vec = vec![5, 2, 8, 1, 9, 3, 7, 6, 4]; + let last = 4; + println!("Before partial sort: {:?}", vec); + + ffi::hpx_partial_sort(&mut vec, last); + println!("After partial sort: {:?}", vec); + + // If first -> last elements are sorted + assert!(vec[..last].windows(2).all(|w| w[0] <= w[1])); + + // If ele of sorted part <= ele of unsorted part + assert!(vec[..last] + .iter() + .all(|&x| vec[last..].iter().all(|&y| x <= y))); + + ffi::finalize() + }; + + unsafe { + let result = ffi::init(hpx_main, argc, argv.as_mut_ptr()); + assert_eq!(result, 0); + } + } + + #[test] + #[serial] + fn test_hpx_partial_sort_comp() { + let (argc, mut argv) = create_c_args(&["test_hpx_partial_sort_comp"]); + + let hpx_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 { + let mut vec = vec![5, 2, 8, 1, 9, 3, 7, 6, 4]; + let last = 4; + println!("Before partial sort: {:?}", vec); + + ffi::hpx_partial_sort_comp(&mut vec, last, |a, b| b < a); + println!("After partial sort: {:?}", vec); + + // If first -> last elements are sorted dec + assert!(vec[..last].windows(2).all(|w| w[0] >= w[1])); + + // If ele of sorted part >= ele of unsorted part + assert!(vec[..last] + .iter() + .all(|&x| vec[last..].iter().all(|&y| x >= y))); + + ffi::finalize() + }; + + unsafe { + let result = ffi::init(hpx_main, argc, argv.as_mut_ptr()); + assert_eq!(result, 0); + } + } } From fa1e347a9d1f0c1b48e1b53ab84a5882d92b41ed Mon Sep 17 00:00:00 2001 From: Dikshant Date: Sat, 10 Aug 2024 19:04:57 +0530 Subject: [PATCH 14/20] moved wrappers from tests to main Signed-off-by: Dikshant --- Cargo.toml | 2 +- hpx-sys/src/lib.rs | 94 +++++++++++++++++++++++++--------------------- 2 files changed, 52 insertions(+), 44 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8cf8c32..2ecd021 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "hpx-rs" version = "0.1.0" -authors = ["Shreyas Atre ", "Dikshant ", "Dikshant "] edition = "2021" readme = "README.md" repository = "https://github.com/STEllAR-GROUP/hpx-rs" diff --git a/hpx-sys/src/lib.rs b/hpx-sys/src/lib.rs index c6f4aaa..208ebb8 100644 --- a/hpx-sys/src/lib.rs +++ b/hpx-sys/src/lib.rs @@ -36,58 +36,66 @@ pub mod ffi { } // ================================================================================================ -// Tests (to be shifted to systests crate within hpx-rs workspace) +// Wrapper for the above Bindings. +// reffer to tests to understand how to use them. [NOTE: Not all bindings have wrapper.] // ================================================================================================ -#[cfg(test)] -mod tests { - use super::ffi; - use serial_test::serial; - use std::ffi::CString; - use std::os::raw::c_char; - use std::thread; - use std::time::Duration; +use std::ffi::CString; +use std::os::raw::c_char; - fn create_c_args(args: &[&str]) -> (i32, Vec<*mut c_char>) { - let c_args: Vec = 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(); - (ptrs.len() as i32, ptrs) - } +pub fn create_c_args(args: &[&str]) -> (i32, Vec<*mut c_char>) { + let c_args: Vec = 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(); + (ptrs.len() as i32, ptrs) +} - fn copy_vector(src: &Vec) -> Vec { - let mut dest = vec![0; src.len()]; - ffi::hpx_copy(src, &mut dest); - dest - } +pub fn copy_vector(src: &Vec) -> Vec { + let mut dest = vec![0; src.len()]; + ffi::hpx_copy(src, &mut dest); + dest +} - fn copy_vector_range(src: &Vec, start: usize, end: usize) -> Vec { - let slice = &src[start..end]; - let mut dest = vec![0; slice.len()]; - ffi::hpx_copy(&slice.to_vec(), &mut dest); - dest - } +pub fn copy_vector_range(src: &Vec, start: usize, end: usize) -> Vec { + let slice = &src[start..end]; + let mut dest = vec![0; slice.len()]; + ffi::hpx_copy(&slice.to_vec(), &mut dest); + dest +} - fn copy_n(src: &[i32], count: usize) -> Vec { - let mut dest = Vec::with_capacity(count); - ffi::hpx_copy_n(&src.to_vec(), count, &mut dest); - dest - } +pub fn copy_n(src: &[i32], count: usize) -> Vec { + let mut dest = Vec::with_capacity(count); + ffi::hpx_copy_n(&src.to_vec(), count, &mut dest); + dest +} - fn copy_if_positive(src: &Vec) -> Vec { - let mut dest = Vec::new(); - ffi::hpx_copy_if(src, &mut dest, |x| x % 3 == 0); - dest - } +pub fn copy_if_positive(src: &Vec) -> Vec { + let mut dest = Vec::new(); + ffi::hpx_copy_if(src, &mut dest, |x| x % 3 == 0); + dest +} - fn count(vec: &Vec, value: i32) -> i64 { - ffi::hpx_count(vec, value) - } +pub fn count(vec: &Vec, value: i32) -> i64 { + ffi::hpx_count(vec, value) +} - fn find(vec: &Vec, value: i32) -> Option { - match ffi::hpx_find(vec, value) { - -1 => None, - index => Some(index as usize), - } +pub fn find(vec: &Vec, value: i32) -> Option { + match ffi::hpx_find(vec, value) { + -1 => None, + index => Some(index as usize), } +} + +// ================================================================================================ +// Tests (to be shifted to systests crate within hpx-rs workspace) +// ================================================================================================ +#[cfg(test)] +mod tests { + use super::ffi; + use crate::{ + copy_if_positive, copy_n, copy_vector, copy_vector_range, count, create_c_args, find, + }; + use serial_test::serial; + use std::ffi::CString; + use std::os::raw::c_char; #[test] #[serial] From c7838510497102a5f502aaf038e10b11c5dfaa2a Mon Sep 17 00:00:00 2001 From: Dikshant Date: Mon, 19 Aug 2024 15:09:24 +0530 Subject: [PATCH 15/20] removed redundant copy from copy, sort Signed-off-by: Dikshant --- hpx-sys/include/wrapper.h | 25 ++++--------------------- hpx-sys/src/lib.rs | 19 +++++-------------- 2 files changed, 9 insertions(+), 35 deletions(-) diff --git a/hpx-sys/include/wrapper.h b/hpx-sys/include/wrapper.h index 42e2af1..8959b71 100644 --- a/hpx-sys/include/wrapper.h +++ b/hpx-sys/include/wrapper.h @@ -34,17 +34,8 @@ inline std::int32_t disconnect_with_timeout(double shutdown_timeout, double loca inline std::int32_t finalize() { return hpx::finalize(); } -inline void hpx_copy(const rust::Vec& src, rust::Vec& dest) { - std::vector cpp_src(src.begin(), src.end()); - std::vector cpp_dest(dest.size()); - - hpx::copy(hpx::execution::par, cpp_src.begin(), cpp_src.end(), cpp_dest.begin()); - - dest.clear(); - dest.reserve(cpp_dest.size()); - for (const auto& item : cpp_dest) { - dest.push_back(item); - } +inline void hpx_copy(rust::Slice src, rust::Slice dest) { + hpx::copy(hpx::execution::par, src.begin(), src.end(), dest.begin()); } inline void hpx_copy_n(const rust::Vec& src, size_t count, rust::Vec& dest) { @@ -137,16 +128,8 @@ inline int64_t hpx_find(const rust::Vec& src, int32_t value) { return -1; } -inline void hpx_sort(rust::Vec& src) { - std::vector cpp_vec(src.begin(), src.end()); - - hpx::sort(hpx::execution::par, cpp_vec.begin(), cpp_vec.end()); - - src.clear(); - src.reserve(cpp_vec.size()); - for (const auto& item : cpp_vec) { - src.push_back(item); - } +inline void hpx_sort(rust::Slice src) { + hpx::sort(hpx::execution::par, src.begin(), src.end()); } inline void hpx_sort_comp(rust::Vec& src, rust::Fn comp) { diff --git a/hpx-sys/src/lib.rs b/hpx-sys/src/lib.rs index 208ebb8..fd9991a 100644 --- a/hpx-sys/src/lib.rs +++ b/hpx-sys/src/lib.rs @@ -18,7 +18,7 @@ pub mod ffi { fn terminate(); fn disconnect() -> i32; fn disconnect_with_timeout(shutdown_timeout: f64, localwait: f64) -> i32; - fn hpx_copy(src: &Vec, dest: &mut Vec); + fn hpx_copy(src: &[i32], dest: &mut [i32]); fn hpx_copy_n(src: &Vec, count: usize, dest: &mut Vec); fn hpx_copy_if(src: &Vec, dest: &mut Vec, pred: fn(i32) -> bool); fn hpx_count(src: &Vec, value: i32) -> i64; @@ -27,7 +27,7 @@ pub mod ffi { fn hpx_equal(slice1: &[i32], slice2: &[i32]) -> bool; fn hpx_fill(src: &mut Vec, value: i32); // will only work for linear vectors fn hpx_find(src: &Vec, value: i32) -> i64; - fn hpx_sort(src: &mut Vec); + fn hpx_sort(src: &mut [i32]); fn hpx_sort_comp(src: &mut Vec, comp: fn(i32, i32) -> bool); fn hpx_merge(src1: &Vec, src2: &Vec, dest: &mut Vec); fn hpx_partial_sort(src: &mut Vec, last: usize); @@ -48,19 +48,12 @@ pub fn create_c_args(args: &[&str]) -> (i32, Vec<*mut c_char>) { (ptrs.len() as i32, ptrs) } -pub fn copy_vector(src: &Vec) -> Vec { +pub fn copy_vector(src: &[i32]) -> Vec { let mut dest = vec![0; src.len()]; ffi::hpx_copy(src, &mut dest); dest } -pub fn copy_vector_range(src: &Vec, start: usize, end: usize) -> Vec { - let slice = &src[start..end]; - let mut dest = vec![0; slice.len()]; - ffi::hpx_copy(&slice.to_vec(), &mut dest); - dest -} - pub fn copy_n(src: &[i32], count: usize) -> Vec { let mut dest = Vec::with_capacity(count); ffi::hpx_copy_n(&src.to_vec(), count, &mut dest); @@ -90,9 +83,7 @@ pub fn find(vec: &Vec, value: i32) -> Option { #[cfg(test)] mod tests { use super::ffi; - use crate::{ - copy_if_positive, copy_n, copy_vector, copy_vector_range, count, create_c_args, find, - }; + use crate::{copy_if_positive, copy_n, copy_vector, count, create_c_args, find}; use serial_test::serial; use std::ffi::CString; use std::os::raw::c_char; @@ -140,7 +131,7 @@ mod tests { let hpx_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 { let src = vec![1, 2, 3, 4, 5]; - let result = copy_vector_range(&src, 0, 3); + let result = copy_vector(&src[0..3]); assert_eq!(&src[0..3], &result); ffi::finalize() }; From e08516a182067d4d5d2659858c1bc3efb19ab19b Mon Sep 17 00:00:00 2001 From: Dikshant Date: Mon, 19 Aug 2024 16:14:30 +0530 Subject: [PATCH 16/20] removed redundant copy from copy_n Signed-off-by: Dikshant --- hpx-sys/include/wrapper.h | 13 ++----------- hpx-sys/src/lib.rs | 25 ++++++++++++++++++------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/hpx-sys/include/wrapper.h b/hpx-sys/include/wrapper.h index 8959b71..411a79f 100644 --- a/hpx-sys/include/wrapper.h +++ b/hpx-sys/include/wrapper.h @@ -38,17 +38,8 @@ inline void hpx_copy(rust::Slice src, rust::Slice dest) hpx::copy(hpx::execution::par, src.begin(), src.end(), dest.begin()); } -inline void hpx_copy_n(const rust::Vec& src, size_t count, rust::Vec& dest) { - std::vector cpp_src(src.begin(), src.end()); - std::vector cpp_dest(count); - - hpx::copy_n(hpx::execution::par, cpp_src.begin(), count, cpp_dest.begin()); - - dest.clear(); - dest.reserve(cpp_dest.size()); - for (const auto& item : cpp_dest) { - dest.push_back(item); - } +inline void hpx_copy_n(rust::Slice src, size_t count, rust::Slice dest) { + hpx::copy_n(hpx::execution::par, src.begin(), count, dest.begin()); } inline void hpx_copy_if(const rust::Vec& src, rust::Vec& dest, diff --git a/hpx-sys/src/lib.rs b/hpx-sys/src/lib.rs index fd9991a..a493597 100644 --- a/hpx-sys/src/lib.rs +++ b/hpx-sys/src/lib.rs @@ -19,7 +19,7 @@ pub mod ffi { fn disconnect() -> i32; fn disconnect_with_timeout(shutdown_timeout: f64, localwait: f64) -> i32; fn hpx_copy(src: &[i32], dest: &mut [i32]); - fn hpx_copy_n(src: &Vec, count: usize, dest: &mut Vec); + fn hpx_copy_n(src: &[i32], count: usize, dest: &mut [i32]); fn hpx_copy_if(src: &Vec, dest: &mut Vec, pred: fn(i32) -> bool); fn hpx_count(src: &Vec, value: i32) -> i64; fn hpx_count_if(src: &Vec, pred: fn(i32) -> bool) -> i64; @@ -54,10 +54,13 @@ pub fn copy_vector(src: &[i32]) -> Vec { dest } -pub fn copy_n(src: &[i32], count: usize) -> Vec { - let mut dest = Vec::with_capacity(count); - ffi::hpx_copy_n(&src.to_vec(), count, &mut dest); - dest +pub fn copy_n(src: &[i32], count: usize) -> Result, &'static str> { + if count > src.len() { + return Err("count larger than source slice length"); + } + let mut dest = vec![0; count]; + ffi::hpx_copy_n(src, count, &mut dest); + Ok(dest) } pub fn copy_if_positive(src: &Vec) -> Vec { @@ -149,8 +152,16 @@ mod tests { let test_func = |_argc: i32, _argv: *mut *mut c_char| -> i32 { let src = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; - let result = copy_n(&src, 5); - assert_eq!(result, vec![1, 2, 3, 4, 5]); + match copy_n(&src, 5) { + Ok(result) => assert_eq!(result, vec![1, 2, 3, 4, 5]), + Err(e) => panic!("Unexpected error: {}", e), + } + + match copy_n(&src, 15) { + // expecting error + Ok(_) => panic!("Expected error, but got Ok"), + Err(e) => assert_eq!(e, "count larger than source slice length"), + } ffi::finalize() }; From eea15699b721a45ceddbd2b8c4bb84dc8a54c244 Mon Sep 17 00:00:00 2001 From: Dikshant Date: Tue, 20 Aug 2024 12:09:31 +0530 Subject: [PATCH 17/20] removed redundant copy from find, fill, sort_comp Signed-off-by: Dikshant --- hpx-sys/include/wrapper.h | 34 ++++++++-------------------------- hpx-sys/src/lib.rs | 12 ++++++++---- 2 files changed, 16 insertions(+), 30 deletions(-) diff --git a/hpx-sys/include/wrapper.h b/hpx-sys/include/wrapper.h index 411a79f..35f8e92 100644 --- a/hpx-sys/include/wrapper.h +++ b/hpx-sys/include/wrapper.h @@ -93,28 +93,18 @@ inline bool hpx_equal(rust::Slice src, rust::Slice ); } -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); - } +inline void hpx_fill(rust::Slice src, int32_t value) { + hpx::fill(hpx::execution::par, src.begin(), src.end(), value); } -inline int64_t hpx_find(const rust::Vec& src, int32_t value) { - std::vector cpp_vec(src.begin(), src.end()); - +inline int64_t hpx_find(rust::Slice src, int32_t value) { auto result = hpx::find(hpx::execution::par, - cpp_vec.begin(), - cpp_vec.end(), + src.begin(), + src.end(), value); - if (result != cpp_vec.end()) { - return static_cast(std::distance(cpp_vec.begin(), result)); + if (result != src.end()) { + return static_cast(std::distance(src.begin(), result)); } return -1; } @@ -124,16 +114,8 @@ inline void hpx_sort(rust::Slice src) { } inline void hpx_sort_comp(rust::Vec& src, rust::Fn comp) { - std::vector cpp_vec(src.begin(), src.end()); - - hpx::sort(hpx::execution::par, cpp_vec.begin(), cpp_vec.end(), + hpx::sort(hpx::execution::par, src.begin(), src.end(), [&](int32_t a, int32_t b) { return comp(a, b); }); - - src.clear(); - src.reserve(cpp_vec.size()); - for (const auto& item : cpp_vec) { - src.push_back(item); - } } inline void hpx_merge(const rust::Vec& src1, diff --git a/hpx-sys/src/lib.rs b/hpx-sys/src/lib.rs index a493597..59e45b4 100644 --- a/hpx-sys/src/lib.rs +++ b/hpx-sys/src/lib.rs @@ -25,8 +25,8 @@ pub mod ffi { fn hpx_count_if(src: &Vec, pred: fn(i32) -> bool) -> i64; fn hpx_ends_with(src: &[i32], dest: &[i32]) -> bool; fn hpx_equal(slice1: &[i32], slice2: &[i32]) -> bool; - fn hpx_fill(src: &mut Vec, value: i32); // will only work for linear vectors - fn hpx_find(src: &Vec, value: i32) -> i64; + fn hpx_fill(src: &mut [i32], value: i32); // will only work for linear vectors + fn hpx_find(src: &[i32], value: i32) -> i64; fn hpx_sort(src: &mut [i32]); fn hpx_sort_comp(src: &mut Vec, comp: fn(i32, i32) -> bool); fn hpx_merge(src1: &Vec, src2: &Vec, dest: &mut Vec); @@ -73,8 +73,8 @@ pub fn count(vec: &Vec, value: i32) -> i64 { ffi::hpx_count(vec, value) } -pub fn find(vec: &Vec, value: i32) -> Option { - match ffi::hpx_find(vec, value) { +pub fn find(slice: &[i32], value: i32) -> Option { + match ffi::hpx_find(slice, value) { -1 => None, index => Some(index as usize), } @@ -325,6 +325,10 @@ mod tests { ffi::hpx_fill(&mut v2, 7); assert!(v2.iter().all(|&x| x == 7)); + let mut v3: Vec = Vec::new(); + ffi::hpx_fill(&mut v3, 100); + assert!(v3.is_empty()); + ffi::finalize() }; From 81cfce787441fd3b894b4b4e80234f86023b28c9 Mon Sep 17 00:00:00 2001 From: Dikshant Date: Tue, 20 Aug 2024 12:46:22 +0530 Subject: [PATCH 18/20] removed redundant copy from merge, partial sort Signed-off-by: Dikshant --- hpx-sys/include/wrapper.h | 59 ++++++++++++++++----------------------- hpx-sys/src/lib.rs | 8 +++++- 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/hpx-sys/include/wrapper.h b/hpx-sys/include/wrapper.h index 35f8e92..715fad7 100644 --- a/hpx-sys/include/wrapper.h +++ b/hpx-sys/include/wrapper.h @@ -118,53 +118,42 @@ inline void hpx_sort_comp(rust::Vec& src, rust::Fn& src1, - const rust::Vec& src2, - rust::Vec& dest) { - std::vector cpp_src1(src1.begin(), src1.end()); - std::vector cpp_src2(src2.begin(), src2.end()); - std::vector cpp_dest(cpp_src1.size() + cpp_src2.size()); - - hpx::merge(hpx::execution::par, - cpp_src1.begin(), cpp_src1.end(), - cpp_src2.begin(), cpp_src2.end(), - cpp_dest.begin()); - +inline void hpx_merge(rust::Slice src1, + rust::Slice src2, + rust::Vec& dest) { dest.clear(); - dest.reserve(cpp_dest.size()); - for (const auto& item : cpp_dest) { - dest.push_back(item); + dest.reserve(src1.size() + src2.size()); + + for (size_t i = 0; i < src1.size() + src2.size(); ++i) { + dest.push_back(0); } + + hpx::merge(hpx::execution::par, + src1.begin(), src1.end(), + src2.begin(), src2.end(), + dest.begin()); } inline void hpx_partial_sort(rust::Vec& src, size_t last) { - std::vector cpp_vec(src.begin(), src.end()); + if (last > src.size()) { + last = src.size(); + } hpx::partial_sort(hpx::execution::par, - cpp_vec.begin(), - cpp_vec.begin() + last, - cpp_vec.end()); - - src.clear(); - src.reserve(cpp_vec.size()); - for (const auto& item : cpp_vec) { - src.push_back(item); - } + src.begin(), + src.begin() + last, + src.end()); } inline void hpx_partial_sort_comp(rust::Vec& src, size_t last, rust::Fn comp) { - std::vector cpp_vec(src.begin(), src.end()); + if (last > src.size()) { + last = src.size(); + } hpx::partial_sort(hpx::execution::par, - cpp_vec.begin(), - cpp_vec.begin() + last, - cpp_vec.end(), + src.begin(), + src.begin() + last, + src.end(), [&](int32_t a, int32_t b) { return comp(a, b); }); - - 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 59e45b4..90fee63 100644 --- a/hpx-sys/src/lib.rs +++ b/hpx-sys/src/lib.rs @@ -29,7 +29,7 @@ pub mod ffi { fn hpx_find(src: &[i32], value: i32) -> i64; fn hpx_sort(src: &mut [i32]); fn hpx_sort_comp(src: &mut Vec, comp: fn(i32, i32) -> bool); - fn hpx_merge(src1: &Vec, src2: &Vec, dest: &mut Vec); + fn hpx_merge(src1: &[i32], src2: &[i32], dest: &mut Vec); fn hpx_partial_sort(src: &mut Vec, last: usize); fn hpx_partial_sort_comp(src: &mut Vec, last: usize, comp: fn(i32, i32) -> bool); } @@ -80,6 +80,12 @@ pub fn find(slice: &[i32], value: i32) -> Option { } } +pub fn merge(src1: &[i32], src2: &[i32]) -> Vec { + let mut dest = Vec::new(); + ffi::hpx_merge(src1, src2, &mut dest); + dest +} + // ================================================================================================ // Tests (to be shifted to systests crate within hpx-rs workspace) // ================================================================================================ From 102982a3b4910f419fb8403722b7e4f861dbf7af Mon Sep 17 00:00:00 2001 From: Dikshant Date: Tue, 20 Aug 2024 13:01:53 +0530 Subject: [PATCH 19/20] removed redundant copy from copy_if Signed-off-by: Dikshant --- hpx-sys/include/wrapper.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/hpx-sys/include/wrapper.h b/hpx-sys/include/wrapper.h index 715fad7..6889219 100644 --- a/hpx-sys/include/wrapper.h +++ b/hpx-sys/include/wrapper.h @@ -44,11 +44,10 @@ inline void hpx_copy_n(rust::Slice src, size_t count, rust::Slice inline void hpx_copy_if(const rust::Vec& src, rust::Vec& dest, rust::Fn pred) { - std::vector cpp_src(src.begin(), src.end()); - std::vector cpp_dest(cpp_src.size()); + std::vector cpp_dest(src.size()); auto result = hpx::copy_if(hpx::execution::par, - cpp_src.begin(), cpp_src.end(), + src.begin(), src.end(), cpp_dest.begin(), [&](int32_t value) { return pred(value); }); From 62f2a8ff7e9d1070afa3a21c81ced37470e9de14 Mon Sep 17 00:00:00 2001 From: Dikshant Date: Tue, 20 Aug 2024 14:03:28 +0530 Subject: [PATCH 20/20] rerun tests Signed-off-by: Dikshant --- hpx-sys/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hpx-sys/src/lib.rs b/hpx-sys/src/lib.rs index 90fee63..c6ecc48 100644 --- a/hpx-sys/src/lib.rs +++ b/hpx-sys/src/lib.rs @@ -63,7 +63,7 @@ pub fn copy_n(src: &[i32], count: usize) -> Result, &'static str> { Ok(dest) } -pub fn copy_if_positive(src: &Vec) -> Vec { +pub fn copy_if_divisiblileityby3(src: &Vec) -> Vec { let mut dest = Vec::new(); ffi::hpx_copy_if(src, &mut dest, |x| x % 3 == 0); dest @@ -92,7 +92,7 @@ pub fn merge(src1: &[i32], src2: &[i32]) -> Vec { #[cfg(test)] mod tests { use super::ffi; - use crate::{copy_if_positive, copy_n, copy_vector, count, create_c_args, find}; + use crate::{copy_if_divisiblileityby3, copy_n, copy_vector, count, create_c_args, find}; use serial_test::serial; use std::ffi::CString; use std::os::raw::c_char; @@ -184,7 +184,7 @@ mod tests { let hpx_main = |_argc: i32, _argv: *mut *mut c_char| -> i32 { let src = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; - let result = copy_if_positive(&src); + let result = copy_if_divisiblileityby3(&src); assert_eq!(result, vec![0, 3, 6, 9, 12]); ffi::finalize() };