-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradix_parallel.c
280 lines (229 loc) · 5.35 KB
/
radix_parallel.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
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
/*************************************************************************
> File Name: radix_parallel.c
> Author: logos
> Mail: [email protected]
> Created Time: 2019年04月26日 星期五 23时23分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
#define BASE 8
#define NUM_THREADS 25
int max(const void*a, const void *b);
void sort_gen(int *d, int N);
void print_array(int *a, int N);
void* counting(void *arg);
void* hashing(void *arg);
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){
for(int cnt=0;cnt<N-1;cnt++){
printf("%d ",a[cnt]>a[cnt+1]? 0:1);
if(cnt%10==0) printf("\n");
}
}
void print_array(int *a, int N){
for(int cnt=0;cnt<N;cnt++){
if(cnt%10==0) printf("\n");
printf("%d ",a[cnt]);
}
printf("\n");
}
struct parameter{
int N;
int *num;
int *D;
int *a;
int *result;
int part;
int cnt;
};
void* counting(void *arg){
struct parameter *p;
p=(struct parameter *) arg;
int N=p->N;
int *num=p->num;
int *a=p->a;
int part=p->part;
int cnt=p->cnt;
int size=N/NUM_THREADS;
int start_index=cnt*size;
int end_index=start_index+size;
int and;
int shift;
if(part==0){
and=0x000000ff;
shift=0;
}
else if(part==1){
and=0x0000ff00;
shift=8;
}
else if(part==2){
and=0x00ff0000;
shift=16;
}
else if(part==3){
and=0xff000000;
shift=24;
}
//scan unsort array, calculating num[B]
for(int i=start_index;i<end_index;i++){
int value= (a[i] & (and))>>(shift);
int index= value%B; //the index (th) hash bucket
//printf("index=%d\n",index);
num[cnt*B+index]++;
}
}
void* hashing(void *arg){
struct parameter *p;
p=(struct parameter *) arg;
int N=p->N;
int *num=p->num;
int *D=p->D;
int *a=p->a;
int *result=p->result;
int part=p->part;
int cnt=p->cnt;
int size=N/NUM_THREADS;
int start_index=size*cnt;
int end_index=start_index+size;
int and;
int shift;
if(part==0){
and=0x000000ff;
shift=0;
}
else if(part==1){
and=0x0000ff00;
shift=8;
}
else if(part==2){
and=0x00ff0000;
shift=16;
}
else if(part==3){
and=0xff000000;
shift=24;
}
for(int i=start_index;i<end_index;i++){
int value = (a[i]&and)>>shift;
int index=value%B;
result[D[cnt*B+index]]=a[i];
D[cnt*B+index]++;
}
}
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));
int *result=(int *)malloc(N*sizeof(int));
//array generation
sort_gen(a,N);
//start time
struct timeval start;
struct timeval end;
gettimeofday(&start,NULL);
//radix sort with thread-parallel, without cache optimization
//init thread parameters
pthread_t threads_one[NUM_THREADS];
struct parameter parameters_one[NUM_THREADS];
int size=N/NUM_THREADS;
for(int part=0;part<NUM;part++){
//the first step
int n[B]={0}; // the size of the ith bucket
int D[NUM_THREADS*B]={0}; // D[i][j] represents the address of thread i in bucket j
int num[NUM_THREADS*B]={0};
for(int cnt=0;cnt<NUM_THREADS;cnt++){
parameters_one[cnt].N=N;
parameters_one[cnt].num=num;
parameters_one[cnt].a=a;
parameters_one[cnt].result=result;
parameters_one[cnt].cnt=cnt;
parameters_one[cnt].part=part;
pthread_create(&threads_one[cnt],NULL,counting,¶meters_one[cnt]);
}
for(int i=0;i<NUM_THREADS;i++){
pthread_join(threads_one[i],NULL);
}
for(int i=0;i<B;i++){
for(int j=0;j<NUM_THREADS;j++){
n[i]+=num[j*B+i];
}
}
//the second step
int address=0;
for(int j=0;j<B;j++){
if(n[j]==0) continue;
int offset=0;
for(int i=0;i<NUM_THREADS;i++){
if(num[i*B+j]==0) continue;
D[i*B+j]=address+offset;
offset+=num[i*B+j];
}
address+=n[j];
}
/*
* print D matrix & num & n matrix
*/
/*
for(int i=0;i<NUM_THREADS;i++){
printf("the %d thread:",i);
for(int j=0;j<B;j++){
printf("num[%d]=%d;",j,num[i*B+j]);
printf("n[%d]=%d;",j,n[j]);
printf("D[%d][%d]=%d\n",i,j,D[i*B+j]);
}
printf("\n");
}
*/
//the third step
struct parameter parameters_two[NUM_THREADS];
pthread_t threads_two[NUM_THREADS];
for(int cnt=0;cnt<NUM_THREADS;cnt++){
parameters_two[cnt].N=N;
parameters_two[cnt].D=D;
parameters_two[cnt].a=a;
parameters_two[cnt].result=result;
parameters_two[cnt].cnt=cnt;
parameters_two[cnt].part=part;
pthread_create(&threads_two[cnt],NULL,hashing,¶meters_two[cnt]);
}
for(int i=0;i<NUM_THREADS;i++){
pthread_join(threads_two[i],NULL);
}
memcpy(a,result,N*sizeof(int));
memset(result,0,(N*sizeof(int)));
}
//end time
gettimeofday(&end,NULL);
long start_time=start.tv_sec;
long end_time=end.tv_sec;
long duration=end_time-start_time;
//ms
//print_array(a,N);
//print_result(a,N);
//time calculation
printf("start_time=%ld,end_time=%ld\n",start_time,end_time);
printf("time=%ld, number=%d",duration,a[N/2]);
free(result);
free(a);
}