-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
189 lines (149 loc) · 4.12 KB
/
Copy pathmain.cpp
File metadata and controls
189 lines (149 loc) · 4.12 KB
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
/**
* Copyright 2025, Aleksandar Colic
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// NOLINTBEGIN
#include <chrono>
#include <cstdint>
#include <functional>
#include <future>
#include <iostream>
#include <memory>
#include <thread>
#include <type_traits>
#include <vector>
#include "array_map.hpp"
#include "async.hpp"
#include "types.hpp"
#include "ums.hpp"
#include "util.hpp"
#include "worker.hpp"
using namespace std::chrono_literals;
using namespace ums;
void f1()
{
auto start = now();
std::vector<u64> v;
for (int i = 0; i < 1000; ++i)
v.push_back(random<uint8_t>());
int funcDur = random() % 11;
int i = 0;
while (true) {
auto end = now();
std::chrono::duration<double, std::milli> duration = end - start;
if (duration.count() > funcDur) {
std::cout << "Task execution exceeded time limit of " << duration.count() << "ms.\n";
break;
}
if (v[random() % v.size()] == random<u8>() % v.size() && i++ % 100 == 0)
worker::get()->yield();
}
return;
}
// Duration of ~1s when plugged in.
//
u64 f3()
{
u64 first = 0, second = 1;
// Fibbonaci seq.
//
for (u64 i = 2; i < 3000000000; ++i) {
u64 sum = first + second;
first = second;
second = sum;
}
return second;
}
void thread_function()
{
for (int i = 0; i < 1000; ++i) {
auto t = async(f3);
t.wait();
}
}
u64 fib(u64 n)
{
u64 first = 0, second = 1;
if (n > 0)
std::cout << first;
for (u64 i = 1; i < n; ++i) {
u64 sum = first + second;
first = second;
second = sum;
std::cout << " " << second;
}
std::cout << "\n";
return second;
}
void ms3_function()
{
Stopwatch s;
for (int i = 0; i < 1000; ++i)
async(fib, 1);
}
class A {
public:
A() : s{"default"} { std::cout << "Default constructor\n"; }
explicit A(std::string value) : s{std::move(value)} { std::cout << "Value constructor\n"; }
A(const A& other) : s{other.s} { std::cout << "Copy constructor\n"; }
A(A&& other) noexcept : s{std::move(other.s)} { std::cout << "Move constructor\n"; }
A& operator=(const A& other)
{
std::cout << "Copy assignment\n";
if (this != &other)
s = other.s;
return *this;
}
A& operator=(A&& other) noexcept
{
std::cout << "Move assignment\n";
if (this != &other)
s = std::move(other.s);
return *this;
}
~A() { std::cout << "Destructor\n"; }
const std::string& value() const { return s; }
private:
std::string s;
};
void ums_main(int argc, char* argv[])
{
std::cout << "argc: " << argc << "\n";
std::cout << "argv: \n";
for (int i = 0; i < argc; ++i)
std::cout << argv[0] << "\n";
std::cout << "Schedulers count: " << sch::cpus_count() << "\n";
std::cout << "Workers count: " << sch::workers_count() << "\n";
Stopwatch<true, std::chrono::microseconds> s;
Task<u64> task{async(fib, 10 * 1024 * 1024)};
std::cout << task.get() << "\n";
std::vector<Task<void>> v;
v.emplace_back(async([] {}));
v[0].wait();
}
int main(int argc, char* argv[])
{
init_ums(
[&] {
constexpr usize tasks_count = 100 * 1024;
std::vector<Task<void>> tasks;
tasks.reserve(tasks_count);
for (usize i = 0; i < tasks_count; ++i)
tasks.emplace_back(async([] { return; }));
for (auto& task : tasks)
task.get();
},
Options{Options::Schedulers_count{4}, Options::Workers_per_scheduler{4}});
}
// NOLINTEND