質問編集履歴

11

ソースコード修正

2019/05/24 18:06

投稿

dream-20xx
dream-20xx

スコア17

test CHANGED
File without changes
test CHANGED
@@ -74,7 +74,7 @@
74
74
 
75
75
  for i in range(num):
76
76
 
77
- img[x[i] : x[i] + 2 , y[i] : y[i] + 2 ] = out[i][:][:]
77
+ img[x[i] : x[i] + 2 , y[i] : y[i] + 2 ] += out[i][:][:]
78
78
 
79
79
 
80
80
 
@@ -156,7 +156,7 @@
156
156
 
157
157
  for i in range(num):
158
158
 
159
- img[x[i] : x[i] + 2 , y[i] : y[i] + 2 ] = out[i][:][:]
159
+ img[x[i] : x[i] + 2 , y[i] : y[i] + 2 ] += out[i][:][:]
160
160
 
161
161
 
162
162
 

10

Cythonコード修正

2019/05/24 18:06

投稿

dream-20xx
dream-20xx

スコア17

test CHANGED
File without changes
test CHANGED
@@ -62,9 +62,19 @@
62
62
 
63
63
 
64
64
 
65
+ # add
66
+
67
+ out = [a[:, :, i] * b[c[i], 0] for i in range(num)]
68
+
69
+
70
+
71
+ #for i in range(num):
72
+
73
+ # img[x[i] : x[i] + 2 , y[i] : y[i] + 2 ] += a[:, :, i] * b[c[i], 0]
74
+
65
75
  for i in range(num):
66
76
 
67
- img[x[i] : x[i] + 2 , y[i] : y[i] + 2 ] += a[:, :, i] * b[c[i], 0]
77
+ img[x[i] : x[i] + 2 , y[i] : y[i] + 2 ] = out[i][:][:]
68
78
 
69
79
 
70
80
 

9

Cythonコード修正

2019/05/24 08:24

投稿

dream-20xx
dream-20xx

スコア17

test CHANGED
File without changes
test CHANGED
@@ -132,9 +132,21 @@
132
132
 
133
133
  img = np.zeros((10000, 10000))
134
134
 
135
+
136
+
137
+   # add
138
+
139
+ out = [a[:, :, i] * b[c[i], 0] for i in range(num)]
140
+
141
+
142
+
143
+ #for i in range(num):
144
+
145
+ # img[x[i] : x[i] + 2 , y[i] : y[i] + 2 ] += a[:, :, i] * b[c[i], 0]
146
+
135
147
  for i in range(num):
136
148
 
137
- img[x[i] : x[i] + 2 , y[i] : y[i] + 2 ] += a[:, :, i] * b[c[i], 0]
149
+ img[x[i] : x[i] + 2 , y[i] : y[i] + 2 ] = out[i][:][:]
138
150
 
139
151
 
140
152
 

8

文章修正

2019/05/24 08:22

投稿

dream-20xx
dream-20xx

スコア17

test CHANGED
File without changes
test CHANGED
@@ -78,7 +78,7 @@
78
78
 
79
79
 
80
80
 
81
- 以下を実行して頂くとコンパイルおよび処理実行が走ります。
81
+ 以下を実行して頂くとコンパイルおよび処理実行されます。
82
82
 
83
83
  $ python setup.py build_ext --inplace ; python main_test_20190524.py
84
84
 

7

文章修正

2019/05/24 06:04

投稿

dream-20xx
dream-20xx

スコア17

test CHANGED
File without changes
test CHANGED
@@ -86,8 +86,6 @@
86
86
 
87
87
  # test_20190524.pyx
88
88
 
89
-
90
-
91
89
  ```Cython
92
90
 
93
91
  import cython
@@ -148,8 +146,6 @@
148
146
 
149
147
  ## main_test_20190524.py
150
148
 
151
-
152
-
153
149
  ```Python
154
150
 
155
151
  import test_20190524 as cy
@@ -194,8 +190,6 @@
194
190
 
195
191
  ## setup.py
196
192
 
197
-
198
-
199
193
  ```Python
200
194
 
201
195
  from distutils.core import setup

6

ソースコード修正

2019/05/24 05:58

投稿

dream-20xx
dream-20xx

スコア17

test CHANGED
File without changes
test CHANGED
@@ -196,7 +196,7 @@
196
196
 
197
197
 
198
198
 
199
- ```
199
+ ```Python
200
200
 
201
201
  from distutils.core import setup
202
202
 

5

ソースコード修正

2019/05/24 05:58

投稿

dream-20xx
dream-20xx

スコア17

test CHANGED
File without changes
test CHANGED
@@ -84,6 +84,10 @@
84
84
 
85
85
 
86
86
 
87
+ # test_20190524.pyx
88
+
89
+
90
+
87
91
  ```Cython
88
92
 
89
93
  import cython
@@ -142,7 +146,7 @@
142
146
 
143
147
 
144
148
 
145
- ## main
149
+ ## main_test_20190524.py
146
150
 
147
151
 
148
152
 
@@ -188,7 +192,7 @@
188
192
 
189
193
 
190
194
 
191
- ## setup
195
+ ## setup.py
192
196
 
193
197
 
194
198
 

4

ソースコード修正

2019/05/24 05:57

投稿

dream-20xx
dream-20xx

スコア17

test CHANGED
File without changes
test CHANGED
@@ -126,7 +126,7 @@
126
126
 
127
127
 
128
128
 
129
- num = 11064
129
+ num = 10000
130
130
 
131
131
  img = np.zeros((10000, 10000))
132
132
 
@@ -160,15 +160,15 @@
160
160
 
161
161
  np.random.seed(0)
162
162
 
