NumPy 向量操作的並行化


NumPy 是一個強大的 Python 庫,用於儲存和操作大型多維陣列。雖然它比其他類似的集合(如列表)更快、更有效,但我們可以透過使用並行化機制進一步提高其效能。並行化意味著將任務拆分為多個程序以實現一個單一目標。Python 提供了幾種並行化 NumPy 向量操作的方法,包括 multiprocessing 和 numexpr 模組。

用於並行化 NumPy 向量操作的 Python 程式

讓我們討論一下並行化 NumPy 向量的方法

使用 multiprocessing

每個 Python 程式都被視為一個單一程序,有時可能需要同時執行多個程序。為此,Python 提供了一個名為 multiprocessing 的模組,該模組有一個名為 'Pool()' 的內建方法,允許同時建立和執行多個任務。

示例

以下示例說明了如何使用並行化和 multiprocessing 來對向量的每個元素進行平方運算。

方法

第一步是匯入 NumPy 庫,引用名稱為 'np',以及 multiprocessing,引用名稱為 'mp'。

接下來,建立一個使用者定義的方法以及一個引數。

  • 在此方法內部,使用 cpu_count() 確定可用 CPU 程序的數量。此值將進一步用於建立用於平行計算的工作程序池。

  • 然後,使用 Pool 建立一個程序池,該程序池將 'num_processes' 作為引數,指定可用的 CPU 程序數。

  • 現在,使用 map 方法將 square() 方法應用於輸入向量的每個元素。此方法會將輸入向量分成塊,並將每個塊分配給一個工作程序進行計算。map 函式會自動將工作負載分配到可用的程序中,並按與輸入向量相同的順序返回結果。

  • 對映完成後,我們將使用 close() 方法關閉池,並使用 join() 方法等待所有工作程序完成。

  • 最後,返回結果,該結果是透過平行計算獲得的平方值的列表。

現在,建立一個 NumPy 向量並將其作為引數傳遞給該方法,並顯示結果。

# importing required packages
import numpy as np
import multiprocessing as mp
# user-defined method to print square of vector
def square_vector_parallel(vector):
   num_processes = mp.cpu_count()
   pool = mp.Pool(processes = num_processes)
   result = pool.map(np.square, vector)
   pool.close()
   pool.join()
   return result
# creating a numpy vector
vec_tr = np.array([1, 2, 3, 4, 5])
# calling the method
result = square_vector_parallel(vec_tr)
# printing the result
print(result)

輸出

[1, 4, 9, 16, 25]

使用 numexpr

這個 Python 包能夠並行化計算並利用多個核心或 SIMD 指令,從而導致 NumPy 向量快速高效地執行。

示例 1

在以下示例中,我們將獲取兩個 NumPy 向量,並使用 'numexpr' 庫對它們執行並行加法運算。

# importing required packages
import numpy as np
import numexpr as nex
# creating two numpy vectors  
a1 = np.array([5, 2, 7, 4, 5])
a2 = np.array([4, 8, 3, 9, 5])
# printing the result
print(nex.evaluate('a1 + a2'))

輸出

[ 9 10 10 13 10]

示例 2

這是另一個演示 numexpr 用法的示例。我們將建立一個使用者定義的方法,該方法將向量作為引數。在此方法內部,將表示式 expr 定義為 'vector**2',它對輸入向量的每個元素進行平方運算,並將其傳遞給 numexpr 的 'evaluate()' 方法以並行方式計算表示式。

import numpy as np
import numexpr as nex
# user-defined method to print square of vector
def square_vector_parallel(vector):
   expr = 'vector**2'
   result = nex.evaluate(expr)
   return result
# creating a numpy vector
vec_tr = np.array([4, 8, 6, 9, 5])
# calling the method
result = square_vector_parallel(vec_tr)
# printing the result
print(result)

輸出

[16 64 36 81 25]

示例 3

在前面示例的相同程式碼中,我們使用 'set_num_threads()' 方法顯式地將執行緒數設定為 4。這允許我們在表示式計算中執行執行緒級並行性。

import numpy as np
import numexpr as nex
# user-defined method to print square of vector
def square_vector_parallel(vector):
# Set the number of threads to utilize
   nex.set_num_threads(4)  
   expr = 'vector**2'
   result = nex.evaluate(expr)
   return result
# creating a numpy vector
vec_tr = np.array([4, 8, 6, 9, 5])
# calling the method
result = square_vector_parallel(vec_tr)
# printing the result
print(result)

輸出

[16 64 36 81 25]

結論

在本文中,我們討論了幾個示例程式,以演示如何並行化 NumPy 向量操作。我們使用了兩個最流行和廣泛使用的 Python 庫來進行併發操作,分別名為 multiprocessing 和 numexpr。

更新於: 2023年7月21日

308 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.