Skip to content

Commit beb59b9

Browse files
Updated Memory and Miscellaneous functions to Python style
1 parent 8143077 commit beb59b9

File tree

3 files changed

+111
-22
lines changed

3 files changed

+111
-22
lines changed

mkl-service/_mkl_service.pxd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ cdef extern from "mkl.h":
5656
int MKL_ENABLE_AVX
5757
int MKL_ENABLE_SSE4_2
5858

59+
int MKL_BLACS_CUSTOM
60+
int MKL_BLACS_MSMPI
61+
int MKL_BLACS_INTELMPI
62+
#int MKL_BLACS_MPICH
63+
5964
# unsigned int vmlSetMode(unsigned int mode)
6065
# In
6166
int VML_HA

mkl-service/_mkl_service.pyx

Lines changed: 104 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,19 @@ from enum import IntEnum
33

44

55
class enums(IntEnum):
6-
# MKL Function Domains
6+
# MKL Function Domains Constants
77
MKL_DOMAIN_BLAS = mkl.MKL_DOMAIN_BLAS
88
MKL_DOMAIN_FFT = mkl.MKL_DOMAIN_FFT
99
MKL_DOMAIN_VML = mkl.MKL_DOMAIN_VML
1010
MKL_DOMAIN_PARDISO = mkl.MKL_DOMAIN_PARDISO
1111
MKL_DOMAIN_ALL = mkl.MKL_DOMAIN_ALL
1212

13-
# MKL_INT64 mkl_peak_mem_usage(int mode)
14-
# In
13+
# MKL Peak Memory Usage Constants
1514
MKL_PEAK_MEM_ENABLE = mkl.MKL_PEAK_MEM_ENABLE
1615
MKL_PEAK_MEM_DISABLE = mkl.MKL_PEAK_MEM_DISABLE
1716
MKL_PEAK_MEM = mkl.MKL_PEAK_MEM
1817
MKL_PEAK_MEM_RESET = mkl.MKL_PEAK_MEM_RESET
1918

20-
# int mkl_set_memory_limit(int mem_type, size_t limit)
21-
# In
22-
MKL_MEM_MCDRAM = mkl.MKL_MEM_MCDRAM
23-
2419
# CNR Control Constants
2520
MKL_CBWR_AUTO = mkl.MKL_CBWR_AUTO
2621
MKL_CBWR_COMPATIBLE = mkl.MKL_CBWR_COMPATIBLE
@@ -40,14 +35,19 @@ class enums(IntEnum):
4035
MKL_CBWR_ERR_UNSUPPORTED_BRANCH = mkl.MKL_CBWR_ERR_UNSUPPORTED_BRANCH
4136
MKL_CBWR_ERR_MODE_CHANGE_FAILURE = mkl.MKL_CBWR_ERR_MODE_CHANGE_FAILURE
4237

43-
# int mkl_enable_instructions(int isa)
44-
# In
38+
# ISA Constants
4539
MKL_ENABLE_AVX512 = mkl.MKL_ENABLE_AVX512
4640
MKL_ENABLE_AVX512_MIC = mkl.MKL_ENABLE_AVX512_MIC
4741
MKL_ENABLE_AVX2 = mkl.MKL_ENABLE_AVX2
4842
MKL_ENABLE_AVX = mkl.MKL_ENABLE_AVX
4943
MKL_ENABLE_SSE4_2 = mkl.MKL_ENABLE_SSE4_2
5044

45+
# MPI Implementation Constants
46+
MKL_BLACS_CUSTOM = mkl.MKL_BLACS_CUSTOM
47+
MKL_BLACS_MSMPI = mkl.MKL_BLACS_MSMPI
48+
MKL_BLACS_INTELMPI = mkl.MKL_BLACS_INTELMPI
49+
#MKL_BLACS_MPICH = mkl.MKL_BLACS_MPICH
50+
5151
# unsigned int vmlSetMode(unsigned int mode)
5252
# In
5353
VML_HA = mkl.VML_HA
@@ -73,14 +73,20 @@ class enums(IntEnum):
7373
VML_STATUS_OVERFLOW = mkl.VML_STATUS_OVERFLOW
7474
VML_STATUS_UNDERFLOW = mkl.VML_STATUS_UNDERFLOW
7575

