Мой текущий подход - использовать пул потоков и объединить задачи, пока не появится бесплатный поток. < /P>
Код: Выделить всё
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
// using namespace std::chrono;
constexpr int factors[] = { 10,100,1000,10000,100000,1000000,10000000 };
mutex mtx;
atomic output = 0;
class threadpool {
vector workers;
queue tasks; //allows me to put a function in a queue
mutex queue_mutex;
condition_variable condition; //allows me to wait for a condition
atomic stop{ false };
public:
threadpool(size_t threads) {
for (size_t i = 0; i < threads; ++i) {
workers.emplace_back([this] {
while (true) {
function task; {
unique_lock lock(this->queue_mutex); //accsess the private queue mutex
this->condition.wait(lock, [this] {return this->stop || !this->tasks.empty(); });
if (this->stop && this->tasks.empty()) {
return;
}
task = move(this->tasks.front());
this->tasks.pop();
}
task();
}
});
}
}
template
void enqueue(F&& f) {
{
unique_lock lock(queue_mutex);
tasks.emplace(forward(f));
}
condition.notify_one();
}
void shutdown() {
{
unique_lock lock(queue_mutex);
stop = true;
}
condition.notify_all();
for (thread& worker : workers) {
worker.join();
}
}
~threadpool() {
if (!stop) {
shutdown();
}
}
};
static void computeRange(int start, int end) {
long long localOutput = 0;
for (long long a = start; a < end; a++) {
int str_b = 0;
int threshold = 10;
int factor = 10;
for (long long b = 1; b < a * 20849; b++) {
long long sum = a + b;
long long total = (sum * sum);
if (total >= 100000000000000000) {
break;
}
if (b == threshold) {
str_b++;
const int factor_of_10 = factors[str_b];
threshold = factor_of_10;
factor = factor_of_10;
}
long long temp = (a * factor) + b;
if (total == temp) {
localOutput += total;
lock_guard lock(mtx);
cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79664549/optimization-writing-code-for-gpu-for-long-maths-question[/url]
Мобильная версия