टास्क ग्राफ और शेड्यूलिंग मेथड्स

Python में Dask के साथ Parallel Programming

James Fulton

Climate Informatics Researcher

टास्क ग्राफ देखना

# 2 delayed ऑब्जेक्ट बनाएँ
delayed_num1 = delayed(my_square_function)(3)
delayed_num2 = delayed(my_square_function)(4)

# इन्हें जोड़ें
result = delayed_num1 + delayed_num2

# टास्क ग्राफ प्लॉट करें result.visualize()

परिणाम निकालने के चरण दिखाने वाला डायग्राम. my-square-function दो बार चलती है, और दोनों आउटपुट add फंक्शन में जाते हैं. इससे एक आउटपुट मिलता है.

Python में Dask के साथ Parallel Programming

ओवरलैपिंग टास्क ग्राफ

delayed_intermediate = delayed(my_square_function)(3)

# ये दोनों परिणाम delayed_intermediate_result का ही उपयोग करते हैं
delayed_result1 = delayed_intermediate - 5
delayed_result2 = delayed_intermediate + 4
Python में Dask के साथ Parallel Programming

ओवरलैपिंग टास्क ग्राफ

delayed_result1.visualize()

result 1 के लिए टास्क ग्राफ दिखाने वाला डायग्राम.

delayed_result2.visualize()

result 2 के लिए टास्क ग्राफ दिखाने वाला डायग्राम.

Python में Dask के साथ Parallel Programming

ओवरलैपिंग टास्क ग्राफ

# टास्क ग्राफ प्लॉट करें
dask.visualize(delayed_result1, delayed_result2)

एक टास्क ग्राफ जो दिखाता है कि result 1 और result 2 एक intermediate परिणाम साझा करते हैं.

Python में Dask के साथ Parallel Programming

मल्टी-थ्रेडिंग बनाम पैरेलल प्रोसेसिंग

डेटा मूव करना

पैरेलल प्रोसेसिंग
  • हर प्रोसेस का अपना RAM स्पेस होता है
मल्टी-थ्रेडिंग
  • थ्रेड्स एक ही RAM स्पेस साझा करते हैं
Python में Dask के साथ Parallel Programming

मल्टी-थ्रेडिंग बनाम पैरेलल प्रोसेसिंग

# दो बड़े arrays पर sum चलाएँ
sum1 = delayed(np.sum)(big_array1)
sum2 = delayed(np.sum)(big_array2)

# प्रोसेसेज़ से compute करें
dask.compute(sum1, sum2)
  • पैरेलल प्रोसेसिंग के साथ धीमा

डायग्राम दिखाता है कि एक Python प्रोसेस से निकले दो arrays दो अन्य Python प्रोसेसेज़ को भेजने पड़ते हैं.

Python में Dask के साथ Parallel Programming

मल्टी-थ्रेडिंग बनाम पैरेलल प्रोसेसिंग

# दो बड़े arrays पर sum चलाएँ
sum1 = delayed(np.sum)(big_array1)
sum2 = delayed(np.sum)(big_array2)

# थ्रेड्स से compute करें
dask.compute(sum1, sum2)
  • मल्टी-थ्रेडिंग के साथ तेज

डायग्राम दिखाता है कि दोनों arrays को कॉपी करने की ज़रूरत नहीं है.

Python में Dask के साथ Parallel Programming

GIL

ग्लोबल इंटरप्रेटर लॉक (GIL) - एक समय में केवल एक थ्रेड Python स्क्रिप्ट पढ़ सकता है

def sum_to_n(n):
    """Sums numbers from 0 to n"""
    total = 0
    for i in range(n+1):
        total += i
    return total
  • यहाँ मल्टी-थ्रेडिंग मदद नहीं करेगी
  • पैरेलल प्रोसेसिंग करेगी
sum1 = delayed(sum_to_n)(1000)
sum2 = delayed(sum_to_n)(1000)
Python में Dask के साथ Parallel Programming

उदाहरण टाइमिंग - GIL

तीन गैंट चार्ट जो एक सरल Python फंक्शन को 16 बार चलाने के टाइमिंग दिखाते हैं. तीन शेड्यूलिंग मेथड्स में से processes सबसे तेज रहा.

Python में Dask के साथ Parallel Programming

फंक्शन जो GIL रिलीज़ करते हैं

  • जैसे pd.read_csv() फंक्शन GIL रिलीज़ करता है
df1 = delayed(pd.read_csv)('file1.csv')
df2 = delayed(pd.read_csv)('file2.csv')
Python में Dask के साथ Parallel Programming

उदाहरण टाइमिंग - डेटा लोड करना

तीन गैंट चार्ट जो CSV से डेटा लोड करने वाला फंक्शन 16 बार चलाने के टाइमिंग दिखाते हैं. तीन शेड्यूलिंग मेथड्स में से threads सबसे तेज रहा.

Python में Dask के साथ Parallel Programming

सारांश

Threads

  • शुरू करने में बहुत तेज़
  • मुख्य सेशन के साथ मेमोरी स्पेस साझा करते हैं
  • मेमोरी ट्रांसफर की ज़रूरत नहीं
  • GIL से सीमित, जो एक समय में एक थ्रेड को ही कोड पढ़ने देता है

Processes

  • सेटअप में समय और मेमोरी लेते हैं
  • अलग मेमोरी पूल होते हैं
  • आपस में और मुख्य Python सेशन तक डेटा ट्रांसफर बहुत धीमा
  • हर एक का अपना GIL, इसलिए बारी-बारी से कोड नहीं पढ़ना पड़ता
Python में Dask के साथ Parallel Programming

अभ्यास करते हैं!

Python में Dask के साथ Parallel Programming

Preparing Video For Download...