Skip to content

Commit acdd456

Browse files
author
cflint
committed
pre-commit
1 parent 4c5f05a commit acdd456

8 files changed

Lines changed: 99 additions & 93 deletions

File tree

common/cuda_hip/sketch/sparse_stack_kernels.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// SPDX-License-Identifier: BSD-3-Clause
44

55
#include "core/sketch/sparse_stack_kernels.hpp"
6+
67
#include <ginkgo/core/base/exception_helpers.hpp>
78

89
namespace gko {
@@ -14,25 +15,26 @@ namespace sparse_stack {
1415
template <typename ValueType, typename IndexType>
1516
void generate(std::shared_ptr<const DefaultExecutor> exec,
1617
size_type sketch_size, size_type input_size, size_type zeta,
17-
array<IndexType>& hash_map, array<ValueType>& signs, uint64 seed)
18-
GKO_NOT_IMPLEMENTED;
19-
20-
GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(GKO_DECLARE_SPARSE_STACK_GENERATE);
18+
array<IndexType>& hash_map, array<ValueType>& signs,
19+
uint64 seed) GKO_NOT_IMPLEMENTED;
20+
21+
GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(
22+
GKO_DECLARE_SPARSE_STACK_GENERATE);
2123

2224
template <typename ValueType, typename IndexType>
2325
void apply(std::shared_ptr<const DefaultExecutor> exec, size_type zeta,
2426
const array<IndexType>& hash_map, const array<ValueType>& signs,
2527
matrix::view::dense<const ValueType> b,
2628
matrix::view::dense<ValueType> x) GKO_NOT_IMPLEMENTED;
27-
29+
2830
GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(GKO_DECLARE_SPARSE_STACK_APPLY);
2931

3032
template <typename ValueType, typename IndexType>
3133
void rapply(std::shared_ptr<const DefaultExecutor> exec, size_type zeta,
3234
const array<IndexType>& hash_map, const array<ValueType>& signs,
3335
matrix::view::dense<const ValueType> b,
3436
matrix::view::dense<ValueType> x) GKO_NOT_IMPLEMENTED;
35-
37+
3638
GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(GKO_DECLARE_SPARSE_STACK_RAPPLY);
3739

3840

core/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,6 @@ target_sources(
9595
matrix/scaled_permutation.cpp
9696
matrix/sellp.cpp
9797
matrix/sparsity_csr.cpp
98-
sketch/count_sketch.cpp
99-
sketch/gaussian_sketch.cpp
100-
sketch/sparse_stack.cpp
10198
multigrid/fixed_coarsening.cpp
10299
multigrid/pgm.cpp
103100
preconditioner/batch_jacobi.cpp
@@ -111,6 +108,9 @@ target_sources(
111108
reorder/mc64.cpp
112109
reorder/rcm.cpp
113110
reorder/scaled_reordered.cpp
111+
sketch/count_sketch.cpp
112+
sketch/gaussian_sketch.cpp
113+
sketch/sparse_stack.cpp
114114
solver/batch_bicgstab.cpp
115115
solver/batch_cg.cpp
116116
solver/bicg.cpp

core/device_hooks/common_kernels.inc.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
1+
// SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
22
//
33
// SPDX-License-Identifier: BSD-3-Clause
44

@@ -57,6 +57,9 @@
5757
#include "core/preconditioner/jacobi_kernels.hpp"
5858
#include "core/preconditioner/sor_kernels.hpp"
5959
#include "core/reorder/rcm_kernels.hpp"
60+
#include "core/sketch/count_sketch_kernels.hpp"
61+
#include "core/sketch/gaussian_sketch_kernels.hpp"
62+
#include "core/sketch/sparse_stack_kernels.hpp"
6063
#include "core/solver/batch_bicgstab_kernels.hpp"
6164
#include "core/solver/batch_cg_kernels.hpp"
6265
#include "core/solver/bicg_kernels.hpp"
@@ -76,9 +79,6 @@
7679
#include "core/solver/multigrid_kernels.hpp"
7780
#include "core/solver/pipe_cg_kernels.hpp"
7881
#include "core/solver/upper_trs_kernels.hpp"
79-
#include "core/sketch/count_sketch_kernels.hpp"
80-
#include "core/sketch/gaussian_sketch_kernels.hpp"
81-
#include "core/sketch/sparse_stack_kernels.hpp"
8282
#include "core/stop/criterion_kernels.hpp"
8383
#include "core/stop/residual_norm_kernels.hpp"
8484

core/sketch/sparse_stack.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
//
33
// SPDX-License-Identifier: BSD-3-Clause
44

5-
#include <ginkgo/core/sketch/sparse_stack.hpp>
6-
75
#include <ginkgo/core/base/precision_dispatch.hpp>
86
#include <ginkgo/core/matrix/dense.hpp>
7+
#include <ginkgo/core/sketch/sparse_stack.hpp>
98

109
#include "core/sketch/sparse_stack_kernels.hpp"
1110

@@ -29,22 +28,24 @@ template <typename ValueType, typename IndexType>
2928
SparseStack<ValueType, IndexType>::SparseStack(
3029
std::shared_ptr<const Executor> exec, size_type sketch_size,
3130
size_type input_size, size_type zeta, uint64 seed)
32-
: EnableLinOp<SparseStack, SketchOperator<ValueType>>(exec, dim<2>{sketch_size, input_size}),
31+
: EnableLinOp<SparseStack, SketchOperator<ValueType>>(
32+
exec, dim<2>{sketch_size, input_size}),
3333
zeta_{zeta},
3434
hash_map_{exec, input_size * zeta},
3535
signs_{exec, input_size * zeta},
3636
seed_{seed}
3737
{
38-
exec->run(sparse_stack::make_generate(sketch_size, input_size, zeta_,
38+
exec->run(sparse_stack::make_generate(sketch_size, input_size, zeta_,
3939
hash_map_, signs_, seed_));
4040
}
4141

4242

4343
template <typename ValueType, typename IndexType>
4444
std::unique_ptr<SparseStack<ValueType, IndexType>>
45-
SparseStack<ValueType, IndexType>::create(
46-
std::shared_ptr<const Executor> exec, size_type sketch_size,
47-
size_type input_size, size_type zeta, uint64 seed)
45+
SparseStack<ValueType, IndexType>::create(std::shared_ptr<const Executor> exec,
46+
size_type sketch_size,
47+
size_type input_size, size_type zeta,
48+
uint64 seed)
4849
{
4950
return std::unique_ptr<SparseStack>{
5051
new SparseStack(exec, sketch_size, input_size, zeta, seed)};
@@ -56,7 +57,8 @@ void SparseStack<ValueType, IndexType>::apply_sketch_impl(
5657
const matrix::Dense<ValueType>* b, matrix::Dense<ValueType>* x) const
5758
{
5859
this->get_executor()->run(sparse_stack::make_apply(
59-
zeta_, hash_map_, signs_, b->get_const_device_view(), x->get_device_view()));
60+
zeta_, hash_map_, signs_, b->get_const_device_view(),
61+
x->get_device_view()));
6062
}
6163

6264

@@ -65,7 +67,8 @@ void SparseStack<ValueType, IndexType>::rapply_sketch_impl(
6567
const matrix::Dense<ValueType>* b, matrix::Dense<ValueType>* x) const
6668
{
6769
this->get_executor()->run(sparse_stack::make_rapply(
68-
zeta_, hash_map_, signs_, b->get_const_device_view(), x->get_device_view()));
70+
zeta_, hash_map_, signs_, b->get_const_device_view(),
71+
x->get_device_view()));
6972
}
7073

7174

core/sketch/sparse_stack_kernels.hpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ namespace gko {
1717
namespace kernels {
1818

1919

20-
#define GKO_DECLARE_SPARSE_STACK_GENERATE(ValueType, IndexType) \
21-
void generate(std::shared_ptr<const DefaultExecutor> exec, \
22-
size_type sketch_size, size_type input_size, \
23-
size_type zeta, array<IndexType>& hash_map, \
24-
array<ValueType>& signs, gko::uint64 seed)
25-
26-
#define GKO_DECLARE_SPARSE_STACK_APPLY(ValueType, IndexType) \
27-
void apply(std::shared_ptr<const DefaultExecutor> exec, size_type zeta, \
28-
const array<IndexType>& hash_map, \
29-
const array<ValueType>& signs, \
30-
matrix::view::dense<const ValueType> b, \
20+
#define GKO_DECLARE_SPARSE_STACK_GENERATE(ValueType, IndexType) \
21+
void generate(std::shared_ptr<const DefaultExecutor> exec, \
22+
size_type sketch_size, size_type input_size, size_type zeta, \
23+
array<IndexType>& hash_map, array<ValueType>& signs, \
24+
gko::uint64 seed)
25+
26+
#define GKO_DECLARE_SPARSE_STACK_APPLY(ValueType, IndexType) \
27+
void apply(std::shared_ptr<const DefaultExecutor> exec, size_type zeta, \
28+
const array<IndexType>& hash_map, \
29+
const array<ValueType>& signs, \
30+
matrix::view::dense<const ValueType> b, \
3131
matrix::view::dense<ValueType> x)
3232

3333
#define GKO_DECLARE_SPARSE_STACK_RAPPLY(ValueType, IndexType) \
@@ -38,12 +38,12 @@ namespace kernels {
3838
matrix::view::dense<ValueType> x)
3939

4040

41-
#define GKO_DECLARE_ALL_AS_TEMPLATES \
42-
template <typename ValueType, typename IndexType> \
43-
GKO_DECLARE_SPARSE_STACK_GENERATE(ValueType, IndexType); \
44-
template <typename ValueType, typename IndexType> \
45-
GKO_DECLARE_SPARSE_STACK_APPLY(ValueType, IndexType); \
46-
template <typename ValueType, typename IndexType> \
41+
#define GKO_DECLARE_ALL_AS_TEMPLATES \
42+
template <typename ValueType, typename IndexType> \
43+
GKO_DECLARE_SPARSE_STACK_GENERATE(ValueType, IndexType); \
44+
template <typename ValueType, typename IndexType> \
45+
GKO_DECLARE_SPARSE_STACK_APPLY(ValueType, IndexType); \
46+
template <typename ValueType, typename IndexType> \
4747
GKO_DECLARE_SPARSE_STACK_RAPPLY(ValueType, IndexType)
4848

4949

examples/sketch-and-solve/sketch-and-solve.cpp

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
// where A is (m x n) with m >> n. Sketching reduces the system from
77
// (m x n) to (k x n) with n < k << m, then solves the smaller problem.
88

9-
#include <ginkgo/ginkgo.hpp>
10-
119
#include <chrono>
1210
#include <iomanip>
1311
#include <iostream>
@@ -17,6 +15,8 @@
1715

1816
#include <cxxopts.hpp>
1917

18+
#include <ginkgo/ginkgo.hpp>
19+
2020

2121
using ValueType = double;
2222
using RealValueType = gko::remove_complex<ValueType>;
@@ -58,9 +58,9 @@ struct LeastSquaresProblem {
5858
std::unique_ptr<vec> x_true;
5959
};
6060

61-
LeastSquaresProblem generate_problem(
62-
std::shared_ptr<const gko::Executor> exec, gko::size_type m,
63-
gko::size_type n, unsigned int data_seed)
61+
LeastSquaresProblem generate_problem(std::shared_ptr<const gko::Executor> exec,
62+
gko::size_type m, gko::size_type n,
63+
unsigned int data_seed)
6464
{
6565
auto host = exec->get_master();
6666
std::mt19937 rng(data_seed);
@@ -96,10 +96,9 @@ LeastSquaresProblem generate_problem(
9696

9797
// Solve the sketched least-squares problem: min_x || S*A*x - S*b ||_2
9898
// via normal equations on the sketched system: (SA)^T SA x = (SA)^T Sb
99-
void sketch_and_solve(std::shared_ptr<const gko::Executor> exec,
100-
const vec* A, const vec* b, vec* x,
101-
gko::LinOp* sketch_op, const std::string& label,
102-
int num_reps, long setup_us)
99+
void sketch_and_solve(std::shared_ptr<const gko::Executor> exec, const vec* A,
100+
const vec* b, vec* x, gko::LinOp* sketch_op,
101+
const std::string& label, int num_reps, long setup_us)
103102
{
104103
auto m = A->get_size()[0];
105104
auto n = A->get_size()[1];
@@ -140,11 +139,10 @@ void sketch_and_solve(std::shared_ptr<const gko::Executor> exec,
140139

141140
auto solver =
142141
gko::solver::Gmres<ValueType>::build()
143-
.with_criteria(
144-
gko::stop::Iteration::build().with_max_iters(
145-
static_cast<gko::uint32>(10 * n)),
146-
gko::stop::ResidualNorm<ValueType>::build()
147-
.with_reduction_factor(RealValueType{1e-14}))
142+
.with_criteria(gko::stop::Iteration::build().with_max_iters(
143+
static_cast<gko::uint32>(10 * n)),
144+
gko::stop::ResidualNorm<ValueType>::build()
145+
.with_reduction_factor(RealValueType{1e-14}))
148146
.on(exec)
149147
->generate(gko::share(std::move(AtA)));
150148

@@ -195,11 +193,10 @@ void direct_solve(std::shared_ptr<const gko::Executor> exec, const vec* A,
195193

196194
auto solver =
197195
gko::solver::Gmres<ValueType>::build()
198-
.with_criteria(
199-
gko::stop::Iteration::build().with_max_iters(
200-
static_cast<gko::uint32>(10 * n)),
201-
gko::stop::ResidualNorm<ValueType>::build()
202-
.with_reduction_factor(RealValueType{1e-14}))
196+
.with_criteria(gko::stop::Iteration::build().with_max_iters(
197+
static_cast<gko::uint32>(10 * n)),
198+
gko::stop::ResidualNorm<ValueType>::build()
199+
.with_reduction_factor(RealValueType{1e-14}))
203200
.on(exec)
204201
->generate(gko::share(std::move(AtA)));
205202

@@ -222,10 +219,11 @@ void direct_solve(std::shared_ptr<const gko::Executor> exec, const vec* A,
222219
auto rel_res = res_norm->at(0, 0) / b_norm->at(0, 0);
223220

224221
std::cout << std::left << std::setw(16) << "Direct" << std::right
225-
<< std::setw(10) << "---" << " " << std::setw(10) << "---"
226-
<< " " << std::setw(10) << solve_us << " us"
227-
<< std::setw(14) << std::scientific << std::setprecision(4)
228-
<< rel_res << std::endl;
222+
<< std::setw(10) << "---"
223+
<< " " << std::setw(10) << "---"
224+
<< " " << std::setw(10) << solve_us << " us" << std::setw(14)
225+
<< std::scientific << std::setprecision(4) << rel_res
226+
<< std::endl;
229227
}
230228

231229

@@ -284,8 +282,8 @@ int main(int argc, char* argv[])
284282

285283
std::cout << "Problem: " << m << " x " << n << " (overdetermined"
286284
<< ", ratio " << m / n << ":1)"
287-
<< "\nSketch size: " << k << " (compression "
288-
<< std::fixed << std::setprecision(1)
285+
<< "\nSketch size: " << k << " (compression " << std::fixed
286+
<< std::setprecision(1)
289287
<< static_cast<double>(m) / static_cast<double>(k) << "x)"
290288
<< "\nExecutor: " << exec_name << " Seed: " << seed
291289
<< " Reps: " << num_reps << "\n"
@@ -297,23 +295,24 @@ int main(int argc, char* argv[])
297295
// Header
298296
// Setup = time to create the sketch operator (generate random data)
299297
// Sketch = time to compute S*A and S*b
300-
// Solve = time to form normal equations + CG solve (on sketched or full system)
298+
// Solve = time to form normal equations + CG solve (on sketched or full
299+
// system)
301300
std::cout << std::left << std::setw(16) << "Method" << std::right
302301
<< std::setw(13) << "Setup" << std::setw(13) << "Sketch"
303-
<< std::setw(13) << "Solve" << std::setw(14)
304-
<< "||Ax-b||/||b||" << "\n"
302+
<< std::setw(13) << "Solve" << std::setw(14) << "||Ax-b||/||b||"
303+
<< "\n"
305304
<< std::string(69, '-') << std::endl;
306305

307306
if (sketch_type == "all" || sketch_type == "gaussian") {
308307
exec->synchronize();
309308
auto t0 = std::chrono::steady_clock::now();
310-
auto gaussian = gko::sketch::GaussianSketch<ValueType>::create(
311-
exec, k, m, seed);
309+
auto gaussian =
310+
gko::sketch::GaussianSketch<ValueType>::create(exec, k, m, seed);
312311
exec->synchronize();
313312
auto t1 = std::chrono::steady_clock::now();
314-
auto setup_us = std::chrono::duration_cast<std::chrono::microseconds>(
315-
t1 - t0)
316-
.count();
313+
auto setup_us =
314+
std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0)
315+
.count();
317316
auto x = vec::create(exec, gko::dim<2>{n, 1});
318317
sketch_and_solve(exec, problem.A.get(), problem.b.get(), x.get(),
319318
gaussian.get(), "Gaussian", num_reps, setup_us);
@@ -326,9 +325,9 @@ int main(int argc, char* argv[])
326325
exec, k, m, seed);
327326
exec->synchronize();
328327
auto t1 = std::chrono::steady_clock::now();
329-
auto setup_us = std::chrono::duration_cast<std::chrono::microseconds>(
330-
t1 - t0)
331-
.count();
328+
auto setup_us =
329+
std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0)
330+
.count();
332331
auto x = vec::create(exec, gko::dim<2>{n, 1});
333332
sketch_and_solve(exec, problem.A.get(), problem.b.get(), x.get(),
334333
cs.get(), "CountSketch", num_reps, setup_us);
@@ -337,17 +336,17 @@ int main(int argc, char* argv[])
337336
if (sketch_type == "all" || sketch_type == "sparsestack") {
338337
exec->synchronize();
339338
auto t0 = std::chrono::steady_clock::now();
340-
339+
341340
auto ss = gko::sketch::SparseStack<ValueType, IndexType>::create(
342341
exec, k, m, zeta, seed);
343-
342+
344343
exec->synchronize();
345344
auto t1 = std::chrono::steady_clock::now();
346-
auto setup_us = std::chrono::duration_cast<std::chrono::microseconds>(
347-
t1 - t0)
348-
.count();
345+
auto setup_us =
346+
std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0)
347+
.count();
349348
auto x = vec::create(exec, gko::dim<2>{n, 1});
350-
349+
351350
sketch_and_solve(exec, problem.A.get(), problem.b.get(), x.get(),
352351
ss.get(), "SparseStack", num_reps, setup_us);
353352
}

include/ginkgo/core/sketch/sparse_stack.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ namespace sketch {
3131
* @tparam IndexType type for hash_map indices
3232
*/
3333
template <typename ValueType = default_precision, typename IndexType = int32>
34-
class SparseStack
35-
: public EnableLinOp<SparseStack<ValueType, IndexType>,
36-
SketchOperator<ValueType>> {
34+
class SparseStack : public EnableLinOp<SparseStack<ValueType, IndexType>,
35+
SketchOperator<ValueType>> {
3736
friend class EnableLinOp<SparseStack>;
3837
friend class EnablePolymorphicObject<SparseStack,
39-
SketchOperator<ValueType>>;
38+
SketchOperator<ValueType>>;
4039

4140
public:
4241
using value_type = ValueType;

0 commit comments

Comments
 (0)