Skip to content
Snippets Groups Projects
Forked from iwr / amdis
1099 commits behind the upstream repository.
MpiHelper.h 2.33 KiB
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ==  http://www.amdis-fem.org                                              ==
// ==                                                                        ==
// ============================================================================
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.



/** \file MpiHelper.h */

#ifndef AMDIS_MPIHELPER_H
#define AMDIS_MPIHELPER_H

#ifdef HAVE_PARALLEL_DOMAIN_AMDIS

#include <time.h>
#include <stdlib.h>
#include <mpi.h>

namespace AMDiS {

  namespace mpi {
    void globalAdd(double &value);

    void globalAdd(int &value);

    void globalMin(double &value);

    void globalMin(int &value);

    void globalMax(double &value);

    void globalMax(int &value);

    inline void startRand()
    {
      srand(time(NULL) * (MPI::COMM_WORLD.Get_rank() + 1));
    }

    /** \brief
     * In many situations a rank computes a number of local DOFs. Then all
     * ranks want to know the number of global DOFs and the starting 
     * displacment number of the DOF numbering in each rank.
     *
     * \param[in]   mpiComm        The MPI communicator.
     * \param[in]   nRankDofs      The number of local DOFs.
     * \param[out]  rStartDofs     Displacment of the DOF numbering. On rank n
     *                             this is the sum of all local DOF numbers in
     *                             ranks 0 to n - 1.
     * \param[out]  nOverallDofs   Global sum of nRankDofs. Is equal on all
     *                             ranks.
     */
    inline void getDofNumbering(MPI::Intracomm& mpiComm,
				int nRankDofs, 
				int& rStartDofs, 
				int& nOverallDofs)
    {
      rStartDofs = 0;
      nOverallDofs = 0;
      mpiComm.Scan(&nRankDofs, &rStartDofs, 1, MPI_INT, MPI_SUM);
      rStartDofs -= nRankDofs;
      mpiComm.Allreduce(&nRankDofs, &nOverallDofs, 1, MPI_INT, MPI_SUM);
    }
  }

}

#endif

#endif