-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathreduce.cpp
More file actions
188 lines (151 loc) · 4.49 KB
/
reduce.cpp
File metadata and controls
188 lines (151 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "reduce.h"
#include <benchmark/benchmark.h>
#include <numeric>
#include <execution>
#include <iostream>
#include "vulkansubgroups_spirv.h"
namespace
{
int NextSize(int size, int localSize)
{
return (size + localSize) / localSize;
}
}
Reduce::Reduce(const Vortex2D::Renderer::Device& device,
int size,
int localSize)
: mTimer(device)
, mUploadCmd(device)
, mDownloadCmd(device)
, mReduceCmd(device)
, mReduceWork(device, {size, localSize}, Vortex2D::SPIRV::Reduce_comp)
, mLocalInput(device, size, VMA_MEMORY_USAGE_CPU_TO_GPU)
, mLocalOutput(device, 1, VMA_MEMORY_USAGE_GPU_TO_CPU)
{
int n = size;
while (n > 1)
{
mBuffers.emplace_back(device, n, VMA_MEMORY_USAGE_GPU_ONLY);
n = NextSize(n, localSize);
}
mBuffers.emplace_back(device, 1, VMA_MEMORY_USAGE_GPU_ONLY);
n = size;
for (std::size_t i = 0; i < mBuffers.size() - 1; i++)
{
mReduce.emplace_back(mReduceWork.Bind({n, localSize}, {mBuffers[i], mBuffers[i + 1]}));
n = NextSize(n, localSize);
}
mUploadCmd.Record([&](vk::CommandBuffer commandBuffer)
{
mBuffers.front().CopyFrom(commandBuffer, mLocalInput);
});
mDownloadCmd.Record([&](vk::CommandBuffer commandBuffer)
{
mLocalOutput.CopyFrom(commandBuffer, mBuffers.back());
});
mReduceCmd.Record([&](vk::CommandBuffer commandBuffer)
{
mTimer.Start(commandBuffer);
int n = size;
for (std::size_t i = 0; i < mReduce.size(); i++)
{
mReduce[i].PushConstant(commandBuffer, n);
mReduce[i].Record(commandBuffer);
Vortex2D::Renderer::BufferBarrier(mBuffers[i + 1].Handle(), commandBuffer, vk::AccessFlagBits::eShaderWrite, vk::AccessFlagBits::eShaderRead);
n = NextSize(n, localSize);
}
mTimer.Stop(commandBuffer);
});
}
void Reduce::Upload(const std::vector<float>& input)
{
Vortex2D::Renderer::CopyFrom(mLocalInput, input);
mUploadCmd.Submit();
}
float Reduce::Download()
{
mDownloadCmd.Submit();
mDownloadCmd.Wait();
float total = 0.0;
Vortex2D::Renderer::CopyTo(mLocalOutput, total);
return total;
}
void Reduce::Submit()
{
mReduceCmd.Submit();
mReduceCmd.Wait();
}
uint64_t Reduce::GetElapsedNs()
{
return mTimer.GetElapsedNs();
}
static void Reduce_CPU_Seq(benchmark::State& state)
{
auto size = state.range(0);
std::vector<float> inputData(size, 1.0f);
std::iota(inputData.begin(), inputData.end(), 1.0f);
for (auto _ : state)
{
benchmark::DoNotOptimize(std::reduce(std::execution::seq, inputData.begin(), inputData.end()));
}
}
static void Reduce_CPU_Par(benchmark::State& state)
{
auto size = state.range(0);
std::vector<float> inputData(size, 1.0f);
std::iota(inputData.begin(), inputData.end(), 1.0f);
for (auto _ : state)
{
benchmark::DoNotOptimize(std::reduce(std::execution::par, inputData.begin(), inputData.end()));
}
}
static void Reduce_GPU_Subgroup(benchmark::State& state)
{
auto size = state.range(0);
Reduce reduce(*gDevice, size, 512);
std::vector<float> inputData(size, 1.0f);
std::iota(inputData.begin(), inputData.end(), 1.0f);
for (auto _ : state)
{
reduce.Submit();
state.SetIterationTime(reduce.GetElapsedNs() / 1000000000.0);
}
}
static void Reduce_GPU_SharedMemory(benchmark::State& state)
{
auto size = state.range(0);
Vortex2D::Renderer::Timer timer(*gDevice);
Vortex2D::Fluid::ReduceSum reduce(*gDevice, {size, 1});
Vortex2D::Renderer::Buffer<float> input(*gDevice, size, VMA_MEMORY_USAGE_GPU_ONLY);
Vortex2D::Renderer::Buffer<float> output(*gDevice, 1, VMA_MEMORY_USAGE_GPU_ONLY);
auto boundReduce = reduce.Bind(input, output);
Vortex2D::Renderer::CommandBuffer cmd(*gDevice);
cmd.Record([&](vk::CommandBuffer commandBuffer)
{
timer.Start(commandBuffer);
boundReduce.Record(commandBuffer);
timer.Stop(commandBuffer);
});
for (auto _ : state)
{
cmd.Submit();
cmd.Wait();
state.SetIterationTime(timer.GetElapsedNs() / 1000000000.0);
}
}
BENCHMARK(Reduce_GPU_Subgroup)->Range(8, 8<<20)->UseManualTime();
BENCHMARK(Reduce_GPU_SharedMemory)->Range(8, 8<<20)->UseManualTime();
BENCHMARK(Reduce_CPU_Seq)->Range(8, 8<<20);
BENCHMARK(Reduce_CPU_Par)->Range(8, 8<<20);
void CheckReduce()
{
int size = 300;
Reduce reduce(*gDevice, size, 256);
std::vector<float> inputData(size, 1.0f);
std::iota(inputData.begin(), inputData.end(), 1.0f);
reduce.Upload(inputData);
reduce.Submit();
float total = reduce.Download();
std::cout << "Total " << total << std::endl;
std::cout << "Expected total " << 0.5f * size * (size + 1) << std::endl;
}