2015-04-10 13 views
6

Tôi muốn biết liệu có thể thay đổi lúc (Python) thời gian chạy tối đa các luồng được sử dụng bởi OpenBLAS sau lưng không?Đặt số lượng chuỗi tối đa khi chạy trên numpy/openblas

Tôi biết có thể đặt nó trước khi chạy trình thông dịch qua biến môi trường OMP_NUM_THREADS, nhưng tôi muốn thay đổi nó khi chạy.

Thông thường, khi sử dụng MKL thay vì OpenBLAS, có thể:

import mkl 
mkl.set_num_threads(n) 
+2

Bạn có thể thử gọi hàm 'openblas_set_num_threads' bằng mô-đun' ctypes'. Tương tự như [câu hỏi này.] (Http://stackoverflow.com/q/28283112/2379410) –

Trả lời

10

Bạn có thể làm điều này bằng cách gọi openblas_set_num_threads chức năng sử dụng ctypes. Tôi thường thấy mình muốn làm điều này, vì vậy tôi đã viết một chút quản lý bối cảnh:

import contextlib 
import ctypes 
from ctypes.util import find_library 

# Prioritize hand-compiled OpenBLAS library over version in /usr/lib/ 
# from Ubuntu repos 
try_paths = ['/opt/OpenBLAS/lib/libopenblas.so', 
      '/lib/libopenblas.so', 
      '/usr/lib/libopenblas.so.0', 
      find_library('openblas')] 
openblas_lib = None 
for libpath in try_paths: 
    try: 
     openblas_lib = ctypes.cdll.LoadLibrary(libpath) 
     break 
    except OSError: 
     continue 
if openblas_lib is None: 
    raise EnvironmentError('Could not locate an OpenBLAS shared library', 2) 


def set_num_threads(n): 
    """Set the current number of threads used by the OpenBLAS server.""" 
    openblas_lib.openblas_set_num_threads(int(n)) 


# At the time of writing these symbols were very new: 
# https://github.com/xianyi/OpenBLAS/commit/65a847c 
try: 
    openblas_lib.openblas_get_num_threads() 
    def get_num_threads(): 
     """Get the current number of threads used by the OpenBLAS server.""" 
     return openblas_lib.openblas_get_num_threads() 
except AttributeError: 
    def get_num_threads(): 
     """Dummy function (symbol not present in %s), returns -1.""" 
     return -1 
    pass 

try: 
    openblas_lib.openblas_get_num_procs() 
    def get_num_procs(): 
     """Get the total number of physical processors""" 
     return openblas_lib.openblas_get_num_procs() 
except AttributeError: 
    def get_num_procs(): 
     """Dummy function (symbol not present), returns -1.""" 
     return -1 
    pass 


@contextlib.contextmanager 
def num_threads(n): 
    """Temporarily changes the number of OpenBLAS threads. 

    Example usage: 

     print("Before: {}".format(get_num_threads())) 
     with num_threads(n): 
      print("In thread context: {}".format(get_num_threads())) 
     print("After: {}".format(get_num_threads())) 
    """ 
    old_n = get_num_threads() 
    set_num_threads(n) 
    try: 
     yield 
    finally: 
     set_num_threads(old_n) 

Bạn có thể sử dụng nó như thế này:

with num_threads(8): 
    np.dot(x, y) 

Như đã đề cập trong các ý kiến, openblas_get_num_threadsopenblas_get_num_procs rất mới các tính năng tại thời điểm viết và do đó không có sẵn trừ khi bạn biên dịch OpenBLAS từ phiên bản mã nguồn mới nhất.

+2

lưu ý rằng khi v0.2.14 pthread openblas_get_num_procs không tính đến ái lực để nó có thể dẫn đến oversubscription khi số lượng cpus khả dụng là bị hạn chế (ví dụ như trong các thùng chứa), sử dụng len (os.sched_getaffinity (0)) (python> = 3.3) để thay thế – jtaylor

Các vấn đề liên quan