Skip to content

Commit

Permalink
Merge pull request #23 from Clemapfel/no_c_adapter
Browse files Browse the repository at this point in the history
Make c_adapter redundant
  • Loading branch information
Clemapfel authored Jun 23, 2022
2 parents 5768f4b + 036b92e commit 0d9dc1d
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 199 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/libjluna.so
/libjluna_c_adapter.so
/.src/include_julia.inl
/docs/bckp.txt
/build
/.vscode
/.benchmark/results
/.idea/
/jluna_c_adapter.lib/
/jluna_c_adapter.so/
/libjluna.so
/libjluna_c_adapter.so
106 changes: 50 additions & 56 deletions .src/c_adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,71 +8,65 @@

#include <.src/c_adapter.hpp>

extern "C"
jluna::unsafe::Value* jluna_make(void* function_ptr, int n_args)
{
namespace jluna::c_adapter
{

unsafe::Value* make(void* function_ptr, int n_args)
{
gc_pause;
static auto* make = (jl_function_t*) jl_eval_string("return jluna.cppcall.make_unnamed_function");
auto* res = jluna::safe_call(make, jl_box_voidpointer(function_ptr), jl_box_int64(n_args));
gc_unpause;
return res;
}
gc_pause;
static auto* make = (jl_function_t*) jl_eval_string("return jluna.cppcall.make_unnamed_function");
auto* res = jluna::safe_call(make, jl_box_voidpointer(function_ptr), jl_box_int64(n_args));
gc_unpause;
return res;
}

void free_lambda(void* in, int n_args)
{
if (n_args == 0)
delete (lambda_0_arg*) in;
else if (n_args == 1)
delete (lambda_1_arg*) in;
else if (n_args == 2)
delete (lambda_2_arg*) in;
else if (n_args == 3)
delete (lambda_3_arg*) in;
else
throw std::invalid_argument("In c_adapter::free: " + std::to_string(n_args) + " is a invalid number of arguments");
}
void jluna_free_lambda(void* in, int n_args)
{
if (n_args == 0)
delete (jluna::detail::lambda_0_arg*) in;
else if (n_args == 1)
delete (jluna::detail::lambda_1_arg*) in;
else if (n_args == 2)
delete (jluna::detail::lambda_2_arg*) in;
else if (n_args == 3)
delete (jluna::detail::lambda_3_arg*) in;
else
throw std::invalid_argument("In c_adapter::jluna_free_lambda: " + std::to_string(n_args) + " is a invalid number of arguments");
}

unsafe::Value* invoke_lambda_0(void* function_ptr)
{
return (*reinterpret_cast<lambda_0_arg*>(function_ptr))();
}
jluna::unsafe::Value* jluna_invoke_lambda_0(void* function_ptr)
{
return (*reinterpret_cast<jluna::detail::lambda_0_arg*>(function_ptr))();
}

unsafe::Value* invoke_lambda_1(void* function_ptr, unsafe::Value* x)
{
return (*reinterpret_cast<lambda_1_arg*>(function_ptr))(x);
}
jluna::unsafe::Value* jluna_invoke_lambda_1(void* function_ptr, jluna::unsafe::Value* x)
{
return (*reinterpret_cast<jluna::detail::lambda_1_arg*>(function_ptr))(x);
}

unsafe::Value* invoke_lambda_2(void* function_ptr, unsafe::Value* x, unsafe::Value* y)
{
return (*reinterpret_cast<lambda_2_arg*>(function_ptr))(x, y);
}
jluna::unsafe::Value* jluna_invoke_lambda_2(void* function_ptr, jluna::unsafe::Value* x, jluna::unsafe::Value* y)
{
return (*reinterpret_cast<jluna::detail::lambda_2_arg*>(function_ptr))(x, y);
}

unsafe::Value* invoke_lambda_3(void* function_ptr, unsafe::Value* x, unsafe::Value* y, unsafe::Value* z)
{
return (*reinterpret_cast<lambda_3_arg*>(function_ptr))(x, y, z);
}
jluna::unsafe::Value* jluna_invoke_lambda_3(void* function_ptr, jluna::unsafe::Value* x, jluna::unsafe::Value* y, jluna::unsafe::Value* z)
{
return (*reinterpret_cast<jluna::detail::lambda_3_arg*>(function_ptr))(x, y, z);
}

void* to_pointer(jl_value_t* in)
{
return (void*) in;
}
void* jluna_to_pointer(jl_value_t* in)
{
return (void*) in;
}

size_t invoke_from_task(size_t function_ptr)
{
return reinterpret_cast<size_t>(
(*reinterpret_cast<std::function<unsafe::Value*()>*>(function_ptr))()
);
}
size_t jluna_invoke_from_task(size_t function_ptr)
{
return reinterpret_cast<size_t>(
(*reinterpret_cast<std::function<jluna::unsafe::Value*()>*>(function_ptr))()
);
}

bool verify()
{
return true;
}
}
bool jluna_verify()
{
return true;
}


