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