Document the fact that Random and setRandom are not reentrant (so not thread-safe)

This commit is contained in:
Gael Guennebaud 2014-01-07 20:17:59 +01:00
parent a6a57748dd
commit ac409f51f1
3 changed files with 20 additions and 6 deletions

View File

@ -34,6 +34,8 @@ struct functor_traits<scalar_random_op<Scalar> >
* The parameters \a rows and \a cols are the number of rows and of columns of * The parameters \a rows and \a cols are the number of rows and of columns of
* the returned matrix. Must be compatible with this MatrixBase type. * the returned matrix. Must be compatible with this MatrixBase type.
* *
* \not_reentrant
*
* This variant is meant to be used for dynamic-size matrix types. For fixed-size types, * This variant is meant to be used for dynamic-size matrix types. For fixed-size types,
* it is redundant to pass \a rows and \a cols as arguments, so Random() should be used * it is redundant to pass \a rows and \a cols as arguments, so Random() should be used
* instead. * instead.
@ -46,7 +48,7 @@ struct functor_traits<scalar_random_op<Scalar> >
* a temporary matrix whenever it is nested in a larger expression. This prevents unexpected * a temporary matrix whenever it is nested in a larger expression. This prevents unexpected
* behavior with expressions involving random matrices. * behavior with expressions involving random matrices.
* *
* \sa MatrixBase::setRandom(), MatrixBase::Random(Index), MatrixBase::Random() * \sa DenseBase::setRandom(), DenseBase::Random(Index), DenseBase::Random()
*/ */
template<typename Derived> template<typename Derived>
inline const CwiseNullaryOp<internal::scalar_random_op<typename internal::traits<Derived>::Scalar>, Derived> inline const CwiseNullaryOp<internal::scalar_random_op<typename internal::traits<Derived>::Scalar>, Derived>
@ -64,6 +66,7 @@ DenseBase<Derived>::Random(Index rows, Index cols)
* Must be compatible with this MatrixBase type. * Must be compatible with this MatrixBase type.
* *
* \only_for_vectors * \only_for_vectors
* \not_reentrant
* *
* This variant is meant to be used for dynamic-size vector types. For fixed-size types, * This variant is meant to be used for dynamic-size vector types. For fixed-size types,
* it is redundant to pass \a size as argument, so Random() should be used * it is redundant to pass \a size as argument, so Random() should be used
@ -76,7 +79,7 @@ DenseBase<Derived>::Random(Index rows, Index cols)
* a temporary vector whenever it is nested in a larger expression. This prevents unexpected * a temporary vector whenever it is nested in a larger expression. This prevents unexpected
* behavior with expressions involving random matrices. * behavior with expressions involving random matrices.
* *
* \sa MatrixBase::setRandom(), MatrixBase::Random(Index,Index), MatrixBase::Random() * \sa DenseBase::setRandom(), DenseBase::Random(Index,Index), DenseBase::Random()
*/ */
template<typename Derived> template<typename Derived>
inline const CwiseNullaryOp<internal::scalar_random_op<typename internal::traits<Derived>::Scalar>, Derived> inline const CwiseNullaryOp<internal::scalar_random_op<typename internal::traits<Derived>::Scalar>, Derived>
@ -100,7 +103,9 @@ DenseBase<Derived>::Random(Index size)
* a temporary matrix whenever it is nested in a larger expression. This prevents unexpected * a temporary matrix whenever it is nested in a larger expression. This prevents unexpected
* behavior with expressions involving random matrices. * behavior with expressions involving random matrices.
* *
* \sa MatrixBase::setRandom(), MatrixBase::Random(Index,Index), MatrixBase::Random(Index) * \not_reentrant
*
* \sa DenseBase::setRandom(), DenseBase::Random(Index,Index), DenseBase::Random(Index)
*/ */
template<typename Derived> template<typename Derived>
inline const CwiseNullaryOp<internal::scalar_random_op<typename internal::traits<Derived>::Scalar>, Derived> inline const CwiseNullaryOp<internal::scalar_random_op<typename internal::traits<Derived>::Scalar>, Derived>
@ -114,6 +119,8 @@ DenseBase<Derived>::Random()
* Numbers are uniformly spread through their whole definition range for integer types, * Numbers are uniformly spread through their whole definition range for integer types,
* and in the [-1:1] range for floating point scalar types. * and in the [-1:1] range for floating point scalar types.
* *
* \not_reentrant
*
* Example: \include MatrixBase_setRandom.cpp * Example: \include MatrixBase_setRandom.cpp
* Output: \verbinclude MatrixBase_setRandom.out * Output: \verbinclude MatrixBase_setRandom.out
* *
@ -131,11 +138,12 @@ inline Derived& DenseBase<Derived>::setRandom()
* and in the [-1:1] range for floating point scalar types. * and in the [-1:1] range for floating point scalar types.
* *
* \only_for_vectors * \only_for_vectors
* \not_reentrant
* *
* Example: \include Matrix_setRandom_int.cpp * Example: \include Matrix_setRandom_int.cpp
* Output: \verbinclude Matrix_setRandom_int.out * Output: \verbinclude Matrix_setRandom_int.out
* *
* \sa MatrixBase::setRandom(), setRandom(Index,Index), class CwiseNullaryOp, MatrixBase::Random() * \sa DenseBase::setRandom(), setRandom(Index,Index), class CwiseNullaryOp, DenseBase::Random()
*/ */
template<typename Derived> template<typename Derived>
EIGEN_STRONG_INLINE Derived& EIGEN_STRONG_INLINE Derived&
@ -150,13 +158,15 @@ PlainObjectBase<Derived>::setRandom(Index newSize)
* Numbers are uniformly spread through their whole definition range for integer types, * Numbers are uniformly spread through their whole definition range for integer types,
* and in the [-1:1] range for floating point scalar types. * and in the [-1:1] range for floating point scalar types.
* *
* \not_reentrant
*
* \param nbRows the new number of rows * \param nbRows the new number of rows
* \param nbCols the new number of columns * \param nbCols the new number of columns
* *
* Example: \include Matrix_setRandom_int_int.cpp * Example: \include Matrix_setRandom_int_int.cpp
* Output: \verbinclude Matrix_setRandom_int_int.out * Output: \verbinclude Matrix_setRandom_int_int.out
* *
* \sa MatrixBase::setRandom(), setRandom(Index), class CwiseNullaryOp, MatrixBase::Random() * \sa DenseBase::setRandom(), setRandom(Index), class CwiseNullaryOp, DenseBase::Random()
*/ */
template<typename Derived> template<typename Derived>
EIGEN_STRONG_INLINE Derived& EIGEN_STRONG_INLINE Derived&

