Skip to content

Commit a576689

Browse files
committed
Initial commit
0 parents  commit a576689

File tree

7 files changed

+233
-0
lines changed

7 files changed

+233
-0
lines changed

.clang-format

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
Language: Cpp
3+
# BasedOnStyle: LLVM
4+
AccessModifierOffset: -4
5+
AlignAfterOpenBracket: true
6+
AlignEscapedNewlinesLeft: false
7+
AlignOperands: true
8+
AlignTrailingComments: false
9+
AllowAllParametersOfDeclarationOnNextLine: true
10+
AllowShortBlocksOnASingleLine: false
11+
AllowShortCaseLabelsOnASingleLine: false
12+
AllowShortIfStatementsOnASingleLine: false
13+
AllowShortLoopsOnASingleLine: false
14+
AllowShortFunctionsOnASingleLine: Inline
15+
AlwaysBreakAfterDefinitionReturnType: false
16+
AlwaysBreakTemplateDeclarations: false
17+
AlwaysBreakBeforeMultilineStrings: false
18+
BreakBeforeBinaryOperators: None
19+
BreakBeforeTernaryOperators: true
20+
BreakConstructorInitializersBeforeComma: false
21+
BinPackParameters: true
22+
BinPackArguments: true
23+
ColumnLimit: 110
24+
ConstructorInitializerAllOnOneLineOrOnePerLine: true
25+
ConstructorInitializerIndentWidth: 4
26+
DerivePointerAlignment: false
27+
ExperimentalAutoDetectBinPacking: false
28+
IndentCaseLabels: true
29+
IndentWrappedFunctionNames: false
30+
IndentFunctionDeclarationAfterType: false
31+
MaxEmptyLinesToKeep: 3
32+
KeepEmptyLinesAtTheStartOfBlocks: true
33+
NamespaceIndentation: None
34+
ObjCBlockIndentWidth: 2
35+
ObjCSpaceAfterProperty: false
36+
ObjCSpaceBeforeProtocolList: true
37+
PenaltyBreakBeforeFirstCallParameter: 19
38+
PenaltyBreakComment: 300
39+
PenaltyBreakString: 1000
40+
PenaltyBreakFirstLessLess: 120
41+
PenaltyExcessCharacter: 1000000
42+
PenaltyReturnTypeOnItsOwnLine: 60
43+
PointerAlignment: Left
44+
SpacesBeforeTrailingComments: 2
45+
Cpp11BracedListStyle: true
46+
Standard: Cpp11
47+
IndentWidth: 4
48+
TabWidth: 8
49+
UseTab: Never
50+
BreakBeforeBraces: Attach
51+
SpacesInParentheses: false
52+
SpacesInSquareBrackets: false
53+
SpacesInAngles: false
54+
SpaceInEmptyParentheses: false
55+
SpacesInCStyleCastParentheses: false
56+
SpaceAfterCStyleCast: false
57+
SpacesInContainerLiterals: true
58+
SpaceBeforeAssignmentOperators: true
59+
ContinuationIndentWidth: 4
60+
CommentPragmas: '^ IWYU pragma:'
61+
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
62+
SpaceBeforeParens: ControlStatements
63+
DisableFormat: false
64+
#SortIncludes: false
65+
...

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.kdev4
2+
*~

CMakeLists.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
cmake_minimum_required(VERSION 2.8)
2+
project(embed_julia)
3+
4+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}")
5+
6+
find_package(Julia)
7+
add_definitions(${Julia_DEFINITIONS})
8+
include_directories(${Julia_INCLUDE_DIRS})
9+
link_directories(${Julia_LIBRARY_DIRS})
10+
11+
add_executable(embedding_julia embedding.cpp)
12+
target_link_libraries(embedding_julia ${Julia_LIBRARIES})

