communicator.cc 3.41 KB
Newer Older
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
/*
 * @file:   communicator.cc
 * @author: Jordan P. Lefebvre, lefebvrejp@ornl.gov
 *
 * Created on July 1, 2013, 12:52 PM
 */
#include <cstdlib>
#include <cstdio>
#include <cstring> // strcpy
#include <string>
#ifdef _WIN32
#include <direct.h>
#else
#include <unistd.h>
#endif
#include "radixpara/communicator.hh"
namespace radix
{
namespace Private {
#ifdef USE_MPI

MPIComm::MPIComm()
    : d_comm(MPI_COMM_WORLD)
{
}

MPIComm::MPIComm(MPI_Comm comm)
    : d_comm(comm)
{
}

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

#else /** not MPI so SerialComm */

SerialComm::SerialComm()
{
}

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

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

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

std::string SerialComm::host() const
{
    char *hostname;
#ifndef _WIN32
    hostname = new char[256];
    if (gethostname(hostname, 256) != 0) {
        std::strcpy(hostname, "Unknown");
    }
    std::string result(hostname);
    delete [] hostname;
#else
    hostname = getenv("COMPUTERNAME");
    std::string result(hostname);
#endif
    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
#endif
}//namespace Private
Communicator comm;
}//namespace radix