Skip to content
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

bindings for hpx/algorithm.hpp #7

Merged
merged 20 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 77 additions & 12 deletions hpx-sys/include/wrapper.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
#pragma once

#include <hpx/hpx_init.hpp>
#include <hpx/algorithm.hpp>
#include <iostream>
#include <cstdint>
#include <vector>

#include "rust/cxx.h"


/*inline std::int32_t start() { return hpx::start(nullptr, 0, nullptr); }*/

/*inline std::int32_t start(rust::Fn<int(int, char **)> 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<int(int, char **)> rust_fn, int argc, char **argv) {
return hpx::init(
[&](int argc, char **argv) {
Expand All @@ -44,4 +34,79 @@ 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<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());
pingu-73 marked this conversation as resolved.
Show resolved Hide resolved

dest.clear();
dest.reserve(cpp_dest.size());
for (const auto& item : cpp_dest) {
dest.push_back(item);
}
pingu-73 marked this conversation as resolved.
Show resolved Hide resolved
}

inline void hpx_copy_n(const rust::Vec<int32_t>& src, size_t count, rust::Vec<int32_t>& dest) {
std::vector<int32_t> cpp_src(src.begin(), src.end());
std::vector<int32_t> 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);
}
}
pingu-73 marked this conversation as resolved.
Show resolved Hide resolved

inline void hpx_copy_if(const rust::Vec<int32_t>& src, rust::Vec<int32_t>& dest,
rust::Fn<bool(int32_t)> pred) {
std::vector<int32_t> cpp_src(src.begin(), src.end());
std::vector<int32_t> 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);
}
}

inline std::int64_t hpx_count(const rust::Vec<int32_t>& vec, int32_t value) {
return hpx::count(hpx::execution::par, vec.begin(), vec.end(), value);
}


inline int64_t hpx_count_if(const rust::Vec<int32_t>& vec, rust::Fn<bool(int32_t)> pred) {
std::vector<int32_t> 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<int64_t>(result);
}

inline bool hpx_ends_with(rust::Slice<const int32_t> src,
rust::Slice<const int32_t> dest) {
return hpx::ends_with(hpx::execution::par,
src.begin(), src.end(),
dest.begin(), dest.end(),
std::equal_to<int32_t>());
}

inline bool hpx_equal(rust::Slice<const int32_t> src, rust::Slice<const int32_t> dest) {
return hpx::equal(
hpx::execution::par,
src.begin(), src.end(),
dest.begin(), dest.end()
);
}
233 changes: 231 additions & 2 deletions hpx-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -19,7 +18,13 @@ 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<i32>, dest: &mut Vec<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(vec: &Vec<i32>, value: i32) -> i64;
fn hpx_count_if(vec: &Vec<i32>, pred: fn(i32) -> bool) -> i64;
SAtacker marked this conversation as resolved.
Show resolved Hide resolved
fn hpx_ends_with(src: &[i32], dest: &[i32]) -> bool;
fn hpx_equal(slice1: &[i32], slice2: &[i32]) -> bool;
}
}

Expand All @@ -41,7 +46,37 @@ mod tests {
(ptrs.len() as i32, ptrs)
}

fn copy_vector(src: &Vec<i32>) -> Vec<i32> {
let mut dest = vec![0; src.len()];
ffi::hpx_copy(src, &mut dest);
dest
}

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
}

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);
dest
}

fn copy_if_positive(src: &Vec<i32>) -> Vec<i32> {
let mut dest = Vec::new();
ffi::hpx_copy_if(src, &mut dest, |x| x % 3 == 0);
dest
}

fn count(vec: &Vec<i32>, value: i32) -> i64 {
ffi::hpx_count(vec, value)
}

#[test]
#[serial]
fn test_init_finalize() {
let (argc, mut argv) = create_c_args(&["testing", "arg1", "arg2"]);

Expand All @@ -57,4 +92,198 @@ mod tests {
assert_eq!(result, 0);
}
}

#[test]
#[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);
}
}

#[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(hpx_main, argc, argv.as_mut_ptr());
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);
}
}

#[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);
}
}

#[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);
}
}

#[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 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<i32> = 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()
};

unsafe {
let result = ffi::init(hpx_main, argc, argv.as_mut_ptr());
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);
}
}
}
Loading