This commit is contained in:
WickedJack99
2025-03-29 13:03:42 +01:00
parent b8b37d6d75
commit 2b257e500c
2 changed files with 48 additions and 11 deletions

View File

@@ -0,0 +1,35 @@
#include "matrix.h"
#include <cmath>
int fib(int n)
{
if (n < 2)
return n;
int a = 0;
int b = 1;
for (int i = 2; i <= n; ++i)
{
int temp = a + b;
a = b;
b = temp;
}
return b;
}
Matrix compute(const Matrix &s, const std::vector<int> &v)
{
Matrix m(s.dim());
const int n = v.size();
for (int j = 0; j < n; ++j)
{
double val = static_cast<double>(fib(v[j] % 256));
double val2 = (sin(val) * tan(val) / sqrt(cos(val) + 2));
for (int i = 0; i < n; ++i)
{
m(j, i) = s(j, i) * val2;
}
}
return m;
}

View File

@@ -43,22 +43,24 @@ static void workerThread(int start, int end) {
int main() {
int range_start = 100;
int range_end = 1'000'000;
int num_threads = std::thread::hardware_concurrency(); // use system's core count
workerThread(range_start, range_end);
int total = range_end - range_start;
int chunk_size = total / num_threads;
// int num_threads = std::thread::hardware_concurrency(); // use system's core count
std::vector<std::thread> threads;
// int total = range_end - range_start;
// int chunk_size = total / num_threads;
for (int i = 0; i < num_threads; ++i) {
int start = range_start + i * chunk_size;
int end = (i == num_threads - 1) ? range_end : start + chunk_size;
// std::vector<std::thread> threads;
threads.emplace_back(workerThread, start, end);
}
// for (int i = 0; i < num_threads; ++i) {
// int start = range_start + i * chunk_size;
// int end = (i == num_threads - 1) ? range_end : start + chunk_size;
for (auto& t : threads) t.join();
// threads.emplace_back(workerThread, start, end);
// }
std::cout << "All threads finished.\n";
// for (auto& t : threads) t.join();
// std::cout << "All threads finished.\n";
return 0;
}