Dask 入門

在 Python 中使用 Dask 進行平行程式設計

James Fulton

Climate Informatics Researcher

用多核心加速運算

  • 電腦有多核心
  • 程式碼必須寫成可用多核心
  • 可用 Dask 套件來達成
  • 更快完成運算
在 Python 中使用 Dask 進行平行程式設計

並行程式設計

一張圖示出一串任務與單一路徑逐一執行。

在 Python 中使用 Dask 進行平行程式設計

多執行緒

把一串任務分成兩部分。

在 Python 中使用 Dask 進行平行程式設計

多執行緒

兩組任務分派到兩個不同的 CPU 核心。

在 Python 中使用 Dask 進行平行程式設計

多執行緒

兩組任務在同一個 Python 行程中執行。

在 Python 中使用 Dask 進行平行程式設計

平行處理

兩組任務在兩個不同的 Python 行程中執行。

在 Python 中使用 Dask 進行平行程式設計

平行程式設計

多執行緒

兩組任務由兩個 CPU 核心執行,且在同一個 Python 行程內。

平行處理

兩組任務由兩個 CPU 核心執行,且在兩個不同的 Python 行程內。

在 Python 中使用 Dask 進行平行程式設計

延遲評估(Lazy evaluation)

  • 直到需要結果時才執行運算
  • 將計算步驟先儲存,稍後再執行
  • Dask 會把任務分配到執行緒或行程
在 Python 中使用 Dask 進行平行程式設計

Dask delayed(延遲)

from dask import delayed

def my_square_function(x):
    return x**2

# Create delayed version of above function delayed_square_function = delayed(my_square_function)
在 Python 中使用 Dask 進行平行程式設計

Dask delayed(延遲)

from dask import delayed

def my_square_function(x):
    return x**2

# Create delayed version of above function
delayed_square_function = delayed(my_square_function)

# Use the delayed function with input 4
delayed_result = delayed_square_function(4)


# Print the delayed answer print(delayed_result)
Delayed('my_square_function-7f71b132-70a9-457a-aa52-604e8c34f8a7')
在 Python 中使用 Dask 進行平行程式設計

Dask delayed(延遲)

from dask import delayed

def my_square_function(x):
    return x**2

# Delay and use function
delayed_result = delayed(my_square_function)(4)

print(delayed_result)
Delayed('my_square_function-7f71b132-70a9-457a-aa52-604e8c34f8a7')
在 Python 中使用 Dask 進行平行程式設計

計算結果

from dask import delayed

def my_square_function(x):
    return x**2

delayed_result = delayed(my_square_function)(4)

real_result = delayed_result.compute() # <- This line is where the calculation happens

# Print the answer
print(real_result)
16
在 Python 中使用 Dask 進行平行程式設計

對延遲物件進行運算

delayed_result1 = delayed(my_square_function)(4)

# Math operations return delayed object
delayed_result2 = (4 + delayed_result1) * 5

print(delayed_result2.compute())
100
在 Python 中使用 Dask 進行平行程式設計

延遲評估(Lazy evaluation)

x_list = [30, 85, 14, 12, 27, 62, 89, 15, 78,  0]

sum_of_squares = 0

for x in x_list:
    # Square and add numbers
    sum_of_squares += delayed(my_square_function)(x)
在 Python 中使用 Dask 進行平行程式設計

延遲評估(Lazy evaluation)

x_list = [30, 85, 14, 12, 27, 62, 89, 15, 78,  0]

sum_of_squares = 0

for x in x_list:
    # Square and add numbers
    sum_of_squares += delayed(my_square_function)(x)

result = sum_of_squares.compute()

# Print the answer
print(result)
27268
在 Python 中使用 Dask 進行平行程式設計

共用計算結果

delayed_intermediate = delayed(my_square_function)(3)

# These two results both use delayed_intermediate
delayed_result1 = delayed_intermediate - 5
delayed_result2 = delayed_intermediate + 4

# delayed_3_squared will be computed twice
print('delayed_result1:', delayed_result1.compute())
print('delayed_result2:', delayed_result2.compute())
delayed_result1: 4
delayed_result2: 13
在 Python 中使用 Dask 進行平行程式設計

共用計算結果

import dask

# delayed_intermediate will be computed once
comp_result1, comp_result2 = dask.compute(delayed_result1, delayed_result2)

print('comp_result1:', comp_result1)
print('comp_result2:', comp_result2)
delayed_result1: 4
delayed_result2: 13
在 Python 中使用 Dask 進行平行程式設計

一起來練習吧!

在 Python 中使用 Dask 進行平行程式設計

Preparing Video For Download...