回答編集履歴

1

edit

2018/07/25 21:57

投稿

mkgrei
mkgrei

スコア8560

test CHANGED
@@ -27,3 +27,163 @@
27
27
 
28
28
 
29
29
  「直したソースコード」が誤っていますが、実行すべきコードを実行できていますか?
30
+
31
+
32
+
33
+ ---
34
+
35
+
36
+
37
+ 私の手元では以下のコードのuseCPUを変えると正常にスケールします。
38
+
39
+
40
+
41
+ ```python
42
+
43
+ import concurrent.futures
44
+
45
+ import numpy as np
46
+
47
+ import time
48
+
49
+
50
+
51
+ img = np.ones(shape=(500,5000,3))
52
+
53
+
54
+
55
+ def main():
56
+
57
+ useCPU = 4
58
+
59
+ step = int(len(img) / useCPU )
60
+
61
+ mulchProcess(useCPU=useCPU, step=step)
62
+
63
+
64
+
65
+ def changeToGray(number: int, length: int):
66
+
67
+ s = time.time()
68
+
69
+ endPioint = number + length if number + length < len(img) else len(img) - 1
70
+
71
+ part_height = img[number:endPioint-1]
72
+
73
+ for width in part_height:
74
+
75
+ for pixel in width:
76
+
77
+ gray = int(pixel[0]*0.3) + int(pixel[1]*0.59) + int(pixel[2]*0.11)
78
+
79
+ pixel[0] = gray
80
+
81
+ pixel[1] = gray
82
+
83
+ pixel[2] = gray
84
+
85
+ t = time.time() - s
86
+
87
+ return number, part_height, t
88
+
89
+
90
+
91
+ def mulchProcess(useCPU: int, step: int):
92
+
93
+ index_list = [i for i in range(0, len(img), step)]
94
+
95
+ with concurrent.futures.ProcessPoolExecutor(max_workers=useCPU) as executer:
96
+
97
+ fs = [executer.submit(changeToGray, i, step) for i in index_list]
98
+
99
+ for future in concurrent.futures.as_completed(fs):
100
+
101
+ line_number = future.result()[0]
102
+
103
+ part_height = future.result()[1]
104
+
105
+ t = future.result()[2]
106
+
107
+ print(t)
108
+
109
+ for i, height in zip(range(line_number, line_number+len(part_height)), part_height):
110
+
111
+ img[i] = height
112
+
113
+
114
+
115
+ s = time.time()
116
+
117
+ main()
118
+
119
+ print(time.time()-s)
120
+
121
+ ```
122
+
123
+
124
+
125
+ 時間は以下のようになります。
126
+
127
+ 手元のPCは4coreなので、8個使おうとしても一度に4つずつしか実行できません。
128
+
129
+ それらがキューに乗せられて順番に実行されるわけではなく、スイッチしながら実行されるので、プロセスあたり2倍の時間がかかり、結局全体の時間は同じになります。
130
+
131
+ むしろスイッチした分だけオーバーヘッドがあって全体の実行時間が長くなっています。
132
+
133
+ ```
134
+
135
+ useCPU = 1
136
+
137
+ 3.6020491123199463
138
+
139
+ total 4.26886773109436
140
+
141
+
142
+
143
+ useCPU = 2
144
+
145
+ 1.7662358283996582
146
+
147
+ 1.7804999351501465
148
+
149
+ total 2.166551113128662
150
+
151
+
152
+
153
+ useCPU = 4
154
+
155
+ 0.9876649379730225
156
+
157
+ 0.9972729682922363
158
+
159
+ 1.0097663402557373
160
+
161
+ 1.0141608715057373
162
+
163
+ total 1.2486088275909424
164
+
165
+
166
+
167
+ useCPU = 8
168
+
169
+ 0.9724259376525879
170
+
171
+ 0.9717621803283691
172
+
173
+ 0.973222017288208
174
+
175
+ 0.97617506980896
176
+
177
+ 0.9814469814300537
178
+
179
+ 0.9806389808654785
180
+
181
+ 0.9841761589050293
182
+
183
+ 0.9821760654449463
184
+
185
+ 0.015022039413452148
186
+
187
+ total 1.2429370880126953
188
+
189
+ ```