-
-
Save ayutaz/51a77142fc73ebe325469312ff8b623b to your computer and use it in GitHub Desktop.
pythoの並列計算,最大のプロセス数とは・・・? ref: https://qiita.com/ayousanz/items/4f82efb47c217b07a0a2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import multiprocessing as mp | |
L = 100000 | |
proc = 500 # 8並列とする | |
# 各プロセスが実行する計算 | |
def subcalc(p): # p = 0,1,...,7 | |
subtotal = 0 | |
# iの範囲を設定 | |
ini = L * p / proc | |
fin = L * (p+1) / proc | |
# 計算を実行 | |
for i in range(ini, fin): | |
for j in range(L): | |
subtotal += i * j | |
return subtotal | |
# 8個のプロセスを用意 | |
pool = mp.Pool(proc) | |
# 各プロセスに subcalc(p) を実行させる | |
# ここで p = 0,1,...,7 | |
# callbackには各戻り値がlistとして格納される | |
callback = pool.map(subcalc, range(8)) | |
# 各戻り値の総和を計算 | |
total = sum(callback) | |
print("core number:"+str(proc)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment