-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmessageBox.h
144 lines (127 loc) · 4.76 KB
/
messageBox.h
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
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Yichao Cheng
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* Message box
*
* Author: Yichao Cheng ([email protected])
* Created on: 2014-11-28
* Last Modified: 2014-11-29
*/
#ifndef MESSAGE_BOX_H
#define MESSAGE_BOX_H
#include "common.h"
/**
* MessageBox does not utilize GRD since the buffer only lives on host.
* MessageBox uses host pinned memory, which is accessible by all CUDA contexts.
* Contexts communicates with each other via asynchronized peer-to-peer access.
*
* A double-buffering method is used to enable the overlap of computation
* and communication. In a super step, remote partition is allowed to copy
* message to local
*
* The pointer to message box can be accessed by GPU and CPU,
* @tparam MessageValue The data type for message contained in the box.
*/
template<typename MessageValue>
class MessageBox {
public:
MessageValue *buffer;
// MessageValue *bufferRecv; * Using a double-buffering method.
size_t maxLength; /** Maximum length of the buffer */
size_t length; /** Current capacity of the message box. */
/**
* Constructor. `deviceId < 0` if there is no memory reserved
* It is important to give a NULL value to avoid delete a effective pointer.
*/
MessageBox(): maxLength(0), length(0), buffer(NULL) {}
/** Allocating space for the message box */
void reserve(size_t len) {
assert(len > 0);
maxLength = len;
length = 0;
CUDA_CHECK(cudaMallocHost(reinterpret_cast<void **>(&buffer),
len * sizeof(MessageValue),
cudaHostAllocPortable));
// CUDA_CHECK(cudaMallocHost(reinterpret_cast<void **>(&bufferRecv),
// len * sizeof(MessageValue),
// cudaHostAllocPortable));
}
/**
* Copies the content of a remote message box to the `bufferRecv`.
* So that the local partition can still work on the `buffer` in computation
* stage.
* Uses asynchronous memory copy to hide the memory latency with computation.
* Assumes the peer-to-peer access is already enabled.
*
* If receives from an empty messagebox, the length is 0.
* @param other The message box to copy.
*
* @stream stream The stream to perform this copy within.
*/
inline void recvMsgs(const MessageBox &other, cudaStream_t stream = 0) {
assert(other.length <= maxLength);
// The length should also be copied asynchronously.
CUDA_CHECK(cudaMemcpyAsync(&length,
&other.length,
sizeof(size_t),
cudaMemcpyDefault,
stream));
CUDA_CHECK(cudaMemcpyAsync(buffer,
other.buffer,
length * sizeof(MessageValue),
cudaMemcpyDefault,
stream));
}
/**
* Exchanges two buffers.
*/
// inline void swapBuffers() {
// MessageValue *temp = buffer;
// buffer = bufferRecv;
// bufferRecv = temp;
// }
inline void clear() {
length = 0;
}
inline void print() {
for (int i = 0; i < length; i++) {
buffer[i].print();
}
printf("\n");
}
/** Deletes the buffer */
void del() {
if (buffer) {
CUDA_CHECK(cudaFreeHost(buffer));
}
// if (bufferRecv) {
// CUDA_CHECK(cudaFreeHost(bufferRecv));
// }
}
/** Destructor */
~MessageBox() {
del();
}
};
#endif // MESSAGE_BOX_H