-
Notifications
You must be signed in to change notification settings - Fork 32
/
needle_kernel.jl
153 lines (120 loc) · 4.68 KB
/
needle_kernel.jl
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
using CUDAdrv
using CUDAnative
const BLOCK_SIZE = 16
const BLOCK_SIZE_P1 = BLOCK_SIZE + 1
# FIXME: remove @inbouds (is work-around for shared memory bug)
function needle_cuda_shared_1(reference_ptr, reference_len, matrix_cuda_ptr,
matrix_cuda_len, cols, penalty, i, block_width)
reference = CuDeviceArray(reference_len, reference_ptr)
matrix_cuda = CuDeviceArray(matrix_cuda_len, matrix_cuda_ptr)
bx = blockIdx().x - 1
tx = threadIdx().x - 1
b_index_x = bx
b_index_y = i - 1 - bx
index = cols * BLOCK_SIZE * b_index_y + BLOCK_SIZE * b_index_x + tx + cols + 1
index_n = cols * BLOCK_SIZE * b_index_y + BLOCK_SIZE * b_index_x + tx + 1
index_w = cols * BLOCK_SIZE * b_index_y + BLOCK_SIZE * b_index_x + cols
index_nw = cols * BLOCK_SIZE * b_index_y + BLOCK_SIZE * b_index_x
temp = @cuStaticSharedMem(Int32, (BLOCK_SIZE_P1, BLOCK_SIZE_P1))
ref = @cuStaticSharedMem(Int32, (BLOCK_SIZE, BLOCK_SIZE))
if tx == 0
@inbounds temp[1, tx + 1] = matrix_cuda[index_nw + 1]
end
for ty = 0:BLOCK_SIZE-1
@inbounds ref[tx + 1, ty + 1] = reference[index + cols * ty + 1]
end
sync_threads()
@inbounds temp[1, tx + 2] = matrix_cuda[index_w + cols * tx + 1]
sync_threads()
@inbounds temp[tx + 2, 1] = matrix_cuda[index_n + 1]
sync_threads()
for m = 0:BLOCK_SIZE-1
if tx <= m
t_index_x = tx + 1
t_index_y = m - tx + 1
@inbounds temp[t_index_x + 1, t_index_y + 1] = max(
temp[t_index_x, t_index_y] +
ref[t_index_x, t_index_y],
temp[t_index_x, t_index_y + 1] - penalty,
temp[t_index_x + 1, t_index_y] - penalty)
end
sync_threads()
end
# TODO convert to for loop
m = BLOCK_SIZE-2
while m >= 0
if tx <= m
t_index_x = tx + BLOCK_SIZE - m
t_index_y = BLOCK_SIZE - tx
@inbounds temp[t_index_x + 1, t_index_y + 1] = max(
temp[t_index_x, t_index_y] +
ref[t_index_x, t_index_y],
temp[t_index_x, t_index_y + 1] - penalty,
temp[t_index_x + 1, t_index_y] - penalty)
end
sync_threads()
m -= 1
end
for ty = 0:BLOCK_SIZE-1
@inbounds matrix_cuda[index + ty * cols + 1] = temp[tx + 2, ty + 2]
end
return nothing
end
# FIXME: remove @inbounds (is work-around for shared memory bug)
function needle_cuda_shared_2(reference_ptr, reference_len, matrix_cuda_ptr,
matrix_cuda_len, cols, penalty, i, block_width)
reference = CuDeviceArray(reference_len, reference_ptr)
matrix_cuda = CuDeviceArray(matrix_cuda_len, matrix_cuda_ptr)
bx = blockIdx().x - 1
tx = threadIdx().x - 1
b_index_x = bx + block_width - i
b_index_y = block_width - bx - 1
index = cols * BLOCK_SIZE * b_index_y + BLOCK_SIZE * b_index_x + tx + cols + 1
index_n = cols * BLOCK_SIZE * b_index_y + BLOCK_SIZE * b_index_x + tx + 1
index_w = cols * BLOCK_SIZE * b_index_y + BLOCK_SIZE * b_index_x + cols
index_nw = cols * BLOCK_SIZE * b_index_y + BLOCK_SIZE * b_index_x
temp = @cuStaticSharedMem(Int32, (BLOCK_SIZE_P1, BLOCK_SIZE_P1))
ref = @cuStaticSharedMem(Int32, (BLOCK_SIZE, BLOCK_SIZE))
for ty = 0:BLOCK_SIZE-1
@inbounds ref[tx + 1, ty + 1] = reference[index + cols * ty + 1]
end
sync_threads()
if tx == 0
@inbounds temp[1, tx + 1] = matrix_cuda[index_nw + 1]
end
@inbounds temp[1, tx + 2] = matrix_cuda[index_w + cols * tx + 1]
sync_threads()
@inbounds temp[tx + 2, 1] = matrix_cuda[index_n + 1]
sync_threads()
for m = 0:BLOCK_SIZE-1
if tx <= m
t_index_x = tx + 1
t_index_y = m - tx + 1
@inbounds temp[t_index_x + 1, t_index_y + 1] = max(
temp[t_index_x, t_index_y] +
ref[t_index_x, t_index_y],
temp[t_index_x, t_index_y + 1] - penalty,
temp[t_index_x + 1, t_index_y] - penalty)
end
sync_threads()
end
# TODO convert to for loop
m = BLOCK_SIZE-2
while m >= 0
if tx <= m
t_index_x = tx + BLOCK_SIZE - m
t_index_y = BLOCK_SIZE - tx
@inbounds temp[t_index_x + 1, t_index_y + 1] = max(
temp[t_index_x, t_index_y] +
ref[t_index_x, t_index_y],
temp[t_index_x, t_index_y + 1] - penalty,
temp[t_index_x + 1, t_index_y] - penalty)
end
sync_threads()
m -= 1
end
for ty = 0:BLOCK_SIZE-1
@inbounds matrix_cuda[index + ty * cols + 1] = temp[tx + 2, ty + 2]
end
return nothing
end