Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
16 changes: 16 additions & 0 deletions make/clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

tidy_base_checks=*perf*,*modernize*,*misc*,*google*,*readability*,*optin*,-clang-diagnostic-error
modern_excludes=,-modernize-replace-random-shuffle,-modernize-use-override,-modernize-concat-nested-namespaces,-modernize-avoid-c-arrays
readability_excludes=,-readability-magic-numbers,-readability-convert-member-functions-to-static,-readability-inconsistent-declaration-parameter-name,-readability-function-size,-readability-named-parameter,-readability-container-size-empty,-readability-implicit-bool-conversion,-readability-else-after-return
misc_excludes=,-misc-non-private-member-variables-in-classes,-misc-unused-parameters,-misc-unused-using-decls,-misc-definitions-in-headers,-misc-unconventional-assign-operator
google_excludes=,-google-runtime-references,-google-global-names-in-headers,-google-build-using-namespace,-google-runtime-int
things_we_should_look_at=,-google-readability-casting,-modernize-use-using,-modernize-pass-by-value,-readability-implicit-bool-conversion,-readability-const-return-type,-readability-avoid-const-params-in-decls,--modernize-return-braced-init-list
tidy_checks?= $(tidy_base_checks) $(modern_excludes) $(readability_excludes) $(misc_excludes) $(google_excludes) $(things_we_should_look_at)

.PHONY: clang-tidy
clang-tidy:
find $(folder) -name '$(files).cpp' -exec clang-tidy {} -checks='$(tidy_checks)' -header-filter='./stan/*' -- $(CXXFLAGS) $(LDFLAGS) $(LDLIBS) $(INC_GTEST) $(CXXFLAGS_GTEST) $(CPPFLAGS) \;

.PHONY: clang-tidy-fix
clang-tidy-fix:
find $(folder) -name '$(files).cpp' -exec clang-tidy {} -checks='$(tidy_checks)' -header-filter='./stan/*' -fix -fix-errors -- $(CXXFLAGS) $(LDFLAGS) $(LDLIBS) $(INC_GTEST) $(CXXFLAGS_GTEST) $(CPPFLAGS) \;
2 changes: 1 addition & 1 deletion make/compiler_flags
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ifeq (default,$(origin CXX))
CXX := clang++
endif
ifeq ($(OS),Linux)
CXX := g++
CXX := clang++
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A to-do here, clang-tidy uses clang, so I'm not sure how to go around having g++ here though I'd like to keep it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to change this here? If we make this a part of the Jenkins process, we can set CXX = clang++ there before running make clang-tidy. Like we set flags for MPI or OPENCL before running those tests.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah idt we need it I'll remove it

endif
ifeq ($(OS),Windows_NT)
CXX := g++
Expand Down
2 changes: 1 addition & 1 deletion make/test-math-dependencies
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
##
##
# Function to search code for warnings.
#
# After all the warnings are found, use
Expand Down
1 change: 1 addition & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ include make/dependencies # rules for generating dependencies
include make/libraries
include make/tests
include make/cpplint
include make/clang-tidy

