mpi_partition_print.cpp
Go to the documentation of this file.
1 // Copyright 2019, University of Maryland and the MANGO development team.
2 //
3 // This file is part of MANGO.
4 //
5 // MANGO is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License as
7 // published by the Free Software Foundation, either version 3 of the
8 // License, or (at your option) any later version.
9 //
10 // MANGO is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with MANGO. If not, see
17 // <https://www.gnu.org/licenses/>.
18 
19 #include <iostream>
20 #include <iomanip>
21 #include <stdexcept>
22 #include <fstream>
23 #include <mpi.h>
24 #include "mango.hpp"
25 
26 void mango::MPI_Partition::print() {
27 
28  if (verbose <= 0) return;
29 
30  // Print the processor assignments in order of their rank in mpi_comm_world.
31  const int buffer_length = 1000;
32  char proc_assignments_string[buffer_length];
33  MPI_Status status;
34  sprintf(proc_assignments_string,"Proc%5d of%5d in comm_world is in worker group%5d, has rank%5d of%5d in comm_worker_groups, and has rank%5d of%5d in comm_group_leaders.\n",rank_world, N_procs_world, worker_group, rank_worker_groups, N_procs_worker_groups, rank_group_leaders, N_procs_group_leaders);
35  int tag;
36  MPI_Barrier(comm_world);
37  if (proc0_world) {
38  std::cout << proc_assignments_string;
39  for (tag = 1; tag < N_procs_world; tag++) {
40  MPI_Recv(proc_assignments_string, buffer_length, MPI_CHAR, tag, tag, comm_world, &status);
41  std::cout << proc_assignments_string;
42  }
43  } else {
44  tag = rank_world;
45  MPI_Send(proc_assignments_string, buffer_length, MPI_CHAR, 0, tag, comm_world);
46  }
47 
48 }
49 
50 
51 void mango::MPI_Partition::write(std::string filename) {
52  const int N_data_items = 8;
53  std::string columns[N_data_items] = {"rank_world","N_procs_world","worker_group","N_worker_groups","rank_worker_groups","N_procs_worker_groups","rank_group_leaders","N_procs_group_leaders"};
54  int data[N_data_items] = { rank_world , N_procs_world , worker_group , N_worker_groups , rank_worker_groups , N_procs_worker_groups , rank_group_leaders , N_procs_group_leaders };
55 
56  std::ofstream output_file;
57  int j;
58 
59  if (proc0_world) {
60  // Open the file
61  output_file.open(filename.c_str());
62  if (!output_file.is_open()) {
63  std::cerr << "MPI_Partition output file: " << filename << std::endl;
64  throw std::runtime_error("Error! Unable to open MPI_Partition output file.");
65  }
66 
67  // Write the header line
68  output_file << columns[0];
69  for (j=1; j<N_data_items; j++) output_file << ", " << columns[j];
70  output_file << std::endl;
71  }
72 
73  MPI_Status status;
74  int tag;
75  MPI_Barrier(comm_world);
76  // Each processor sends their data to proc0_world, and proc0_world writes the result to the file in order.
77  if (proc0_world) {
78  write_line(output_file, N_data_items, columns, data);
79  for (tag = 1; tag < N_procs_world; tag++) {
80  MPI_Recv(data, N_data_items, MPI_INT, tag, tag, comm_world, &status);
81  write_line(output_file, N_data_items, columns, data);
82  }
83  } else {
84  tag = rank_world;
85  MPI_Send(data, N_data_items, MPI_INT, 0, tag, comm_world);
86  }
87 
88 
89  if (proc0_world) output_file.close();
90 }
91 
92 
93 void mango::MPI_Partition::write_line(std::ofstream& output_file, int N_data_items, std::string columns[], int data[]) {
94  // This subroutine writes one line of the mango_mpi output file.
95  output_file << std::setw(columns[0].length()) << data[0];
96  for (int j=1; j<N_data_items; j++) output_file << ", " << std::setw(columns[j].length()) << data[j];
97  output_file << std::endl;
98 }
mango.hpp
mango::MPI_Partition::verbose
int verbose
If true, information is printed to stdout that may be useful for debugging.
Definition: mango.hpp:219
mango::MPI_Partition::write
void write(std::string filename)
Write a file with the given filename, showing the worker group assignments and rank of each process i...
Definition: mpi_partition_print.cpp:51