Skip to content

Commit 314b2a4

Browse files
Work on a python integration
1 parent 68e8cfd commit 314b2a4

File tree

8 files changed

+234
-18
lines changed

8 files changed

+234
-18
lines changed

core/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@
1212
<artifactId>aika-core</artifactId>
1313
<version>${revision}</version>
1414

15-
<properties>
16-
<maven.compiler.source>22</maven.compiler.source>
17-
<maven.compiler.target>22</maven.compiler.target>
18-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19-
</properties>
20-
2115
<dependencies>
2216
<dependency>
2317
<groupId>network.aika</groupId>

debug/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@
1212
<artifactId>aika-debug</artifactId>
1313
<version>${revision}</version>
1414

15-
<properties>
16-
<maven.compiler.source>22</maven.compiler.source>
17-
<maven.compiler.target>22</maven.compiler.target>
18-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19-
</properties>
20-
2115
<dependencies>
2216
<dependency>
2317
<groupId>network.aika</groupId>

fields/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@
1111

1212
<artifactId>aika-fields</artifactId>
1313

14-
<properties>
15-
<maven.compiler.source>22</maven.compiler.source>
16-
<maven.compiler.target>22</maven.compiler.target>
17-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18-
</properties>
19-
2014
<dependencies>
2115
<dependency>
2216
<groupId>org.apache.commons</groupId>

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<module>fields</module>
1010
<module>core</module>
1111
<module>debug</module>
12+
<module>python-bindings</module>
1213
</modules>
1314
<name>aika-project</name>
1415
<url>https://aika.network</url>

python-bindings/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
project(PybindExample)
3+
4+
# Find pybind11
5+
find_package(pybind11 REQUIRED)
6+
7+
# Define the C++ extension module
8+
pybind11_add_module(example src/example.cpp)
9+
10+
# Find Python and link it
11+
find_package(Python3 REQUIRED)
12+
target_link_libraries(example PRIVATE Python3::Python)

