File: cart_comm.cpp

package info (click to toggle)
esys-particle 2.3.4%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,036 kB
  • ctags: 10,805
  • sloc: cpp: 80,009; python: 5,872; makefile: 1,243; sh: 313; perl: 225
file content (214 lines) | stat: -rw-r--r-- 5,215 bytes parent folder | download
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
83
84
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/////////////////////////////////////////////////////////////
//                                                         //
// Copyright (c) 2003-2014 by The University of Queensland //
// Centre for Geoscience Computing                         //
// http://earth.uq.edu.au/centre-geoscience-computing      //
//                                                         //
// Primary Business: Brisbane, Queensland, Australia       //
// Licensed under the Open Software License version 3.0    //
// http://www.apache.org/licenses/LICENSE-2.0          //
//                                                         //
/////////////////////////////////////////////////////////////


#include "tml/comm/cart_comm.h"
#include "Foundation/console.h"

//--- I/0 ---
#include <iostream>
#include <sstream>

/*!
  Constructor, using an STL vector for boundary conditions and optionally prescribing
  dimensions. Dimensions are choosen according to the size of the communicator
  via MPI_Create_dims.

  \param old_comm the old communicator
  \param dims the dimensions
  \param circular circular boundaries
*/
TML_CartComm::TML_CartComm(TML_Comm* old_comm, vector<unsigned int> dims, vector<bool> circular)
{
  const unsigned int ndims = 3;
  while (dims.size() < ndims)
  {
    dims.push_back((dims.size() > 0 ? 1 : 0));
  }
  int *mpi_dims=new int[ndims];
  for(unsigned int i=0;i<ndims;i++){
    mpi_dims[i]=dims[i];
  }

  // get dims
  MPI_Dims_create(old_comm->size(),ndims,mpi_dims);
  
  console.Debug() << "ndims = " << ndims << "\n";
  std::stringstream str;
  str << "dims = [";
  if (ndims > 0) {
    str << mpi_dims[0];
    for (unsigned int i = 1; i < ndims; i++) {
      str << ", " << mpi_dims[i];
    }
  }
  str << "]";
  console.Debug() << str.str() << "\n";

  for(unsigned int i=0;i<ndims;i++){ 
    m_dims.push_back(mpi_dims[i]);
  }
  // C-array boundary conditions
  int *l_circ=new int[ndims];

  // fill in
  for(unsigned int i=0;i<ndims;i++){
    l_circ[i]=circular[i];
  }

  // generate new communicator
  MPI_Cart_create(old_comm->comm(), ndims, mpi_dims, l_circ, 0, &m_comm);
  m_ndims=ndims;

  // clean up
  delete [] mpi_dims;
  delete [] l_circ;
}


/*!
  Constructor, using STL vectors for dimensions and boundary conditions 

  \param old_comm the old communicator
  \param ndims the number of dimensions
  \param dims the dimensions
  \param circular circular boundaries
*/
TML_CartComm::TML_CartComm(TML_Comm* old_comm, unsigned int ndims, const vector<int> &dims, const vector<bool> &circular)
{
  // do checks
  if((dims.size()!=ndims) || (circular.size()!=ndims)){
    std::cerr << "wrong nr. of dims in TML_CartComm constructor" << std::endl;
    exit(1);
  } 
  m_dims=dims;
  int nelem=1;
  for(std::vector<int>::const_iterator iter=dims.begin();
      iter!=dims.end();
      iter++){
    nelem*=*iter;
  }
  if(nelem!=old_comm->size()){
    std::cerr << "wrong nr. of processes TML_CartComm constructor" << std::endl;
    exit(1);
  }

  // C-arrays for dims and boundary conditions
  int *l_dims=new int[ndims];
  int *l_circ=new int[ndims];

  // fill in
  for(unsigned int i=0;i<ndims;i++){
    l_dims[i]=dims[i];
    l_circ[i]=circular[i];
  }

  // construct the new communicator
  MPI_Cart_create(old_comm->comm(),ndims,l_dims,l_circ,0,&m_comm);
  m_ndims=ndims;

  delete [] l_dims;
  delete [] l_circ;
}

/*!
  Constructor, using C arrays for dimensions and boundary conditions 

  \param old_comm the old communicator
  \param ndims the number of dimensions
  \param dims the dimensions
  \param circular circular boundaries
  \warning no checking of nr. of dims and boundary cond.
*/
TML_CartComm::TML_CartComm(TML_Comm* old_comm,unsigned int ndims,int* dims,int* circular)
{
  int nelem=1;

  // check number of processes
  for(unsigned int i=0;i<ndims;i++){
    nelem*=dims[i];
  }
  for(unsigned int i=0;i<ndims;i++){ 
    m_dims.push_back(dims[i]);
  }

  if(nelem!=old_comm->size()){
    std::cerr << "wrong nr. of processes TML_CartComm constructor" << std::endl;
    exit(1);
  }

  // generate new communicator
  MPI_Cart_create(old_comm->comm(),ndims,dims,circular,0,&m_comm);
  m_ndims=ndims;
}

/*!
  Get cartesian coordinates of a given process in the communicator

  \param rank the rank of the process
*/
vector<int> TML_CartComm::get_coords(int rank) 
{
  int *coords=new int[m_ndims];
  
  MPI_Cart_coords(m_comm,rank,m_ndims,coords);
  
  vector<int> res=vector<int>(coords,&(coords[m_ndims]));
  delete [] coords;

  return res;
}

/*!
  Get cartesian coordinates of local process
*/
vector<int> TML_CartComm::get_coords() const
{
  int *coords=new int[m_ndims];
  
  MPI_Cart_coords(m_comm,rank(),m_ndims,coords);
  
  vector<int> res=vector<int>(coords,&(coords[m_ndims]));
  delete [] coords;
  
  return res;
} 
 
/*!
  Get  size of the communicator in all directions
*/
vector<int> TML_CartComm::get_all_dims() const
{
  vector<int> res;

  for(int i=0;i<m_ndims;i++){
    res.push_back(m_dims[i]);
  }

  return res; 
}

/*!
  get size of communicator in direction i

  \param i the number of the direction 
*/
int TML_CartComm::get_dim(int i)
{
  int res;
  if((0<=i)&&(i<m_ndims)){
    res=m_dims[i];
  }else{
    res=0;
  }
  return res;
}