Simpatico  v1.10
MpiSendRecv.cpp
1 #ifdef UTIL_MPI
2 
3 #include <string.h>
4 /*
5 * Util Package - C++ Utilities for Scientific Computation
6 *
7 * Copyright 2010 - 2017, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include "MpiSendRecv.h"
12 
13 namespace Util
14 {
15 
16  // bool explicit specializations
17 
18  template <>
19  void send<bool>(MPI::Comm& comm, bool& data, int dest, int tag)
20  {
21  int value = data ? 1 : 0;
22  send<int>(comm, value, dest, tag);
23  }
24 
25  template <>
26  void recv<bool>(MPI::Comm& comm, bool& data, int source, int tag)
27  {
28  int value;
29  recv<int>(comm, value, source, tag);
30  data = value ? true : false;
31  }
32 
33  template <>
34  void bcast<bool>(MPI::Intracomm& comm, bool& data, int root)
35  {
36  int value;
37  int rank = comm.Get_rank();
38  if (rank == root)
39  value = data ? 1 : 0;
40  bcast<int>(comm, value, root);
41  if (rank != root)
42  data = value ? true : false;
43  }
44 
45  // std::string explicit specializations
46 
47  template <>
48  void send<std::string>(MPI::Comm& comm, std::string& data, int dest, int tag)
49  {
50 
51  // Send size of char C array
52  int count = data.size() + 1;
53  send<int>(comm, count, dest, tag);
54 
55  // Send string contents as char array
56  char* cstr = new char[count];
57  strcpy(cstr, data.c_str());
58  send<char>(comm, cstr, count, dest, tag);
59  delete [] cstr;
60 
61  }
62 
63  template <>
64  void recv<std::string>(MPI::Comm& comm, std::string& data, int source, int tag)
65  {
66 
67  // Receive size of char C array
68  int count;
69  recv<int>(comm, count, source, tag);
70 
71  // Receive contents as char C array
72  char* cstr = new char[count];
73  recv<char>(comm, cstr, count, source, tag);
74  data = cstr;
75  delete [] cstr;
76 
77  }
78 
79  template <>
80  void bcast<std::string>(MPI::Intracomm& comm, std::string& data, int root)
81  {
82  int rank = comm.Get_rank();
83  int count;
84 
85  // Broadcast string count
86  if (rank == root)
87  count = data.size() + 1;
88  bcast<int>(comm, count, root);
89 
90  // Broadcast std::string contents as C string
91  char* cstr = new char[count];
92  if (rank == root)
93  strcpy(cstr, data.c_str());
94  bcast<char>(comm, cstr, count, root);
95  if (rank != root)
96  data = cstr;
97  delete [] cstr;
98 
99  }
100 
101 }
102 #endif
void send< bool >(MPI::Comm &comm, bool &data, int dest, int tag)
Explicit specialization of send for bool data.
Definition: MpiSendRecv.cpp:19
void bcast< bool >(MPI::Intracomm &comm, bool &data, int root)
Explicit specialization of bcast for bool data.
Definition: MpiSendRecv.cpp:34
Utility classes for scientific computation.
Definition: accumulators.mod:1
This file contains templates for global functions send<T>, recv<T> and bcast<T>.
void recv< bool >(MPI::Comm &comm, bool &data, int source, int tag)
Explicit specialization of recv for bool data.
Definition: MpiSendRecv.cpp:26