/
string_sort_try0.cu
executable file
·143 lines (118 loc) · 3.12 KB
/
string_sort_try0.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
//Taken from http://ldn.linuxfoundation.org/article/c-gpu-and-thrust-strings-gpu
//Also, https://groups.google.com/group/thrust-users/msg/0eac80d2e41cbcfb?pli=1, https://groups.google.com/group/thrust-users/browse_thread/thread/f4b1b825cc927df9?pli=1,
#include <thrust/device_ptr.h>
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/sort.h>
#include <thrust/copy.h>
#include <cstring>
#include <vector>
#include <iterator>
#define POOL_SZ (10*1024*1024)
using namespace std;
class device_string
{
public:
int cstr_len;
char* raw;
thrust::device_ptr<char> cstr;
static char* pool_raw;
static thrust::device_ptr<char> pool_cstr;
static thrust::device_ptr<char> pool_top;
// Sets the variables up the first time its used.
__host__ static void init()
{
static bool v = true;
if( v )
{
v = false;
pool_cstr = thrust::device_malloc(POOL_SZ);
pool_raw = (char*)raw_pointer_cast( pool_cstr );
pool_top = pool_cstr;
}
}
// Destructor for device variables used.
__host__ static void fin()
{
init();
thrust::device_free(pool_cstr);
}
// Parametrized constructor to copy one device_string to another.
__host__ device_string( const device_string& s )
{
cstr_len = s.cstr_len;
raw = s.raw;
cstr = s.cstr;
}
// Parametrized constructor to copy a std::string to device_string type
__host__ device_string( const std::string& s )
{
cstr_len = s.length();
init();
cstr = pool_top;
pool_top += cstr_len+1;
raw = (char *) raw_pointer_cast(cstr);
cudaMemcpy( raw, s.c_str(), cstr_len+1, cudaMemcpyHostToDevice );
}
// Default constructor.
__host__ __device__ device_string()
{
cstr_len = -1;
raw = NULL;
}
// Conversion operator to copy device_string type to std::string
// This is where the problem is
__host__ operator std::string(void)
{
std::string ret;
//device_ptr<char*>::iterator it = cstr.begin();
thrust::copy(cstr, cstr+cstr_len, back_inserter(ret));
return ret;
}
};
char* device_string::pool_raw;
thrust::device_ptr<char> device_string::pool_cstr;
thrust::device_ptr<char> device_string::pool_top;
// User-defined comparison operator
bool __device__ operator< (device_string lhs, device_string rhs)
{
char *l = lhs.raw;
char *r = rhs.raw;
for( ; *l && *r && *l==*r; )
{
++l;
++r;
}
return *l < *r;
}
int main()
{
char* all_repeats_h = "abcb\0bcba\0cbab\0babc";
int max_width = 4;
vector <std::string> h_vec;
for (int i = 0; i < max_width; i++) //rotation
{
h_vec.push_back(all_repeats_h + i*(max_width+1)*sizeof(char));
}
std::cout << "Content of h_vec..\n";
for(int i = 0; i<h_vec.size(); i++)
{
std::cout << h_vec[i] << endl;
}
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() ); //sort
std::cout << " Done with sort().. \nThe sorted list of conjugates are: \n\n";
for(int i = 0; i < d_vec.size(); i++)
{
device_string d_str(d_vec[i]);
h_vec[i] = d_str;
std::cout << h_vec[i] <<endl;
}
return 0;
}