-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculate_GPU_CPU.py
417 lines (318 loc) · 9.99 KB
/
Calculate_GPU_CPU.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import pycuda.driver as drv
import numpy as np
from pycuda.compiler import SourceModule
import time
# kernel func
kernel_code = """
#include<stdio.h>
#include<math.h>
extern "C" {
__global__ void add(float *result, float *para1, float *para2, const int N)
{
const int i = blockDim.x * blockIdx.x + threadIdx.x;
if (i < N) {
result[i] = para1[i] + para2[i];
}
}
__global__ void minus(float *result, float *para1, float *para2, const int N)
{
const int i = blockDim.x * blockIdx.x + threadIdx.x;
if (i < N) {
result[i] = para1[i] - para2[i];
}
}
__global__ void multiply(float *result, float *para1, float *para2, const int N)
{
const int i = blockDim.x * blockIdx.x + threadIdx.x;
if (i < N) {
result[i] = para1[i] * para2[i];
}
}
__global__ void divi(float *result, float *para1, float *para2, const int N)
{
const int i = blockDim.x * blockIdx.x + threadIdx.x;
if (i < N) {
if (para2[i] == 0)
result[i] = 0;//Do you have any good solution
else
result[i] = para1[i] / para2[i];
}
}
__global__ void power(float *result, float *para1, int *para2, const int N)
{
const int i = blockDim.x * blockIdx.x + threadIdx.x;
if (i < N) {
int count = para2[i];
float result1 = 0;
result1 = para1[i];
//printf("count = %d", count);
//printf("result1 = %f", result1);
while (count > 1) {
result1 = result1 * para1[i];
count--;
}
result[i] = result1;
}
}
__global__ void greater_op(float *result, float *para1, float *para2, const int N)
{
const int i = blockDim.x * blockIdx.x + threadIdx.x;
if (i < N) {
if (para1[i] > para2[i])
{
result[i] = para1[i];
}
else
result[i] = para2[i];
}
}
__global__ void lesser_op(float *result, float *para1, float *para2, const int N)
{
const int i = blockDim.x * blockIdx.x + threadIdx.x;
if (i < N) {
if (para1[i] < para2[i])
{
result[i] = para1[i];
}
else
result[i] = para2[i];
}
}
__global__ void if_op(float *result, float *para1, float *para2, const int N)
{
return ;
}//There`s two arity
__global__ void and_op(float *result, float *para1, float *para2, const int N)
{
return ;
}
__global__ void or_op(float *result, float *para1, float *para2, const int N)
{
return ;
}
__global__ void sqrt_op(float *result, float *para1, const int N)
{
const int i = blockDim.x * blockIdx.x + threadIdx.x;
float new_guess;
float last_guess;
float number = para1[i];
if (number < 0) {
result[i] = para1[i];
}
else if (number == 0) result[i] = 0;
else {
new_guess = 1;
do {
last_guess = new_guess;
new_guess = (last_guess + number / last_guess) / 2;
} while (new_guess != last_guess);
result[i] = new_guess;
}
}
}//extern
"""
# init datasets
# init opeartors
mod = SourceModule(kernel_code, no_extern_c=True)
add = mod.get_function("add")
minus = mod.get_function("minus")
multiply = mod.get_function("multiply")
divi = mod.get_function("divi")
power = mod.get_function("power")
sqrt_op = mod.get_function("sqrt_op")
# init expression
expression = "a**i+b-c/a+b-c+(a+b)/c*a+b-c+a**i+b-c/a+b-c+(a+b)/c*a+b-c-a**i+b-c/a+b-c+(a+b)/c*a+b-c-a**i+b-c/a+b-c+(a+b)/c*a+b-c+a**i+b-c/a+b-c+(a+b)/c*a+b-c-a**i+b-c/a+b-c+(a+b)/c*a+b-c"
print("prototype:", expression)
# init data
N = 10000000 # This num is used for standing for dimmensionality and a placeholder for the amount of threads
a = np.random.randint(1, 10, size=(N, 1))
a = a.astype(np.float32)
b = np.random.randint(1, 10, size=(N, 1))
b = b.astype(np.float32)
c = np.random.randint(1, 10, size=(N, 1))
c = c.astype(np.float32)
index = 0.5
print("a:", a,end='')
print("b:", b,end='')
print("c:", c,end='')
# fun to deal with illegal expr,power expr and minus expr
def deal_minus_num(str1):
i = 0
str_list = list(str1) # 字符串转list
length = len(str_list)
if str_list[i] == '-':
if str_list[i + 1].isalpha():
str_list.insert(i, '(')
str_list.insert(i + 1, '0')
str_list.insert(i + 4, ')')
for i, element in enumerate(str_list):
if str_list[i] == '-':
if (str_list[i - 1] == '('):
str_list.insert(i, '0')
elif (str_list[i - 1] == "+") or (str_list[i - 1] == "-") or (str_list[i - 1] == "*") or (
str_list[i - 1] == "/"):
str_list.insert(i, '0')
elif str_list[i] == ' ':
str_list.pop(i)
elif str_list[i] == '*':
if str_list[i + 1] == '*':
str_list.pop(i)
str_list[i] = '^'
str_fin = ''.join(str_list)
return str_fin
expression = deal_minus_num(expression)
# Medial expressions to suffix expressions
def middle2behind(expresssion):
result1 = []
stack1 = []
for item in expression:
if item.isalpha() or item.isdigit():
result1.append(item)
else:
if len(stack1) == 0:
stack1.append(item)
elif item in '^':
stack1.append(item)
elif item in '*/(':
stack1.append(item)
elif item == ')':
t = stack1.pop()
while t != '(':
result1.append(t)
t = stack1.pop()
elif item in '+-' and stack1[len(stack1) - 1] in '*/^':
if stack1.count('(') == 0:
while stack1:
result1.append(stack1.pop())
else:
t = stack1.pop()
while t != '(':
result1.append(t)
t = stack1.pop()
stack1.append('(')
stack1.append(item)
else:
stack1.append(item)
while stack1:
result1.append(stack1.pop())
return "".join(result1)
expr = middle2behind(expression) # This is the expression
print("middle to suffix:", expr)
begin = time.time() # Let`s calc the time of CPU
# the fuc to avoid 0 divisor
def divide(tensor1, tensor2):
result = np.zeros_like(tensor2)
for i in range(0, len(tensor2)):
if tensor2[i] == 0:
result[i] = 0
print("unforunately")
else:
result[i] = tensor1[i] / tensor2[i]
return result
# calc postfix expr
def calculate_postfix(postfix, tensors1, tensors2, tensors3):
stack_cpu = [] # 用list模拟栈的后进先出
for p in postfix:
if p in '+-*/^': # operator
value_2 = stack_cpu.pop() # first operand
value_1 = stack_cpu.pop() # second oprand
if p == '+':
result_cpu = value_1 + value_2
elif p == '-':
result_cpu = value_1 - value_2
elif p == '*':
result_cpu = value_1 * value_2
elif p == '/':
result_cpu = divide(value_1, value_2)
elif p == '^':
result_cpu = value_1
for y in range(value_2 - 1):
result_cpu = result_cpu * value_1
stack_cpu.append(result_cpu)
elif p == 'a':
stack_cpu.append(tensors1)
elif p == 'b':
stack_cpu.append(tensors2)
elif p == 'c':
stack_cpu.append(tensors3)
elif p == 'i':
num = int(index)
stack_cpu.append(num)
elif p.isdigit():
num = int(p)
stack_cpu.append(num)
return stack_cpu.pop()
# excute calc with CPU
d = calculate_postfix(expr, a, b, c)
end = time.time() - begin
print("cpu time is:", end)
# init arrays for storing and prepare for CUDA program
result = np.zeros_like(a)
result = result.astype(np.float32)
marker = np.zeros_like(a) # stands for d_res pointer
stack = [] # Calculating suffix expressions
stack_pointer = [] # store d_res device pointer
# start calc with CUDA
def isAString(obj):
return isinstance(obj, str)
d_a = drv.mem_alloc(a.nbytes)
drv.memcpy_htod(d_a, a)
d_b = drv.mem_alloc(b.nbytes)
drv.memcpy_htod(d_b, b)
d_c = drv.mem_alloc(c.nbytes)
drv.memcpy_htod(d_c, c)
start = time.time()
for i, element in enumerate(expr):
if element in '+-*/^':
d_arg1 = stack.pop()
d_arg2 = stack.pop()
if element == '+':
d_res = drv.mem_alloc(result.nbytes)
add(d_res, d_arg2, d_arg1, np.int32(N), grid=((N - 1) // 128 + 1, 1), block=(128, 1, 1))
d_resu = d_res
stack.append(d_resu)
elif element == '-':
d_res = drv.mem_alloc(result.nbytes)
minus(d_res, d_arg2, d_arg1, np.int32(N), grid=((N - 1) // 128 + 1, 1), block=(128, 1, 1))
d_resu = d_res
stack.append(d_resu)
elif element == '*':
d_res = drv.mem_alloc(result.nbytes)
multiply(d_res, d_arg2, d_arg1, np.int32(N), grid=((N - 1) // 128 + 1, 1), block=(128, 1, 1))
d_resu = d_res
stack.append(d_resu)
elif element == '/':
d_res = drv.mem_alloc(result.nbytes)
divi(d_res, d_arg2, d_arg1, np.int32(N), grid=((N - 1) // 128 + 1, 1), block=(128, 1, 1))
d_resu = d_res
stack.append(d_resu)
elif element == '^':
d_res = drv.mem_alloc(result.nbytes)
power(d_res, d_arg2, d_arg1, np.int32(N), grid=((N - 1) // 128 + 1, 1), block=(128, 1, 1))
d_resu = d_res
stack.append(d_resu)
elif element == 'a':
stack.append(d_a)
elif element == 'b':
stack.append(d_b)
elif element == 'c':
stack.append(d_c)
elif element == 'i':
y = int(index)
h_i_y = np.full((N, 1), y, dtype=np.int32)
d_i_y = drv.mem_alloc(h_i_y.nbytes)
drv.memcpy_htod(d_i_y, h_i_y)
stack.append(d_i_y)
elif element.isdigit():
y = int(element)
h_y = np.full((N, 1), y, dtype=np.int32)
d_y = drv.mem_alloc(h_y.nbytes)
drv.memcpy_htod(d_y, h_y)
stack.append(d_y)
d_result = stack.pop()
drv.memcpy_dtoh(result, d_result)
final = time.time() - start
print("\nGPU time is:", final)
print("cpu answer is:", d)
print("gpu answer is:", result)
print("error:", abs(result) - abs(d))