-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpu_sort.cu
112 lines (98 loc) · 2.89 KB
/
gpu_sort.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
#include <thrust/device_vector.h>
#include <thrust/transform.h>
#include <thrust/sequence.h>
#include <thrust/copy.h>
#include <thrust/fill.h>
#include <thrust/replace.h>
#include <thrust/functional.h>
#include <thrust/sort.h>
#include <thrust/functional.h>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
// Device String Comparator Function
template <typename T>
struct greater_functor {
thrust::device_ptr<unsigned long> gene;
unsigned long i,j;
greater_functor(thrust::device_ptr<unsigned long> _gene) : gene(_gene) {}
__device__
int operator()( T x, T y){
for(i = x,j=y;; j++,i++)
{
if(gene[i] == (unsigned long)'\0')
return(0<1);
if (gene[j]== (unsigned long)'\0')
return(1<0);
if(gene[i] != gene[j])
return(gene[i] < gene[j]);
}
}
};
// Allocate space on device and copy genome onto it
// call Thrust::stable_sort function with our custom comparator.
void sort_Suffixes(unsigned long* gene, thrust::device_vector<unsigned long>& A ,unsigned long N){
unsigned long *dgene;
cudaMalloc((void **) &dgene, (N+1) * sizeof(unsigned long));
cudaMemcpy(dgene,gene, (N+1) * sizeof(unsigned long), cudaMemcpyHostToDevice);
thrust::device_ptr<unsigned long> dev_ptr(dgene);
thrust::stable_sort(A.begin(),A.end(),greater_functor<unsigned long>(dev_ptr));
cudaFree(dgene);
}
void print_suffix_list(thrust::device_vector<unsigned long>& list, unsigned long len, char* genome){
int i=0;
for(i=0; i<len; i++){
printf("%ld: %s\n", (unsigned long)list[i], genome+(unsigned long)list[i]);
}
}
void read_genome2(char *filename, char *buffer, unsigned long num){
FILE *fh;
fh = fopen(filename, "r");
fread(buffer, 1, num, fh);
buffer[num] = '\0';
fclose(fh);
}
unsigned long setup(unsigned long num, char* filename, char** genome){
*genome = (char *) malloc((num+1)*sizeof(char));
read_genome2(filename, *genome, num);
return (strlen(*genome));
}
int main(int argc, char* argv[])
{
if(argc < 5){
printf("Less Arguments!! \n");
return 0;
}
unsigned long count = atol(argv[1]);
unsigned long increaseSize = atol(argv[2]);
unsigned long maxSize = atol(argv[3]);
while(count <= maxSize){
char * genome;
unsigned long N = setup(count,argv[4], &genome);
unsigned long i = 0;
unsigned long * genome2;
genome2 =(unsigned long *)malloc(N*sizeof(unsigned long));
for(i=0;i<N;i++){
genome2[i] = (unsigned long)genome[i];
}
free(genome);
thrust::device_vector<unsigned long> A(count);
thrust::sequence(A.begin(),A.end());
clock_t start, end;
double runTime;
start = clock();
try
{
sort_Suffixes(genome2, A,N);
}
catch(thrust::system_error e) // Terminate Gracefully
{
std::cerr << "Error inside sort: " << e.what() << std::endl;
}
end = clock();
runTime = (end - start) / (double) CLOCKS_PER_SEC ;
printf("%ld %f\n",count, runTime);
count = count + increaseSize;
}
}