-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiThreads_cache_v2.c
185 lines (149 loc) · 3.41 KB
/
multiThreads_cache_v2.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
/*************************************************************************
> File Name: multiThreads_cache_v2.c
> Author: logos
> Mail: [email protected]
> Created Time: 2019年04月18日 Friday 13时52分44秒
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
#include<pthread.h>
#define NUM_THREADS 16
#define M 64
float rand_float(float s){
return 4*s*(1-s);
}
void matrix_gen(float *a,float *b,int N,float seed){
float s=seed;
for(int i=0;i<N*N;i++){
s=rand_float(s);
a[i]=s;
s=rand_float(s);
b[i]=s;
}
}
void print_matrix(float *a,int N){
for(int i=0;i<N;i++){
if(i%M==0){
printf("\n");
}
for(int j=0;j<N;j++){
if(j%M==0){
printf(" ");
}
printf("%f ",a[i*N+j]);
}
printf("\n");
}
printf("\n");
}
float cal_trace(float *a,int N){
float result=0;
for(int i=0;i<N;i++){
result+=a[i*(N+1)];
}
return result;
}
struct parameter{
int N;
float *a;
float *b;
float *c;
int number;
};
void matrix_c_add(float record[M*M], float *c, int N, int r, int l){
for(int i=0;i<M;i++){
for(int j=0;j<M;j++){
c[(r*M+i)*N+l*M+j]+=record[i*M+j];
}
}
}
void set_small_matrix(float record[M*M],float *a,int N, int r,int l){
for(int i=0;i<M;i++){
for(int j=0;j<M;j++){
record[i*M+j]=a[(r*M+i)*N+(l*M+j)];
}
}
}
void* matrix_mul(void *arg){
struct parameter *p;
p=(struct parameter *) arg;
const int N=p->N;
float *a=p->a;
float *b=p->b;
float *c=p->c;
int number=p->number;
// the number of blocks in a row(length)
const int P=N/M;
const int size_block_thread=P/NUM_THREADS;
int i_start=number*size_block_thread; //the cnt thread is for the cnt row
int i_finish=i_start+size_block_thread;
int j_start=0;
int j_finish=P;
for(int i=i_start;i<i_finish;i++){
for(int j=j_start;j<j_finish;j++){
//c[i][j]+=a[i][k]*b[k][j];
for(int k=0; k<P; k++){
//M*M matrix multi
for(int x=0;x<M;x++){
for(int y=0;y<M;y++){
for(int z=0;z<M;z++){
c[(i*M+x)*N+(j*M+y)]+=(a[((i*M+x)%N)*N+((k*M+y)+z)%N]*b[((k*M+x+z)%N)*N+((j*M+y))%N]);
}
}
}
}
}
}
}
/*
* inputs: N, seed
*/
int main(int argc, char *argv[]){
//parameter initiation
const int N= atoi(argv[1]); //the size of matrix
float seed=atof(argv[2]);
//matrix init
float *a;
float *b;
float *c;
a=(float*)malloc(N*N*sizeof(float));
b=(float*)malloc(N*N*sizeof(float));
c=(float*)malloc(N*N*sizeof(float));
//matrix generation
matrix_gen(a,b,N,seed);
//run time calculation
struct timeval start;
struct timeval end;
//init thread parameters
pthread_t threads[NUM_THREADS];
struct parameter parameters[NUM_THREADS];
gettimeofday(&start,NULL);
//matrix c calculation
//parallel optimization
for(int cnt=0;cnt<NUM_THREADS;cnt++){
//parameters set value
parameters[cnt].N=N;
parameters[cnt].a=a;
parameters[cnt].b=b;
parameters[cnt].c=c;
parameters[cnt].number=cnt;
pthread_create(&threads[cnt],NULL,matrix_mul,¶meters[cnt]); //create thread
//printf("The %d thread, calculating c[%d~%d][%d~%d]\n",cnt,i_start,i_finish,j_start,j_finish);
}
for(int cnt=0;cnt<NUM_THREADS;cnt++){
pthread_join(threads[cnt],NULL);
}
gettimeofday(&end,NULL);
float duration=end.tv_sec-start.tv_sec;
float trace=0;
//trace calculation
trace=cal_trace(c,N);
printf("%f %f\n",trace,duration);
/*
* code for test
*/
// print_matrix(a,N);
// print_matrix(b,N);
// print_matrix(c,N);
}