communicator.cc 3.25 KB
Newer Older
1
2
3
4
5
6
7
/*
 * @file:   communicator.cc
 * @author: Jordan P. Lefebvre, lefebvrejp@ornl.gov
 *
 * Created on July 1, 2013, 12:52 PM
 */
#include <cstdio>
8
9
#include <cstdlib>
#include <cstring>  // strcpy
10
11
12
13
14
15
16
#include <string>
#ifdef _WIN32
#include <direct.h>
#else
#include <unistd.h>
#endif
#include "radixpara/communicator.hh"
17
18
namespace radix {
namespace Private {
19
20
#ifdef USE_MPI

21
22
MPIComm::MPIComm()
    : d_comm(MPI_COMM_WORLD) {}
23

24
25
MPIComm::MPIComm(MPI_Comm comm)
    : d_comm(comm) {}
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

MPIComm::~MPIComm() {}  // destructor

MPI_Comm MPIComm::getRawComm() const { return d_comm; }

int MPIComm::rank() const {
  int result = 0;
  MPI_Comm_rank(d_comm, &result);
  return result;
}  // rank

int MPIComm::size() const {
  int result = 0;
  MPI_Comm_size(d_comm, &result);
  return result;
}  // nodes

std::string MPIComm::host() const {
  char processor_name[MPI_MAX_PROCESSOR_NAME];
  int length = 0;
  MPI_Get_processor_name(processor_name, &length);
  return std::string(processor_name);
}  // host

CommunicatorGroup MPIComm::group() const {
  MPI_Group group;
  MPI_Comm_group(d_comm, &group);
  return MPICommGroup(group);
}  // group

Communicator MPIComm::split(int color) const {
  int key = 0;
  MPI_Comm new_comm;
  MPI_Comm_split(d_comm, color, key, &new_comm);
  return MPIComm(new_comm);
}  // spilt

Communicator MPIComm::split(int color, int key) const {
  MPI_Comm new_comm;
  MPI_Comm_split(d_comm, color, key, &new_comm);
  return MPIComm(new_comm);
}  // spilt

CommunicatorStatus MPIComm::probe(int source, int tag) const {
  MPI_Status status;
  int error = MPI_Probe(source, tag, d_comm, &status);
  CommunicatorStatus cstatus(
      MPICommStatus(status, source, tag, error, MPI_DATATYPE_NULL));
  return cstatus;
}  // probe

CommunicatorRequest MPIComm::iprobe(int source, int tag) const {
  radix_insist(false, "Method has not yet been implemented");
  return CommunicatorRequest();
}  // iprobe

void MPIComm::barrier() const { MPI_Barrier(d_comm); }  // barrier

void MPIComm::abort(int error_code) const {
  MPI_Abort(d_comm, error_code);
}  // abort
87
88
89

#else /** not MPI so SerialComm */

90
SerialComm::SerialComm() {}
91

92
SerialComm::~SerialComm() {}  // destructor
93

94
int SerialComm::rank() const { return 0; }  // rank
95

96
int SerialComm::size() const { return 1; }  // nodes
97

98
99
std::string SerialComm::host() const {
  char *hostname;
100
#ifndef _WIN32
101
102
103
104
105
106
  hostname = new char[256];
  if (gethostname(hostname, 256) != 0) {
    std::strcpy(hostname, "Unknown");
  }
  std::string result(hostname);
  delete[] hostname;
107
#else
108
109
  hostname = getenv("COMPUTERNAME");
  std::string result(hostname);
110
#endif
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
  return result;
}  // host

CommunicatorGroup SerialComm::group() const {
  return CommunicatorGroup();
}  // group

Communicator SerialComm::split(int color) const {
  return SerialComm();
}  // spilt

Communicator SerialComm::split(int color, int key) const {
  return SerialComm();
}  // spilt

CommunicatorStatus SerialComm::probe(int source, int tag) const {
  return CommunicatorStatus();
}  // probe

CommunicatorRequest SerialComm::iprobe(int source, int tag) const {
  return CommunicatorRequest();
}  // iprobe

void SerialComm::barrier() const {}  // barrier

void SerialComm::abort(int error_code) const { std::abort(); }  // abort
137
#endif
138
}  // namespace Private
139
Communicator comm;
140
}  // namespace radix