Параллельность на уровне Cuda
This commit is contained in:
@@ -30,7 +30,7 @@ extern "C" int gpu_is_available() {
|
|||||||
return (n > 0) ? 1 : 0;
|
return (n > 0) ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Kernel для агрегации (один поток обрабатывает все данные)
|
// Kernel для агрегации (каждый поток обрабатывает один день)
|
||||||
__global__ void aggregate_kernel(
|
__global__ void aggregate_kernel(
|
||||||
const GpuRecord* records,
|
const GpuRecord* records,
|
||||||
int num_records,
|
int num_records,
|
||||||
@@ -40,8 +40,11 @@ __global__ void aggregate_kernel(
|
|||||||
int num_days,
|
int num_days,
|
||||||
GpuDayStats* out_stats)
|
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 offset = day_offsets[d];
|
||||||
int count = day_counts[d];
|
int count = day_counts[d];
|
||||||
|
|
||||||
@@ -75,7 +78,6 @@ __global__ void aggregate_kernel(
|
|||||||
stats.avg = (stats.low + stats.high) / 2.0;
|
stats.avg = (stats.low + stats.high) / 2.0;
|
||||||
out_stats[d] = stats;
|
out_stats[d] = stats;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Функция агрегации, вызываемая из C++
|
// Функция агрегации, вызываемая из C++
|
||||||
extern "C" int gpu_aggregate_days(
|
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);
|
err = cudaMemcpy(d_day_indices, h_day_indices, num_days * sizeof(long long), cudaMemcpyHostToDevice);
|
||||||
if (err != cudaSuccess) return -13;
|
if (err != cudaSuccess) return -13;
|
||||||
|
|
||||||
// Запускаем kernel (1 блок, 1 поток)
|
// Запускаем kernel: каждый поток обрабатывает один день
|
||||||
aggregate_kernel<<<1, 1>>>(
|
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_records, num_records,
|
||||||
d_day_offsets, d_day_counts, d_day_indices,
|
d_day_offsets, d_day_counts, d_day_indices,
|
||||||
num_days, d_out_stats
|
num_days, d_out_stats
|
||||||
|
|||||||
Reference in New Issue
Block a user