Skip to content

Commit c0e3f62

Browse files
committed
Experimental work
1 parent 755ef8e commit c0e3f62

File tree

8 files changed

+4711
-0
lines changed

8 files changed

+4711
-0
lines changed

py/queryosity/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .dataflow import LazyFlow
2+
from .jit import jit

py/queryosity/cppfunc.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#pragma once
2+
3+
#include <Python.h>
4+
5+
#include <functional>
6+
7+
namespace queryosity {
8+
9+
namespace py {
10+
11+
template <typename Ret, typename... Args>
12+
std::function<Ret(Args...)> cppfunc(const std::string& module_name, const std::string& fn_name) {
13+
Py_Initialize();
14+
15+
// Import the module where your cfunc is defined
16+
PyObject* myModule = PyImport_ImportModule(module_name.c_str());
17+
if (!myModule) {
18+
PyErr_Print();
19+
}
20+
21+
// Get the function object from the module
22+
PyObject* cfuncObj = PyObject_GetAttrString(myModule, fn_name.c_str());
23+
if (!cfuncObj) {
24+
PyErr_Print();
25+
}
26+
27+
// Get the address attribute
28+
PyObject* cfuncAddr = PyObject_GetAttrString(cfuncObj, "address");
29+
if (!cfuncAddr) {
30+
PyErr_Print();
31+
}
32+
33+
// Convert address to a callable function pointer in C++
34+
void* ptr = PyLong_AsVoidPtr(cfuncAddr);
35+
if (!ptr) {
36+
PyErr_Print();
37+
}
38+
typedef Ret(*Callable)(Args...);
39+
Callable callable = reinterpret_cast<Callable>(ptr);
40+
41+
Py_Finalize();
42+
43+
return std::function<Ret(Args...)>(callable);
44+
}
45+
46+
}
47+
48+
}

py/queryosity/dataflow.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import os
2+
import cppyy
3+
4+
# from .lazy import LazyNode
5+
# from .todo import TodoItem
6+
7+
queryosity_h = os.path.join(os.path.dirname(os.path.abspath(__file__)),'queryosity.h')
8+
cppyy.include(queryosity_h)
9+
10+
qty = cppyy.gbl.queryosity
11+
dataflow = qty.dataflow
12+
dataset = qty.dataset
13+
multithread = qty.multithread
14+
column = qty.column
15+
query = qty.query
16+
systematic = qty.systematic
17+
18+
class LazyFlow(object):
19+
20+
def __init__(self, *, mt=1, nrows=-1, weight=1.0):
21+
if mt is True: mt = -1
22+
self._df = dataflow(cppyy.gbl.std.move(multithread.enable(mt)), cppyy.gbl.std.move(dataset.weight(weight)))
23+
24+
def load(self, dataset):
25+
return self._df.load(dataset)
26+
27+
def define(self, *, definition=None, expression=None, constant=None, observables=[]):
28+
return None
29+
30+
def filter(self, column=None, *, expression=None):
31+
return None
32+
33+
def weight(self, column=None, *, expression=None):
34+
return None
35+
36+
def make(self):
37+
return None

py/queryosity/jit.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
import cppyy
3+
import cppyy.numba_ext
4+
import numba
5+
from numba import njit
6+
7+
cppfunc_h = os.path.join(os.path.dirname(os.path.abspath(__file__)),'cppfunc.h')
8+
cppyy.include(cppfunc_h)
9+
cppfunc = cppyy.gbl.queryosity.py.cppfunc
10+
11+
# Define the custom decorator with its own arguments
12+
def jit(args=[], ret=''):
13+
14+
if not len(args):
15+
raise ValueError("no argument types specified.")
16+
17+
if not ret:
18+
raise ValueError("return type not specified.")
19+
20+
def decorator(func):
21+
# Apply njit with parallel=True to the function
22+
jitted_func = njit(parallel=True)(func)
23+
24+
print(jitted_func.__dict__)
25+
26+
# Example pre-processing logic using decorator arguments
27+
print("Before calling the njitted function")
28+
29+
fn = cppfunc[ret,*args](func.__module__, func.__name__)
30+
31+
# Example post-processing logic
32+
print("After calling the njitted function")
33+
# Modify the result or perform additional actions here
34+
35+
return fn
36+
return decorator

py/queryosity/lazy.py

Whitespace-only changes.

0 commit comments

Comments
 (0)