This repository has been archived by the owner on May 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hell2.cpp
81 lines (66 loc) · 3.37 KB
/
hell2.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
#include <chrono>
#include <emmintrin.h> // SSE header
#include <iostream>
#include <unordered_map>
// Benches (Ryzen 3600X CPU -Ofast flag)
// 445 microseconds, 568 microseconds, 827 microseconds, 705 microseconds
std::unordered_map<int, long long int> cache;
void sum_up_to_n(int n) {
if (cache.find(n) != cache.end()) {
std::cout << "Sum of integers up to " << n << ": " << cache[n] << '\n';
return;
}
// Initialize sum variable
long long int sum = 0;
// Use SSE to perform parallel additions
__m128i sum_vector = _mm_setzero_si128(); // Initialize SSE vector with zeros
// Loop through integers from 1 to n using SSE
for (int i = 1; i <= n; i += 4) {
// Create an SSE vector with consecutive integers [i, i+1, i+2, i+3]
__m128i current_int = _mm_setr_epi32(i, i + 1, i + 2, i + 3);
// Add current_int to sum_vector using SSE instructions
sum_vector = _mm_add_epi32(sum_vector, current_int);
}
// Sum up the elements of the sum_vector
int sse_sum[4]; // 4 integers in an SSE register
_mm_storeu_si128((__m128i *)sse_sum, sum_vector);
sum = sse_sum[0] + sse_sum[1] + sse_sum[2] + sse_sum[3];
cache[n] = sum;
std::cout << "Sum of integers up to " << n << ": " << sum << '\n';
}
int main() {
auto start = std::chrono::high_resolution_clock::now();
int nums[] = {
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000,
100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000, 100000};
for (int num : nums)
sum_up_to_n(num);
auto end = std::chrono::high_resolution_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::microseconds>(end - start)
.count();
std::cout << "Program finished in " << duration << " microseconds."
<< std::endl;
return 0;
}