-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlibrary.py
More file actions
211 lines (164 loc) · 6.96 KB
/
library.py
File metadata and controls
211 lines (164 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2019 Shapelets.io
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
########################################################################################################################
# IMPORT
########################################################################################################################
from enum import Enum
import ctypes
import platform
import logging
import sys
########################################################################################################################
KHIVA_ERROR_LENGTH = 256
########################################################################################################################
class KhivaLibrary(object):
class __KhivaLibrary:
def __init__(self):
try:
if platform.system() == 'Darwin':
self.c_khiva_library = ctypes.CDLL('libkhiva_c.dylib')
elif platform.system() == 'Windows':
# This lines below are needed to find dll_dir in python 3.8 an above
if sys.version_info.major == 3 and sys.version_info.minor >= 8:
import os
os.add_dll_directory(os.getenv("KHIVA_DLL_DIR", default=r'C:\Program Files\Khiva\v0\lib'))
self.c_khiva_library = ctypes.CDLL('khiva_c.dll')
elif platform.system() == 'Linux':
self.c_khiva_library = ctypes.CDLL('libkhiva_c.so')
except:
raise Exception("Khiva C++ library is required in order to use the Python Khiva library")
instance = None
def __new__(cls):
if not KhivaLibrary.instance:
KhivaLibrary.instance = KhivaLibrary.__KhivaLibrary()
return KhivaLibrary.instance
def __getattr__(self, name):
return getattr(self.instance, name)
def __setattr__(self, name):
return setattr(self.instance, name)
class KHIVABackend(Enum):
"""
KHIVA Backend.
"""
KHIVA_BACKEND_DEFAULT = 0
"""
Default Backend.
"""
KHIVA_BACKEND_CPU = 1
"""
CPU Backend.
"""
KHIVA_BACKEND_CUDA = 2
"""
CUDA Backend.
"""
KHIVA_BACKEND_OPENCL = 4
"""
OPENCL Backend.
"""
def get_backend_info():
""" Gets information from the current backend.
:return: A string with information from the current backend.
"""
info_pointer = ctypes.c_char_p((" " * 1000).encode('utf8'))
error_code = ctypes.c_int(0)
error_message = ctypes.create_string_buffer(KHIVA_ERROR_LENGTH)
KhivaLibrary().c_khiva_library.backend_info(ctypes.pointer(info_pointer),
ctypes.pointer(error_code),
error_message)
if error_code.value != 0:
raise Exception(str(error_message.value.decode()))
return info_pointer.value.decode('utf8')
def set_backend(backend):
""" Sets the KHIVABackend.
:param backend: The desired backend. KHIVABackend type.
"""
error_code = ctypes.c_int(0)
error_message = ctypes.create_string_buffer(KHIVA_ERROR_LENGTH)
KhivaLibrary().c_khiva_library.set_backend(ctypes.pointer(ctypes.c_int(backend.value)),
ctypes.pointer(error_code),
error_message)
if error_code.value != 0:
raise Exception(str(error_message.value.decode()))
def get_backend():
""" Gets the active backend.
:return: The active backend. KHIVABackend type.
"""
backend = (ctypes.c_int * 1)(*[0])
error_code = ctypes.c_int(0)
error_message = ctypes.create_string_buffer(KHIVA_ERROR_LENGTH)
KhivaLibrary().c_khiva_library.get_backend(ctypes.pointer(backend),
ctypes.pointer(error_code),
error_message)
if error_code.value != 0:
raise Exception(str(error_message.value.decode()))
return KHIVABackend(backend[0])
def get_backends():
""" Gets the available backends.
:return: The available backends.
"""
backends = (ctypes.c_int * 1)(*[0])
error_code = ctypes.c_int(0)
error_message = ctypes.create_string_buffer(KHIVA_ERROR_LENGTH)
KhivaLibrary().c_khiva_library.get_backends(ctypes.pointer(backends),
ctypes.pointer(error_code),
error_message)
if error_code.value != 0:
raise Exception(str(error_message.value.decode()))
return backends[0]
def set_device(device):
""" Sets the device.
:param device: The desired device.
"""
error_code = ctypes.c_int(0)
error_message = ctypes.create_string_buffer(KHIVA_ERROR_LENGTH)
KhivaLibrary().c_khiva_library.set_device(ctypes.pointer(ctypes.c_int(device)),
ctypes.pointer(error_code),
error_message)
if error_code.value != 0:
raise Exception(str(error_message.value.decode()))
def get_device_id():
""" Gets the active device.
:return: The active device.
"""
device = (ctypes.c_int * 1)(*[0])
error_code = ctypes.c_int(0)
error_message = ctypes.create_string_buffer(KHIVA_ERROR_LENGTH)
KhivaLibrary().c_khiva_library.get_device_id(ctypes.pointer(device),
ctypes.pointer(error_code),
error_message)
if error_code.value != 0:
raise Exception(str(error_message.value.decode()))
return device[0]
def get_device_count():
""" Gets the devices count.
:return: The devices count.
"""
device_count = (ctypes.c_int * 1)(*[0])
error_code = ctypes.c_int(0)
error_message = ctypes.create_string_buffer(KHIVA_ERROR_LENGTH)
KhivaLibrary().c_khiva_library.get_device_count(ctypes.pointer(device_count),
ctypes.pointer(error_code),
error_message)
if error_code.value != 0:
raise Exception(str(error_message.value.decode()))
return device_count[0]
def version():
""" Returns a string with the current version of the library.
:return: A string with the current version of the library.
"""
v = ctypes.c_char_p((" " * 40).encode('utf8'))
error_code = ctypes.c_int(0)
error_message = ctypes.create_string_buffer(KHIVA_ERROR_LENGTH)
KhivaLibrary().c_khiva_library.version(ctypes.pointer(v),
ctypes.pointer(error_code),
error_message)
if error_code.value != 0:
raise Exception(str(error_message.value.decode()))
return v.value.decode('utf8')