.PHONY: help
help:
Expand Down
42 changes: 28 additions & 14 deletions stan/math/memory/stack_alloc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ const size_t DEFAULT_INITIAL_NBYTES = 1 << 16; // 64KB
// big fun to inline, but only called twice
inline char* eight_byte_aligned_malloc(size_t size) {
char* ptr = static_cast<char*>(malloc(size));
if (!ptr)
if (!ptr) {
return ptr; // malloc failed to alloc
}
if (!is_aligned(ptr, 8U)) {
std::stringstream s;
s << "invalid alignment to 8 bytes, ptr="
Expand Down Expand Up @@ -94,17 +95,20 @@ class stack_alloc {
char* result;
++cur_block_;
// Find the next block (if any) containing at least len bytes.
while ((cur_block_ < blocks_.size()) && (sizes_[cur_block_] < len))
while ((cur_block_ < blocks_.size()) && (sizes_[cur_block_] < len)) {
++cur_block_;
}
// Allocate a new block if necessary.
if (unlikely(cur_block_ >= blocks_.size())) {
// New block should be max(2*size of last block, len) bytes.
size_t newsize = sizes_.back() * 2;
if (newsize < len)
if (newsize < len) {
newsize = len;
}
blocks_.push_back(internal::eight_byte_aligned_malloc(newsize));
if (!blocks_.back())
if (!blocks_.back()) {
throw std::bad_alloc();
}
sizes_.push_back(newsize);
}
result = blocks_[cur_block_];
Expand All @@ -130,8 +134,9 @@ class stack_alloc {
cur_block_(0),
cur_block_end_(blocks_[0] + initial_nbytes),
next_loc_(blocks_[0]) {
if (!blocks_[0])
if (!blocks_[0]) {
throw std::bad_alloc(); // no msg allowed in bad_alloc ctor
}
}

/**
Expand All @@ -142,9 +147,11 @@ class stack_alloc {
*/
~stack_alloc() {
// free ALL blocks
for (auto& block : blocks_)
if (block)
for (auto& block : blocks_) {
if (block) {
free(block);
}
}
}

/**
Expand All @@ -164,8 +171,9 @@ class stack_alloc {
char* result = next_loc_;
next_loc_ += len;
// Occasionally, we have to switch blocks.
if (unlikely(next_loc_ >= cur_block_end_))
if (unlikely(next_loc_ >= cur_block_end_)) {
result = move_to_next_block(len);
}
return reinterpret_cast<void*>(result);
}

Expand Down Expand Up @@ -208,8 +216,9 @@ class stack_alloc {
* recover memory back to the last start_nested call.
*/
inline void recover_nested() {
if (unlikely(nested_cur_blocks_.empty()))
if (unlikely(nested_cur_blocks_.empty())) {
recover_all();
}

cur_block_ = nested_cur_blocks_.back();
nested_cur_blocks_.pop_back();
Expand All @@ -228,9 +237,11 @@ class stack_alloc {
*/
inline void free_all() {
// frees all BUT the first (index 0) block
for (size_t i = 1; i < blocks_.size(); ++i)
if (blocks_[i])
for (size_t i = 1; i < blocks_.size(); ++i) {
if (blocks_[i]) {
free(blocks_[i]);
}
}
sizes_.resize(1);
blocks_.resize(1);
recover_all();
Expand Down Expand Up @@ -263,11 +274,14 @@ class stack_alloc {
* false otherwise.
*/
inline bool in_stack(const void* ptr) const {
for (size_t i = 0; i < cur_block_; ++i)
if (ptr >= blocks_[i] && ptr < blocks_[i] + sizes_[i])
for (size_t i = 0; i < cur_block_; ++i) {
if (ptr >= blocks_[i] && ptr < blocks_[i] + sizes_[i]) {
return true;
if (ptr >= blocks_[cur_block_] && ptr < next_loc_)
}
}
if (ptr >= blocks_[cur_block_] && ptr < next_loc_) {
return true;
}
return false;
}
};
Expand Down
3 changes: 2 additions & 1 deletion stan/math/opencl/cholesky_decompose.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ namespace math {
*/
template <typename T, typename = enable_if_floating_point<T>>
inline void cholesky_decompose(matrix_cl<T>& A) {
if (A.rows() == 0)
if (A.rows() == 0) {
return;
}
// Repeats the blocked cholesky decomposition until the size of the remaining
// submatrix is smaller or equal to the minimum blocks size
// or a heuristic of 100.
Expand Down
6 changes: 4 additions & 2 deletions stan/math/opencl/diagonal_multiply.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ template <typename T1, typename T2, typename = enable_if_all_arithmetic<T1, T2>>
inline matrix_cl<return_type_t<T1, T2>> diagonal_multiply(
const matrix_cl<T1>& A, const T2 scalar) {
matrix_cl<return_type_t<T1, T2>> B(A);
if (B.size() == 0)
if (B.size() == 0) {
return B;
}
// For rectangular matrices
int min_dim = B.rows();
if (B.cols() < min_dim)
if (B.cols() < min_dim) {
min_dim = B.cols();
}
try {
opencl_kernels::scalar_mul_diagonal(cl::NDRange(min_dim), B, scalar,
B.rows(), min_dim);
Expand Down
3 changes: 2 additions & 1 deletion stan/math/opencl/err/check_diagonal_zeros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ namespace math {
template <typename T, typename = enable_if_arithmetic<T>>
inline void check_diagonal_zeros(const char* function, const char* name,
const matrix_cl<T>& y) {
if (y.size() == 0)
if (y.size() == 0) {
return;
}
cl::CommandQueue cmd_queue = opencl_context.queue();
cl::Context ctx = opencl_context.context();
try {
Expand Down
3 changes: 2 additions & 1 deletion stan/math/opencl/err/check_nan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ namespace math {
template <typename T, typename = enable_if_floating_point<T>>
inline void check_nan(const char* function, const char* name,
const matrix_cl<T>& y) {
if (y.size() == 0)
if (y.size() == 0) {
return;
}
try {
int nan_flag = 0;
matrix_cl<int> nan_chk(1, 1);
Expand Down
3 changes: 2 additions & 1 deletion stan/math/opencl/err/check_symmetric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ namespace math {
template <typename T, typename = enable_if_arithmetic<T>>
inline void check_symmetric(const char* function, const char* name,
const matrix_cl<T>& y) {
if (y.size() == 0)
if (y.size() == 0) {
return;
}
check_square(function, name, y);
try {
int symmetric_flag = 1;
Expand Down
9 changes: 6 additions & 3 deletions stan/math/opencl/err/check_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ namespace math {
template <typename T>
inline void check_vector(const char* function, const char* name,
const matrix_cl<T>& x) {
if (x.rows() == 1)
if (x.rows() == 1) {
return;
if (x.cols() == 1)
}
if (x.cols() == 1) {
return;
if (x.rows() == 1 || x.cols() == 1)
}
if (x.rows() == 1 || x.cols() == 1) {
return;
}

std::ostringstream msg;
msg << ") has " << x.rows() << " rows and " << x.cols()
Expand Down
9 changes: 6 additions & 3 deletions stan/math/opencl/matrix_cl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,9 @@ class matrix_cl<T, enable_if_arithmetic<T>> {

matrix_cl(const matrix_cl<T>& A)
: rows_(A.rows()), cols_(A.cols()), view_(A.view()) {
if (A.size() == 0)
if (A.size() == 0) {
return;
}
this->wait_for_read_write_events();
cl::Context& ctx = opencl_context.context();
cl::CommandQueue queue = opencl_context.queue();
Expand Down Expand Up @@ -224,8 +225,9 @@ class matrix_cl<T, enable_if_arithmetic<T>> {
explicit matrix_cl(const std::vector<Eigen::Matrix<T, R, C>>& A) try
: rows_(A.empty() ? 0 : A[0].size()),
cols_(A.size()) {
if (this->size() == 0)
if (this->size() == 0) {
return;
}
cl::Context& ctx = opencl_context.context();
cl::CommandQueue& queue = opencl_context.queue();
// creates the OpenCL buffer to copy the Eigen
Expand Down Expand Up @@ -378,8 +380,9 @@ class matrix_cl<T, enable_if_arithmetic<T>> {
a.rows(), "destination.rows()", rows());
check_size_match("assignment of (OpenCL) matrices", "source.cols()",
a.cols(), "destination.cols()", cols());
if (a.size() == 0)
if (a.size() == 0) {
return *this;
}
view_ = a.view();
this->wait_for_read_write_events();
cl::CommandQueue queue = opencl_context.queue();
Expand Down
3 changes: 2 additions & 1 deletion stan/math/opencl/multiply.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ template <typename T1, typename T2, typename = enable_if_all_arithmetic<T1, T2>>
inline matrix_cl<return_type_t<T1, T2>> multiply(const matrix_cl<T1>& A,
const T2 scalar) {
matrix_cl<return_type_t<T1, T2>> temp(A.rows(), A.cols(), A.view());
if (A.size() == 0)
if (A.size() == 0) {
return temp;
}
try {
opencl_kernels::scalar_mul(cl::NDRange(A.rows(), A.cols()), temp, A, scalar,
A.rows(), A.cols(), A.view());
Expand Down
3 changes: 2 additions & 1 deletion stan/math/opencl/multiply_transpose.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ inline matrix_cl<T> multiply_transpose(const matrix_cl<T>& A) {
? matrix_cl_view::Diagonal
: matrix_cl_view::Entire);

if (A.size() == 0)
if (A.size() == 0) {
return temp;
}
// padding the matrices so the dimensions are divisible with local
// improves performance becasuse we can omit if statements in the
// multiply kernel
Expand Down
3 changes: 2 additions & 1 deletion stan/math/opencl/opencl_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ inline cl::size_t<N> to_size_t(const size_t (&values)[N]) {
template <>
inline cl::size_t<3> to_size_t(const size_t (&values)[3]) {
cl::size_t<3> s;
for (size_t i = 0; i < 3; i++)
for (size_t i = 0; i < 3; i++) {
s[i] = values[i];
}
return s;
}
} // namespace opencl
Expand Down
3 changes: 2 additions & 1 deletion stan/math/opencl/transpose.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ namespace math {
template <typename T, typename = enable_if_arithmetic<T>>
inline matrix_cl<T> transpose(const matrix_cl<T>& src) {
matrix_cl<T> dst(src.cols(), src.rows(), transpose(src.view()));
if (dst.size() == 0)
if (dst.size() == 0) {
return dst;
}
try {
opencl_kernels::transpose(cl::NDRange(src.rows(), src.cols()), dst, src,
src.rows(), src.cols());
Expand Down
3 changes: 2 additions & 1 deletion stan/math/opencl/zeros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ namespace math {
template <typename T>
template <matrix_cl_view matrix_view>
inline void matrix_cl<T, enable_if_arithmetic<T>>::zeros() try {
if (size() == 0)
if (size() == 0) {
return;
}
this->view_ = both(this->view_, invert(matrix_view));
cl::CommandQueue cmdQueue = opencl_context.queue();
opencl_kernels::zeros(cl::NDRange(this->rows(), this->cols()), *this,
Expand Down
3 changes: 2 additions & 1 deletion stan/math/prim/arr/err/check_nonzero_size.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ namespace math {
template <typename T_y>
inline void check_nonzero_size(const char* function, const char* name,
const T_y& y) {
if (y.size() > 0)
if (y.size() > 0) {
return;
}
invalid_argument(function, name, 0, "has size ",
", but must have a non-zero size");
}
Expand Down
3 changes: 2 additions & 1 deletion stan/math/prim/arr/err/is_ordered.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ namespace math {
template <typename T_y>
inline bool is_ordered(const std::vector<T_y>& y) {
for (size_t n = 1; n < y.size(); ++n) {
if (!(y[n] > y[n - 1]))
if (!(y[n] > y[n - 1])) {
return false;
}
}
return true;
}
Expand Down
3 changes: 2 additions & 1 deletion stan/math/prim/arr/fun/dot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ namespace math {

inline double dot(const std::vector<double>& x, const std::vector<double>& y) {
double sum = 0.0;
for (size_t i = 0; i < x.size(); ++i)
for (size_t i = 0; i < x.size(); ++i) {
sum += x[i] * y[i];
}
return sum;
}

Expand Down
3 changes: 2 additions & 1 deletion stan/math/prim/arr/fun/dot_self.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ namespace math {

inline double dot_self(const std::vector<double>& x) {
double sum = 0.0;
for (double i : x)
for (double i : x) {
sum += i * i;
}
return sum;
}

Expand Down
3 changes: 2 additions & 1 deletion stan/math/prim/arr/fun/fill.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ namespace math {
*/
template <typename T, typename S>
void fill(std::vector<T>& x, const S& y) {
for (typename std::vector<T>::size_type i = 0; i < x.size(); ++i)
for (typename std::vector<T>::size_type i = 0; i < x.size(); ++i) {
fill(x[i], y);
}
}

} // namespace math
Expand Down
3 changes: 2 additions & 1 deletion stan/math/prim/arr/fun/inverse_softmax.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ template <typename Vector>
void inverse_softmax(const Vector& simplex, Vector& y) {
using std::log;
check_matching_sizes("inverse_softmax", "simplex", simplex, "y", y);
for (size_t i = 0; i < simplex.size(); ++i)
for (size_t i = 0; i < simplex.size(); ++i) {
y[i] = log(simplex[i]);
}
}

} // namespace math
Expand Down
Loading