r90: Convert MPI structure to C++ class
[ctsim.git] / src / 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: mpiworld.cpp,v 1.1 2000/06/09 01:35:33 kevin Exp $
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**& argv)
33 {
34   MPI::Init (argc, 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