-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathParallelAlgorithms.py
190 lines (159 loc) · 5.49 KB
/
ParallelAlgorithms.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
import pycuda.autoinit
import pycuda.driver as drv
import pycuda.gpuarray as gpuarray
import pycuda.cumath
import skcuda.misc
import skcuda.linalg as linalg
import numpy as np
import matplotlib.pyplot as plt
import time
import scipy.io as sio
from CSMSSMTools import *
from pycuda.compiler import SourceModule
bitonicSort_ = None
getSumSquares_ = None
finishCSM_ = None
CSM = None
def initParallelAlgorithms():
global bitonicSort_
fin = open("ParallelAlgorithms/bitonicSort.cu")
mod = SourceModule(fin.read())
fin.close()
bitonicSort_ = mod.get_function("bitonicSort")
global finishCSM_
global getSumSquares_
fin = open("ParallelAlgorithms/CSMHelper.cu")
mod = SourceModule(fin.read())
fin.close()
finishCSM_ = mod.get_function("finishCSM")
getSumSquares_ = mod.get_function("getSumSquares")
#Run each of the algorithms on dummy data so that they're pre-compiled
#1) Bitonic Sort
X = np.random.randn(16, 16)
N = np.int32(16)
NPow2 = N
NThreads = N/2
XG = gpuarray.to_gpu(X)
bitonicSort_(XG, N, NPow2, block=(NThreads, 1, 1), grid=(X.shape[0], 1), shared=4*NPow2)
linalg.init()
#2) Other primitive operations
NegXDotX = linalg.dot(XG, XG)
XPlusX = skcuda.misc.add(XG, XG)
XSqr = skcuda.misc.multiply(XG, XG)
XSqr = skcuda.misc.sum(XSqr, 1)
XPlusCol = skcuda.misc.add_matvec(XG, XSqr, 0)
def roundUpPow2(x):
return np.int32(int(2**np.ceil(np.log2(float(x)))))
def bitonicSort(XG):
N = np.int32(XG.shape[1])
NPow2 = np.int32(2**np.ceil(np.log2(N)))
N2 = NPow2/2
NThreads = min(N2, 512)
bitonicSort_(XG, N, NPow2, block=(NThreads, 1, 1), grid=(XG.shape[0], 1), shared=4*NPow2)
def getCSMGPU(XG, YG):
tbegin = time.time()
GPUNeg2 = gpuarray.to_gpu(np.array([-2.0], dtype=np.float32))
YGT = linalg.transpose(YG)
XSqr = skcuda.misc.multiply(XG, XG)
XSqr = skcuda.misc.sum(XSqr, 1)
YSqr = skcuda.misc.multiply(YG, YG)
YSqr = skcuda.misc.sum(YSqr, 1)
C = linalg.dot(XG, YGT)
C = skcuda.misc.multiply(GPUNeg2, C)
skcuda.misc.add_matvec(C, XSqr, 0, C)
skcuda.misc.add_matvec(C, YSqr, 1, C)
return C
def getCSMGPU2(XG, YG):
#Step 1: Sum of squares across rows
dim = np.int32(XG.shape[1])
dimpow2 = roundUpPow2(dim)
NThreads = np.int32(min(dimpow2, 512))
XSqr = gpuarray.empty(XG.shape[0], np.float32)
YSqr = gpuarray.empty(YG.shape[0], np.float32)
getSumSquares_(XG, XSqr, dim, dimpow2, block=(NThreads, 1, 1), grid=(XG.shape[0], 1), shared=4*dimpow2)
getSumSquares_(YG, YSqr, dim, dimpow2, block=(NThreads, 1, 1), grid=(YG.shape[0], 1), shared=4*dimpow2)
#Step 2: Do multiplication part
YGT = linalg.transpose(YG)
CSM = linalg.dot(XG, YGT)
#Step 3: Add everything together
Mp = np.array(XG.shape[0], dtype=np.int32)
Np = np.array(YG.shape[0], dtype=np.int32)
MPow2 = roundUpPow2(XG.shape[0])
NThreads = min(MPow2, 512)
#CSM is N x M
finishCSM_(CSM, XSqr, YSqr, Np, Mp, MPow2, block=(NThreads, 1, 1), grid=(YG.shape[0], 1))
return (CSM, XSqr, YSqr)
def testBitonicSort(N, doPlot = False):
X = np.array(np.random.rand(N*400, N), dtype=np.float32)
tic = time.time()
XG = gpuarray.to_gpu(X)
toc = time.time()
print("Elapsed memcopy time: %g"%(toc-tic))
tic = time.time()
bitonicSort(XG)
skcuda.misc.add(XG, XG)
toc = time.time()
GPUTime = toc-tic
tic = time.time()
X2 = np.sort(X, 1)
toc = time.time()
CPUTime = toc-tic
tic = time.time()
J = np.argpartition(X, N/10, 1)
toc = time.time()
print("Elapsed Time Partition: %g"%(toc-tic))
print("N = %i"%N)
print("Elapsed Time CPU: %g"%CPUTime)
print("Elapsed Time GPU: %g (Ratio %.3g)"%(GPUTime, CPUTime/GPUTime))
print("AllClose: ", np.allclose(np.sort(X, 1), XG.get()))
if doPlot:
plt.subplot(121)
plt.imshow(X, interpolation = 'none')
plt.subplot(122)
plt.imshow(XG.get(), interpolation = 'none')
plt.show()
return (CPUTime, GPUTime)
def testBitonicSortTimeRatios(sizes, NTrials):
np.random.seed(100)
CPUTimes = np.zeros((len(sizes), NTrials))
GPUTimes = np.zeros((len(sizes), NTrials))
for i in range(len(sizes)):
N = sizes[i]
for t in range(NTrials):
(CPUTimes[i, t], GPUTimes[i, t]) = testBitonicSort(N)
sio.savemat("Timings.mat", {"CPUTimes":CPUTimes, "GPUTimes":GPUTimes})
def testCSMTimes():
N = 800
K = 1
X = np.array(np.random.randn(N, 25*25), dtype = np.float32)
Y = np.array(np.random.randn(N*K, 25*25), dtype=np.float32)
XG = gpuarray.to_gpu(X)
YG = gpuarray.to_gpu(Y)
tic = time.time()
CSM = getCSM(X, Y)
CPUTime = time.time() - tic
tic = time.time()
#(CSMG, XSqr, YSqr) = getCSMGPU2(XG, YG)
CSMG = getCSMGPU(XG, YG)
CSMG = CSMG.get()
GPUTime = time.time() - tic
#plt.plot(XSqr.get(), np.sum(X**2, 1), '.')
#plt.show()
print("CPUTime: %g"%CPUTime)
print("GPUTime: %g"%GPUTime)
plt.subplot(131)
plt.imshow(CSM, interpolation = 'none', cmap = 'afmhot')
plt.subplot(132)
plt.imshow(CSMG, interpolation = 'none', cmap = 'afmhot')
plt.subplot(133)
plt.imshow(CSM - CSMG, interpolation = 'none', cmap = 'spectral')
plt.show()
if __name__ == '__main__2':
np.random.seed(100)
initParallelAlgorithms()
t = testCSM(1000, 1000, 60, doPlot = False)
print("Return time: %g"%(time.time() - t))
if __name__ == '__main__':
initParallelAlgorithms()
#testBitonicSort(800, False)
testCSMTimes()