forked from ShiqiYu/CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatoperation.cpp
159 lines (137 loc) · 3.41 KB
/
matoperation.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
#include <iostream>
#include "matoperation.hpp"
#ifdef WITH_AVX2
#include <immintrin.h>
#endif
#ifdef WITH_NEON
#include <arm_neon.h>
#endif
#ifdef _OPENMP
#include <omp.h>
#endif
float dotproduct(const float *p1, const float * p2, size_t n)
{
float sum = 0.0f;
for (size_t i = 0; i < n ; i++)
sum += (p1[i] * p2[i]);
return sum;
}
float dotproduct_unloop(const float *p1, const float * p2, size_t n)
{
if(n % 8 != 0)
{
std::cerr << "The size n must be a multiple of 8." <<std::endl;
return 0.0f;
}
float sum = 0.0f;
for (size_t i = 0; i < n; i+=8)
{
sum += (p1[i] * p2[i]);
sum += (p1[i+1] * p2[i+1]);
sum += (p1[i+2] * p2[i+2]);
sum += (p1[i+3] * p2[i+3]);
sum += (p1[i+4] * p2[i+4]);
sum += (p1[i+5] * p2[i+5]);
sum += (p1[i+6] * p2[i+6]);
sum += (p1[i+7] * p2[i+7]);
}
return sum;
}
float dotproduct_avx2(const float *p1, const float * p2, size_t n)
{
#ifdef WITH_AVX2
if(n % 8 != 0)
{
std::cerr << "The size n must be a multiple of 8." <<std::endl;
return 0.0f;
}
float sum[8] = {0};
__m256 a, b;
__m256 c = _mm256_setzero_ps();
for (size_t i = 0; i < n; i+=8)
{
a = _mm256_loadu_ps(p1 + i);
b = _mm256_loadu_ps(p2 + i);
c = _mm256_add_ps(c, _mm256_mul_ps(a, b));
}
_mm256_storeu_ps(sum, c);
return (sum[0]+sum[1]+sum[2]+sum[3]+sum[4]+sum[5]+sum[6]+sum[7]);
#else
std::cerr << "AVX2 is not supported" << std::endl;
return 0.0;
#endif
}
float dotproduct_avx2_omp(const float *p1, const float * p2, size_t n)
{
#ifdef WITH_AVX2
if(n % 8 != 0)
{
std::cerr << "The size n must be a multiple of 8." <<std::endl;
return 0.0f;
}
float sum[8] = {0};
__m256 a, b;
__m256 c = _mm256_setzero_ps();
#pragma omp parallel for
for (size_t i = 0; i < n; i+=8)
{
a = _mm256_loadu_ps(p1 + i);
b = _mm256_loadu_ps(p2 + i);
c = _mm256_add_ps(c, _mm256_mul_ps(a, b));
}
_mm256_storeu_ps(sum, c);
return (sum[0]+sum[1]+sum[2]+sum[3]+sum[4]+sum[5]+sum[6]+sum[7]);
#else
std::cerr << "AVX2 is not supported" << std::endl;
return 0.0;
#endif
}
float dotproduct_neon(const float *p1, const float * p2, size_t n)
{
#ifdef WITH_NEON
if(n % 4 != 0)
{
std::cerr << "The size n must be a multiple of 4." <<std::endl;
return 0.0f;
}
float sum[4] = {0};
float32x4_t a, b;
float32x4_t c = vdupq_n_f32(0);
for (size_t i = 0; i < n; i+=4)
{
a = vld1q_f32(p1 + i);
b = vld1q_f32(p2 + i);
c = vaddq_f32(c, vmulq_f32(a, b));
}
vst1q_f32(sum, c);
return (sum[0]+sum[1]+sum[2]+sum[3]);
#else
std::cerr << "NEON is not supported" << std::endl;
return 0.0;
#endif
}
float dotproduct_neon_omp(const float *p1, const float * p2, size_t n)
{
#ifdef WITH_NEON
if(n % 4 != 0)
{
std::cerr << "The size n must be a multiple of 4." <<std::endl;
return 0.0f;
}
float sum[4] = {0};
float32x4_t a, b;
float32x4_t c = vdupq_n_f32(0);
#pragma omp parallel for
for (size_t i = 0; i < n; i+=4)
{
a = vld1q_f32(p1 + i);
b = vld1q_f32(p2 + i);
c = vaddq_f32(c, vmulq_f32(a, b));
}
vst1q_f32(sum, c);
return (sum[0]+sum[1]+sum[2]+sum[3]);
#else
std::cerr << "NEON is not supported" << std::endl;
return 0.0;
#endif
}