Skip to content

Commit 167ef5f

Browse files
authored
Merge pull request #1 from Krekep/master
Pycubool wrapper [v.0]
2 parents bed615c + 6791bac commit 167ef5f

File tree

6 files changed

+213
-0
lines changed

6 files changed

+213
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import atexit
2+
import os
3+
from ctypes import *
4+
from .ctypesWrapper import *
5+
from .codes import check
6+
7+
_lib_wrapper = None
8+
9+
10+
class InternalWrapper:
11+
12+
def __init__(self):
13+
self._load_path = os.environ["CUBOOL_PATH"]
14+
self._lib = cdll.LoadLibrary(self._load_path)
15+
self._instance_new()
16+
17+
self._declarations = FuncDeclaration(self._lib)
18+
19+
def unload(self):
20+
self._instance_free()
21+
del self._lib
22+
handle = self._lib._handle
23+
self._lib.dlclose(handle)
24+
25+
def _instance_new(self):
26+
self._lib.CuBool_Instance_New.restype = c_int
27+
self._lib.CuBool_Instance_New.argtypes = [POINTER(CuBoolInstanceDesc),
28+
POINTER(CuBoolInstance)]
29+
self._instance = CuBoolInstance()
30+
self._descInstance = CuBoolInstanceDesc()
31+
status = self._lib.CuBool_Instance_New(pointer(self._instance),
32+
pointer(self._descInstance))
33+
check(status.value)
34+
# if status != c_int(0):
35+
# raise Exception("Error at create instance. Code - " + str(status))
36+
# print("Status of creating instance is", status)
37+
38+
def _instance_free(self):
39+
self._lib.CuBool_Instance_Free.restype = c_int
40+
self._lib.CuBool_Instance_Free.argtypes = [POINTER(CuBoolInstanceDesc),
41+
POINTER(CuBoolInstance)]
42+
status = self._lib.CuBool_Instance_Free(self._instance)
43+
check(status.value)
44+
45+
def matrix_new(self, nrows, ncols):
46+
matrix = CuBoolMatrix()
47+
status = self._declarations.matrix_creator(self._instance,
48+
pointer(matrix),
49+
c_int(nrows),
50+
c_int(ncols))
51+
return status.value, matrix
52+
53+
def matrix_free(self, matrix):
54+
status = self._declarations.matrix_destructor(self._instance,
55+
matrix)
56+
return status.value
57+
58+
def matrix_resize(self, matrix, nrows, ncols):
59+
status = self._declarations.matrix_resizer(self._instance,
60+
matrix,
61+
c_int(nrows),
62+
c_int(ncols))
63+
return status.value
64+
65+
def matrix_build(self, matrix, rows, cols, nvals):
66+
t_rows = (c_int * len(rows))(*rows)
67+
t_cols = (c_int * len(cols))(*cols)
68+
69+
status = self._declarations.matrix_builder(self._instance,
70+
matrix,
71+
pointer(t_rows),
72+
pointer(t_cols),
73+
c_int(nvals))
74+
return status.value
75+
76+
def matrix_add(self, acc_matrix, add_matrix):
77+
status = self._declarations.matrix_builder(self._instance,
78+
acc_matrix,
79+
add_matrix)
80+
return status.value, acc_matrix
81+
82+
83+
_lib_wrapper = InternalWrapper()
84+
85+
86+
def action_at_exit():
87+
global _lib_wrapper
88+
_lib_wrapper.unload()

