Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add an example of passing an array from C to Julia.
  • Loading branch information
GunnarFarneback committed Dec 14, 2020
commit 75bc6fe566e07b6635685f2d0ee77dad931bd68d
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DynamicallyLoadedEmbedding"
uuid = "bb81f446-86f5-47d0-8ffc-7ac2adc264fd"
authors = ["Gunnar Farnebäck <gunnar.farneback@contextvision.se>"]
version = "0.1.0"
version = "0.1.1"

[deps]

Expand Down
8 changes: 8 additions & 0 deletions c/julia_cfunctions.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,13 @@ int load_julia_cfunctions()
if (!mutual_fibonacci)
return 0;

add_element_number_int = get_cfunction_pointer("add_element_number_int");
if (!add_element_number_int)
return 0;

add_element_number_float = get_cfunction_pointer("add_element_number_float");
if (!add_element_number_float)
return 0;

return 1;
}
2 changes: 2 additions & 0 deletions c/julia_cfunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
double (*julia_sind)(double x);
int (*mutual_fibonacci)(int n, int (*callback)(int n));
void (*add_element_number_int)(int *x, int n);
void (*add_element_number_float)(float *x, int n);

/* Loads the function pointers above. */
int load_julia_cfunctions(void);
Expand Down
18 changes: 18 additions & 0 deletions c/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ int main(int argc, const char **argv)

printf("fibonacci(10) = %d\n", fibonacci(10));

int xi[4] = {1, 1, 1, 0};
float xf[3] = {0.0, 0.5, 1.0};

add_element_number_int(xi, 4);
add_element_number_float(xf, 3);

printf("xi =");
for (int i = 0; i < 4; i++) {
printf(" %d", xi[i]);
}
printf("\n");

printf("xf =");
for (int i = 0; i < 3; i++) {
printf(" %3.1f", xf[i]);
}
printf("\n");

/* Test that exceptions are handled gracefully. */
julia_eval_string("function this_function_has_no_methods end; this_function_has_no_methods()", "Exception test: ");
fflush(stdout);
Expand Down
17 changes: 17 additions & 0 deletions src/julia_cfunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,20 @@ function mutual_fibonacci(n::Cint, callback::Ptr{Cvoid})::Cint
end

const mutual_fibonacci_ptr = @cfunction(mutual_fibonacci, Cint, (Cint, Ptr{Cvoid}))

# Example of receiving an array of data. This function adds 1 to the
# first element, 2 to the second, and so on.
function add_element_number(x::Ptr, n::Integer)
for i = 1:n
element = unsafe_load(x, i)
unsafe_store!(x, element + i, i)
end
return
end

# @cfunction can instantiate pointers to multiple specializations of
# the function for different argument types.
const add_element_number_int = @cfunction(add_element_number, Cvoid,
(Ptr{Cint}, Cint))
const add_element_number_float = @cfunction(add_element_number, Cvoid,
(Ptr{Cfloat}, Cint))
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ expected_stdout =
Julia version $(version_string)
0.5
fibonacci(10) = 55
xi = 2 3 4 4
xf = 1.0 2.5 4.0
"""

expected_stderr =
Expand Down