-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallel_cache_SSE.c
275 lines (220 loc) · 6.33 KB
/
parallel_cache_SSE.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
/*************************************************************************
> File Name: parallel_cache_SSE.c
> Author: logos
> Mail: [email protected]
> Created Time: 2019年04月23日 Tuesday 18时01分44秒
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
#include<pthread.h>
#include<string.h>
#include<xmmintrin.h>
#define NUM_THREADS 16
#define M 64
#define SSE_SIZE 4
#define AVX_SIZE 8
float rand_float(float s);
void matrix_gen(float *a,float *b,int N,float seed);
void print_matrix(float *a,int N);
float cal_trace(float *a,int N);
void matrix_mul_blocks(float a[M*M], float b[M*M], float c[M*M]);
void* matrix_mul(void *arg);
void matrix_c_add(float *record,int len, float *c, int N, int r, int l);
void set_small_matrix(float *record,int len,float *a,int N, int r,int l);
float rand_float(float s){// to produce a random float, 0<seed<1
return 4*s*(1-s);
}
void matrix_gen(float *a,float *b,int N,float seed){//to generate two N*N(float) matrixs a & b
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){// to print a N*N matrix a
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){// to calculate the trace of N*N matrix a
float result=0;
for(int i=0;i<N;i++){
result+=a[i*(N+1)];
}
return result;
}
struct parameter{//to creata a struce which will be passed to the threads as argument
int N;
float *a;
float *b;
float *c;
int number;
};
void matrix_mul_blocks(float a[M*M],float b[M*M], float c[M*M]){ // c= a*b
int num= M/SSE_SIZE; // the number of blocks in a row, now the a,b,c is a num*num matrix, SSE*SSE size each block
for(int i=0;i<num;i++){
int op1=i*SSE_SIZE*M;
for(int j=0;j<num;j++){//calculating c[i][j] = sum(a[i][k]*b[k][j]);i
int op2=j*SSE_SIZE*M;
int op3=j*SSE_SIZE;
float *record_c=(float *)malloc(SSE_SIZE*SSE_SIZE*sizeof(float)); //record of c
for(int k=0;k<num;k++){//calculating a[i][k]*b[k][j], multiplication of SSE_SIZE*SSE_SIZE matrix
int op4=k*SSE_SIZE;
// data initiating
float *a0=a+(op1+op4); //the first address of block a[i][k];
float *b0=b+(op4*M+op3); //the first address of block b[k][j];
__m128 row0=_mm_load_ps(b0); b0+=M; //the first row of b
__m128 row1=_mm_load_ps(b0); b0+=M;// the second row of b
__m128 row2=_mm_load_ps(b0); b0+=M; // the third row of b
__m128 row3=_mm_load_ps(b0); //the fourth row of b
for(int cnt=0;cnt<SSE_SIZE;cnt++){
__m128 c0= _mm_set1_ps(a0[M*cnt+0]); //a[cnt][0]
__m128 c1= _mm_set1_ps(a0[M*cnt+1]); //a[cnt][1]
__m128 c2= _mm_set1_ps(a0[M*cnt+2]); //a[cnt][2]
__m128 c3= _mm_set1_ps(a0[M*cnt+3]); //a[cnt][3]
__m128 row =_mm_add_ps(
_mm_add_ps(_mm_mul_ps(c0,row0),_mm_mul_ps(c1,row1)),
_mm_add_ps(_mm_mul_ps(c2,row2),_mm_mul_ps(c3,row3))
);
_mm_store_ps(&record_c[SSE_SIZE*cnt],row);
}
matrix_c_add(record_c,SSE_SIZE,c,M,i,j);
}
//print_matrix(record_c,SSE_SIZE);
//print_matrix(c,M);
free(record_c);
}
}
}
void matrix_c_add(float *record,int len, float *c, int N, int r, int l){
for(int i=0;i<len;i++){
for(int j=0;j<len;j++){
c[(r*len+i)*N+l*len+j]+=record[i*len+j];
}
}
}
void set_small_matrix(float *record,int len,float *a,int N, int r,int l){
for(int i=0;i<len;i++){
for(int j=0;j<len;j++){
//printf("record[%d][%d]=a[%d][%d]\n",i,j,r*M+i,l*M+j);
//printf("record[%d][%d]=%f\n",i,j,a[(r*M+i)*N+(l*M+j)]);
record[i*len+j]=a[(r*len+i)*N+(l*len+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;
const int P= N/M; //the number of a blocks in a row, divide the N*N matrix into P*P matrix
int size_block_thread=P/NUM_THREADS; //the number of a blocks per thread
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++){
for(int k=0; k<P; k++){
//c[i][j]+=a[i][k]+b[k][j];
float *a_copy=(float*)malloc(M*M*sizeof(float)); // block in a matrix
float *b_copy=(float*)malloc(M*M*sizeof(float)); // block in a matrix
float *record=(float*)malloc(M*M*sizeof(float)); // block in a matrix
//init a_copy & b_copy
//a_copy = a[i][k]
/*
printf("a=\n");
print_matrix(a,N);
*/
set_small_matrix(a_copy,M,a,N,i,k);
//b_copy = b[k][j]
set_small_matrix(b_copy,M,b,N,k,j);
//block mul
matrix_mul_blocks(a_copy,b_copy,record);
/*
printf("a_copy=\n");
print_matrix(a_copy,M);
printf("b_copy=\n");
print_matrix(b_copy,M);
printf("record=\n");
print_matrix(record,M);
*/
matrix_c_add(record,M,c,N,i,j);
/*
free(a_copy);
free(b_copy);
free(record);
*/
}
}
}
}
/*
* 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
}
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);
free(a);
free(b);
free(c);
}