Параллельность на уровне Cuda

This commit is contained in:
2025-12-02 12:58:49 +00:00
parent 143e01b2dd
commit 68ea345a35

View File

@@ -30,7 +30,7 @@ extern "C" int gpu_is_available() {
return (n > 0) ? 1 : 0;
}
// Kernel для агрегации (один поток обрабатывает все данные)
// Kernel для агрегации (каждый поток обрабатывает один день)
__global__ void aggregate_kernel(
const GpuRecord* records,
int num_records,
@@ -40,8 +40,11 @@ __global__ void aggregate_kernel(
int num_days,
GpuDayStats* out_stats)
{
// Один поток обрабатывает все дни последовательно
for (int d = 0; d < num_days; d++) {
// Глобальный индекс потока = индекс дня
int d = blockIdx.x * blockDim.x + threadIdx.x;
if (d >= num_days) return;
int offset = day_offsets[d];
int count = day_counts[d];
@@ -75,7 +78,6 @@ __global__ void aggregate_kernel(
stats.avg = (stats.low + stats.high) / 2.0;
out_stats[d] = stats;
}
}
// Функция агрегации, вызываемая из C++
extern "C" int gpu_aggregate_days(
@@ -124,8 +126,11 @@ extern "C" int gpu_aggregate_days(
err = cudaMemcpy(d_day_indices, h_day_indices, num_days * sizeof(long long), cudaMemcpyHostToDevice);
if (err != cudaSuccess) return -13;
// Запускаем kernel (1 блок, 1 поток)
aggregate_kernel<<<1, 1>>>(
// Запускаем kernel: каждый поток обрабатывает один день
const int THREADS_PER_BLOCK = 256;
int num_blocks = (num_days + THREADS_PER_BLOCK - 1) / THREADS_PER_BLOCK;
aggregate_kernel<<<num_blocks, THREADS_PER_BLOCK>>>(
d_records, num_records,
d_day_offsets, d_day_counts, d_day_indices,
num_days, d_out_stats