-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernels.cu
120 lines (103 loc) · 2.62 KB
/
kernels.cu
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
#include "kernels.cuh"
namespace kernels
{
__global__ void lowerData(
char* data
, const size_t data_length)
{
size_t tx{ threadIdx.x };
size_t bx{ blockIdx.x };
size_t bdim{ blockDim.x };
size_t gdim{ gridDim.x * blockDim.x };
const size_t data_size{ data_length * general::word_size };
char c;
const char addition{ 'a' - 'A' };
for (size_t i{ bx * bdim + tx }; i < data_size; i += gdim) {
c = data[i];
if (c <= 'Z' && c >= 'A')
data[i] += addition;
}
}
__global__ void removeExcessives(
char* data
, const size_t data_length)
{
size_t tx{ threadIdx.x };
size_t bx{ blockIdx.x };
size_t bdim{ blockDim.x };
size_t gdim{ gridDim.x * blockDim.x };
size_t wdidx;
size_t j;
char c;
for (size_t i{ bx * bdim + tx }; i < data_length; i += gdim) {
// Word index
wdidx = i * general::word_size;
for (j = 0; j < general::word_size; ++j) {
c = data[wdidx + j];
if (c <= 'z' && c >= 'a') {
while (c <= 'z' && c >= 'a') {
data[wdidx] = c;
++wdidx;
c = data[wdidx + j];
}
data[wdidx] = '\0';
break;
}
}
}
}
__global__ void countWords(
const char* data
, const size_t data_length
, const char* keywords
, int* histogram)
{
__shared__ char kws[general::keywords_length * general::word_size];
__shared__ int lcl_hist[general::keywords_length];
size_t tx{ threadIdx.x };
size_t bx{ blockIdx.x };
size_t bdim{ blockDim.x };
size_t gdim{ gridDim.x * blockDim.x };
// Copy keywords to shared memory
size_t i{ tx };
for (; i < general::keywords_length * general::word_size; i += bdim)
kws[i] = keywords[i];
// Initialise local histogram
for (i = tx; i < general::keywords_length; i += bdim)
lcl_hist[i] = 0;
__syncthreads();
// Process data
size_t wdidx;
size_t j;
size_t kwidx;
bool equal;
size_t k;
for (i = bx * bdim + tx; i < data_length; i += gdim) {
// Word index
wdidx = i * general::word_size;
// Search the keywords to find the index and update local histogram
for (j = 0; j < general::keywords_length; ++j) {
// Keyword index
kwidx = j * general::word_size;
equal = true;
for (k = 0; k < general::word_size; ++k) {
if (kws[kwidx + k] == '\0' && data[wdidx + k] == '\0')
break;
if (kws[kwidx + k] != data[wdidx + k]) {
equal = false;
break;
}
}
if (equal) {
atomicAdd(&lcl_hist[j], 1);
break; // Stop searching the keywords
}
}
}
__syncthreads();
// Add local histogram onto the global one
i = tx;
for (; i < general::keywords_length; i += bdim)
atomicAdd(&histogram[i], lcl_hist[i]);
}
}