-
Notifications
You must be signed in to change notification settings - Fork 0
/
cuda-dumb.cu
304 lines (225 loc) · 7.86 KB
/
cuda-dumb.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <cuda.h>
#include <cuda_fp16.h>
#include <stdio.h>
#include <stdint.h>
#include <zlib.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <fcntl.h>
#include <limits.h>
typedef struct __ReadSeqList {
char* sequence;
unsigned int length;
struct __ReadSeqList* next;
} ReadSeqList;
typedef struct HashTable {
unsigned int bits;
unsigned int count;
unsigned int read_count;
unsigned int read_length;
unsigned long long int *keys;
unsigned int *values;
} HashTable;
HashTable* HashTable_init(unsigned int bits, unsigned int read_count, unsigned int read_length){
HashTable *ht;
ht = (HashTable*)calloc(1, sizeof(HashTable));
ht->read_count = read_count;
ht->read_length = read_length;
ht->bits = bits;
ht->count = 0;
return ht;
}
void HashTable_destory(HashTable *ht) {
if (!ht) return;
free(ht);
}
__device__ const unsigned char seq_nt4_table[256] = { // translate ACGT to 0123
0, 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 0, 4, 1, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
};
// funcion para calcular un hash de 64 bits
__device__ unsigned int hash_uint64(unsigned long long int key) {
key = ~key + (key << 21);
key = key ^ key >> 24;
key = (key + (key << 3)) + (key << 8);
key = key ^ key >> 14;
key = (key + (key << 2)) + (key << 4);
key = key ^ key >> 28;
key = key + (key << 31);
return (unsigned int)key;
}
__device__ unsigned int h2b(unsigned int hash, unsigned int bits) {
return hash * 2654435769U >> (32 - bits);
}
__device__ void hash_insert(HashTable *ht, unsigned long long int kmer) {
unsigned int iKey, last;
iKey = last = h2b(hash_uint64(kmer), ht->bits);
while (ht->values[iKey] > 0 && ht->keys[iKey] != kmer) {
iKey = (iKey + 1U) & ((1U << ht->bits) - 1);
if (iKey == last) break;
}
// Comprobar si se ha encontrado un slot vacío
if (ht->values[iKey] == 0) { // no se ha encontrado la llave
ht->keys[iKey] = kmer;
ht->values[iKey] = 1;
++ht->count;
} else {
ht->values[iKey]++;
}
}
// insert k-mers in $seq to hash table $ht
__global__ void kernel_count_seq_kmers(HashTable *ht, int k, char *d_reads)
{
unsigned int tid = blockIdx.x * blockDim.x + threadIdx.x;
if(tid < ht->read_count) {
unsigned int i, l;
unsigned int len = ht->read_length;
//char *seq = d_reads + (tid * len);
//int len = strlen(seq);
unsigned long long int x[2], mask = (1ULL<<k*2) - 1, shift = (k - 1) * 2;
for (i = l = 0, x[0] = x[1] = 0; i < len; ++i) {
//int c = seq_nt4_table[(unsigned char)seq[i]];
int c = seq_nt4_table[(unsigned char)d_reads[(tid*len)+i]];
if (c < 4) { // not an "N" base
x[0] = (x[0] << 2 | c) & mask; // forward strand
x[1] = x[1] >> 2 | (unsigned long long int)(3 - c) << shift; // reverse strand
if (++l >= k) { // we find a k-mer
unsigned long long int kmer = x[0] < x[1]? x[0] : x[1];
hash_insert(ht, kmer); // only add one strand!
}
} else l = 0, x[0] = x[1] = 0; // if there is an "N", restart
}
}
}
__global__ void kernel_print_hist(const HashTable *ht, unsigned int *cnt_d)
{
unsigned int tid = blockIdx.x * blockDim.x + threadIdx.x;
unsigned int pos;
if(tid < (1U << ht->bits)) {
if (ht->values[tid] > 0) {
pos = ht->values[tid] < 256U ? ht->values[tid] : 255;
atomicAdd(&(cnt_d[pos]), 1U);
}
}
}
static int count_file(const char *fn, int k, unsigned int p)
{
HashTable *ht;
unsigned int i;
unsigned int capacity = 1U << p;
unsigned int cnt[256];
unsigned int read_count = 0;
unsigned int read_length = 0;
unsigned int fullength = 0;
char *reads;
// variables para cuda
HashTable *ht_d;
char *reads_d;
unsigned int *cnt_d;
unsigned long long int *keys_d;
unsigned int *values_d;
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen(fn, "r");
if (fp == NULL) exit(EXIT_FAILURE);
ReadSeqList *current, *head;
head = current = NULL;
while ((read = getline(&line, &len, fp)) != -1) {
read_count++;
line[strcspn(line, "\n")] = 0;
ReadSeqList *node = (ReadSeqList*)malloc(sizeof(ReadSeqList));
node->sequence = (char*)malloc(strlen(line));
strcpy(node->sequence, line);
node->length = strlen(line);
node->next = NULL;
fullength += strlen(line);
if(head == NULL){
current = head = node;
} else {
current = current->next = node;
}
}
fclose(fp);
if (line) free(line);
read_length = head->length;
// almacenar los caracteres en una array 1D
reads = (char*)malloc(read_length * read_count * sizeof(char));
for(i=0, current = head; current; current=current->next){
memcpy(reads + (i * read_length), current->sequence, read_length);
i++;
}
// inicializar hashtable
ht = HashTable_init(p, read_count, read_length);
printf("read count: %d\t read length: %d\t avg. length: %d\n", read_count, read_length, fullength/read_count);
// allocate memory in device
cudaMalloc((void **)&ht_d, sizeof(HashTable));
cudaMalloc((void **)&reads_d, read_length * read_count * sizeof(char));
cudaMalloc((void **)&keys_d, capacity * sizeof(unsigned long long int));
cudaMalloc((void **)&values_d, capacity * sizeof(unsigned int));
cudaMalloc((void **)&cnt_d, 256 * sizeof(unsigned int));
// initialize values
cudaMemset(keys_d, 0ULL, capacity * sizeof(unsigned long long int));
cudaMemset(values_d, 0, capacity * sizeof(unsigned int));
cudaMemset(cnt_d, 0, 256 * sizeof(unsigned int));
// copy data to device
ht->keys = keys_d;
ht->values = values_d;
cudaMemcpy(ht_d, ht, sizeof(HashTable), cudaMemcpyHostToDevice);
cudaMemcpy(reads_d, reads, read_length * read_count * sizeof (char), cudaMemcpyHostToDevice);
// invocar kernels
unsigned int thr = 1024;
kernel_count_seq_kmers<<<ceil(read_count/(float)thr), thr>>>(ht_d, k, reads_d);
cudaDeviceSynchronize();
kernel_print_hist<<<ceil(capacity/(float)thr), thr>>>(ht_d, cnt_d);
cudaDeviceSynchronize();
// copy data from device
cudaMemcpy(ht, ht_d, sizeof(HashTable), cudaMemcpyDeviceToHost);
cudaMemcpy(ht->keys, keys_d, capacity * sizeof(unsigned long long int), cudaMemcpyDeviceToHost);
cudaMemcpy(ht->values, values_d, capacity * sizeof(unsigned int), cudaMemcpyDeviceToHost);
cudaMemcpy(cnt, cnt_d, 256 * sizeof(unsigned int), cudaMemcpyDeviceToHost);
printf("read count: %d\t read length: %d\t avg. length: %d\n", read_count, read_length, fullength/read_count);
printf("COUNT: %d\n\n", ht->count);
// print histogram
for (i = 1; i < 256; ++i)
printf("%d\t%d\n", i, cnt[i]);
// limpieza
cudaFree(reads_d);
cudaFree(ht_d);
cudaFree(cnt_d);
cudaFree(keys_d);
cudaFree(values_d);
// limpieza
for(current = head; current; current=current->next){
free(current->sequence);
free(current);
}
free(reads);
HashTable_destory(ht);
return 0;
}
int main(int argc, char *argv[])
{
int k = 31;
unsigned int p = 27;
k = (int)strtol(argv[1], NULL, 10);
p = (unsigned int)strtol(argv[2], NULL, 10);
count_file(argv[3], k, p);
return 0;
}