-
Notifications
You must be signed in to change notification settings - Fork 0
/
xCMMA.c
236 lines (213 loc) · 7.08 KB
/
xCMMA.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
///////////////////////////////////////////////////////////////////////////////
// xCMMA.c - Tests for Intel(R) Xeon Phi(TM) Processor.
// Implemented by Yash Akhauri.
// Notes:
// - Performance tests matrix multiply algorithms on a Intel Xeon Phi 7210 Processor.
// - To compile, make sure the directory of echo ~/_director_/xconv.out | qsub matches.
// To Compile:
// icpc -xMIC-AVX512 -qopenmp -mkl -fp-model fast=2 -fma -unroll=4 xCMMA.c -o xcmma.out && echo ~/xcmma.out | qsub
//
///////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <pthread.h>
#include <omp.h>
#include <math.h>
#include <mkl.h>
#include <iostream>
#define FPUTYPE float
#define BINTYPE unsigned int
// #define MX_SIZE 16384
// #define MX_SIZE 8192
#define MX_SIZE 4096
// #define MX_SIZE 2048
// #define MX_SIZE 1024
// #define MX_SIZE 512
// #define MX_SIZE 256
#define NUM_OF_THREADS 64
#define TEST_LOOP 100
// printBits prints the binary format of the unsigned int passed to it.
void printBits(size_t const size, void const * const ptr){
unsigned char *b = (unsigned char*) ptr;
unsigned char byte;
int i, j;
printf("\n");
for (i=size-1;i>=0;i--)
for (j=7;j>=0;j--)
{
byte = (b[i] >> j) & 1;
printf("%u", byte);
}
puts(""); printf("\n");
}
int main( void )
{
size_t m, n, p;
size_t r, i, j, k, sm;
double dTimeS, dTimeE;
m = p = n = MX_SIZE;
printf("Matrix size: %d x %d\n", m, p);
putenv("KMP_AFFINITY=scatter");
// putenv("KMP_AFFINITY=balanced, granularity=fine");
// putenv("KMP_AFFINITY=compact");
omp_set_num_threads(NUM_OF_THREADS);
printf("Number of OpenMP threads: %3d\n", NUM_OF_THREADS);
//////////////////////// Allocate full precision matrices ///////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
__attribute__( ( aligned( 64 ) ) ) FPUTYPE **pA = NULL; // Allocating memory
__attribute__( ( aligned( 64 ) ) ) FPUTYPE **pB = NULL; // for matrices aligned
__attribute__( ( aligned( 64 ) ) ) FPUTYPE **pC = NULL; // on 64-byte boundary
pA = ( FPUTYPE ** )_mm_malloc(m*sizeof(FPUTYPE *), 64); // These loops can
for(int i = 0; i < m; i++){ // be collapsed
pA[i] = ( FPUTYPE * )_mm_malloc(p*sizeof(FPUTYPE), 64); // as m = n = p = MX_SIZE
}
pB = ( FPUTYPE ** )_mm_malloc(p*sizeof(FPUTYPE *), 64);
for(int i = 0; i < p; i++){
pB[i] = ( FPUTYPE * )_mm_malloc(n*sizeof(FPUTYPE), 64);
}
pC = ( FPUTYPE ** )_mm_malloc(m*sizeof(FPUTYPE *), 64);
for(int i = 0; i < m; i++){
pC[i] = ( FPUTYPE * )_mm_malloc(n*sizeof(FPUTYPE), 64);
}
if( pA == NULL || pB == NULL || pC == NULL ) // Error handling
{ // if any array is
printf( "\nERROR: Can't allocate memory for matrices\n" ); // not allocated
_mm_free( pA );
_mm_free( pB );
_mm_free( pC );
return ( int )0;
}
for(int j = 0; j < m; j++){
for( i = 0; i < p; i++)
{
FPUTYPE x = (FPUTYPE) rand()/RAND_MAX; // Create random
pA[j][i] = ( x < 0.5 ) ? -1 : 1; // +1/-1 matrices
}
}
for(int j = 0; j < p; j++){
for( i = 0; i < n; i++)
{
FPUTYPE x = (FPUTYPE) rand()/RAND_MAX; // Create random
pB[j][i] = ( x > 0.5 ) ? -1 : 1; // +1/-1 matrices
}
}
for(int j = 0; j < m; j++){
for( i = 0; i < n; i++)
{
pC[j][i] = 0;
}
}
//////////////////////// FP Matrix multiplication ///////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
float sum = 0;
dTimeS = dsecnd();
for(int jj = 0; jj < TEST_LOOP; jj++){
#pragma omp parallel for private(i, j, k, sum) num_threads(NUM_OF_THREADS)
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
sum = 0.0;
for(int k = 0; k < p; k++){
sum += pA[i][k]*pB[k][j];
}
pC[i][j] = sum;
}
}
}
dTimeE = dsecnd();
printf( "\nFull precision CMMA - Completed in: %.7f seconds\n", ( dTimeE - dTimeS ) / (double) TEST_LOOP);
printf("\nFull precision multiplication result:\n");
for(int i = 0; i<4; i++){
for(j = 0; j<5; j++){
printf("%f\t", pC[i][j]);
}
printf("\n");
} printf("\n");
//////////////////////// Allocate binary matrices ///////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
__attribute__( ( aligned( 64 ) ) ) BINTYPE **bA = NULL; // Allocated binary
__attribute__( ( aligned( 64 ) ) ) BINTYPE **bB = NULL; // matrices A and B
bA = ( BINTYPE ** )_mm_malloc(m*sizeof(BINTYPE *), 64);
bB = ( BINTYPE ** )_mm_malloc(n*sizeof(BINTYPE *), 64);
if( bA == NULL || bB == NULL ) // Error handling
{ // if any array is
printf( "\nERROR: Can't allocate memory for matrices\n" ); // not allocated
_mm_free( bA );
_mm_free( bB );
return ( int )0;
}
for(int i = 0; i<m; i++){
bA[i] = (BINTYPE *)_mm_malloc((p/32)*sizeof(BINTYPE), 64);
}
for(int i = 0; i<n; i++){
bB[i] = (BINTYPE *)_mm_malloc((p/32)*sizeof(BINTYPE), 64);
}
//////////////////////// Binarization of A&B ///////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
int sign; BINTYPE tbA; BINTYPE tbB;
dTimeS = dsecnd();
for(int jj = 0; jj < TEST_LOOP; jj++){
#pragma omp parallel for
for (int i = 0; i < MX_SIZE; i++)
{
for(int seg = 0; seg < MX_SIZE/32; seg++)
{
tbA = 0;
for(int j = 0; j < 32; j++)
{// [i*n + seg*32 + j] For flattened matrices
sign = (int) (pA[i][seg*32 + j] >= 0);
tbA = tbA|(sign<<j);
}
bA[i][seg] = tbA;
}
}
}
dTimeE = dsecnd();
printf( "\nBinarization A - Completed in: %.7f seconds\n", ( dTimeE - dTimeS ) / TEST_LOOP);
dTimeS = dsecnd();
for(int jj = 0; jj < TEST_LOOP; jj++){
#pragma omp parallel for
for (int i = 0; i < MX_SIZE; i++)
{
for(int seg = 0; seg < MX_SIZE/32; seg++)
{
tbB = 0;
for(int j = 0; j < 32; j++)
{// [i+seg*32*n + j*n] For flattened matrices
sign = (int) (pB[seg*32 + j][i] >= 0);
tbB = tbB|(sign<<j);
}
bB[i][seg] = tbB;
}
}
}
dTimeE = dsecnd();
printf( "\nBinarization B - Completed in: %.7f seconds\n\n\n", ( dTimeE - dTimeS ) / TEST_LOOP );
//////////////////////// Binarized Multiplication ///////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
int temp;
int storeto;
dTimeS = dsecnd();
for(int jj = 0; jj < TEST_LOOP; jj++){
#pragma omp parallel for private(i, j, sm, temp) num_threads(NUM_OF_THREADS)
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
temp = 0;
for(int sm = 0; sm < p/32; sm++){
temp += 2*(__builtin_popcount(~(bA[i][sm]^bB[j][sm]))) - 32;
}
pC[i][j] = temp;
}
}
}
dTimeE = dsecnd();
printf( "\nBinarized Multiplication - Completed in: %.7f seconds\n\n\n", ( dTimeE - dTimeS ) / (double) TEST_LOOP);
printf("\nBinarized multiplication result:\n");
for(int i = 0; i<4; i++){
for(j = 0; j<5; j++){
printf("%f\t", pC[i][j]);
}
printf("\n");
}
}