clean a bit the parallelizer

This commit is contained in:
Gael Guennebaud 2010-02-22 11:08:37 +01:00
parent b20935be9b
commit 3e62fafce8
3 changed files with 71 additions and 42 deletions

View File

@ -89,6 +89,14 @@
#endif
#endif
#ifdef _OPENMP
#define EIGEN_HAS_OPENMP
#endif
#ifdef EIGEN_HAS_OPENMP
#include <omp.h>
#endif
#include <cstdlib>
#include <cmath>
#include <complex>
@ -209,6 +217,7 @@ struct Dense {};
#include "src/Core/TriangularMatrix.h"
#include "src/Core/SelfAdjointView.h"
#include "src/Core/SolveTriangular.h"
#include "src/Core/products/Parallelizer.h"
#include "src/Core/products/CoeffBasedProduct.h"
#include "src/Core/products/GeneralBlockPanelKernel.h"
#include "src/Core/products/GeneralMatrixVector.h"

View File

@ -128,30 +128,10 @@ struct ei_traits<GeneralProduct<Lhs,Rhs,GemmProduct> >
: ei_traits<ProductBase<GeneralProduct<Lhs,Rhs,GemmProduct>, Lhs, Rhs> >
{};
template<bool Prallelize,typename Functor>
void ei_multithreaded_product(const Functor& func, int size)
template<typename Scalar, typename Gemm, typename Lhs, typename Rhs, typename Dest>
struct ei_gemm_functor
{
if(!Prallelize)
return func(0,size);
#ifdef OMP
int threads = omp_get_num_procs();
#else
int threads = 1;
#endif
int blockSize = size / threads;
#pragma omp parallel for schedule(static,1)
for(int i=0; i<threads; ++i)
{
int blockStart = i*blockSize;
int actualBlockSize = std::min(blockSize, size - blockStart);
func(blockStart, actualBlockSize);
}
}
template<typename Scalar, typename Gemm, typename Lhs, typename Rhs, typename Dest> struct ei_gemm_callback
{
ei_gemm_callback(const Lhs& lhs, const Rhs& rhs, Dest& dest, Scalar actualAlpha)
ei_gemm_functor(const Lhs& lhs, const Rhs& rhs, Dest& dest, Scalar actualAlpha)
: m_lhs(lhs), m_rhs(rhs), m_dest(dest), m_actualAlpha(actualAlpha)
{}
@ -194,28 +174,18 @@ class GeneralProduct<Lhs, Rhs, GemmProduct>
Scalar actualAlpha = alpha * LhsBlasTraits::extractScalarFactor(m_lhs)
* RhsBlasTraits::extractScalarFactor(m_rhs);
typedef ei_gemm_callback<Scalar,ei_general_matrix_matrix_product<
Scalar,
(_ActualLhsType::Flags&RowMajorBit) ? RowMajor : ColMajor, bool(LhsBlasTraits::NeedToConjugate),
(_ActualRhsType::Flags&RowMajorBit) ? RowMajor : ColMajor, bool(RhsBlasTraits::NeedToConjugate),
(Dest::Flags&RowMajorBit) ? RowMajor : ColMajor>,
_ActualLhsType, _ActualRhsType, Dest> Functor;
#ifdef OMP
ei_multithreaded_product<true>(Functor(lhs, rhs, dst, actualAlpha), this->cols());
#else
typedef ei_gemm_functor<
Scalar,
ei_general_matrix_matrix_product<
Scalar,
(_ActualLhsType::Flags&RowMajorBit) ? RowMajor : ColMajor, bool(LhsBlasTraits::NeedToConjugate),
(_ActualRhsType::Flags&RowMajorBit) ? RowMajor : ColMajor, bool(RhsBlasTraits::NeedToConjugate),
(Dest::Flags&RowMajorBit) ? RowMajor : ColMajor>
::run(
this->rows(), this->cols(), lhs.cols(),
(const Scalar*)&(lhs.const_cast_derived().coeffRef(0,0)), lhs.stride(),
(const Scalar*)&(rhs.const_cast_derived().coeffRef(0,0)), rhs.stride(),
(Scalar*)&(dst.coeffRef(0,0)), dst.stride(),
actualAlpha);
#endif
(Dest::Flags&RowMajorBit) ? RowMajor : ColMajor>,
_ActualLhsType,
_ActualRhsType,
Dest> Functor;
ei_run_parallel_1d<true>(Functor(lhs, rhs, dst, actualAlpha), this->cols());
}
};

View File

@ -0,0 +1,50 @@
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2010 Gael Guennebaud <g.gael@free.fr>
//
// Eigen is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// Alternatively, you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License and a copy of the GNU General Public License along with
// Eigen. If not, see <http://www.gnu.org/licenses/>.
#ifndef EIGEN_PARALLELIZER_H
#define EIGEN_PARALLELIZER_H
template<bool Parallelize,typename Functor>
void ei_run_parallel_1d(const Functor& func, int size)
{
#ifndef EIGEN_HAS_OPENMP
func(0,size);
#else
if(!Parallelize)
return func(0,size);
int threads = omp_get_num_procs();
int blockSize = size / threads;
#pragma omp parallel for schedule(static,1)
for(int i=0; i<threads; ++i)
{
int blockStart = i*blockSize;
int actualBlockSize = std::min(blockSize, size - blockStart);
func(blockStart, actualBlockSize);
}
#endif
}
#endif // EIGEN_GENERAL_MATRIX_MATRIX_H