python-bindings/pom.xml

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>network.aika</groupId>
8+
<artifactId>aika-project</artifactId>
9+
<version>${revision}</version>
10+
</parent>
11+
12+
<artifactId>aika-python-bindings</artifactId>
13+
14+
<properties>
15+
<cmake.dir>./target</cmake.dir>
16+
</properties>
17+
18+
<build>
19+
<plugins>
20+
<plugin>
21+
<groupId>org.graalvm.nativeimage</groupId>
22+
<artifactId>native-image-maven-plugin</artifactId>
23+
<version>21.2.0</version> <!-- Use the appropriate version for GraalVM -->
24+
<executions>
25+
<execution>
26+
<goals>
27+
<goal>native-image</goal>
28+
</goals>
29+
<configuration>
30+
<imageName>libhello-world</imageName>
31+
<!-- Optional: Enable the no-fallback option -->
32+
<buildArgs>
33+
<buildArg>--no-fallback</buildArg>
34+
<buildArg>--shared</buildArg>
35+
</buildArgs>
36+
</configuration>
37+
</execution>
38+
</executions>
39+
</plugin>
40+
41+
<!-- Plugin to invoke CMake for C++ code compilation -->
42+
<plugin>
43+
<groupId>org.codehaus.mojo</groupId>
44+
<artifactId>exec-maven-plugin</artifactId>
45+
<version>3.5.0</version>
46+
<executions>
47+
<execution>
48+
<phase>compile</phase>
49+
<goals>
50+
<goal>exec</goal>
51+
</goals>
52+
<configuration>
53+
<executable>cmake</executable>
54+
<arguments>
55+
<argument>-DCMAKE_BUILD_TYPE=Release</argument>
56+
<argument>-DCMAKE_POSITION_INDEPENDENT_CODE=ON</argument>
57+
<argument>-S</argument>
58+
<argument>${project.basedir}/src/main/cpp</argument> <!-- C++ source directory -->
59+
<argument>-B</argument>
60+
<argument>${cmake.dir}</argument> <!-- CMake build directory -->
61+
</arguments>
62+
</configuration>
63+
</execution>
64+
</executions>
65+
</plugin>
66+
67+
</plugins>
68+
</build>
69+
70+
71+
<dependencies>
72+
73+
<!-- Logging -->
74+
<dependency>
75+
<groupId>org.slf4j</groupId>
76+
<artifactId>slf4j-api</artifactId>
77+
</dependency>
78+
79+
<!-- dependencies for tests: -->
80+
<dependency>
81+
<groupId>org.junit.jupiter</groupId>
82+
<artifactId>junit-jupiter-engine</artifactId>
83+
<scope>test</scope>
84+
</dependency>
85+
86+
<dependency>
87+
<groupId>org.junit.jupiter</groupId>
88+
<artifactId>junit-jupiter-params</artifactId>
89+
<scope>test</scope>
90+
</dependency>
91+
92+
<dependency>
93+
<groupId>org.junit.jupiter</groupId>
94+
<artifactId>junit-jupiter-api</artifactId>
95+
<scope>test</scope>
96+
</dependency>
97+
98+
<dependency>
99+
<groupId>ch.qos.logback</groupId>
100+
<artifactId>logback-core</artifactId>
101+
<scope>test</scope>
102+
</dependency>
103+
104+
<dependency>
105+
<groupId>ch.qos.logback</groupId>
106+
<artifactId>logback-classic</artifactId>
107+
<scope>test</scope>
108+
</dependency>
109+
</dependencies>
110+
</project>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <pybind11/pybind11.h>
2+
#include <pybind11/stl.h>
3+
#include <vector>
4+
#include <iostream>
5+
#include <dlfcn.h> // For dlopen and dlsym
6+
#include <hello-world.h>
7+
8+
9+
10+
// ----------------
11+
// Regular C++ code
12+
// ----------------
13+
14+
// multiply all entries by 2.0
15+
// input: std::vector ([...]) (read only)
16+
// output: std::vector ([...]) (new copy)
17+
std::vector<double> modify(const std::vector<double>& input)
18+
{
19+
std::vector<double> output;
20+
21+
std::transform(
22+
input.begin(),
23+
input.end(),
24+
std::back_inserter(output),
25+
[](double x) -> double { return 2.*x; }
26+
);
27+
28+
// N.B. this is equivalent to (but there are also other ways to do the same)
29+
//
30+
// std::vector<double> output(input.size());
31+
//
32+
// for ( size_t i = 0 ; i < input.size() ; ++i )
33+
// output[i] = 2. * input[i];
34+
35+
return output;
36+
}
37+
38+
// ----------------
39+
// Python interface
40+
// ----------------
41+
42+
namespace py = pybind11;
43+
44+
// Load and call the GraalVM native library
45+
const char* call_graalvm_method(char* input)
46+
{
47+
graal_isolate_t *isolate = NULL;
48+
graal_isolatethread_t *thread = NULL;
49+
50+
if (graal_create_isolate(NULL, &isolate, &thread) != 0) {
51+
return "initialization error\n";
52+
}
53+
54+
// Call the function
55+
const char* result = greet(thread, input);
56+
57+
// Duplicate the string to ensure the memory is managed independently
58+
size_t len = strlen(result);
59+
char* duplicated_result = new char[len + 1];
60+
strncpy(duplicated_result, result, len);
61+
duplicated_result[len] = '\0'; // Ensure null termination
62+
63+
graal_tear_down_isolate(thread);
64+
65+
return duplicated_result;
66+
}
67+
68+
69+
PYBIND11_MODULE(example,m)
70+
{
71+
m.doc() = "pybind11 example plugin";
72+
73+
m.def("modify", &modify, "Multiply all entries of a list by 2.0");
74+
75+
// GraalVM native function exposed to Python
76+
m.def("call_graalvm", &call_graalvm_method, "Call a GraalVM native method");
77+
}
78+
79+
int main() {
80+
// Example input string
81+
char input[] = "World";
82+
83+
// Call the GraalVM native method
84+
const char* result = call_graalvm_method(input);
85+
86+
// Check if the result is valid and print the result
87+
if (result) {
88+
std::cout << "GraalVM result: " << result << std::endl;
89+
} else {
90+
std::cerr << "Error calling GraalVM method." << std::endl;
91+
}
92+
93+
return 0;
94+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package network.aika;
2+
3+
import org.graalvm.nativeimage.IsolateThread;
4+
import org.graalvm.nativeimage.c.function.CEntryPoint;
5+
import org.graalvm.nativeimage.c.type.CCharPointer;
6+
import org.graalvm.nativeimage.c.type.CTypeConversion;
7+
8+
public class HelloWorld {
9+
10+
// Method to return a greeting message
11+
@CEntryPoint(name = "greet")
12+
public static CCharPointer greet(IsolateThread thread, CCharPointer cFilter) {
13+
String input = CTypeConversion.toJavaString(cFilter);
14+
String result = "Hello, " + new String(input) + "!";
15+
return CTypeConversion.toCString(result).get();
16+
}
17+
}

0 commit comments

Comments
 (0)