FindJulia.cmake

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#.rst:
2+
# FindJulia
3+
# ---------
4+
#
5+
# Finds Julia library for embedding
6+
#
7+
# This will define the following variables::
8+
#
9+
# Julia_FOUND - True if the system has Julia
10+
# Julia_VERSION - The version of Julia which was found
11+
#
12+
13+
14+
find_program(Julia_EXECUTABLE julia DOC "Julia executable")
15+
16+
if(NOT Julia_EXECUTABLE)
17+
return()
18+
endif()
19+
20+
21+
execute_process(
22+
COMMAND ${Julia_EXECUTABLE} --version
23+
OUTPUT_VARIABLE Julia_VERSION
24+
RESULT_VARIABLE RESULT
25+
)
26+
if(RESULT EQUAL 0)
27+
string(REGEX REPLACE ".*([0-9]+\\.[0-9]+\\.[0-9]+).*" "\\1"
28+
Julia_VERSION ${Julia_VERSION})
29+
endif()
30+
31+
32+
execute_process(
33+
COMMAND ${Julia_EXECUTABLE} -e "print(JULIA_HOME)"
34+
OUTPUT_VARIABLE Julia_HOME
35+
RESULT_VARIABLE RESULT
36+
)
37+
38+
get_filename_component(Julia_DIR ${Julia_HOME} DIRECTORY)
39+
40+
41+
42+
set(Julia_DEFINITIONS -fPIC -DJULIA_INIT_DIR="${Julia_HOME}")
43+
set(Julia_INCLUDE_DIRS ${Julia_DIR}/include/julia)
44+
set(Julia_LIBRARY_DIRS ${Julia_DIR}/lib ${Julia_DIR}/lib/julia)
45+
46+
find_library( Julia_LIBRARIES
47+
NAMES julia
48+
PATHS ${Julia_LIBRARY_DIRS}
49+
)
50+
51+
52+
include(FindPackageHandleStandardArgs)
53+
find_package_handle_standard_args(Julia
54+
FOUND_VAR Julia_FOUND
55+
REQUIRED_VARS
56+
Julia_DIR
57+
Julia_HOME
58+
Julia_EXECUTABLE
59+
Julia_DEFINITIONS
60+
Julia_INCLUDE_DIRS
61+
Julia_LIBRARIES
62+
Julia_LIBRARY_DIRS
63+
VERSION_VAR Julia_VERSION
64+
)

LICENSE.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2016 Cristóvão D. Sousa
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# embedding_julia
2+
3+
Trying to call a Julia function from C++ as a native C function.

embedding.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <iostream>
2+
3+
#include <julia.h>
4+
5+
int main(int argc, char* argv[]) {
6+
static const int N = 10;
7+
double a[N];
8+
9+
/* required: setup the Julia context */
10+
jl_init(JULIA_INIT_DIR);
11+
12+
13+
//////////// Usual stuff
14+
15+
/* run Julia commands */
16+
jl_eval_string("println(sqrt(2.0))");
17+
18+
jl_value_t* array_type = jl_apply_array_type(jl_float64_type, 1);
19+
jl_array_t* a_jl = jl_ptr_to_array_1d(array_type, a, N, 0);
20+
21+
jl_eval_string("function g(a) \n a[:] = 1:length(a) \nend");
22+
jl_call1(jl_get_function(jl_main_module, "g"), (jl_value_t*)a_jl);
23+
24+
jl_eval_string("f(x) = x .^ 2");
25+
jl_function_t* func = jl_get_function(jl_main_module, "f");
26+
for (int i = 0; i < N; ++i) {
27+
double ret = jl_unbox_float64(jl_call1(func, jl_box_float64(a[i])));
28+
std::cout << ret << std::endl;
29+
}
30+
31+
32+
33+
std::cout << std::endl;
34+
35+
36+
//////////// Getting a native function pointer to a Julia function
37+
38+
jl_eval_string("f(x::Int)::Int = 2x");
39+
40+
int (*f)(int) = (int (*)(int))(jl_unbox_voidpointer(jl_eval_string("cfunction(f, Int, (Int,))")));
41+
42+
std::cout << "This is really nice! " << f(123) << std::endl;
43+
44+
// jl_eval_string("@code_native f(123)");
45+
46+
47+
48+
// jl_eval_string("this_function_does_not_exist()");
49+
if (jl_exception_occurred()) {
50+
jl_show(jl_stderr_obj(), jl_exception_occurred());
51+
jl_printf(jl_stderr_stream(), "\n");
52+
}
53+
54+
55+
/* strongly recommended: notify Julia that the
56+
program is about to terminate. this allows
57+
Julia time to cleanup pending write requests
58+
and run all finalizers
59+
*/
60+
jl_atexit_hook(0);
61+
62+
std::cout << " ... Ok ... " << std::endl;
63+
64+
return 0;
65+
}

0 commit comments

Comments
 (0)