#endif
81 changes: 33 additions & 48 deletions .src/c_adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,46 @@

#include <stddef.h>

#ifdef __cplusplus

#include <include/julia_wrapper.hpp>
#include <functional>
#include <string>

extern "C"
{
namespace jluna::c_adapter
namespace jluna::detail
{
using lambda_0_arg = std::function<jl_value_t*()>;
using lambda_1_arg = std::function<jl_value_t*(jl_value_t*)>;
using lambda_2_arg = std::function<jl_value_t*(jl_value_t*, jl_value_t*)>;
using lambda_3_arg = std::function<jl_value_t*(jl_value_t*, jl_value_t*, jl_value_t*)>;
using lambda_0_arg = std::function<jl_value_t *()>;
using lambda_1_arg = std::function<jl_value_t *(jl_value_t *)>;
using lambda_2_arg = std::function<jl_value_t *(jl_value_t *, jl_value_t *)>;
using lambda_3_arg = std::function<jl_value_t *(jl_value_t *, jl_value_t *, jl_value_t *)>;
//using lambda_n_arg = std::function<jl_value_t*(const std::vector<jl_value_t*>&)>;

/// @brief construct an UnnamedFunctionProxy object
/// @param function_ptr: allocated with `new`
/// @param n_args: number of arguments: 0, 1, 2, 3 or -1
/// @returns ptr to UnnamedFunctionProxy object
jl_value_t* make(void* function_ptr, int n_args);

jl_value_t* invoke_lambda_0(void* function_ptr);
jl_value_t* invoke_lambda_1(void* function_ptr, jl_value_t*);
jl_value_t* invoke_lambda_2(void* function_ptr, jl_value_t*, jl_value_t*);
jl_value_t* invoke_lambda_3(void* function_ptr, jl_value_t*, jl_value_t*, jl_value_t*);
//jl_value_t* invoke_lambda_n(void* function_ptr, unsafe::Array* vector);

/// @brief `delete` a function pointer held
/// @param pointer to function
/// @param n_args: 0, 1, 2, 3 or -1
void free_lambda(void* function_ptr, int n_args);

/// @brief get pointer to arbitrary object
void* to_pointer(jl_value_t*);

/// @brief invoke function ptr, used within threadpool
/// @param function_pointer
/// @returns result pointer
size_t invoke_from_task(size_t function_ptr);

/// @brief verify c_adapter is working, used for test
/// @returns true
bool verify();
}
}

#else // exposed to juila as pure C header:

void free_lambda(void*, int);
jl_value_t* invoke_lambda_0(void*);
jl_value_t* invoke_lambda_1(void*, jl_value_t*);
jl_value_t* invoke_lambda_2(void*, jl_value_t*, jl_value_t*);
jl_value_t* invoke_lambda_3(void*, jl_value_t*, jl_value_t*, jl_value_t*);
void* to_pointer(jl_value_t*);
bool verify();

#endif
/// @brief construct an UnnamedFunctionProxy object
/// @param function_ptr: allocated with `new`
/// @param n_args: number of arguments: 0, 1, 2, 3 or -1
/// @returns ptr to UnnamedFunctionProxy object
jl_value_t* jluna_make(void* function_ptr, int n_args);

jl_value_t* jluna_invoke_lambda_0(void* function_ptr);
jl_value_t* jluna_invoke_lambda_1(void* function_ptr, jl_value_t*);
jl_value_t* jluna_invoke_lambda_2(void* function_ptr, jl_value_t*, jl_value_t*);
jl_value_t* jluna_invoke_lambda_3(void* function_ptr, jl_value_t*, jl_value_t*, jl_value_t*);

/// @brief `delete` a function pointer held
/// @param pointer to function
/// @param n_args: 0, 1, 2, 3 or -1
void jluna_free_lambda(void* function_ptr, int n_args);

/// @brief get pointer to arbitrary object
void* jluna_to_pointer(jl_value_t*);

/// @brief invoke function ptr, used within threadpool
/// @param function_pointer
/// @returns result pointer
size_t jluna_invoke_from_task(size_t function_ptr);

