Exemplo n.º 1
0
void td_jl_init(char *home_dir)
{
    jl_init(home_dir);

    td_env_t *env = (td_env_t*)malloc(sizeof(td_env_t));
    env->name = "julia";

    env->eval = &td_jl_eval;
    env->invoke0 = &td_jl_invoke0;
    env->invoke1 = &td_jl_invoke1;
    //env->invoke2
    //env->invoke3

    //env->retain
    //env->release

    env->get_type = &td_jl_get_type;
    env->get_eltype = &td_jl_get_eltype;
    env->get_dataptr = &td_jl_get_dataptr;
    env->get_length = &td_jl_get_length;
    env->get_ndims = &td_jl_get_ndims;

    //env->get_dims
    //env->get_strides

    td_provide_julia(env);
}
Exemplo n.º 2
0
SqwJl::SqwJl(const char* pcFile) : m_pmtx(std::make_shared<std::mutex>())
{
	std::string strFile = pcFile;
	std::string strDir = tl::get_dir(strFile);
	const bool bSetScriptCWD = 1;

	// init interpreter
	static bool bInited = 0;
	if(!bInited)
	{
		jl_init(0);
		std::string strJl = jl_ver_string();
		tl::log_debug("Initialised Julia interpreter version ", strJl, ".");
		bInited = 1;
	}

	// include module
	jl_function_t *pInc = jl_get_function(jl_base_module, "include");
	jl_value_t *pMod = jl_cstr_to_string(pcFile);
	jl_call1(pInc, pMod);

	// working dir
	if(bSetScriptCWD)
	{
		jl_function_t *pCwd = jl_get_function(jl_base_module, "cd");
		jl_value_t *pDir = jl_cstr_to_string(strDir.c_str());
		jl_call1(pCwd, pDir);
	}

	// import takin functions
	m_pInit = jl_get_function(jl_main_module, "TakinInit");
	m_pSqw = jl_get_function(jl_main_module, "TakinSqw");

	if(!m_pSqw)
	{
		m_bOk = 0;
		tl::log_err("Julia script has no TakinSqw function.");
		return;
	}
	else
	{
		m_bOk = 1;
	}

	if(m_pInit)
		jl_call0((jl_function_t*)m_pInit);
	else
		tl::log_warn("Julia script has no TakinInit function.");
}
Exemplo n.º 3
0
Arquivo: jl4R.c Projeto: rcqls/jl4R
SEXP jl4R_init(SEXP args)
{
  char *julia_home_dir;

  if(!jl4R_julia_running) {
    if(!isValidString(CADR(args)))
     error("invalid argument");
    julia_home_dir=(char*)CHAR(STRING_ELT(CADR(args), 0));
    Rprintf("julia_home_dir=%s\n",julia_home_dir);
    jl_init(julia_home_dir);
    jl4R_julia_running=1;
    //printf("julia initialized!!!\n");
  }
  return R_NilValue;
}
Exemplo n.º 4
0
int main()
{
    jl_init(NULL);

    {
        // Simple running Julia code

        jl_eval_string("println(sqrt(2.0))");
    }

    {
        // Accessing the return value

        jl_value_t *ret = jl_eval_string("sqrt(2.0)");

        if (jl_is_float64(ret)) {
            double retDouble = jl_unbox_float64(ret);
            printf("sqrt(2.0) in C: %e\n", retDouble);
        }
    }

    {
        // Same as above but with function handle (more flexible)

        jl_function_t *func = jl_get_function(jl_base_module, "sqrt");
        jl_value_t* argument = jl_box_float64(2.0);
        jl_value_t* ret = jl_call1(func, argument);

        if (jl_is_float64(ret)) {
            double retDouble = jl_unbox_float64(ret);
            printf("sqrt(2.0) in C: %e\n", retDouble);
        }
    }

    {
        // 1D arrays

        jl_value_t* array_type = jl_apply_array_type( jl_float64_type, 1 );
        jl_array_t* x          = jl_alloc_array_1d(array_type , 10);
        JL_GC_PUSH1(&x);

        double* xData = jl_array_data(x);

        size_t i;
        for(i=0; i<jl_array_len(x); i++)
            xData[i] = i;

        jl_function_t *func  = jl_get_function(jl_base_module, "reverse!");
        jl_call1(func, (jl_value_t*) x);

        printf("x = [");
        for(i=0; i<jl_array_len(x); i++)
            printf("%e ", xData[i]);
        printf("]\n");

        JL_GC_POP();
    }

    {
        // define julia function and call it

        jl_eval_string("my_func(x) = 2*x");

        jl_function_t *func = jl_get_function(jl_current_module, "my_func");
        jl_value_t* arg = jl_box_float64(5.0);
        double ret = jl_unbox_float64(jl_call1(func, arg));

        printf("my_func(5.0) = %f\n", ret);
    }

    {
        // call c function

        jl_eval_string("println( ccall( :my_c_sqrt, Float64, (Float64,), 2.0 ) )");
    }

    {
        // check for exceptions

        jl_eval_string("this_function_does_not_exist()");

        if (jl_exception_occurred()) {
            jl_show(jl_stderr_obj(), jl_exception_occurred());
            jl_printf(jl_stderr_stream(), "\n");
        }
    }

    jl_atexit_hook();
    return 0;
}