cdcead769d629cbb63ee7ff5ddaf44e7e98aea03
[ctsim.git] / tools / mpiworld.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          mpiworld.cpp
5 **   Purpose:       MPI Support class
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  June 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id$
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28 #include <mpi++.h>
29 #include <mpiworld.h>
30
31
32 MPIWorld::MPIWorld (int& argc, char* const *& argv)
33 {
34   MPI::Init (argc, const_cast<char**&>(argv));
35   m_comm = MPI::COMM_WORLD.Dup();
36   m_nProcessors = m_comm.Get_size();
37   m_myRank = m_comm.Get_rank();
38   m_vLocalWorkUnits.reserve (m_nProcessors);
39   m_vStartWorkUnit.reserve (m_nProcessors);
40   m_vEndWorkUnit.reserve (m_nProcessors);
41 }
42
43
44 void
45 MPIWorld::setTotalWorkUnits(int totalWorkUnits)
46 {
47   if (m_nProcessors < 1)
48       return;
49
50   int baseLocalWorkUnits = totalWorkUnits / m_nProcessors;
51   int remainderWorkUnits = totalWorkUnits % m_nProcessors;
52
53   int currWorkUnits = 0;
54   for (int iProc = 0; iProc < m_nProcessors; iProc++) {
55     m_vLocalWorkUnits[iProc] = baseLocalWorkUnits;
56     if (iProc < remainderWorkUnits)
57       m_vLocalWorkUnits[iProc]++;
58
59     m_vStartWorkUnit[iProc] = currWorkUnits;
60     m_vEndWorkUnit[iProc] = m_vStartWorkUnit[iProc] + m_vLocalWorkUnits[iProc]  - 1;
61
62     currWorkUnits += m_vLocalWorkUnits[iProc];
63   }
64
65 }
66
67 void
68 MPIWorld::BcastString (string& str)
69 {
70   int len;
71
72   if (m_myRank == 0)
73     len = str.length();
74   m_comm.Bcast (&len, 1, MPI::INT, 0);
75   char buf [ len + 1];
76
77   if (m_myRank == 0)
78     strcpy (buf, str.c_str());
79
80   m_comm.Bcast (buf, len + 1, MPI::CHAR, 0);
81
82   if (m_myRank > 0)
83     str = buf;
84 }