Skip to content

Commit fc0f5dd

Browse files
committed
Run clang-format.
1 parent 288482b commit fc0f5dd

32 files changed

+1070
-1605
lines changed

.clang-format

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
---
22
Language: Cpp
33
BasedOnStyle: LLVM
4+
PenaltyBreakBeforeFirstCallParameter: 0
5+
PenaltyBreakTemplateDeclaration: 0
46
AlignAfterOpenBracket: DontAlign
57
AlwaysBreakTemplateDeclarations: Yes
8+
AllowShortFunctionsOnASingleLine: All
9+
AllowShortCaseLabelsOnASingleLine: true
10+
AllowShortBlocksOnASingleLine: Always
11+
AllowShortIfStatementsOnASingleLine: Always
12+
AllowShortLoopsOnASingleLine: true
613
ColumnLimit: 100
7-
PointerAlignment: Left
14+
PointerAlignment: Left

array.h

Lines changed: 563 additions & 825 deletions
Large diffs are not rendered by default.

ein_reduce.h

Lines changed: 103 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
/** \file ein_reduce.h
1616
* \brief Optional helper for computing Einstein reductions on arrays.
17-
*/
17+
*/
1818

1919
#ifndef NDARRAY_EIN_REDUCE_H
2020
#define NDARRAY_EIN_REDUCE_H
@@ -61,22 +61,38 @@ struct ein_op {
6161
}
6262

6363
template <class T, class = enable_if_ein_op<T>>
64-
auto operator+(const T& r) const { return make_ein_op_add(*this, r); }
64+
auto operator+(const T& r) const {
65+
return make_ein_op_add(*this, r);
66+
}
6567
template <class T, class = enable_if_ein_op<T>>
66-
auto operator-(const T& r) const { return make_ein_op_sub(*this, r); }
68+
auto operator-(const T& r) const {
69+
return make_ein_op_sub(*this, r);
70+
}
6771
template <class T, class = enable_if_ein_op<T>>
68-
auto operator*(const T& r) const { return make_ein_op_mul(*this, r); }
72+
auto operator*(const T& r) const {
73+
return make_ein_op_mul(*this, r);
74+
}
6975
template <class T, class = enable_if_ein_op<T>>
70-
auto operator/(const T& r) const { return make_ein_op_div(*this, r); }
76+
auto operator/(const T& r) const {
77+
return make_ein_op_div(*this, r);
78+
}
7179

7280
template <class T, class = enable_if_ein_op<T>>
73-
auto operator=(const T& r) const { return make_ein_op_assign(*this, r); }
81+
auto operator=(const T& r) const {
82+
return make_ein_op_assign(*this, r);
83+
}
7484
template <class T, class = enable_if_ein_op<T>>
75-
auto operator+=(const T& r) const { return make_ein_op_add_assign(*this, r); }
85+
auto operator+=(const T& r) const {
86+
return make_ein_op_add_assign(*this, r);
87+
}
7688
template <class T, class = enable_if_ein_op<T>>
77-
auto operator-=(const T& r) const { return make_ein_op_sub_assign(*this, r); }
89+
auto operator-=(const T& r) const {
90+
return make_ein_op_sub_assign(*this, r);
91+
}
7892
template <class T, class = enable_if_ein_op<T>>
79-
auto operator*=(const T& r) const { return make_ein_op_mul_assign(*this, r); }
93+
auto operator*=(const T& r) const {
94+
return make_ein_op_mul_assign(*this, r);
95+
}
8096
};
8197

8298
// A binary operation of two operands.
@@ -94,46 +110,55 @@ struct ein_bin_op {
94110
const Derived& derived() const { return *static_cast<const Derived*>(this); }
95111

96112
template <class T, class = enable_if_ein_op<T>>
97-
auto operator+(const T& r) const { return make_ein_op_add(derived(), r); }
113+
auto operator+(const T& r) const {
114+
return make_ein_op_add(derived(), r);
115+
}
98116
template <class T, class = enable_if_ein_op<T>>
99-
auto operator-(const T& r) const { return make_ein_op_sub(derived(), r); }
117+
auto operator-(const T& r) const {
118+
return make_ein_op_sub(derived(), r);
119+
}
100120
template <class T, class = enable_if_ein_op<T>>
101-
auto operator*(const T& r) const { return make_ein_op_mul(derived(), r); }
121+
auto operator*(const T& r) const {
122+
return make_ein_op_mul(derived(), r);
123+
}
102124
template <class T, class = enable_if_ein_op<T>>
103-
auto operator/(const T& r) const { return make_ein_op_div(derived(), r); }
125+
auto operator/(const T& r) const {
126+
return make_ein_op_div(derived(), r);
127+
}
104128
};
105129

