-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_baseline.c
161 lines (122 loc) · 3.11 KB
/
merge_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*************************************************************************
> File Name: merge_baseline.c
> Author: logos
> Mail: [email protected]
> Created Time: 2019年04月28日 星期日 15时24分56秒
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
#include<pthread.h>
#include<string.h>
#include<immintrin.h>
#define seed 1
#define NUM_THREADS 16
#define MAX(a,b) (((a)>(b)?:(a):(b)))
#define M 64
void max(const void *a, const void *b);
void sort_gen(int *d, int N, int s);
void print_array(int *d, int N);
void print_result(int *d, int N);
void* sort(void *arg);
void* merge_small(void *arg);
void* merge_large(void* arg);
void max(const void *a, const void *b){
return (*(int*)a-*(int*)b);
}
void sort_gen(int *d, int N,int s){
srand(s);
for(int i=0;i<N;i++){
d[i]=rand();
}
}
void print_array(int *d, int N){
for(int i=0;i<N;i++){
if(i%10==0) printf("\n");
printf("%d ",d[i]);
}
printf("\n");
}
void print_array(int *d, int N){
for(int i=0;i<N-1;i++){
if(i%10==0) printf("\n");
printf("%d ",d[i]>d[i+1]?0:1);
}
printf("\n");
}
struct paramter{
int N;
int *a; //unsorted array
int cnt;
int num; //the num block
}
void* sort(void *arg){
struct parameter *p;
p=(struct parameter *)arg;
int N=p->N;
int cnt=p->cnt;
int *a=p->a;
int num=p->num;
int thread_size=M/NUM_THREADS;
int start_index=num*M+thread_size*cnt;
int end_index=start_index+thread_size;
//sort a[start_index-end_index]
}
void* merge_small(void *arg){
}
void* merge_large(void *arg){
}
int main(int argc,char *argv[]){
//parameter init
const int N=atoi(argv[1]);
//float seed=atof(argv[2]);
int *a=(int *)malloc(N*sizeof(int));
//array generation
sort_gen(a,N,seed);
//start time
struct timeval start;
gettimeofday(&start,NULL);
//merge sort with threads-parallel
const int block_num=N/M; //divide the array into a[N/M] array
for(int i=0;i<block_num;i++){
//the i block
//init threads parameters
pthread_t threads[NUM_THREADS0];
struct parameter parameters[NUM_THREADS];
//divide the M block into NUM_THREADS * thread_size block
int thread_size=M/NUM_THREADS;
for(int cnt=0;cnt<NUM_THREADS;cnt++){
//init parameter
parameters[cnt].N=N;
parameters[cnt].a=a;
parameters[cnt].cnt=cnt;
parameters[cnt].num=i;
pthread_create(&threads[cnt],NULL,sort,¶meters[cnt]);
}
for(int cnt=0;cnt<NUM_THREADS;cnt++){
pthread_join(threads[cnt],NULL);
}
//merge T sorted array
for(int cnt=0;cnt<NUM_THREADS;cnt++){
//init parameter
pthread_create(&threads[cnt],NULL,merge_small,¶meters[cnt]);
}
for(int cnt=0;cnt<NUM_THREADS;cnt++){
pthread_join(threads[cnt],NULL);
}
//merge N/M sorted array
for(int cnt=0;cnt<NUM_THREADS;cnt++){
//init parameter
pthread_create(&threads[cnt],NULL,merge_large,¶meters[cnt]);
}
for(int cnt=0;cnt<NUM_THREADS;cnt++){
pthread_join(threads[cnt],NULL);
}
}
//end time
struct timeval end;
gettimeofday(&end,NULL);
long duration=end.tv_sec-start.tv_sec;
printf("time=%ld, number=%d\n",duration,a[N/2]);
free(a);
}