forked from SaiGunaranjan/gpu_programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumbaCudaKernels.py
More file actions
230 lines (151 loc) · 12.4 KB
/
Copy pathnumbaCudaKernels.py
File metadata and controls
230 lines (151 loc) · 12.4 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 22 15:22:01 2020
@author: Sai Gunaranjan Pelluri
"""
from numba import cuda
import numba
import cupy as cp
@cuda.jit('void(float32[:],float32[:],float32[:],int32,int32)')
def conv_1d(inp_signal_ext,impulse_response,output_signal,signal_len,impulse_response_len):
thrdID = cuda.blockIdx.x * cuda.blockDim.x + cuda.threadIdx.x
if thrdID >= signal_len+impulse_response_len-1:
return;
temp_sum = 0;
for ele in range(impulse_response_len):
temp_sum += inp_signal_ext[thrdID+ele]*impulse_response[impulse_response_len-ele-1]
output_signal[thrdID] = temp_sum
@cuda.jit('void(float32[:,:],float32[:,:],float32[:,:], int32, int32, int32, int32)')
def conv_2d(inputImageExtnd, pointSpreadFn, outputImage, InputLenX, InputLenY, psfOneSideLenX, psfOneSideLenY):
thrdIDx = cuda.blockIdx.x * cuda.blockDim.x + cuda.threadIdx.x
thrdIDy = cuda.blockIdx.y * cuda.blockDim.y + cuda.threadIdx.y
psfLenX = 2*psfOneSideLenX + 1
psfLenY = 2*psfOneSideLenY + 1
if (thrdIDx >= psfOneSideLenX + InputLenX + psfLenX -1) or (thrdIDx < psfOneSideLenX) or (thrdIDy >= psfOneSideLenY + InputLenY + psfLenY -1) or (thrdIDy < psfOneSideLenY):
return;
convSum = cp.float32(0)
for x in range(-psfOneSideLenX,psfOneSideLenX+1):
for y in range(-psfOneSideLenY,psfOneSideLenY+1):
convSum += inputImageExtnd[thrdIDy+y,thrdIDx+x]*pointSpreadFn[psfOneSideLenY-y,psfOneSideLenX-x];
# if (thrdIDx == psfOneSideLenX + InputLenX + psfLenX -2) and (thrdIDy == psfOneSideLenY + InputLenY + psfLenY -2):
# print('x:',x,'y:',y,'imagVal:',inputImageExtnd[thrdIDy+y,thrdIDx+x],'psfval:',pointSpreadFn[2*psfOneSideLenY-y,2*psfOneSideLenX-x])
# if (thrdIDx == psfOneSideLenX + InputLenX + psfLenX -2) and (thrdIDy == psfOneSideLenY + InputLenY + psfLenY -2):
# print('conSum:',convSum)
outputImage[thrdIDy-psfOneSideLenY,thrdIDx-psfOneSideLenX] = convSum;
@cuda.jit('void(float32[:], int32, int32, int32, float32[:,:], float32, int32[:])')
def CFAR_CA_GPU(signal_ext, origSignalLen , guardBandLen_1side, validSampLen_1side, scratchPad, noiseMargin, outputBoolVector):
thrdID = cuda.blockIdx.x * cuda.blockDim.x + cuda.threadIdx.x
if (thrdID < origSignalLen-1) or (thrdID > 2*origSignalLen-2):
return;
# check for local maxima on the CUT i.e. signal_ext[thrdID]
if (signal_ext[thrdID] >= signal_ext[thrdID-1]) and (signal_ext[thrdID] >= signal_ext[thrdID+1]):
count = cp.int32(0)
for i in range(thrdID-guardBandLen_1side-validSampLen_1side, thrdID-guardBandLen_1side):
# scratchPad[count] = signal_ext[i]; # This should not be done. There should be a separate scratch pad for each thread when it is vector/matrix copying
scratchPad[thrdID-(origSignalLen-1),count] = signal_ext[i]
count += 1;
for j in range(thrdID+guardBandLen_1side+1, thrdID+guardBandLen_1side+validSampLen_1side+1):
# scratchPad[count] = signal_ext[j]; # This should not be done. There should be a separate scratch pad for each thread when it is vector/matrix copying
scratchPad[thrdID-(origSignalLen-1),count] = signal_ext[j];
count += 1
avgNoisePower = cp.float32(0)
for ele in range(2*validSampLen_1side):
avgNoisePower += scratchPad[thrdID-(origSignalLen-1),ele];
avgNoisePower = avgNoisePower/(2*validSampLen_1side)
if (signal_ext[thrdID] > noiseMargin*avgNoisePower):
outputBoolVector[thrdID-(origSignalLen-1)] = 1
@cuda.jit('void(float32[:], int32, int32, int32, float32[:,:], float32, int32, int32[:])')
def CFAR_OS_GPU(signal_ext, origSignalLen , guardBandLen_1side, validSampLen_1side, scratchPad, noiseMargin, ordStat, outputBoolVector):
thrdID = cuda.blockIdx.x * cuda.blockDim.x + cuda.threadIdx.x
if (thrdID < origSignalLen-1) or (thrdID > 2*origSignalLen-2):
return;
# check for local maxima on the CUT i.e. signal_ext[thrdID]
if (signal_ext[thrdID] >= signal_ext[thrdID-1]) and (signal_ext[thrdID] >= signal_ext[thrdID+1]):
count = cp.int32(0)
for i in range(thrdID-guardBandLen_1side-validSampLen_1side, thrdID-guardBandLen_1side):
scratchPad[thrdID-(origSignalLen-1),count] = signal_ext[i]
count += 1;
for j in range(thrdID+guardBandLen_1side+1, thrdID+guardBandLen_1side+validSampLen_1side+1):
scratchPad[thrdID-(origSignalLen-1),count] = signal_ext[j];
count += 1
temp = cp.float32(0);
ordStat_largestVal = cp.float32(0)
# sort in decreasing order of strength upto the ordStat kth largest value
for i in range(ordStat):
for j in range(i+1,2*validSampLen_1side):
if (scratchPad[thrdID-(origSignalLen-1),i] < scratchPad[thrdID-(origSignalLen-1),j]):
temp = scratchPad[thrdID-(origSignalLen-1),i];
scratchPad[thrdID-(origSignalLen-1),i] = scratchPad[thrdID-(origSignalLen-1),j];
scratchPad[thrdID-(origSignalLen-1),j] = temp
ordStat_largestVal = scratchPad[thrdID-(origSignalLen-1),ordStat-1]
if (signal_ext[thrdID] > noiseMargin*ordStat_largestVal):
outputBoolVector[thrdID-(origSignalLen-1)] = 1
@cuda.jit('void(float32[:,:], int32, int32, int32, int32, int32, int32, float32[:,:,:], float32, int32[:,:])')
def CFAR_CA_2D_cross_GPU(signal_ext, origSignalLenX, origSignalLenY, guardBandLen_1sideX, guardBandLen_1sideY, validSampLen_1sideX, validSampLen_1sideY, scratchPad, noiseMargin, outputBoolVector):
thrdIDx = cuda.blockIdx.x * cuda.blockDim.x + cuda.threadIdx.x
thrdIDy = cuda.blockIdx.y * cuda.blockDim.y + cuda.threadIdx.y
if (thrdIDx < guardBandLen_1sideX + validSampLen_1sideX) or (thrdIDx > origSignalLenX + guardBandLen_1sideX + validSampLen_1sideX - 1) or (thrdIDy < guardBandLen_1sideY + validSampLen_1sideY) or (thrdIDy > origSignalLenY+ guardBandLen_1sideY + validSampLen_1sideY - 1):
return;
# check for local maxima on the CUT i.e. signal_ext[thrdID]
if (signal_ext[thrdIDy,thrdIDx] >= signal_ext[thrdIDy,thrdIDx-1]) and (signal_ext[thrdIDy,thrdIDx] >= signal_ext[thrdIDy,thrdIDx+1]) and (signal_ext[thrdIDy,thrdIDx] >= signal_ext[thrdIDy-1,thrdIDx]) and (signal_ext[thrdIDy,thrdIDx] >= signal_ext[thrdIDy+1,thrdIDx]):
count = cp.int32(0)
for i in range(thrdIDx-guardBandLen_1sideX-validSampLen_1sideX, thrdIDx-guardBandLen_1sideX):
scratchPad[thrdIDy-(guardBandLen_1sideY + validSampLen_1sideY), \
thrdIDx-(guardBandLen_1sideX + validSampLen_1sideX),count] = signal_ext[thrdIDy,i]
count += 1;
for j in range(thrdIDx+guardBandLen_1sideX+1, thrdIDx+guardBandLen_1sideX+validSampLen_1sideX+1):
scratchPad[thrdIDy-(guardBandLen_1sideY + validSampLen_1sideY), \
thrdIDx-(guardBandLen_1sideX + validSampLen_1sideX),count] = signal_ext[thrdIDy,j];
count += 1
for k in range(thrdIDy-guardBandLen_1sideY-validSampLen_1sideY, thrdIDy-guardBandLen_1sideY):
scratchPad[thrdIDy-(guardBandLen_1sideY + validSampLen_1sideY), \
thrdIDx-(guardBandLen_1sideX + validSampLen_1sideX),count] = signal_ext[k,thrdIDx]
count += 1;
for l in range(thrdIDy+guardBandLen_1sideY+1, thrdIDy+guardBandLen_1sideY+validSampLen_1sideY+1):
scratchPad[thrdIDy-(guardBandLen_1sideY + validSampLen_1sideY), \
thrdIDx-(guardBandLen_1sideX + validSampLen_1sideX),count] = signal_ext[l,thrdIDx];
count += 1
avgNoisePower = cp.float32(0)
for ele in range(2*validSampLen_1sideX + 2*validSampLen_1sideX):
avgNoisePower += scratchPad[thrdIDy-(guardBandLen_1sideY + validSampLen_1sideY), \
thrdIDx-(guardBandLen_1sideX + validSampLen_1sideX),ele];
avgNoisePower = avgNoisePower/(2*validSampLen_1sideX + 2*validSampLen_1sideX)
if (signal_ext[thrdIDy,thrdIDx] > noiseMargin*avgNoisePower):
outputBoolVector[thrdIDy-(guardBandLen_1sideY + validSampLen_1sideY), thrdIDx-(guardBandLen_1sideX + validSampLen_1sideX)] = 1
@cuda.jit('void(float32[:,:], int32, int32, int32, int32, int32, int32, float32[:,:,:], float32, int32, int32[:,:])')
def CFAR_OS_2D_cross_GPU(signal_ext, origSignalLenX, origSignalLenY, guardBandLen_1sideX, guardBandLen_1sideY, validSampLen_1sideX, validSampLen_1sideY, scratchPad, noiseMargin, ordStat, outputBoolVector):
thrdIDx = cuda.blockIdx.x * cuda.blockDim.x + cuda.threadIdx.x
thrdIDy = cuda.blockIdx.y * cuda.blockDim.y + cuda.threadIdx.y
if (thrdIDx < guardBandLen_1sideX + validSampLen_1sideX) or (thrdIDx > origSignalLenX + guardBandLen_1sideX + validSampLen_1sideX - 1) or (thrdIDy < guardBandLen_1sideY + validSampLen_1sideY) or (thrdIDy > origSignalLenY+ guardBandLen_1sideY + validSampLen_1sideY - 1):
return;
# check for local maxima on the CUT i.e. signal_ext[thrdID]
if (signal_ext[thrdIDy,thrdIDx] >= signal_ext[thrdIDy,thrdIDx-1]) and (signal_ext[thrdIDy,thrdIDx] >= signal_ext[thrdIDy,thrdIDx+1]) and (signal_ext[thrdIDy,thrdIDx] >= signal_ext[thrdIDy-1,thrdIDx]) and (signal_ext[thrdIDy,thrdIDx] >= signal_ext[thrdIDy+1,thrdIDx]):
count = cp.int32(0)
for i in range(thrdIDx-guardBandLen_1sideX-validSampLen_1sideX, thrdIDx-guardBandLen_1sideX):
scratchPad[thrdIDy-(guardBandLen_1sideY + validSampLen_1sideY), \
thrdIDx-(guardBandLen_1sideX + validSampLen_1sideX),count] = signal_ext[thrdIDy,i]
count += 1;
for j in range(thrdIDx+guardBandLen_1sideX+1, thrdIDx+guardBandLen_1sideX+validSampLen_1sideX+1):
scratchPad[thrdIDy-(guardBandLen_1sideY + validSampLen_1sideY), \
thrdIDx-(guardBandLen_1sideX + validSampLen_1sideX),count] = signal_ext[thrdIDy,j];
count += 1
for k in range(thrdIDy-guardBandLen_1sideY-validSampLen_1sideY, thrdIDy-guardBandLen_1sideY):
scratchPad[thrdIDy-(guardBandLen_1sideY + validSampLen_1sideY), \
thrdIDx-(guardBandLen_1sideX + validSampLen_1sideX),count] = signal_ext[k,thrdIDx]
count += 1;
for l in range(thrdIDy+guardBandLen_1sideY+1, thrdIDy+guardBandLen_1sideY+validSampLen_1sideY+1):
scratchPad[thrdIDy-(guardBandLen_1sideY + validSampLen_1sideY), \
thrdIDx-(guardBandLen_1sideX + validSampLen_1sideX),count] = signal_ext[l,thrdIDx];
count += 1
temp = cp.float32(0);
ordStat_largestVal = cp.float32(0)
# sort in decreasing order of strength upto the ordStat kth largest value
for i in range(ordStat):
for j in range(i+1,2*validSampLen_1sideX + 2*validSampLen_1sideX):
if (scratchPad[thrdIDy-(guardBandLen_1sideY + validSampLen_1sideY), thrdIDx-(guardBandLen_1sideX + validSampLen_1sideX),i] < scratchPad[thrdIDy-(guardBandLen_1sideY + validSampLen_1sideY), thrdIDx-(guardBandLen_1sideX + validSampLen_1sideX),j]):
temp = scratchPad[thrdIDy-(guardBandLen_1sideY + validSampLen_1sideY), thrdIDx-(guardBandLen_1sideX + validSampLen_1sideX),i];
scratchPad[thrdIDy-(guardBandLen_1sideY + validSampLen_1sideY), thrdIDx-(guardBandLen_1sideX + validSampLen_1sideX),i] = scratchPad[thrdIDy-(guardBandLen_1sideY + validSampLen_1sideY), thrdIDx-(guardBandLen_1sideX + validSampLen_1sideX),j];
scratchPad[thrdIDy-(guardBandLen_1sideY + validSampLen_1sideY), thrdIDx-(guardBandLen_1sideX + validSampLen_1sideX),j] = temp
ordStat_largestVal = scratchPad[thrdIDy-(guardBandLen_1sideY + validSampLen_1sideY), thrdIDx-(guardBandLen_1sideX + validSampLen_1sideX),ordStat-1]
if (signal_ext[thrdIDy,thrdIDx] > noiseMargin*ordStat_largestVal):
outputBoolVector[thrdIDy-(guardBandLen_1sideY + validSampLen_1sideY), thrdIDx-(guardBandLen_1sideX + validSampLen_1sideX)] = 1