106-
#define NDARRAY_MAKE_EIN_BIN_HELPERS(name, op) \
107-
template <class OpA, class OpB> auto make_##name(const OpA& a, const OpB& b) { \
108-
name<OpA, OpB> result; \
109-
result.op_a = a; \
110-
result.op_b = b; \
111-
return result; \
112-
}
130+
#define NDARRAY_MAKE_EIN_BIN_HELPERS(name, op) \
131+
template <class OpA, class OpB> \
132+
auto make_##name(const OpA& a, const OpB& b) { \
133+
name<OpA, OpB> result; \
134+
result.op_a = a; \
135+
result.op_b = b; \
136+
return result; \
137+
}
113138

114-
#define NDARRAY_MAKE_EIN_BIN_OP(name, op, is_assign_) \
115-
template <class OpA, class OpB> \
116-
struct name : public ein_bin_op<OpA, OpB, name<OpA, OpB>> { \
117-
using is_assign = is_assign_; \
118-
template <class Idx> \
119-
NDARRAY_INLINE auto operator()(const Idx& i) const { \
120-
using base = ein_bin_op<OpA, OpB, name>; \
121-
return base::op_a(i) op base::op_b(i); \
122-
} \
123-
}; \
124-
NDARRAY_MAKE_EIN_BIN_HELPERS(name, op)
125-
126-
#define NDARRAY_MAKE_EIN_BIN_FN(name, fn, is_assign_) \
127-
template <class OpA, class OpB> \
128-
struct name : public ein_bin_op<OpA, OpB, name<OpA, OpB>> { \
129-
using is_assign = is_assign_; \
130-
template <class Idx> \
131-
NDARRAY_INLINE auto operator()(const Idx& i) const { \
132-
using base = ein_bin_op<OpA, OpB, name>; \
133-
return fn(base::op_a(i), base::op_b(i)); \
134-
} \
135-
}; \
136-
NDARRAY_MAKE_EIN_BIN_HELPERS(name, op)
139+
#define NDARRAY_MAKE_EIN_BIN_OP(name, op, is_assign_) \
140+
template <class OpA, class OpB> \
141+
struct name : public ein_bin_op<OpA, OpB, name<OpA, OpB>> { \
142+
using is_assign = is_assign_; \
143+
template <class Idx> \
144+
NDARRAY_INLINE auto operator()(const Idx& i) const { \
145+
using base = ein_bin_op<OpA, OpB, name>; \
146+
return base::op_a(i) op base::op_b(i); \
147+
} \
148+
}; \
149+
NDARRAY_MAKE_EIN_BIN_HELPERS(name, op)
150+
151+
#define NDARRAY_MAKE_EIN_BIN_FN(name, fn, is_assign_) \
152+
template <class OpA, class OpB> \
153+
struct name : public ein_bin_op<OpA, OpB, name<OpA, OpB>> { \
154+
using is_assign = is_assign_; \
155+
template <class Idx> \
156+
NDARRAY_INLINE auto operator()(const Idx& i) const { \
157+
using base = ein_bin_op<OpA, OpB, name>; \
158+
return fn(base::op_a(i), base::op_b(i)); \
159+
} \
160+
}; \
161+
NDARRAY_MAKE_EIN_BIN_HELPERS(name, op)
137162

138163
// Define the expression types for the operations we support.
139164
NDARRAY_MAKE_EIN_BIN_OP(ein_op_add, +, std::false_type);
@@ -153,9 +178,13 @@ NDARRAY_MAKE_EIN_BIN_OP(ein_op_mul_assign, *=, std::true_type);
153178
#undef NDARRAY_MAKE_EIN_BIN_HELPERS
154179

