Passing a C struct into Julia

Finally,
it worked with the jl_new_struct.
I am pasting here code.

The code in Julia is:

module MyModule

struct the_numbers
       the_integers::Vector{Cint}
       the_integers_2::Vector{Cint}
       the_doubles::Vector{Cdouble}
end

struct test_struct_1
       a::Cint
       my_nums::the_numbers
end

function print_nums(my_struct::the_numbers)::Cdouble
	return my_struct.the_doubles[1] + my_struct.the_doubles[2];
end

function quick_sum_struct(my_struct::test_struct_1)::Cdouble
	return my_struct.my_nums.the_doubles[1] + my_struct.my_nums.the_doubles[2];

end

if abspath(PROGRAM_FILE) == @__FILE__
	my_numbers = the_numbers([1,2,3],[4,5,6],[7.7,8.8,9.9])

	res_0 = print_nums(my_numbers)
	println("Res_0 is: ",  res_0)

	my_n_struct = test_struct_1(3, my_numbers)
	
	res = quick_sum_struct(my_n_struct)
	println("Res is: ",  res)
end

end

and the C++ code:

#include <iostream>
#include <string>

#include <unistd.h>

#include <julia.h>

JULIA_DEFINE_FAST_TLS()

typedef struct
{
   int* the_integers;
   int* the_integers_2;
   double* the_doubles;
}the_numbers;

typedef struct 
{
	int a;
	the_numbers my_nums;
}test_struct_1;

void fill_my_struct(test_struct_1* my_struct)
{
	

	const size_t array_size=5000*5000;
        my_struct->a = array_size;
	my_struct->my_nums.the_integers = new int[array_size];
	my_struct->my_nums.the_integers_2 = new int[array_size];
	my_struct->my_nums.the_doubles = new double[array_size];

	for(size_t i=0; i<array_size; i++)
	{
		my_struct->my_nums.the_integers[i] = 2000;
		my_struct->my_nums.the_integers_2[i] = 4000;
		my_struct->my_nums.the_doubles[i] = 7.7;
	}
}

int main()
{

	jl_options.image_file = JULIAC_PROGRAM_LIBNAME;
    julia_init(JL_IMAGE_JULIA_HOME);

	test_struct_1 my_struct;

	fill_my_struct(&my_struct);

	// box all C variables
	
	jl_array_t *jl_my_struct_my_nums_the_integers = NULL;
	jl_array_t *jl_my_struct_my_nums_the_integers_2 = NULL;
	jl_array_t *jl_my_struct_my_nums_the_doubles = NULL;
	jl_value_t* jl_my_struct_a = NULL;

	JL_GC_PUSH4(jl_my_struct_a, 
		       jl_my_struct_my_nums_the_integers,
		       jl_my_struct_my_nums_the_integers_2,
		       jl_my_struct_my_nums_the_doubles);

	
	jl_my_struct_my_nums_the_integers = jl_ptr_to_array_1d(jl_apply_array_type((jl_value_t*)jl_int64_type, 1),
		                                                               my_struct.my_nums.the_integers,
		                                                               my_struct.a,
		                                                               0);
	jl_my_struct_my_nums_the_integers_2 = jl_ptr_to_array_1d(jl_apply_array_type((jl_value_t*)jl_int64_type, 1),
		                                                               my_struct.my_nums.the_integers_2,
		                                                               my_struct.a,
		                                                               0);
	jl_my_struct_my_nums_the_doubles = jl_ptr_to_array_1d(jl_apply_array_type((jl_value_t*)jl_float64_type, 1),
		                                                               my_struct.my_nums.the_doubles,
		                                                               my_struct.a,
		                                                               0);
	jl_my_struct_a = jl_box_int64(my_struct.a);

	//box done
	jl_value_t* mod = (jl_value_t*) jl_eval_string("MyModule");

	//first create the "the_numbers" struct
	jl_value_t* jl_the_numbers = jl_new_struct((jl_datatype_t*)jl_get_function((jl_module_t*)mod, "the_numbers"),
		                                     jl_my_struct_my_nums_the_integers,
		                                     jl_my_struct_my_nums_the_integers_2,
		                                     jl_my_struct_my_nums_the_doubles);
	jl_value_t* jl_test_struct_1 = jl_new_struct((jl_datatype_t*)jl_get_function((jl_module_t*)mod, "test_struct_1"),
		                                          jl_my_struct_a,
		                                          jl_the_numbers);


	jl_function_t* jl_print_struct = jl_get_function((jl_module_t*)mod, "print_struct");
	jl_value_t* res = jl_call1(jl_print_struct, jl_test_struct_1);

	std::cout << "Res: " << jl_unbox_float64(res) << std::endl;

	JL_GC_POP();

	jl_atexit_hook(0);

return 0;
}


1 Like