-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradix_baseline.c
137 lines (112 loc) · 2.71 KB
/
radix_baseline.c
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
/*************************************************************************
> File Name: radix_baseline.c
> Author: logos
> Mail: [email protected]
> Created Time: 2019年04月26日 星期五 14时58分01秒
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
#include<pthread.h>
#include<immintrin.h>
#include<string.h>
//#define N 1000000000 //the size of unsorted array
#define seed 1 //the random seed
#define B 256 //divide the int into 4 parts, the size of each part is 256
#define NUM 4 //the number of parts divided, the size of int is 4 byte
#define AVX_SIZE 8
#define SSE_SIZE 4
#define SIZE AVX_SIZE
int max(const void*a, const void *b);
void sort_gen(int *d, int N);
void print_array(int *a, int N);
int max(const void *a, const void *b){
return (*(int*)a-*(int*)b);
}
void sort_gen(int *d,int N){
srand(seed);
for(int i=0;i<N;i++){
d[i]=rand();
}
}
void print_result(int *a,int N){
int end=1000;
for(int cnt=0;cnt<end-1;cnt++){
printf("%d ",a[cnt]<a[cnt+1]? 1:0);
if(cnt%10==0) printf("\n");
}
}
void print_array(int *a, int N){
for(int cnt=0;cnt<N;cnt++){
printf("%d ",a[cnt]);
if(cnt%30==0) printf("\n");
}
printf("\n");
}
int main(int argc, char *argv[]){
//parameter initiation
const int N = atoi(argv[1]);
//float seed = atof(argv[2]);
int *a=(int *)malloc(N*sizeof(int));
//array generation
sort_gen(a,N);
//start time
struct timeval start;
gettimeofday(&start,NULL);
//radix sort without thread-parallel & cache optimization
for(int cnt=0;cnt<NUM;cnt++){
int *result=(int *)malloc(N*sizeof(int));
int n[B]={0}; // n[i] record the size of the ith bucket
int P[B]={0}; // P[i] record the head address of the ith bucket
int b[B]={0};
int and,shift;
if(cnt==0){
and=0x000000ff;
shift=0;
}
else if(cnt==1){
and=0x0000ff00;
shift=8;
}
else if(cnt==2){
and=0x00ff0000;
shift=16;
}
else if(cnt==3){
and=0xff000000;
shift=24;
}
//scan unsort array, calculating n[B]
for(int i=0;i<N;i++){
int value= (a[i]&and)>>(shift);
int index= value%B; //the index (th) hash bucket
n[index]+=1;
}
int address=0;
//caclulating P[B]
for(int i=0;i<B;i++){
if(n[i]==0){ //empty hash bucket
P[i]=-1;
continue;
}
P[i]=address;
address+=n[i];
}
//put the value into bucket
for(int i=0;i<N;i++){
int value = (a[i]&and)>>shift;
int index=value%B;
result[P[index]+b[index]]=a[i];
b[index]++;
}
memcpy(a,result,N*sizeof(int));
free(result);
}
//end time
struct timeval end;
gettimeofday(&end,NULL);
//print_result(a,N);
//time calculation
printf("time=%d, number=%d",(int)end.tv_usec-(int)start.tv_usec,a[N/2]);
free(a);
}