-
Notifications
You must be signed in to change notification settings - Fork 21
/
bwt.cu
executable file
·99 lines (79 loc) · 2.01 KB
/
bwt.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
#ifndef BWT_CU
#define BWT_CU
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/device_ptr.h>
#include <thrust/copy.h>
#include <thrust/sequence.h>
#include <thrust/sort.h>
#include <thrust/fill.h>
#include <cstdio>
#include <iostream>
#include <cstring>
#include <iterator>
#include "../lib/device_string.cu"
#define POOL_SZ (10*1024*1024)
using namespace std;
void rotate(int N, char *word, vector<string> *h_vec)
{
char *str, *rot;
cudaMalloc((void**)&str, /*sizeof(char) * */(N + 1));
cudaMalloc((void**)&rot, /*sizeof(char) * */((N + 1) * (N + 1)));
thrust::device_ptr<char> strD(str);
thrust::device_ptr<char> rotD(rot);
thrust::copy(word, word + N, strD);
for (int i = 0; i < N; i++) //Rotations happen in this loop
{
thrust::copy(strD + i, strD + N, rotD + (i * N));
thrust::copy(strD, strD + i, rotD + (i * N) + (N - i));
}
for (int i = 0; i < N; i++) //We extract data back from the GPU
{
cudaMemcpy(word, rot + (i * N), N, cudaMemcpyDeviceToHost);
h_vec->push_back(word);
}
cudaFree(str);
cudaFree(rot);
}
void sort(vector<string> *h_vec, char *result)
{
thrust::device_vector<device_string> d_vec;
d_vec.reserve(h_vec->size());
for(vector<std::string>::iterator iter = h_vec->begin(); iter!=h_vec->end(); ++iter)
{
device_string d_str(*iter);
d_vec.push_back(d_str);
}
thrust::sort(d_vec.begin(), d_vec.end() );
for(int i = 0; i < d_vec.size(); i++)
{
device_string d_str(d_vec[i]);
h_vec->at(i) = d_str;
result[i] = (h_vec->at(i)).at(h_vec->at(i).length() - 1);
}
}
char* bwt( char *word)
{
int N = strlen(word);
vector<string> h_vec;
rotate(N, word, &h_vec);
char *result = new char(N);
sort(&h_vec, result);
return result;
}
int main(int argc, char *argv[])
{
if (argc != 2)
{
cout << "Usage: bwt.out STRING_INPUT" << endl;
exit(1);
}
// char word[256];
int N = strlen(argv[1]);
// char * word = new char(N);
// strncpy(word, bwt(argv[1]), N);
// cout << word << endl;
cout << bwt(argv[1]) << endl;
return 0;
}
#endif