/// @brief verify c_adapter is working, used for test
/// @returns true
bool jluna_verify();
}
16 changes: 8 additions & 8 deletions .src/cppcall.inl
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,45 @@ namespace jluna
template<typename Return_t>
unsafe::Value* register_function(std::function<Return_t()> f)
{
auto* out = new c_adapter::lambda_0_arg([f]() -> unsafe::Value*
auto* out = new detail::lambda_0_arg([f]() -> unsafe::Value*
{
return box_function_result(f);
});

return c_adapter::make(out, 0);
return jluna_make(out, 0);
}

template<typename Return_t, typename Arg1_t>
unsafe::Value* register_function(std::function<Return_t(Arg1_t)> f)
{
auto* out = new c_adapter::lambda_1_arg([f](unsafe::Value* arg1)
auto* out = new detail::lambda_1_arg([f](unsafe::Value* arg1)
{
return box_function_result(f, unbox<Arg1_t>(arg1));
});

return c_adapter::make(out, 1);
return jluna_make(out, 1);
}

template<typename Return_t, typename Arg1_t, typename Arg2_t>
unsafe::Value* register_function(std::function<Return_t(Arg1_t, Arg2_t)> f)
{
auto* out = new c_adapter::lambda_2_arg([f](unsafe::Value* arg1, unsafe::Value* arg2)
auto* out = new detail::lambda_2_arg([f](unsafe::Value* arg1, unsafe::Value* arg2)
{
return box_function_result(f, unbox<Arg1_t>(arg1), unbox<Arg2_t>(arg2));
});

return c_adapter::make(out, 2);
return jluna_make(out, 2);
}

template<typename Return_t, typename Arg1_t, typename Arg2_t, typename Arg3_t>
unsafe::Value* register_function(std::function<Return_t(Arg1_t, Arg2_t, Arg3_t)> f)
{
auto* out = new c_adapter::lambda_3_arg([f](unsafe::Value* arg1, unsafe::Value* arg2, unsafe::Value* arg3)
auto* out = new detail::lambda_3_arg([f](unsafe::Value* arg1, unsafe::Value* arg2, unsafe::Value* arg3)
{
return box_function_result(f, unbox<Arg1_t>(arg1), unbox<Arg2_t>(arg2), unbox<Arg3_t>(arg3));
});

return c_adapter::make(out, 3);
return jluna_make(out, 3);
}

template<typename Function_t, typename _>
Expand Down
4 changes: 1 addition & 3 deletions .src/include_julia.inl.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

namespace jluna::detail
{
static inline const char* c_adapter_path = "@C_ADAPTER_NAME@";
static inline std::string c_adapter_path_override = "";

static inline const char* shared_library_name = "@SHARED_LIBRARY_NAME@";
static inline const char* julia_source = R"(@JLUNA_JULIA_SOURCE@)";
}
27 changes: 11 additions & 16 deletions .src/safe_utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,12 @@ namespace jluna
static inline std::mutex initialize_lock = std::mutex();
}

void set_c_adapter_path(const std::string& path)
{
jluna::detail::c_adapter_path_override = path;
}

void initialize(size_t n_threads, bool suppress_log)
{
initialize("", n_threads, suppress_log);
}

void initialize(const std::string& path, size_t n_threads, bool suppress_log)
void initialize(
size_t n_threads,
bool suppress_log,
const std::string& jluna_shared_library_path,
const std::string& julia_image_path
)
{
static bool is_initialized = false;

Expand All @@ -50,10 +45,10 @@ namespace jluna
#endif

detail::_num_threads = n_threads;
if (path.empty())
if (julia_image_path.empty())
jl_init();
else
jl_init_with_image(path.c_str(), nullptr);
jl_init_with_image(julia_image_path.c_str(), nullptr);

forward_last_exception();

Expand All @@ -74,9 +69,9 @@ namespace jluna
forward_last_exception();

std::stringstream str;
str << "jluna.cppcall.eval(:(const _c_adapter_path = \"";
str << (jluna::detail::c_adapter_path_override.empty() ? jluna::detail::c_adapter_path : jluna::detail::c_adapter_path_override);
str << "\"))";
str << "jluna.cppcall.eval(:(const _lib = \""
<< (jluna_shared_library_path == "" ? jluna::detail::shared_library_name : jluna_shared_library_path)
<< "\"))";

jl_eval_string(str.str().c_str());
forward_last_exception();
Expand Down
3 changes: 2 additions & 1 deletion .test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ int main()

Test::test("c_adapter found", [](){

Test::assert_that(jl_unbox_bool(jl_eval_string("return jluna.cppcall.verify_library()")));
auto a = safe_eval("return jluna.cppcall.verify_library()");
Test::assert_that(jl_unbox_bool(a));
});

Test::test("unsafe: gc_push / gc_pop", [](){
Expand Down
Loading

0 comments on commit 0d9dc1d

Please sign in to comment.