View File

@ -206,6 +206,7 @@ TAB_SIZE = 8
# You can put \n's in the value part of an alias to insert newlines. # You can put \n's in the value part of an alias to insert newlines.
ALIASES = "only_for_vectors=This is only for vectors (either row-vectors or column-vectors), i.e. matrices which are known at compile-time to have either one row or one column." \ ALIASES = "only_for_vectors=This is only for vectors (either row-vectors or column-vectors), i.e. matrices which are known at compile-time to have either one row or one column." \
"not_reentrant=\warning This function is not re-entrant." \
"array_module=This is defined in the %Array module. \code #include <Eigen/Array> \endcode" \ "array_module=This is defined in the %Array module. \code #include <Eigen/Array> \endcode" \
"cholesky_module=This is defined in the %Cholesky module. \code #include <Eigen/Cholesky> \endcode" \ "cholesky_module=This is defined in the %Cholesky module. \code #include <Eigen/Cholesky> \endcode" \
"eigenvalues_module=This is defined in the %Eigenvalues module. \code #include <Eigen/Eigenvalues> \endcode" \ "eigenvalues_module=This is defined in the %Eigenvalues module. \code #include <Eigen/Eigenvalues> \endcode" \
@ -224,6 +225,7 @@ ALIASES = "only_for_vectors=This is only for vectors (either row-
"note_try_to_help_rvo=This function returns the result by value. In order to make that efficient, it is implemented as just a return statement using a special constructor, hopefully allowing the compiler to perform a RVO (return value optimization)." \ "note_try_to_help_rvo=This function returns the result by value. In order to make that efficient, it is implemented as just a return statement using a special constructor, hopefully allowing the compiler to perform a RVO (return value optimization)." \
"nonstableyet=\warning This is not considered to be part of the stable public API yet. Changes may happen in future releases. See \ref Experimental \"Experimental parts of Eigen\" "nonstableyet=\warning This is not considered to be part of the stable public API yet. Changes may happen in future releases. See \ref Experimental \"Experimental parts of Eigen\"
ALIASES += "eigenAutoToc= " ALIASES += "eigenAutoToc= "
ALIASES += "eigenManualPage=\defgroup" ALIASES += "eigenManualPage=\defgroup"

View File

@ -39,6 +39,8 @@ int main(int argc, char** argv)
} }
\endcode \endcode
\warning note that all functions generating random matrices are \b not re-entrant nor thread-safe. Those include DenseBase::Random(), and DenseBase::setRandom() despite a call to Eigen::initParallel(). This is because these functions are based on std::rand which is not re-entrant. For thread-safe random generator, we recommend the use of boost::random of c++11 random feature.
In the case your application is parallelized with OpenMP, you might want to disable Eigen's own parallization as detailed in the previous section. In the case your application is parallelized with OpenMP, you might want to disable Eigen's own parallization as detailed in the previous section.
*/ */