163
- a = np.random.rand(2, 2, 11064)
163
+ a = np.random.rand(2, 2, 10000)
164
-
164
+
165
- b = np.random.rand(11064, 7074)
165
+ b = np.random.rand(10000, 10000)
166
-
166
+
167
- c = (np.array(list(range(11064)))).astype('i')
167
+ c = (np.array(list(range(10000)))).astype('i')
168
-
168
+
169
- x = np.zeros(11064).astype('i')
169
+ x = (np.random.rand(10000) * 1000).astype('i')
170
-
170
+
171
- y = np.full(11064, 1963).astype('i')
171
+ y = np.full(10000, 1000).astype('i')
172
172
 
173
173
 
174
174
 

3

文章修正

2019/05/24 05:56

投稿

dream-20xx
dream-20xx

スコア17

test CHANGED
File without changes
test CHANGED
@@ -188,7 +188,7 @@
188
188
 
189
189
 
190
190
 
191
- ## setup.py
191
+ ## setup
192
192
 
193
193
 
194
194
 

2

Cythonコード追加

2019/05/24 05:52

投稿

dream-20xx
dream-20xx

スコア17

test CHANGED
File without changes
test CHANGED
@@ -14,8 +14,6 @@
14
14
 
15
15
  ■ Cython ・・・2倍程度速度向上しましたがこれ以上の速度向上を望んでいます。
16
16
 
17
- (非掲載ですが必要ならアップさせて頂きます。)
18
-
19
17
  ■ numba(jit) ・・・殆ど変化しませんでした。
20
18
 
21
19
 
@@ -30,7 +28,7 @@
30
28
 
31
29
 
32
30
 
33
- ```python
31
+ ```Python
34
32
 
35
33
  import numpy as np
36
34
 
@@ -73,3 +71,143 @@
73
71
  print('time : ' + str(round((t.time() - start),5)) + ' [sec]')
74
72
 
75
73
  ```
74
+
75
+
76
+
77
+ # サンプルコード(Cython化)
78
+
79
+
80
+
81
+ 以下を実行して頂くとコンパイルおよび処理実行が走ります。
82
+
83
+ $ python setup.py build_ext --inplace ; python main_test_20190524.py
84
+
85
+
86
+
87
+ ```Cython
88
+
89
+ import cython
90
+
91
+ import numpy as np
92
+
93
+ cimport numpy as np
94
+
95
+
96
+
97
+ ctypedef np.int32_t np_int_t
98
+
99
+ ctypedef np.float64_t np_float_t
100
+
101
+
102
+
103
+ @cython.boundscheck(False)
104
+
105
+ @cython.wraparound(False)
106
+
107
+
108
+
109
+ cpdef np.ndarray[np_int_t, ndim=1] test_20190524(
110
+
111
+ np.ndarray[np_float_t, ndim=3] a,
112
+
113
+ np.ndarray[np_float_t, ndim=2] b,
114
+
115
+ np.ndarray[np_int_t, ndim=1] c,
116
+
117
+ np.ndarray[np_int_t, ndim=1] x,
118
+
119
+ np.ndarray[np_int_t, ndim=1] y):
120
+
121
+
122
+
123
+ cdef np.ndarray[np_float_t, ndim=2] img
124
+
125
+ cdef int i, num
126
+
127
+
128
+
129
+ num = 11064
130
+
131
+ img = np.zeros((10000, 10000))
132
+
133
+ for i in range(num):
134
+
135
+ img[x[i] : x[i] + 2 , y[i] : y[i] + 2 ] += a[:, :, i] * b[c[i], 0]
136
+
137
+
138
+
139
+ return img
140
+
141
+ ```
142
+
143
+
144
+
145
+ ## main
146
+
147
+
148
+
149
+ ```Python
150
+
151
+ import test_20190524 as cy
152
+
153
+ import numpy as np
154
+
155
+ import time as t
156
+
157
+
158
+
159
+ def main():
160
+
161
+ np.random.seed(0)
162
+
163
+ a = np.random.rand(2, 2, 11064)
164
+
165
+ b = np.random.rand(11064, 7074)
166
+
167
+ c = (np.array(list(range(11064)))).astype('i')
168
+
169
+ x = np.zeros(11064).astype('i')
170
+
171
+ y = np.full(11064, 1963).astype('i')
172
+
173
+
174
+
175
+ start = t.time()
176
+
177
+ test = cy.test_20190524(a, b, c, x, y)
178
+
179
+ print('time : ' + str(round((t.time() - start),5)) + ' [sec]')
180
+
181
+
182
+
183
+ if __name__ == "__main__":
184
+
185
+ main()
186
+
187
+ ```
188
+
189
+
190
+
191
+ ## setup.py
192
+
193
+
194
+
195
+ ```
196
+
197
+ from distutils.core import setup
198
+
199
+ from distutils.extension import Extension
200
+
201
+ from Cython.Distutils import build_ext
202
+
203
+
204
+
205
+ setup(
206
+
207
+ cmdclass = {'build_ext': build_ext},
208
+
209
+ ext_modules = [Extension("test_20190524", ["test_20190524.pyx"])]
210
+
211
+ )
212
+
213
+ ```

1

ソース修正

2019/05/24 05:50

投稿

dream-20xx
dream-20xx

スコア17

test CHANGED
File without changes
test CHANGED
@@ -46,7 +46,9 @@
46
46
 
47
47
  c = (np.array(list(range(10000)))).astype('i')
48
48
 
49
- x = np.zeros(10000).astype('i')
49
+ #x = np.zeros(10000).astype('i')
50
+
51
+ x = (np.random.rand(10000) * 1000).astype('i')
50
52
 
51
53
  y = np.full(10000, 1000).astype('i')
52
54