File: mpiworld.cpp

package info (click to toggle)
ctsim 6.0.2-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,868 kB
  • sloc: cpp: 26,967; sh: 7,782; ansic: 1,256; perl: 296; makefile: 148
file content (82 lines) | stat: -rw-r--r-- 2,355 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*****************************************************************************
** FILE IDENTIFICATION
**
**   Name:          mpiworld.cpp
**   Purpose:       MPI Support class
**   Programmer:    Kevin Rosenberg
**   Date Started:  June 2000
**
**  This is part of the CTSim program
**  Copyright (C) 1983-2009 Kevin Rosenberg
**
**  This program is free software; you can redistribute it and/or modify
**  it under the terms of the GNU General Public License (version 2) as
**  published by the Free Software Foundation.
**
**  This program 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 General Public License for more details.
**
**  You should have received a copy of the GNU General Public License
**  along with this program; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
******************************************************************************/

#include <mpi++.h>
#include <mpiworld.h>


MPIWorld::MPIWorld (int& argc, char* const *& argv)
{
  MPI::Init (argc, const_cast<char**&>(argv));
  m_comm = MPI::COMM_WORLD.Dup();
  m_nProcessors = m_comm.Get_size();
  m_myRank = m_comm.Get_rank();
  m_vLocalWorkUnits.reserve (m_nProcessors);
  m_vStartWorkUnit.reserve (m_nProcessors);
  m_vEndWorkUnit.reserve (m_nProcessors);
}


void
MPIWorld::setTotalWorkUnits(int totalWorkUnits)
{
  if (m_nProcessors < 1)
      return;

  int baseLocalWorkUnits = totalWorkUnits / m_nProcessors;
  int remainderWorkUnits = totalWorkUnits % m_nProcessors;

  int currWorkUnits = 0;
  for (int iProc = 0; iProc < m_nProcessors; iProc++) {
    m_vLocalWorkUnits[iProc] = baseLocalWorkUnits;
    if (iProc < remainderWorkUnits)
      m_vLocalWorkUnits[iProc]++;

    m_vStartWorkUnit[iProc] = currWorkUnits;
    m_vEndWorkUnit[iProc] = m_vStartWorkUnit[iProc] + m_vLocalWorkUnits[iProc]  - 1;

    currWorkUnits += m_vLocalWorkUnits[iProc];
  }

}

void
MPIWorld::BcastString (string& str)
{
  int len;

  if (m_myRank == 0)
    len = str.length();
  m_comm.Bcast (&len, 1, MPI::INT, 0);
  char buf [ len + 1];

  if (m_myRank == 0)
    strcpy (buf, str.c_str());

  m_comm.Bcast (buf, len + 1, MPI::CHAR, 0);

  if (m_myRank > 0)
    str = buf;
}