Перешёл к произвольным периодам агрегации

This commit is contained in:
2025-12-16 13:53:36 +00:00
parent 2833d2f7b4
commit a5aadbc774
12 changed files with 211 additions and 267 deletions

View File

@@ -1,4 +1,5 @@
#include "gpu_loader.hpp"
#include "utils.hpp"
#include <dlfcn.h>
#include <map>
#include <algorithm>
@@ -29,58 +30,54 @@ bool gpu_is_available() {
return false;
}
gpu_aggregate_days_fn load_gpu_aggregate_days() {
gpu_aggregate_periods_fn load_gpu_aggregate_periods() {
void* h = get_gpu_lib_handle();
if (!h) return nullptr;
auto fn = (gpu_aggregate_days_fn)dlsym(h, "gpu_aggregate_days");
auto fn = (gpu_aggregate_periods_fn)dlsym(h, "gpu_aggregate_periods");
return fn;
}
bool aggregate_days_gpu(
bool aggregate_periods_gpu(
const std::vector<Record>& records,
std::vector<DayStats>& out_stats,
gpu_aggregate_days_fn gpu_fn)
std::vector<PeriodStats>& out_stats,
gpu_aggregate_periods_fn gpu_fn)
{
if (!gpu_fn || records.empty()) {
return false;
}
// Общий таймер всей функции
int64_t interval = get_aggregation_interval();
double t_total_start = omp_get_wtime();
// Таймер CPU preprocessing
double t_preprocess_start = omp_get_wtime();
// Группируем записи по дням и подготавливаем данные для GPU
std::map<DayIndex, std::vector<size_t>> day_record_indices;
std::map<PeriodIndex, std::vector<size_t>> period_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);
PeriodIndex period = static_cast<PeriodIndex>(records[i].timestamp) / interval;
period_record_indices[period].push_back(i);
}
int num_days = static_cast<int>(day_record_indices.size());
int num_periods = static_cast<int>(period_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;
std::vector<int> period_offsets;
std::vector<int> period_counts;
std::vector<long long> period_indices;
gpu_records.reserve(records.size());
day_offsets.reserve(num_days);
day_counts.reserve(num_days);
day_indices.reserve(num_days);
period_offsets.reserve(num_periods);
period_counts.reserve(num_periods);
period_indices.reserve(num_periods);
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 (auto& [period, indices] : period_record_indices) {
period_indices.push_back(period);
period_offsets.push_back(current_offset);
period_counts.push_back(static_cast<int>(indices.size()));
// Добавляем записи этого дня
for (size_t idx : indices) {
const auto& r = records[idx];
GpuRecord gr;
@@ -96,22 +93,19 @@ bool aggregate_days_gpu(
current_offset += static_cast<int>(indices.size());
}
// Выделяем память для результата
std::vector<GpuDayStats> gpu_stats(num_days);
std::vector<GpuPeriodStats> gpu_stats(num_periods);
double t_preprocess_ms = (omp_get_wtime() - t_preprocess_start) * 1000.0;
std::cout << " GPU CPU preprocessing: " << std::fixed << std::setprecision(3)
<< std::setw(7) << t_preprocess_ms << " ms" << std::endl << std::flush;
// Вызываем GPU функцию (включает: malloc, memcpy H->D, kernel, memcpy D->H, free)
// Детальные тайминги выводятся внутри 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,
period_offsets.data(),
period_counts.data(),
period_indices.data(),
num_periods,
gpu_stats.data()
);
@@ -120,23 +114,21 @@ bool aggregate_days_gpu(
return false;
}
// Конвертируем результат в DayStats
out_stats.clear();
out_stats.reserve(num_days);
out_stats.reserve(num_periods);
for (const auto& gs : gpu_stats) {
DayStats ds;
ds.day = gs.day;
ds.avg = gs.avg;
ds.open_min = gs.open_min;
ds.open_max = gs.open_max;
ds.close_min = gs.close_min;
ds.close_max = gs.close_max;
ds.count = gs.count;
out_stats.push_back(ds);
PeriodStats ps;
ps.period = gs.period;
ps.avg = gs.avg;
ps.open_min = gs.open_min;
ps.open_max = gs.open_max;
ps.close_min = gs.close_min;
ps.close_max = gs.close_max;
ps.count = gs.count;
out_stats.push_back(ps);
}
// Общее время всей GPU функции (включая preprocessing)
double t_total_ms = (omp_get_wtime() - t_total_start) * 1000.0;
std::cout << " GPU TOTAL (with prep): " << std::fixed << std::setprecision(3)
<< std::setw(7) << t_total_ms << " ms" << std::endl << std::flush;