155180
template <class OpA, class OpB>
156-
auto min(const OpA& a, const OpB& b) { return make_ein_op_min(a, b); }
181+
auto min(const OpA& a, const OpB& b) {
182+
return make_ein_op_min(a, b);
183+
}
157184
template <class OpA, class OpB>
158-
auto max(const OpA& a, const OpB& b) { return make_ein_op_max(a, b); }
185+
auto max(const OpA& a, const OpB& b) {
186+
return make_ein_op_max(a, b);
187+
}
159188

160189
// Helper to reinterpret a dim/shape with a new stride.
161190
template <index_t NewStride, index_t Min, index_t Extent, index_t Stride>
@@ -200,14 +229,18 @@ auto reconcile_dim(const std::tuple<Dims...>& dims) {
200229

201230
// Get the shape of an ein_reduce operand, or an empty shape if not an array.
202231
template <class T, class Shape>
203-
const auto& dims_of(const array_ref<T, Shape>& op) { return op.shape().dims(); }
232+
const auto& dims_of(const array_ref<T, Shape>& op) {
233+
return op.shape().dims();
234+
}
204235
template <class T>
205-
auto dims_of(const T& op) { return std::tuple<>(); }
236+
auto dims_of(const T& op) {
237+
return std::tuple<>();
238+
}
206239

207240
// These types are flags that let us overload behavior based on these 3 options.
208-
class is_inferred_shape{};
209-
class is_result_shape{};
210-
class is_operand_shape{};
241+
class is_inferred_shape {};
242+
class is_result_shape {};
243+
class is_operand_shape {};
211244

212245
// Get a dim from an operand, depending on the intended use of the shape.
213246
template <size_t Dim, class Dims, size_t... Is>
@@ -240,30 +273,37 @@ auto make_ein_reduce_shape(index_sequence<Is...>, const Ops&... ops) {
240273
return make_shape(gather_dims<Is>(ops...)...);
241274
}
242275

243-
} // namespace internal
276+
} // namespace internal
244277

245278
/** Operand for an Einstein summation, which is an array or other
246279
* callable object, along with a set of dimension indices.
247280
* `ein<i, j, ...>(a)` means the dimensions `i, j, ...` of the
248281
* summation index are used to address `a` during Einstein
249282
* summation. See `ein_reduce` for more details. */
250-
template <size_t... Is, class Op,
251-
class = internal::enable_if_callable<Op, decltype(Is)...>>
252-
auto ein(Op op) { return internal::ein_op<Op, Is...>{op}; }
283+
template <size_t... Is, class Op, class = internal::enable_if_callable<Op, decltype(Is)...>>
284+
auto ein(Op op) {
285+
return internal::ein_op<Op, Is...>{op};
286+
}
253287
template <size_t... Is, class T, class Shape, class Alloc,
254288
class = std::enable_if_t<sizeof...(Is) == Shape::rank()>>
255-
auto ein(array<T, Shape, Alloc>& op) { return ein<Is...>(op.ref()); }
289+
auto ein(array<T, Shape, Alloc>& op) {
290+
return ein<Is...>(op.ref());
291+
}
256292
template <size_t... Is, class T, class Shape, class Alloc,
257293
class = std::enable_if_t<sizeof...(Is) == Shape::rank()>>
258-
auto ein(const array<T, Shape, Alloc>& op) { return ein<Is...>(op.ref()); }
294+
auto ein(const array<T, Shape, Alloc>& op) {
295+
return ein<Is...>(op.ref());
296+
}
259297

260298
/** Define an Einstein summation operand for a scalar. The scalar
261299
* is broadcasted as needed during the summation. Because this
262300
* operand does not provide a shape, the dimensions of the sum
263301
* must be inferred from other operands. See `ein_reduce` for more
264302
* details. */
265303
template <class T>
266-
auto ein(T& scalar) { return ein<>(array_ref<T, shape<>>(&scalar, {})); }
304+
auto ein(T& scalar) {
305+
return ein<>(array_ref<T, shape<>>(&scalar, {}));
306+
}
267307

