Giới thiệu về Dask

Lập trình song song với Dask trong Python

James Fulton

Climate Informatics Researcher

Tăng tốc tính toán với đa lõi

  • Máy tính có nhiều lõi
  • Cần viết mã để tận dụng chúng
  • Có thể dùng gói Dask để làm việc này
  • Hoàn thành phép tính nhanh hơn
Lập trình song song với Dask trong Python

Lập trình đồng thời

Sơ đồ hiển thị danh sách tác vụ và một đường đi qua chúng.

Lập trình song song với Dask trong Python

Đa luồng

Danh sách tác vụ được tách thành hai phần.

Lập trình song song với Dask trong Python

Đa luồng

Hai nhóm tác vụ được gửi tới hai lõi CPU khác nhau.

Lập trình song song với Dask trong Python

Đa luồng

Hai nhóm tác vụ chạy trong cùng một tiến trình Python.

Lập trình song song với Dask trong Python

Xử lý song song

Hai nhóm tác vụ giờ chạy từ hai tiến trình Python khác nhau.

Lập trình song song với Dask trong Python

Lập trình song song

Đa luồng

Hai nhóm tác vụ chạy trên hai lõi CPU trong cùng một tiến trình Python.

Xử lý song song

Hai nhóm tác vụ chạy trên hai lõi CPU trong hai tiến trình Python khác nhau.

Lập trình song song với Dask trong Python

Đánh giá lười (lazy evaluation)

  • Chỉ tính khi cần kết quả
  • Lưu các bước tính cho sau này
  • Dask phân chia tác vụ giữa luồng hoặc tiến trình
Lập trình song song với Dask trong Python

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)
Lập trình song song với Dask trong Python

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')
Lập trình song song với Dask trong Python

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')
Lập trình song song với Dask trong Python

Tính kết quả

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
Lập trình song song với Dask trong Python

Dùng phép toán trên đối tượng delayed

delayed_result1 = delayed(my_square_function)(4)

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

print(delayed_result2.compute())
100
Lập trình song song với Dask trong Python

Đánh giá lười (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)
Lập trình song song với Dask trong Python

Đánh giá lười (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
Lập trình song song với Dask trong Python

Chia sẻ tính toán

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
Lập trình song song với Dask trong Python

Chia sẻ tính toán

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
Lập trình song song với Dask trong Python

Ayo berlatih!

Lập trình song song với Dask trong Python

Preparing Video For Download...