76-
# MKL Function Domains
76+
# MKL Function Domains Constants
7777
__mkl_domain_enums = {'blas': mkl.MKL_DOMAIN_BLAS,
7878
'fft': mkl.MKL_DOMAIN_FFT,
7979
'vml': mkl.MKL_DOMAIN_VML,
8080
'pardiso': mkl.MKL_DOMAIN_PARDISO,
8181
'all': mkl.MKL_DOMAIN_ALL}
8282

83-
# CNR Control Constants
83+
# MKL Peak Memory Usage Constants
84+
__mkl_peak_mem_usage_enums = {'enable': mkl.MKL_PEAK_MEM_ENABLE,
85+
'disable': mkl.MKL_PEAK_MEM_DISABLE,
86+
'peak_mem': mkl.MKL_PEAK_MEM,
87+
'peak_mem_reset': mkl.MKL_PEAK_MEM_RESET}
88+
89+
# CNR Control Constants Constants
8490
__mkl_cbwr_set_in_enums = {'auto': mkl.MKL_CBWR_AUTO,
8591
'compatible': mkl.MKL_CBWR_COMPATIBLE,
8692
'sse2': mkl.MKL_CBWR_SSE2,
@@ -108,6 +114,19 @@ __mkl_cbwr_get_out_enums.update({value: key for key, value in __mkl_cbwr_set_in_
108114
__mkl_cbwr_get_auto_branch_out_enums = {}
109115
__mkl_cbwr_get_auto_branch_out_enums.update({value: key for key, value in __mkl_cbwr_set_in_enums.items()})
110116

117+
# ISA Constants
118+
__mkl_isa_enums = {'avx512': mkl.MKL_ENABLE_AVX512,
119+
'avx512_mic': mkl.MKL_ENABLE_AVX512_MIC,
120+
'avx2': mkl.MKL_ENABLE_AVX2,
121+
'avx': mkl.MKL_ENABLE_AVX,
122+
'sse4_2': mkl.MKL_ENABLE_SSE4_2}
123+
124+
# MPI Implementation Constants
125+
__mkl_blacs_enums = {'custom': mkl.MKL_BLACS_CUSTOM,
126+
'msmpi': mkl.MKL_BLACS_MSMPI,
127+
'intelmpi': mkl.MKL_BLACS_INTELMPI}
128+
#'mpich': mkl.MKL_BLACS_MPICH}
129+
111130

112131
'''
113132
# MKL support functions
@@ -287,12 +306,35 @@ def mkl_mem_stat():
287306
return AllocatedBytes, AllocatedBuffers
288307

289308

290-
def mkl_peak_mem_usage(mode):
291-
return mkl.mkl_peak_mem_usage(mode)
309+
def mkl_peak_mem_usage(mem_const):
310+
mem_const_type = type(mem_const)
311+
if mem_const_type is str:
312+
assert(mem_const in __mkl_peak_mem_usage_enums.keys())
313+
mem_const = __mkl_peak_mem_usage_enums[mem_const]
314+
else:
315+
assert((mem_const_type is int) and (mem_const in __mkl_peak_mem_usage_enums.values()))
316+
317+
memory_allocator = mkl.mkl_peak_mem_usage(mem_const)
318+
assert(type(memory_allocator) is int)
319+
assert(memory_allocator >= -1)
320+
321+
if memory_allocator == -1:
322+
memory_allocator = 'error'
323+
324+
return memory_allocator
292325

293326

294-
def mkl_set_memory_limit(mem_type, limit):
295-
return mkl.mkl_set_memory_limit(mem_type, limit)
327+
def mkl_set_memory_limit(limit):
328+
assert(limit >= 0)
329+
status = mkl.mkl_set_memory_limit(mkl.MKL_MEM_MCDRAM, limit)
330+
assert((status == 0) or (status == 1))
331+
332+
if status == 1:
333+
status = 'success'
334+
else:
335+
status = 'error'
336+
337+
return status
296338

297339

298340
'''
@@ -301,7 +343,7 @@ def mkl_set_memory_limit(mem_type, limit):
301343
int mkl_cbwr_get(int option)
302344
int mkl_cbwr_get_auto_branch()
303345
'''
304-
def mkl_cbwr_set(branch=''):
346+
def mkl_cbwr_set(branch=None):
305347
branch_type = type(branch)
306348
if branch_type is str:
307349
assert(branch in __mkl_cbwr_set_in_enums.keys())
@@ -315,7 +357,7 @@ def mkl_cbwr_set(branch=''):
315357
return __mkl_cbwr_set_out_enums[status]
316358

317359

318-
def mkl_cbwr_get(cnr_const=''):
360+
def mkl_cbwr_get(cnr_const=None):
319361
cnr_const_type = type(cnr_const)
320362
if cnr_const_type is str:
321363
assert(cnr_const in __mkl_cbwr_get_in_enums)
@@ -346,11 +388,31 @@ def mkl_cbwr_get_auto_branch():
346388
int mkl_verbose(int enable)
347389
int mkl_set_mpi (int vendor, const char *custom_library_name)
348390
'''
349-
def mkl_enable_instructions(isa):
350-
return mkl.mkl_enable_instructions(isa)
391+
__mkl_isa_enums
392+
def mkl_enable_instructions(isa=None):
393+
isa_type = type(isa)
394+
if isa_type is str:
395+
assert(isa in __mkl_isa_enums)
396+
isa = __mkl_isa_enums[isa]
397+
else:
398+
assert(issubclass(isa_type, IntEnum))
399+
assert(type(isa.value) is int)
400+
assert(isa.value in __mkl_isa_enums.values())
401+
isa = isa.value
402+
403+
status = mkl.mkl_enable_instructions(isa)
404+
assert((status == 0) or (status == 1))
405+
406+
if (status == 1):
407+
status = 'success'
408+
else:
409+
status = 'error'
410+
411+
return status
351412

352413

353414
def mkl_set_env_mode(mode):
415+
assert((mode == 0) or (mode == 1))
354416
return mkl.mkl_set_env_mode(mode)
355417

356418

@@ -360,9 +422,31 @@ def mkl_verbose(enable):
360422

361423

362424
def mkl_set_mpi(vendor, custom_library_name):
425+
vendor_type = type(vendor)
426+
if vendor_type is str:
427+
assert(vendor in __mkl_blacs_enums)
428+
vendor = __mkl_blacs_enums[vendor]
429+
else:
430+
assert(issubclass(vendor_type, IntEnum))
431+
assert(type(vendor.value) is int)
432+
assert(vendor.value in __mkl_blacs_enums.values())
433+
vendor = vendor.value
434+
363435
cdef bytes c_bytes = custom_library_name.encode()
364436
cdef char* c_string = c_bytes
365-
return mkl.mkl_set_mpi(vendor, c_string)
437+
status = mkl.mkl_set_mpi(vendor, c_string)
438+
assert(status in range(-3, 1))
439+
440+
if status == 0:
441+
status = 'success'
442+
elif status == -1:
443+
status = 'vendor_invalid'
444+
elif status == '-2':
445+
status = 'custom_library_name_invalid'
446+
else:
447+
status = 'the MPI library cannot be set at this point'
448+
449+
return status
366450

367451

368452
'''

mkl-service/tests/test_mkl_service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def test_mkl_peak_mem_usage(self):
8282
mkl_service.mkl_peak_mem_usage(0)
8383

8484
def test_mkl_set_memory_limit(self):
85-
mkl_service.mkl_set_memory_limit(0, 128)
85+
mkl_service.mkl_set_memory_limit(128)
8686

8787

8888
class test_conditional_numerical_reproducibility_control:
@@ -110,7 +110,7 @@ def test_mkl_verbose(self):
110110
mkl_service.mkl_verbose(False)
111111

112112
def test_mkl_set_mpi(self):
113-
mkl_service.mkl_set_mpi(1, 'test')
113+
mkl_service.mkl_set_mpi('intelmpi', 'test')
114114

115115
class test_vm_service_functions():
116116
# https://software.intel.com/en-us/mkl-developer-reference-c-vm-service-functions

0 commit comments

Comments
 (0)