Create gpu_nano_f1.py
Browse files- gpu_nano_f1.py +34 -0
gpu_nano_f1.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import ctypes
|
| 2 |
+
import numpy as np
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
class GPUNanoF1:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
# Cherche le kernel dans le dossier local
|
| 8 |
+
path = os.path.join(os.path.dirname(__file__), 'f1_kernel.so')
|
| 9 |
+
if not os.path.exists(path):
|
| 10 |
+
raise Exception("Le Kernel F-1 n'est pas compilé. Lancez 'sh compile.sh' d'abord.")
|
| 11 |
+
|
| 12 |
+
self.lib = ctypes.CDLL(path)
|
| 13 |
+
self.lib.launch_f1_kernel.argtypes = [
|
| 14 |
+
ctypes.POINTER(ctypes.c_float),
|
| 15 |
+
ctypes.POINTER(ctypes.c_float),
|
| 16 |
+
ctypes.POINTER(ctypes.c_float),
|
| 17 |
+
ctypes.c_int
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
+
def compute(self, A, B):
|
| 21 |
+
# Conversion automatique en float32 pour le Kernel
|
| 22 |
+
A = A.astype(np.float32)
|
| 23 |
+
B = B.astype(np.float32)
|
| 24 |
+
size = A.shape[0]
|
| 25 |
+
C = np.zeros((size, size), dtype=np.float32)
|
| 26 |
+
|
| 27 |
+
self.lib.launch_f1_kernel(
|
| 28 |
+
A.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
|
| 29 |
+
B.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
|
| 30 |
+
C.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
|
| 31 |
+
size
|
| 32 |
+
)
|
| 33 |
+
return C
|
| 34 |
+
|