-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixTrans.py
More file actions
209 lines (154 loc) · 6.27 KB
/
Copy pathMatrixTrans.py
File metadata and controls
209 lines (154 loc) · 6.27 KB
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
import time
import csv
import numpy as np
import pyopencl as cl
import matplotlib as mpl
mpl.use('agg')
import matplotlib.pyplot as plt
mpl.rcParams['savefig.dpi'] = 100
from pylab import *
ctx = cl.create_some_context()
queue=cl.CommandQueue(ctx) #COMMAND QUEUE
print "##############################################"
print "Matrix Transpose\n"
#### Defining the Initial Kernel ####
###func0: Naive Implementation ###
func0= cl.Program(ctx,"""
#pragma OPENCL EXTENSION cl_khr_fp64: enable
__kernel void mat_transpose(__global float* A, __global float *A_trans, unsigned int H_A, unsigned int W_A) {
unsigned int i = get_global_id(0);
unsigned int j = get_global_id(1);
A_trans[i*H_A+j]=0;
A_trans[i*H_A + j]= A[j*W_A +i];
}
""").build().mat_transpose #KERNEL
""" """
func0.set_scalar_arg_dtypes([None, None, np.uint32, np.uint32])
def trans_naive(a_buf, atrans_buf, H_A, W_A):
start = time.time()
func0(queue, (W_A, H_A), None, a_buf, atrans_buf, np.uint32(H_A), np.uint32(W_A))
return time.time()-start
def cl_naive_trans(a,a_trans,HA,WA):
a_buf, atrans0_buf = mem_alloc(a, a_trans)
t=trans_naive(a_buf,atrans0_buf, HA, WA)
a_trans0=mem_transfer(a_trans,atrans0_buf)
return t, a_trans0
###func1: Row Optimisation (All Global)###
func1= cl.Program(ctx,"""
#pragma OPENCL EXTENSION cl_khr_fp64: enable
__kernel void mat_transpose(__global float* A, __global float *A_trans, unsigned int H_A, unsigned int W_A) {
unsigned int i = get_global_id(0);
unsigned int j;
for (j=0;j<H_A;j++) {
A_trans[i*H_A + j]=0.0;
}
for (j=0;j<H_A;j++) {
A_trans[i*H_A + j]= A[j*W_A +i];
}
}
""").build().mat_transpose #KERNEL
""" """
func1.set_scalar_arg_dtypes([None, None, np.uint32, np.uint32])
def trans_row_opt(a_buf, atrans_buf, H_A, W_A):
start = time.time()
func1(queue, (W_A, ), None, a_buf, atrans_buf, np.uint32(H_A), np.uint32(W_A))
return time.time()-start
def cl_row_opt_trans(a,a_trans,HA,WA):
a_buf, atrans1_buf = mem_alloc(a, a_trans)
t=trans_row_opt(a_buf,atrans1_buf, HA, WA)
a_trans1=mem_transfer(a_trans,atrans1_buf)
return t, a_trans1
""" """
##########################################################################################
def create_arrays(height_A,width_A):
A=np.random.random((height_A,width_A)).astype(np.float32)
A_trans=np.zeros((width_A,height_A)).astype(np.float32)
return A, A_trans
def mem_alloc(A, A_trans):
mf=cl.mem_flags #MEMORY_FLAG allocation
a_buf=cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=A)
atrans_buf=cl.Buffer(ctx, mf.WRITE_ONLY, A_trans.nbytes)
init_arr=np.zeros((W_A,H_A)).astype(np.float32)
cl.enqueue_copy(queue,atrans_buf,init_arr) #Initializing the Memory of the Output Buffers
return a_buf, atrans_buf
def mem_transfer(A_trans, atrans_buf):
cl.enqueue_copy(queue,A_trans,atrans_buf) #Copying Final Data into Python Buffers
return A_trans
def py_trans(A,y):
start=time.time()
y=np.transpose(A)
t= time.time()-start
return t,y
def py_time(HA,WA,M=4):
times = []
A,y=create_arrays(HA,WA)
for i in xrange(M):
t,y=py_trans(A,y)
times.append(t)
#print 'python time: ', np.average(times)
return np.average(times)
def cl_naive_time(HA, WA, M=4):
times = []
a, atrans =create_arrays(HA,WA)
a_buf, atrans_buf = mem_alloc(a, atrans)
for i in xrange(M):
t=trans_naive(a_buf,atrans_buf, HA, WA)
times.append(t)
atrans=mem_transfer(atrans,atrans_buf)
#print 'opencl naive time: ', np.average(times)
return np.average(times)
def cl_row_opt_time(HA, WA, M=4):
times = []
a, atrans =create_arrays(HA,WA)
a_buf, atrans_buf = mem_alloc(a, atrans)
for i in xrange(M):
t=trans_row_opt(a_buf,atrans_buf, HA, WA)
times.append(t)
atrans=mem_transfer(atrans,atrans_buf)
#print 'opencl row_opt time: ', np.average(times)
return np.average(times)
######################################################################
### Initialising the parameters ####
H_A=6
W_A=8
a, atrans=create_arrays(H_A,W_A)
#print "A\n", a
a_buf, atrans_buf=mem_alloc(a,atrans)
atrans=mem_transfer(atrans, atrans_buf)
python_time,A_trans_py =py_trans(a, atrans)
#print "A' Python:\n", A_trans_py
### Verifying that the results are equal ###
pyopencl_time0, A_trans_cl0=cl_naive_trans(a,atrans,H_A,W_A)
#print "A' naive\n",A_trans_cl0
print "Output for Python-CPU and Naive-Kernel-GPU are equal:\t",np.allclose(A_trans_py,A_trans_cl0)
pyopencl_time1, A_trans_cl1=cl_row_opt_trans(a,atrans,H_A,W_A)
#print "A' row_opt\n",A_trans_cl1
print "Output for Python-CPU and RowOpt-Kernel-GPU are equal:\t",np.allclose(A_trans_py,A_trans_cl1)
#############################################################################################
### Comparing differnet pyopenCL kernel timings ###
python_times=[]
pyopencl_naive_times=[]
pyopencl_row_opt_times=[]
param=np.arange(1,201,1).astype(np.int32)
for i in param:
python_times.append(py_time(i*H_A,i*W_A,4))
pyopencl_naive_times.append(cl_naive_time(i*H_A,i*W_A,4))
pyopencl_row_opt_times.append(cl_row_opt_time(i*H_A,i*W_A,4))
print "\nDim\t", "Python_time\t", "Naive_transpose\t", "Row Optimisation\t"
for i in param:
print "(",i*H_A, ",",i*W_A,")\t", python_times[i-1],"\t", pyopencl_naive_times[i-1], "\t", pyopencl_row_opt_times[i-1], "\t"
for i in param:
if pyopencl_row_opt_times[i-1] < pyopencl_naive_times[i-1]:
print "\nAt a dimension size of (", i*H_A, ",", i*W_A, "), Row Optimization beats Naive Transpose implementations"
break
plt.clf()
plt.plot(param*H_A*param*W_A, pyopencl_naive_times, 'r*-',
param*H_A*param*W_A, pyopencl_row_opt_times, 'b*-',
param*H_A*param*W_A, python_times, 'k*-')
plt.xlabel('# elements in matrix A')
plt.ylabel('$t$')
plt.title('Time vs Size for different Transpose Implementations')
plt.legend(('Naive-Kernel-GPU', 'RowOpt-Kernel-GPU', 'Python-CPU'), loc='upper left')
plt.grid(True)
#plt.draw()
plt.savefig('Transpose_scaling.png')