-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.cpp
196 lines (167 loc) · 4.79 KB
/
main.cpp
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
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <chrono>
#include <omp.h>
#include <immintrin.h>
#include <zmmintrin.h>
#define DOU float
#define INT int
#define DOU_VEC __m512
using namespace std;
typedef chrono::high_resolution_clock Clock;
const int m=1638400; // DO NOT CHANGE!!
const int K=100000; // DO NOT CHANGE!!
DOU logDataVSPrior(const DOU* __restrict__ val, const INT* __restrict__ begin, const INT* __restrict__ end, DOU_VEC* __restrict__ res,
const DOU_VEC disturb0 , const INT thread_num);
int main ( int argc, char *argv[] )
{
int length = (m>>4)+(((m&15)+15)>>4);
register int thread_num = omp_get_num_procs();
int MM = (length<<4);
DOU * val = (DOU*)aligned_alloc(64,sizeof(DOU)*6*MM);
DOU * disturb = (DOU*)aligned_alloc(64,sizeof(DOU)*K);
DOU * result = (DOU*)aligned_alloc(64,sizeof(DOU)*K);
INT * begin = (INT*)aligned_alloc(64,sizeof(INT)*thread_num);
INT * end = (INT*)aligned_alloc(64,sizeof(INT)*thread_num);
DOU_VEC * res = (DOU_VEC*)aligned_alloc(64,sizeof(DOU_VEC)*thread_num);
DOU dat0, dat1, pri0, pri1, ctf0, sigRcp0;
register int tmp1, i;
register int num_per_thread = length/thread_num;
begin[0] = 0;
#pragma unroll(32)
for(i=0;i<thread_num-1;i++)
{
tmp1 = begin[i] + num_per_thread;
end[i] = tmp1;
begin[i+1] = tmp1;
}
end[thread_num-1] = length;
/***************************
* Read data from input.dat
* *************************/
ifstream fin;
fin.open("input.dat");
if(!fin.is_open())
{
cout << "Error opening file input.dat" << endl;
exit(1);
}
i=0;
int j;
while( !fin.eof() )
{
fin >> dat0 >> dat1 >> pri0 >> pri1 >> ctf0 >> sigRcp0;
if(!(i&15))
j = (i<<2)+(i<<1)-1;
j++;
val[j] = pri0;
val[j+16] = dat0;
val[j+32] = pri1;
val[j+48] = dat1;
val[j+64] = sigRcp0;
val[j+80] = ctf0;
i++;
if(i == m) break;
}
for(;i<MM;i++)
{
if(!(i&15))
j = (i<<2)+(i<<1)-1;
j++;
val[j] = 0.00;
val[j+16] = 0.00;
val[j+32] = 0.00;
val[j+48] = 0.00;
val[j+64] = 0.00;
val[j+80] = 0.00;
}
fin.close();
fin.open("K.dat");
if(!fin.is_open())
{
cout << "Error opening file K.dat" << endl;
exit(1);
}
i=0;
while( !fin.eof() )
{
fin >> disturb[i];
i++;
if(i == K) break;
}
fin.close();
/***************************
* main computation is here
* ************************/
auto startTime = Clock::now();
FILE * fp;
fp = fopen("result.dat","w");
if(!fp)
{
cout << "Error opening file for result" << endl;
exit(1);
}
unsigned INT t;
DOU_VEC disturb0;
for(t = 0; t < K; t++)
{
disturb0 = _mm512_set1_ps(disturb[t]);
result[t] = logDataVSPrior(val, begin, end, res, disturb0, thread_num);
}
#pragma unroll(32)
for(t = 0; t < K; t++)
{
fprintf(fp,"%d: %.5e\n",t+1,result[t]);
}
fclose(fp);
auto endTime = Clock::now();
auto compTime = chrono::duration_cast<chrono::microseconds>(endTime - startTime);
cout << "Computing time=" << compTime.count() << " microseconds" << endl;
free(val);
free(result);
free(disturb);
free(begin);
free(end);
return EXIT_SUCCESS;
}
DOU logDataVSPrior(const DOU* __restrict__ val, const INT* __restrict__ begin, const INT* __restrict__ end, DOU_VEC* __restrict__ res,
const DOU_VEC tmp , const INT thread_num)
{
register DOU_VEC res1,ans1,ans2,ans3,tmp0,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6;
register int i,i_begin,i_end;
#pragma omp parallel private(i,i_begin,i_end,res1,ans1,ans2,ans3,tmp0,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6)
{
#pragma omp for schedule(static) nowait
for(int j=0; j<thread_num; j++)
{
res1 = _mm512_setzero_ps();
i_begin = *(begin+j);
i_end = *(end+j);
#pragma unroll(32)
for(i = i_begin;i<i_end;i++)
{
register int index = (i<<6)+(i<<5);
tmp1 = _mm512_load_ps(val+index);
tmp2 = _mm512_load_ps(val+index+16);
tmp3 = _mm512_load_ps(val+index+32);
tmp4 = _mm512_load_ps(val+index+48);
tmp5 = _mm512_load_ps(val+index+64);
tmp6 = _mm512_load_ps(val+index+80);
tmp0 = _mm512_mul_ps(tmp6,tmp);
ans1 = _mm512_fnmadd_ps(tmp1,tmp0,tmp2);
ans2 = _mm512_fnmadd_ps(tmp3,tmp0,tmp4);
ans3 = _mm512_fmadd_ps(ans1,ans1,_mm512_mul_ps(ans2,ans2));
res1 = _mm512_fmadd_ps(ans3,tmp5,res1);
}
*(res+j)= res1;
}
}
res1 = res[0];
#pragma unroll(32)
for (i = 1; i < thread_num; i++)
{
res1 = _mm512_add_ps(res1,*(res+i));
}
return _mm512_reduce_add_ps(res1);
}