Skip to content

Embedding Julia in C++ : differences between 32 and 64 bit #52872

Open

Description

When calling Julia from a C++ console program, I encounter differences between the 32 and 64 bit version.
Info

What happens is that after the comment lines starting with // WIN 32, the 32 bit version gives TypeError whereas the 64 bit version works normally.

Also, both in 32 as 64 I get an UndefVarError for the next line
jl_eval_string("versioninfo(verbose = true)");
I would expect this to work.

Here is the c++ source code

#include <iostream>
#include <thread>
#include <julia.h>
#include <chrono>
#include <conio.h>

JULIA_DEFINE_FAST_TLS // only define this once, in an executable (not in a shared library) if you want fast code.
CWinApp theApp;
using namespace std;

void CheckResetError()
{
	if (jl_exception_occurred())
	{
		std::cerr << jl_typeof_str(jl_exception_occurred()) << std::endl;
		jl_exception_clear();
	}
}

int main(int argc, char* argv[])
{
	// console program compiled with MFC support
	AfxWinInit(::GetModuleHandle(nullptr), nullptr, ::GetCommandLine(), 0);
	int nSize{ 10 };
	double* pdoubleArray = new double[nSize] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

	/* required: setup the Julia context */
#ifdef _WIN64
	jl_init_with_image("C:\\Julia_64\\bin", nullptr);
#else
	jl_init_with_image("C:\\Julia_32\\bin", nullptr);
#endif
// WIN 32 + 64 : jl_exception_occurred returns UndefVarError
	jl_eval_string("versioninfo(verbose = true)");
	CheckResetError();
	jl_gc_enable(0);
	jl_function_t* jlPrintln = jl_get_function(jl_main_module, "println");
	CheckResetError();

	// Create a Julia array from the C++ array
	// Assign a name to the Julia array
	// Optionally, run some Julia code using the array
	jl_array_t* juliaArray = jl_ptr_to_array_1d(jl_apply_array_type((jl_value_t*)jl_float64_type, 1), pdoubleArray, nSize, 0);
	CheckResetError();

	jl_set_global(jl_main_module, jl_symbol("myArray"), (jl_value_t*)juliaArray);
	CheckResetError();
	jl_call1(jlPrintln, (jl_value_t*)juliaArray);
	CheckResetError();
// WIN 32 : jl_exception_occurred returns TypeError 
	jl_eval_string("myArray[2] = 100.0"); 
	CheckResetError();
	jl_call1(jlPrintln, (jl_value_t*)juliaArray);
	CheckResetError();
// WIN 32 : jl_exception_occurred returns TypeError 
	jl_eval_string("println(sum(myArray))");
	CheckResetError();

// WIN 32 : 0xC0000005: Access violation reading location 0x00000000
	double (*jlSqrt)(double) = static_cast<double(*)(double)>(jl_unbox_voidpointer(jl_eval_string("@cfunction(sqrt, Float64, (Float64,))"))); 
	double ret = jlSqrt(2.0);

	// memory interactions
	jl_value_t* array_type = jl_apply_array_type((jl_value_t*)jl_float64_type, 1);
	CheckResetError();
	jl_array_t* x = jl_alloc_array_1d(array_type, 10);
	CheckResetError();
	double* xData = (double*)jl_array_data(x);
	for (size_t i = 0; i < jl_array_len(x); i++)
		xData[i] = i;
	jl_call1(jlPrintln, (jl_value_t*)x);
	CheckResetError();
	cout << endl;

	jl_gc_enable(1);
	jl_gc_collect(JL_GC_FULL);

	jl_atexit_hook(0);
	delete[] pdoubleArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    embeddingEmbedding Julia using the C APIsystem:32-bitAffects only 32-bit systems

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions