На GPU вычисления

This commit is contained in:
2025-12-02 12:39:09 +00:00
parent 78bdb1ddb7
commit 73c9e580e4
5 changed files with 344 additions and 17 deletions

View File

@@ -1,12 +1,116 @@
#include "gpu_loader.hpp"
#include <dlfcn.h>
#include <map>
#include <algorithm>
static void* get_gpu_lib_handle() {
static void* h = dlopen("./libgpu_compute.so", RTLD_NOW | RTLD_LOCAL);
return h;
}
gpu_is_available_fn load_gpu_is_available() {
void* h = dlopen("./libgpu_compute.so", RTLD_NOW | RTLD_LOCAL);
void* h = get_gpu_lib_handle();
if (!h) return nullptr;
auto fn = (gpu_is_available_fn)dlsym(h, "gpu_is_available");
if (!fn) return nullptr;
return fn;
}
gpu_aggregate_days_fn load_gpu_aggregate_days() {
void* h = get_gpu_lib_handle();
if (!h) return nullptr;
auto fn = (gpu_aggregate_days_fn)dlsym(h, "gpu_aggregate_days");
return fn;
}
bool aggregate_days_gpu(
const std::vector<Record>& records,
std::vector<DayStats>& out_stats,
gpu_aggregate_days_fn gpu_fn)
{
if (!gpu_fn || records.empty()) {
return false;
}
// Группируем записи по дням и подготавливаем данные для GPU
std::map<DayIndex, std::vector<size_t>> day_record_indices;
for (size_t i = 0; i < records.size(); i++) {
DayIndex day = static_cast<DayIndex>(records[i].timestamp) / 86400;
day_record_indices[day].push_back(i);
}
int num_days = static_cast<int>(day_record_indices.size());
// Подготавливаем массивы для GPU
std::vector<GpuRecord> gpu_records;
std::vector<int> day_offsets;
std::vector<int> day_counts;
std::vector<long long> day_indices;
gpu_records.reserve(records.size());
day_offsets.reserve(num_days);
day_counts.reserve(num_days);
day_indices.reserve(num_days);
int current_offset = 0;
for (auto& [day, indices] : day_record_indices) {
day_indices.push_back(day);
day_offsets.push_back(current_offset);
day_counts.push_back(static_cast<int>(indices.size()));
// Добавляем записи этого дня
for (size_t idx : indices) {
const auto& r = records[idx];
GpuRecord gr;
gr.timestamp = r.timestamp;
gr.open = r.open;
gr.high = r.high;
gr.low = r.low;
gr.close = r.close;
gr.volume = r.volume;
gpu_records.push_back(gr);
}
current_offset += static_cast<int>(indices.size());
}
// Выделяем память для результата
std::vector<GpuDayStats> gpu_stats(num_days);
// Вызываем GPU функцию
int result = gpu_fn(
gpu_records.data(),
static_cast<int>(gpu_records.size()),
day_offsets.data(),
day_counts.data(),
day_indices.data(),
num_days,
gpu_stats.data()
);
if (result != 0) {
return false;
}
// Конвертируем результат в DayStats
out_stats.clear();
out_stats.reserve(num_days);
for (const auto& gs : gpu_stats) {
DayStats ds;
ds.day = gs.day;
ds.low = gs.low;
ds.high = gs.high;
ds.open = gs.open;
ds.close = gs.close;
ds.avg = gs.avg;
ds.first_ts = gs.first_ts;
ds.last_ts = gs.last_ts;
out_stats.push_back(ds);
}
return true;
}