Skip to content

Commit e7104a6

Browse files
committed
improve python file structure
1 parent 6ec6acd commit e7104a6

File tree

3 files changed

+86
-81
lines changed

3 files changed

+86
-81
lines changed

PYTHON/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ add_custom_command(
2929
COMMENT "Copying pddrive.py to ${CMAKE_BINARY_DIR}/PYTHON"
3030
)
3131

32+
set(PY_SCRIPT_SOURCE ${CMAKE_SOURCE_DIR}/PYTHON/pdbridge.py)
33+
set(PY_SCRIPT_DEST ${CMAKE_BINARY_DIR}/PYTHON/pdbridge.py)
34+
# Create a custom command to copy the Python script
35+
add_custom_command(
36+
OUTPUT ${PY_SCRIPT_DEST}
37+
COMMAND ${CMAKE_COMMAND} -E copy ${PY_SCRIPT_SOURCE} ${PY_SCRIPT_DEST}
38+
DEPENDS ${PY_SCRIPT_SOURCE}
39+
COMMENT "Copying pdbridge.py to ${CMAKE_BINARY_DIR}/PYTHON"
40+
)
41+
3242
# Create a custom target named 'python' that depends on the output file
3343
add_custom_target(python
3444
DEPENDS ${PY_SCRIPT_DEST}
@@ -43,6 +53,7 @@ install(TARGETS superlu_dist_python
4353
ARCHIVE DESTINATION "${INSTALL_LIB_DIR}"
4454
)
4555
install(FILES pddrive.py DESTINATION "${INSTALL_LIB_DIR}/PYTHON")
56+
install(FILES pdbridge.py DESTINATION "${INSTALL_LIB_DIR}/PYTHON")
4657

4758

4859

PYTHON/pdbridge.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import os
2+
import ctypes
3+
import sys
4+
from sys import platform
5+
6+
def setup_pdbridge(sp, INT64):
7+
# Define the function signatures as shown in your original code
8+
sp.pdbridge_init.restype = None
9+
if INT64 == 0:
10+
sp.pdbridge_init.argtypes = [
11+
ctypes.c_int, ctypes.c_int, ctypes.c_int,
12+
ctypes.POINTER(ctypes.c_int),
13+
ctypes.POINTER(ctypes.c_int),
14+
ctypes.POINTER(ctypes.c_double),
15+
ctypes.POINTER(ctypes.c_void_p),
16+
ctypes.c_int,
17+
ctypes.POINTER(ctypes.c_char_p)
18+
]
19+
else:
20+
sp.pdbridge_init.argtypes = [
21+
ctypes.c_int64, ctypes.c_int64, ctypes.c_int64,
22+
ctypes.POINTER(ctypes.c_int64),
23+
ctypes.POINTER(ctypes.c_int64),
24+
ctypes.POINTER(ctypes.c_double),
25+
ctypes.POINTER(ctypes.c_void_p),
26+
ctypes.c_int,
27+
ctypes.POINTER(ctypes.c_char_p)
28+
]
29+
30+
sp.pdbridge_factor.restype = None
31+
sp.pdbridge_factor.argtypes = [ctypes.POINTER(ctypes.c_void_p)]
32+
sp.pdbridge_solve.restype = None
33+
sp.pdbridge_solve.argtypes = [ctypes.POINTER(ctypes.c_void_p), ctypes.c_int, ctypes.POINTER(ctypes.c_double)]
34+
sp.pdbridge_logdet.restype = None
35+
sp.pdbridge_logdet.argtypes = [ctypes.POINTER(ctypes.c_void_p), ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_double)]
36+
sp.pdbridge_free.restype = None
37+
sp.pdbridge_free.argtypes = [ctypes.POINTER(ctypes.c_void_p)]
38+
39+
def load_library(INT64):
40+
# Check platform and set library extension
41+
if platform == "linux" or platform == "linux2":
42+
pos = '.so'
43+
elif platform == "darwin":
44+
pos = '.dylib'
45+
elif platform == "win32":
46+
raise Exception("Windows is not yet supported")
47+
48+
DLLFOUND = False
49+
INSTALLDIR = os.getenv('SUPERLU_PYTHON_LIB_PATH')
50+
51+
DLL = os.path.abspath(__file__ + "/../../") + '/libsuperlu_dist_python' + pos
52+
if os.path.exists(DLL):
53+
DLLFOUND = True
54+
elif INSTALLDIR is not None:
55+
DLL = os.path.join(INSTALLDIR, 'libsuperlu_dist_python' + pos)
56+
if os.path.exists(DLL):
57+
DLLFOUND = True
58+
if DLLFOUND:
59+
sp = ctypes.cdll.LoadLibrary(DLL)
60+
setup_pdbridge(sp, INT64)
61+
return sp
62+
else:
63+
raise Exception("Cannot find the superlu_dist_python library. Try to set the SUPERLU_PYTHON_LIB_PATH environment variable correctly.")

PYTHON/pddrive.py

