Comments (5)
Great! I've just about finished up a PR with these changes, will open that later today
from math.
A couple of other accumulator thoughts.
At the moment, Eigen types are summed and then appended to the buffer, but std::vector types are simply elementwise appended - ostensibly for the code to also be compatible with std::vector<container>
types:
template <typename S, require_matrix_t<S>* = nullptr>
inline void add(const S& m) {
buf_.push_back(stan::math::sum(m));
}
template <typename S>
inline void add(const std::vector<S>& xs) {
for (size_t i = 0; i < xs.size(); ++i) {
this->add(xs[i]);
}
}
If we change the templates such that the Eigen overload also takes non-nested std:vectors and the std::vector overload is only for nested containers, then this should dramatically improve performance with larger std::vector
inputs. It would also reduce memory usage, since the non-nested std::vectors are no longer just being appended to the buffer.
One last thought, would there be any benefit from std::move()
-ing the result?:
template <typename S, require_matrix_t<S>* = nullptr>
inline void add(const S& m) {
buf_.push_back(std::move(stan::math::sum(m)));
}
from math.
We can't use log_sum_exp here since we're after the sum of the logs, not the log of the sum of the exps.
Ooops yes totally forgot, forget this idea.
But it did make me have a look at the accumulator() and sum() code, and I think we should change the std::vector overload to use Eigen::Map<> and then sum, rather than std::accumulate: https://godbolt.org/z/zhe3eMWv7
Especially since accumulator sums its buffer using the std::vector<> method, which doesn't look to get the vectorised benefits that Eigen does
Yes I like this idea!
If we change the templates such that the Eigen overload also takes non-nested std:vectors and the std::vector overload is only for nested containers, then this should dramatically improve performance with larger std::vector inputs. It would also reduce memory usage, since the non-nested std::vectors are no longer just being appended to the buffer.
Yes I agree that would be a good change!
One last thought, would there be any benefit from std::move()-ing the result?:
Not here, we only need to move if the container we are moving has a good amount of dynamically allocated memory. In the code above sum()
returns a scalar so it would not need to be moved.
You can think of moves like it's giving ownership of deleting dynamically allocated memory to a new object from an old object.
struct foo {
double* data_;//
std::size_t n_;
// Copy makes a copy of the memory
foo(foo& x) : data_(copy(x.data_, n)), n_(x.n_) {}
foo(foo&& x) : data_(x.data_), n_(x.n_) {
// Moves takes the pointer and then nulls out the input's pointer
// This means the new object is now in charge of calling delete
x.data_ = nullptr;
}
}
from math.
We can't use log_sum_exp
here since we're after the sum of the logs, not the log of the sum of the exps.
But it did make me have a look at the accumulator()
and sum()
code, and I think we should change the std::vector<T>
overload to use Eigen::Map<>
and then sum, rather than std::accumulate
: https://godbolt.org/z/zhe3eMWv7
Especially since accumulator
sums its buffer using the std::vector<>
method, which doesn't look to get the vectorised benefits that Eigen does
from math.
Also changed the name of the issue to reflect topic changed
from math.
Related Issues (20)
- [Feature] Add a bracketing rootfinder HOT 6
- Jenkins OpenCL tests failing on jenkins2 but not jenkins1 HOT 3
- Testing: decrease impact of distribution tests on CI hardware HOT 5
- SDE distribution
- csr_to_dense_matrix fails when first row is all zeros HOT 1
- Compilation error when including Eigen and Stan together HOT 5
- normal_lcdf and normal_lccdf give infinite gradients for infinite inputs. HOT 3
- [Admin] `licenses/` directory out of date
- [BUG] `eigenvalues_sym` returns a matrix instead of a vector
- eigenvalues_sym return type breaks RcppEigen compat
- Failure to build TBB when using GCC 13 HOT 6
- Compiling with O1 leads to failing chains with compound declaration and assignment with range indices HOT 7
- boost 1.82 upgrade HOT 8
- Distributions - Replace separate `propto` template with default argument?
- feature request: configurable constraint tolerance
- Hessian-vector product with central finite differences
- tbb/area.cpp triggers clang compiler warning unused-but-set-variable (mac-osx)
- Student T CDF precision for large values
- vectorize binary log_sum_exp HOT 1
- Vectorized logical functions HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from math.