268308
/** Compute an Einstein reduction. This function allows one to specify
269309
* many kinds of array transformations and reductions using Einstein
@@ -322,8 +362,7 @@ NDARRAY_UNIQUE auto ein_reduce(const Expr& expr) {
322362
// first dimension it finds, so we want that to be the result dimension if it
323363
// is present. If not, this selects one of the operand dimensions, which are
324364
// given stride 0.
325-
auto reduction_shape = internal::make_ein_reduce_shape(
326-
internal::make_index_sequence<loop_rank>(),
365+
auto reduction_shape = internal::make_ein_reduce_shape(internal::make_index_sequence<loop_rank>(),
327366
std::make_tuple(internal::is_result_shape(), expr.op_a),
328367
std::make_tuple(internal::is_operand_shape(), expr.op_b));
329368

@@ -338,19 +377,16 @@ NDARRAY_UNIQUE auto ein_reduce(const Expr& expr) {
338377

339378
/** Wrapper for `ein_reduce` computing the sum of the operand operand
340379
* expression via `ein_reduce(result += expr)`. */
341-
template <class Expr, class Result,
342-
class = internal::enable_if_ein_op<Expr>,
380+
template <class Expr, class Result, class = internal::enable_if_ein_op<Expr>,
343381
class = internal::enable_if_ein_op<Result>>
344382
NDARRAY_UNIQUE auto ein_sum(const Expr& expr, const Result& result) {
345383
return ein_reduce(result += expr);
346384
}
347385

348386
/** Infer the shape of the result of `make_ein_reduce`. */
349-
template <size_t... ResultIs, class Expr,
350-
class = internal::enable_if_ein_op<Expr>>
387+
template <size_t... ResultIs, class Expr, class = internal::enable_if_ein_op<Expr>>
351388
auto make_ein_reduce_shape(const Expr& expr) {
352-
auto result_shape = internal::make_ein_reduce_shape(
353-
internal::index_sequence<ResultIs...>(),
389+
auto result_shape = internal::make_ein_reduce_shape(internal::index_sequence<ResultIs...>(),
354390
std::make_tuple(internal::is_inferred_shape(), expr));
355391
// TODO: This would really benefit from addressing https://github.com/dsharlet/array/issues/31
356392
return make_compact(result_shape);
@@ -388,6 +424,6 @@ NDARRAY_UNIQUE auto make_ein_sum(
388424
return result;
389425
}
390426

391-
} // namespace nda
427+
} // namespace nda
392428

393-
#endif // NDARRAY_EIN_REDUCE_H
429+
#endif // NDARRAY_EIN_REDUCE_H

examples/benchmark.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
#ifndef NDARRAY_EXAMPLES_BENCHMARK_H
1616
#define NDARRAY_EXAMPLES_BENCHMARK_H
1717

18-
#include <cmath>
19-
#include <chrono>
2018
#include <algorithm>
19+
#include <chrono>
20+
#include <cmath>
2121

2222
namespace nda {
2323

@@ -30,21 +30,18 @@ double benchmark(F op) {
3030
long iterations = 1;
3131
for (int trials = 0; trials < max_trials; trials++) {
3232
auto t1 = std::chrono::high_resolution_clock::now();
33-
for (int j = 0; j < iterations; j++) {
34-
op();
35-
}
33+
for (int j = 0; j < iterations; j++) { op(); }
3634
auto t2 = std::chrono::high_resolution_clock::now();
37-
time_per_iteration_s = std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1).count() / (iterations * 1e9);
38-
if (time_per_iteration_s * iterations > min_time_s) {
39-
break;
40-
}
35+
time_per_iteration_s =
36+
std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1).count() / (iterations * 1e9);
37+
if (time_per_iteration_s * iterations > min_time_s) { break; }
4138

4239
long next_iterations = static_cast<long>(std::ceil((min_time_s * 2) / time_per_iteration_s));
4340
iterations = std::min(std::max(next_iterations, iterations), iterations * 10);
4441
}
4542
return time_per_iteration_s;
4643
}
4744

48-
} // namespace nda
45+
} // namespace nda
4946

50-
#endif // NDARRAY_EXAMPLES_BENCHMARK_H
47+
#endif // NDARRAY_EXAMPLES_BENCHMARK_H

0 commit comments

Comments
 (0)