Dask 배열

Python에서 Dask로 병렬 프로그래밍

James Fulton

Climate informatics researcher

배열 청크 나누기

배열이 하나의 덩어리로 보임

Python에서 Dask로 병렬 프로그래밍

배열 청크 나누기

배열이 여러 청크로 나뉘어 보임

Python에서 Dask로 병렬 프로그래밍

NumPy vs. Dask 배열

배열이 하나의 덩어리로 보임

import numpy as np

x = np.ones((4000, 6000))
print(x.sum())
24000000.0
  • 실행 시간: 740ms

배열이 여러 청크로 나뉘어 보임

import dask.array as da

x = da.ones((4000, 6000), chunks=(1000,2000))
print(x.sum().compute())
24000000.0
  • 실행 시간: 60ms
Python에서 Dask로 병렬 프로그래밍

Dask 배열 태스크 그래프

최종 결과로 수렴하는 태스크 그래프 가지를 보여줌

Python에서 Dask로 병렬 프로그래밍

Dask 배열 메서드

Dask 배열은 거의 모든 NumPy 배열 메서드를 지원합니다.

  • x.max()
  • x.min()
  • x.sum()
  • x.mean()
  • 등등
print(sum_down_columns.compute())
array([1000., 1000., 1000., 1000., 
    1000., 1000., 1000., 1000., 1000.,
    1000.])
Python에서 Dask로 병렬 프로그래밍

NumPy처럼 Dask 배열 다루기

# Dask 배열로 지연 계산
y1 = x**2 + 2*x + 1

# 지연 슬라이싱
y2 = x[:10]

# NumPy 함수 적용도 지연됨
y3 = np.sin(x)
print(y1)
dask.array<add, shape=(1000, 10), ...
print(y2)
dask.array<getitem, shape=(10, 10), ...
print(y3)
dask.array<sin, shape=(1000, 10), ...
Python에서 Dask로 병렬 프로그래밍

이미지 배열 불러오기

import dask.array as da

import da.image
image_array = da.image.imread('images/*.png')
print(image_array)
dask.array<imread, shape=(40000, 256, 256, 3), dtype=uint8, 
    chunksize=(1, 256, 256, 3), chunktype=numpy.ndarray>
Python에서 Dask로 병렬 프로그래밍

청크별 사용자 함수 적용

def instagram_filter(image):
    ...
    return pretty_image

# 각 이미지를 독립적으로 처리 pretty_image_array = image_array.map_blocks(instagram_filter)
print(pretty_image_array)
dask.array<instagram_filter, shape=(40000, 256, 256, 3), dtype=uint8, 
    chunksize=(1, 256, 256, 3), chunktype=numpy.ndarray>
Python에서 Dask로 병렬 프로그래밍

연습해 봅시다!

Python에서 Dask로 병렬 프로그래밍

Preparing Video For Download...