Skip to content

Commit

Permalink
removed redundant copy from copy, sort
Browse files Browse the repository at this point in the history
Signed-off-by: Dikshant <[email protected]>
  • Loading branch information
pingu-73 committed Aug 19, 2024
1 parent fa1e347 commit c783851
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 35 deletions.
25 changes: 4 additions & 21 deletions hpx-sys/include/wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int32_t>& src, rust::Vec<int32_t>& dest) {
std::vector<int32_t> cpp_src(src.begin(), src.end());
std::vector<int32_t> 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<const int32_t> src, rust::Slice<int32_t> dest) {
hpx::copy(hpx::execution::par, src.begin(), src.end(), dest.begin());
}

inline void hpx_copy_n(const rust::Vec<int32_t>& src, size_t count, rust::Vec<int32_t>& dest) {
Expand Down Expand Up @@ -137,16 +128,8 @@ inline int64_t hpx_find(const rust::Vec<int32_t>& src, int32_t value) {
return -1;
}

inline void hpx_sort(rust::Vec<int32_t>& src) {
std::vector<int32_t> 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<int32_t> src) {
hpx::sort(hpx::execution::par, src.begin(), src.end());
}

inline void hpx_sort_comp(rust::Vec<int32_t>& src, rust::Fn<bool(int32_t, int32_t)> comp) {
Expand Down
19 changes: 5 additions & 14 deletions hpx-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32>, dest: &mut Vec<i32>);
fn hpx_copy(src: &[i32], dest: &mut [i32]);
fn hpx_copy_n(src: &Vec<i32>, count: usize, dest: &mut Vec<i32>);
fn hpx_copy_if(src: &Vec<i32>, dest: &mut Vec<i32>, pred: fn(i32) -> bool);
fn hpx_count(src: &Vec<i32>, value: i32) -> i64;
Expand All @@ -27,7 +27,7 @@ pub mod ffi {
fn hpx_equal(slice1: &[i32], slice2: &[i32]) -> bool;
fn hpx_fill(src: &mut Vec<i32>, value: i32); // will only work for linear vectors
fn hpx_find(src: &Vec<i32>, value: i32) -> i64;
fn hpx_sort(src: &mut Vec<i32>);
fn hpx_sort(src: &mut [i32]);
fn hpx_sort_comp(src: &mut Vec<i32>, comp: fn(i32, i32) -> bool);
fn hpx_merge(src1: &Vec<i32>, src2: &Vec<i32>, dest: &mut Vec<i32>);
fn hpx_partial_sort(src: &mut Vec<i32>, last: usize);
Expand All @@ -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<i32>) -> Vec<i32> {
pub fn copy_vector(src: &[i32]) -> Vec<i32> {
let mut dest = vec![0; src.len()];
ffi::hpx_copy(src, &mut dest);
dest
}

pub fn copy_vector_range(src: &Vec<i32>, start: usize, end: usize) -> Vec<i32> {
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<i32> {
let mut dest = Vec::with_capacity(count);
ffi::hpx_copy_n(&src.to_vec(), count, &mut dest);
Expand Down Expand Up @@ -90,9 +83,7 @@ pub fn find(vec: &Vec<i32>, value: i32) -> Option<usize> {
#[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;
Expand Down Expand Up @@ -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()
};
Expand Down

0 comments on commit c783851

Please sign in to comment.