-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
233 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#include <dlfcn.h> | ||
|
||
#include <cuda.h> | ||
#include <cudaTypedefs.h> | ||
#include <rapidjson/reader.h> | ||
|
||
#include "Neon/Neon.h" | ||
#include "Neon/core/core.h" | ||
#include "Neon/domain/Grids.h" | ||
#include "Neon/domain/interface/GridBase.h" | ||
#include "Neon/py/CudaDriver.h" | ||
#include "Neon/set/container/ContainerAPI.h" | ||
#include "Neon/set/container/Loader.h" | ||
|
||
#include "Neon/py/macros.h" | ||
#include "Neon/skeleton/Skeleton.h" | ||
|
||
extern "C" auto warp_skeleton_new( | ||
void** out_handle, | ||
void* bkPtr) -> int | ||
{ | ||
NEON_PY_PRINT_BEGIN(*out_handle); | ||
|
||
Neon::Backend& bk = *static_cast<Neon::Backend*>(bkPtr); | ||
std::cout << "warp_skeleton_new " | ||
<< "bk " << bkPtr << " " << bk.toString() << std::endl; | ||
auto* skeleton = new (std::nothrow) Neon::skeleton::Skeleton(bk); | ||
if (skeleton == nullptr) { | ||
return -1; | ||
} | ||
(*out_handle) = skeleton; | ||
NEON_PY_PRINT_END(*out_handle); | ||
return 0; | ||
} | ||
|
||
extern "C" auto warp_skeleton_delete( | ||
void** out_handle) -> int | ||
{ | ||
try { | ||
auto* skeleton = static_cast<Neon::skeleton::Skeleton*>(*out_handle); | ||
delete skeleton; | ||
(*out_handle) = nullptr; | ||
return 0; | ||
} catch (...) { | ||
return -1; | ||
} | ||
} | ||
|
||
extern "C" void warp_skeleton_sequence( | ||
void* handle, | ||
const char* graphName, | ||
int numContainers, | ||
Neon::set::Container** containers) | ||
{ | ||
// # Load the shared library | ||
// # On Linux | ||
// lib = ctypes.CDLL('./libexample.so') | ||
// | ||
// # On Windows | ||
// # lib = ctypes.CDLL('example.dll') | ||
// | ||
// # Define the argument type for the C++ function | ||
// lib.print_message.argtypes = [ctypes.c_char_p] | ||
// | ||
// # Convert Python string to bytes (C char array) | ||
// message = "Hello from Python".encode('utf-8') | ||
// | ||
// # Call the C++ function | ||
// lib.print_message(message) | ||
NEON_PY_PRINT_BEGIN(handle); | ||
|
||
Neon::skeleton::Skeleton* skeleton = static_cast<Neon::skeleton::Skeleton*>(handle); | ||
std::string name(graphName); | ||
std::vector<Neon::set::Container> operations; | ||
for (int i = 0; i < numContainers; i++) { | ||
operations.push_back(*containers[i]); | ||
} | ||
skeleton->sequence(operations, name, Neon::skeleton::Options()); | ||
NEON_PY_PRINT_END(handle); | ||
} | ||
|
||
extern "C" auto warp_skeleton_run( | ||
void* handle) -> int | ||
{ | ||
try { | ||
auto* s = static_cast<Neon::skeleton::Skeleton*>(handle); | ||
s->run(); | ||
return 0; | ||
} catch (...) { | ||
return -1; | ||
} | ||
} | ||
|
||
extern "C" auto warp_skeleton_ioToDot( | ||
void* handle, | ||
const char* fname, | ||
const char* gname, | ||
int debug) | ||
-> int | ||
{ | ||
try { | ||
|
||
auto* s = static_cast<Neon::skeleton::Skeleton*>(handle); | ||
s->ioToDot(fname, gname, debug == 1); | ||
} catch (...) { | ||
return -1; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import ctypes | ||
from typing import List | ||
|
||
from py_neon import Backend | ||
from py_neon import Py_neon | ||
from wpne import Container | ||
|
||
|
||
class Skeleton(object): | ||
def __init__(self, | ||
backend: Backend): | ||
|
||
self.skeleton_handle: ctypes.c_void_p = ctypes.c_void_p(0) | ||
self.backend = backend | ||
|
||
try: | ||
self.py_neon: Py_neon = Py_neon() | ||
except Exception as e: | ||
self.skeleton_handle = ctypes.c_void_p(0) | ||
raise Exception('Failed to initialize PyNeon: ' + str(e)) | ||
|
||
self.help_load_api() | ||
self.help_skeleton_new() | ||
|
||
def __del__(self): | ||
if self.skeleton_handle == 0: | ||
return | ||
self.help_skeleton_new() | ||
pass | ||
|
||
def help_load_api(self): | ||
lib_obj = self.py_neon.lib | ||
# ------------------------------------------------------------------ | ||
# warp_skeleton_new | ||
self.api_new = lib_obj.warp_skeleton_new | ||
self.api_new.argtypes = [ctypes.POINTER(self.py_neon.handle_type), | ||
self.py_neon.handle_type] | ||
self.api_new.restype = ctypes.c_int | ||
# ------------------------------------------------------------------ | ||
# warp_skeleton_delete | ||
self.api_delete = lib_obj.warp_skeleton_delete | ||
self.api_delete.argtypes = [ctypes.POINTER(self.py_neon.handle_type)] | ||
self.api_delete.restype = ctypes.c_int | ||
# ------------------------------------------------------------------ | ||
# warp_skeleton_sequence | ||
self.api_sequence = lib_obj.warp_skeleton_sequence | ||
self.api_sequence.argtypes = [self.py_neon.handle_type, | ||
ctypes.c_char_p, | ||
ctypes.c_int, | ||
ctypes.POINTER(self.py_neon.handle_type)] | ||
self.api_sequence.restype = ctypes.c_int | ||
# ------------------------------------------------------------------ | ||
# warp_skeleton_run | ||
self.api_run = lib_obj.warp_skeleton_run | ||
self.api_run.argtypes = [self.py_neon.handle_type] | ||
self.api_run.restype = ctypes.c_int | ||
# ------------------------------------------------------------------ | ||
# warp_skeleton_ioToDot | ||
self.api_run = lib_obj.warp_skeleton_ioToDot | ||
self.api_run.argtypes = [self.py_neon.handle_type, | ||
ctypes.c_char_p, | ||
ctypes.c_char_p, | ||
ctypes.c_int] | ||
self.api_run.restype = ctypes.c_int | ||
|
||
def help_skeleton_new(self): | ||
if self.skeleton_handle.value != ctypes.c_void_p(0).value: | ||
raise Exception(f'Skeleton: Invalid handle {self.skeleton_handle}') | ||
|
||
res = self.py_neon.lib.warp_skeleton_new(ctypes.pointer(self.skeleton_handle), | ||
self.backend.backend_handle) | ||
|
||
if res != 0: | ||
raise Exception('Backend: Failed to initialize backend') | ||
|
||
def help_skeleton_delete(self): | ||
if self.skeleton_handle == 0: | ||
return | ||
res = self.api_delete(ctypes.pointer(self.skeleton_handle)) | ||
if res != 0: | ||
raise Exception('Failed to delete backend') | ||
|
||
def sequence(self, name: str, containers: List[Container]): | ||
self.containers = containers | ||
self.handle_list = (ctypes.c_void_p * len(containers))() | ||
for i in range(len(self.handle_list)): | ||
self.handle_list[i] = containers[i].container_handle | ||
print(f"PYTHON handle_list {self.handle_list}") | ||
print(f"PYTHON handle_list[0] {hex(self.handle_list[0])}") | ||
self.api_sequence(self.skeleton_handle, | ||
name.encode('utf-8'), | ||
len(self.handle_list), | ||
self.handle_list) | ||
|
||
def run(self): | ||
self.api_run(self.skeleton_handle) |