MpiHelper.h 3.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/
20
21


22
23
24
25
26
27

/** \file MpiHelper.h */

#ifndef AMDIS_MPIHELPER_H
#define AMDIS_MPIHELPER_H

28
#include <mpi.h>
29
30
#include <time.h>
#include <stdlib.h>
31
#include "Global.h"
32

33
namespace AMDiS { namespace Parallel {
34
35

  namespace mpi {
36
    void globalAdd(MPI::Intracomm &mpiComm, double &value);
37

38
39
40
41
    inline void globalAdd(double &value)
    {
      globalAdd(MPI::COMM_WORLD, value);
    }
42
    
43
44
45
46
47
48
    void globalAdd(MPI::Intracomm &mpiComm, int &value);

    inline void globalAdd(int &value)
    {
      globalAdd(MPI::COMM_WORLD, value);
    }
49
50
    
    template<typename T>
51
52
    void globalAdd(T &value) 
    {
53
54
      WARNING("Unknown type for globalAdd. Can not sum up the values of all processors!\n");
    }
55

56
57
58
59
60
61
62
63
    template<typename VectorType>
    inline typename boost::enable_if< mtl::traits::is_vector<VectorType>, void >
    globalAdd(VectorType &value)
    {
      for (size_t i = 0; i < num_rows(value); i++)
	globalAdd(value[i]);
    }

64
65
    void globalMin(double &value);

66
67
    void globalMin(int &value);

68
    template<typename T>
69
70
    void globalMin(T &value) 
    {
71
72
73
      WARNING("Unknown type for globalMin. Can not determine minimal value of all processors!\n");
    }
    
74
    void globalMax(double &value);
75

76
77
    void globalMax(int &value);

78
    template<typename T>
79
80
    void globalMax(T &value) 
    {
81
82
83
      WARNING("Unknown type for globalMax. Can not determine maximal value of all processors!\n");
    }
    
84
    void startRand();
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

    /** \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);
    }
110
111
  }

112
} }
113

114
#endif