python/pycubool/CuBoolWrapper/add.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from CuBoolWrapper import _lib_wrapper, check
2+
3+
4+
def Add(accumulate_matrix, add_matrix):
5+
temp = _lib_wrapper.matrix_add(accumulate_matrix, add_matrix)
6+
check(temp[0])
7+
return temp[1]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
_return_codes = {
2+
0: "CUBOOL_STATUS_SUCCESS",
3+
1: "CUBOOL_STATUS_ERROR",
4+
2: "CUBOOL_STATUS_DEVICE_NOT_PRESENT",
5+
3: "CUBOOL_STATUS_DEVICE_ERROR",
6+
4: "CUBOOL_STATUS_MEM_OP_FAILED",
7+
5: "CUBOOL_STATUS_INVALID_ARGUMENT",
8+
6: "CUBOOL_STATUS_INVALID_STATE",
9+
7: "CUBOOL_STATUS_NOT_IMPLEMENTED"
10+
}
11+
12+
13+
def check(res):
14+
if res != 0:
15+
raise _return_codes[res]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from ctypes import *
2+
3+
4+
class CuBoolInstance(Structure):
5+
_fields_ = []
6+
7+
8+
class CuBoolInstanceDesc(Structure):
9+
_fields_ = []
10+
11+
12+
class CuBoolMatrix(Structure):
13+
_fields_ = []
14+
15+
16+
class FuncDeclaration:
17+
def __init__(self, lib):
18+
self.matrix_creator = FuncDeclaration._matrix_creator(lib)
19+
self.matrix_destructor = FuncDeclaration._matrix_destructor(lib)
20+
self.matrix_resizer = FuncDeclaration._matrix_resizer(lib)
21+
self.matrix_builder = FuncDeclaration._matrix_builder(lib)
22+
self.matrix_adder = FuncDeclaration._matrix_adder(lib)
23+
24+
@staticmethod
25+
def _matrix_creator(lib):
26+
lib.CuBool_Matrix_New.restype = c_int
27+
lib.CuBool_Matrix_New.argtypes = [CuBoolInstance,
28+
POINTER(CuBoolMatrix),
29+
c_int,
30+
c_int]
31+
return lib.CuBool_Matrix_New
32+
33+
@staticmethod
34+
def _matrix_destructor(lib):
35+
lib.CuBool_Matrix_Free.restype = c_int
36+
lib.CuBool_Matrix_Free.argtypes = [CuBoolInstance,
37+
CuBoolMatrix]
38+
return lib.CuBool_Matrix_Free
39+
40+
@staticmethod
41+
def _matrix_resizer(lib):
42+
lib.CuBool_Matrix_Resize.restype = c_int
43+
lib.CuBool_Matrix_Resize.argtypes = [CuBoolInstance,
44+
CuBoolMatrix,
45+
c_int,
46+
c_int]
47+
return lib.CuBool_Matrix_Resize
48+
49+
@staticmethod
50+
def _matrix_builder(lib):
51+
lib.CuBool_Matrix_Build.restype = c_int
52+
lib.CuBool_Matrix_Build.argtypes = [CuBoolInstance,
53+
CuBoolMatrix,
54+
POINTER(c_int),
55+
POINTER(c_int),
56+
c_int]
57+
return lib.CuBool_Matrix_Build
58+
59+
@staticmethod
60+
def _matrix_adder(lib):
61+
lib.CuBool_Matrix_Add.restype = c_int
62+
lib.CuBool_Matrix_Add.argtypes = [CuBoolInstance,
63+
CuBoolMatrix,
64+
CuBoolMatrix]
65+
return lib.CuBool_Matrix_Add
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from CuBoolWrapper import _lib_wrapper
2+
from codes import check
3+
4+
5+
class Matrix:
6+
def __init__(self, matrix):
7+
self.matrix = matrix
8+
9+
def __del__(self):
10+
check(_lib_wrapper.matrix_free(self, self.matrix))
11+
12+
@classmethod
13+
def sparse(cls, nrows=0, ncols=0):
14+
temp = _lib_wrapper.matrix_new(nrows, ncols)
15+
check(temp[0])
16+
m = cls(temp[1])
17+
return m
18+
19+
def resize(self, nrows, ncols):
20+
check(_lib_wrapper.matrix_resize(self.matrix, nrows, ncols))
21+
22+
def build(self, rows, cols, nvals):
23+
check(_lib_wrapper.matrix_build(self.matrix, rows, cols, nvals))

python/pycubool/userApp.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import CuBoolWrapper
2+
from CuBoolWrapper.add import Add
3+
from CuBoolWrapper.matrix import Matrix
4+
5+
print("Enter num of rows and cols (a, b)")
6+
a, b = map(int, input().split())
7+
matr = Matrix.sparse(a, b)
8+
9+
print("Enter num of values, rows and cols ")
10+
n = int(input())
11+
rows = list(map(int, input().split()))
12+
cols = list(map(int, input().split()))
13+
matr.build(rows, cols, n)
14+
15+
matr = Add(matr, matr)

0 commit comments

Comments
 (0)