Lines changed: 12 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -7,62 +7,7 @@
77
import sys
88
import mpi4py
99
from mpi4py import MPI
10-
11-
12-
13-
def setup_pdbridge(sp,INT64):
14-
# Define the function signature for pdbridge_init
15-
sp.pdbridge_init.restype = None
16-
if(INT64==0):
17-
sp.pdbridge_init.argtypes = [
18-
ctypes.c_int, # int_t m
19-
ctypes.c_int, # int_t n
20-
ctypes.c_int, # int_t nnz
21-
ctypes.POINTER(ctypes.c_int), # int_t *rowind
22-
ctypes.POINTER(ctypes.c_int), # int_t *colptr
23-
ctypes.POINTER(ctypes.c_double), # double *nzval
24-
ctypes.POINTER(ctypes.c_void_p), # void **pyobj
25-
ctypes.c_int, # int argc
26-
ctypes.POINTER(ctypes.c_char_p) # char *argv[]
27-
]
28-
else:
29-
sp.pdbridge_init.argtypes = [
30-
ctypes.c_int64, # int_t m
31-
ctypes.c_int64, # int_t n
32-
ctypes.c_int64, # int_t nnz
33-
ctypes.POINTER(ctypes.c_int64), # int_t *rowind
34-
ctypes.POINTER(ctypes.c_int64), # int_t *colptr
35-
ctypes.POINTER(ctypes.c_double), # double *nzval
36-
ctypes.POINTER(ctypes.c_void_p), # void **pyobj
37-
ctypes.c_int, # int argc
38-
ctypes.POINTER(ctypes.c_char_p) # char *argv[]
39-
]
40-
41-
42-
sp.pdbridge_factor.restype = None
43-
sp.pdbridge_factor.argtypes = [
44-
ctypes.POINTER(ctypes.c_void_p), # void **pyobj
45-
]
46-
47-
sp.pdbridge_solve.restype = None
48-
sp.pdbridge_solve.argtypes = [
49-
ctypes.POINTER(ctypes.c_void_p), # void **pyobj
50-
ctypes.c_int, # int nrhs
51-
ctypes.POINTER(ctypes.c_double), # double *b_global
52-
]
53-
# Define the function signature for pdbridge_logdet
54-
sp.pdbridge_logdet.restype = None
55-
sp.pdbridge_logdet.argtypes = [
56-
ctypes.POINTER(ctypes.c_void_p), # void **pyobj
57-
ctypes.POINTER(ctypes.c_int), # int* sign
58-
ctypes.POINTER(ctypes.c_double), # double *logdet
59-
]
60-
# Define the function signature for pdbridge_free
61-
sp.pdbridge_free.restype = None
62-
sp.pdbridge_free.argtypes = [
63-
ctypes.POINTER(ctypes.c_void_p), # void **pyobj
64-
]
65-
10+
import pdbridge
6611

6712

6813
comm = MPI.COMM_WORLD
@@ -74,6 +19,8 @@ def setup_pdbridge(sp,INT64):
7419
print('MPI count:', size)
7520

7621

22+
####################################################################################################
23+
####################################################################################################
7724
####################### create the matrix
7825
INT64 = 1 # whether to use 64bit integer (requring superlu_dist to be compiled with 64-bit indexing)
7926
rng = np.random.default_rng()
@@ -101,6 +48,11 @@ def setup_pdbridge(sp,INT64):
10148
nnz=np.int64(m.nnz)
10249
n = np.int64(n)
10350

51+
52+
53+
54+
####################################################################################################
55+
####################################################################################################
10456
####################### handle options
10557
argv=sys.argv
10658
if(len(argv)==1): # options are not passed via command line, set them manually here. If they are not set here, default values are used
@@ -121,32 +73,11 @@ def setup_pdbridge(sp,INT64):
12173

12274

12375

124-
####################### load DLL
125-
import sys
126-
from sys import platform
127-
if platform == "linux" or platform == "linux2":
128-
pos='.so'
129-
elif platform == "darwin":
130-
pos='.dylib'
131-
elif platform == "win32":
132-
raise Exception(f"Windows is not yet supported")
133-
134-
DLLFOUND=False
135-
INSTALLDIR=os.getenv('SUPERLU_PYTHON_LIB_PATH')
136-
137-
DLL = os.path.abspath(__file__ + "/../../")+'/libsuperlu_dist_python%s'%(pos)
138-
if(os.path.exists(DLL)):
139-
DLLFOUND=True
140-
elif(INSTALLDIR is not None):
141-
DLL = INSTALLDIR+'/libsuperlu_dist_python%s'%(pos)
142-
if(os.path.exists(DLL)):
143-
DLLFOUND=True
144-
if(DLLFOUND == True):
145-
sp = ctypes.cdll.LoadLibrary(DLL)
146-
setup_pdbridge(sp,INT64)
147-
else:
148-
raise Exception(f"Cannot find the superlu_dist_python library. Try to set env variable SUPERLU_PYTHON_LIB_PATH correctly.")
14976

77+
####################################################################################################
78+
####################################################################################################
79+
####################### call the APIs
80+
sp = pdbridge.load_library(INT64)
15081
####################### initialization
15182
pyobj = ctypes.c_void_p()
15283
if(INT64==0):

0 commit comments

Comments
 (0)