-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_list.cu
241 lines (213 loc) · 6.76 KB
/
index_list.cu
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// ICON
//
// ---------------------------------------------------------------
// Copyright (C) 2004-2024, DWD, MPI-M, DKRZ, KIT, ETH, MeteoSwiss
// Contact information: icon-model.org
//
// See AUTHORS.TXT for a list of authors
// See LICENSES/ for license information
// SPDX-License-Identifier: BSD-3-Clause
// ---------------------------------------------------------------
#include "index_list.h"
#include <cuda.h>
#include <cub/device/device_select.cuh>
#include <cub/iterator/counting_input_iterator.cuh>
#include <unordered_map>
#include <memory>
// Use stream-ordered allocs if we're capturing a graph
namespace {
bool isStreamCapturing(gpuStream_t stream) {
cudaStreamCaptureStatus captureStatus;
cudaStreamIsCapturing(stream, &captureStatus);
return captureStatus != cudaStreamCaptureStatusNone;
}
}
class Storage {
public:
virtual void requestSize(size_t requestedSize) = 0;
int* getNvalidPtr() {
return reinterpret_cast<int*>(data);
}
char* getScratchPtr() {
return data + alignment;
}
virtual ~Storage() = default;
protected:
char* data = nullptr;
static const int alignment = 512;
};
class AsyncStorage: public Storage {
public:
AsyncStorage(gpuStream_t stream) :
stream(stream) { }
void requestSize(size_t requestedSize) override final {
if (data != nullptr) {
cudaFreeAsync(data, stream);
}
cudaMallocAsync(&data, alignment+requestedSize, stream);
}
~AsyncStorage() override {
cudaFreeAsync(data, stream);
}
private:
gpuStream_t stream;
};
class SyncStorage : public Storage {
public:
void requestSize(size_t requestedSize) override final {
if (curSize < requestedSize+alignment) {
cudaFree(data);
cudaMalloc(&data, requestedSize+alignment);
curSize = requestedSize+alignment;
}
}
~SyncStorage() override {
cudaFree(data);
}
private:
size_t curSize = 0;
};
std::unordered_map<gpuStream_t, std::shared_ptr<SyncStorage>> syncStorageMap;
template<typename T>
struct ZeroCmp
{
const T* conditions;
const int startid;
ZeroCmp(const int startid, const T* conditions) :
startid(startid), conditions(conditions)
{ }
__device__ __host__ __forceinline__
bool operator() (const int &id)
{
return (conditions[ id - startid ] != 0);
}
};
template <typename T>
static
void c_generate_index_list_gpu_generic_device(
const T* dev_conditions,
const int startid, const int endid,
int* dev_indices,
int* dev_nvalid, gpuStream_t stream)
{
const int n = endid - startid + 1;
// Argument is the offset of the first element
cub::CountingInputIterator<int> iterator(startid);
// Determine temporary device storage requirements
size_t storageRequirement;
cub::DeviceSelect::Flagged(nullptr, storageRequirement,
iterator, dev_conditions, dev_indices,
dev_nvalid, n, stream);
// Allocate temporary storage
// Use async storage in case we're capturing a graph
// otherwise the sync storage per-stream
std::shared_ptr<Storage> storage;
if (isStreamCapturing(stream)) {
storage = std::make_shared<AsyncStorage>(stream);
} else {
if (syncStorageMap.find(stream) == syncStorageMap.end()) {
syncStorageMap[stream] = std::make_shared<SyncStorage>();
}
storage = syncStorageMap[stream];
}
storage->requestSize(storageRequirement);
if (dev_nvalid == nullptr) {
dev_nvalid = storage->getNvalidPtr();
}
ZeroCmp<T> select(startid, dev_conditions);
cub::DeviceSelect::If(
storage->getScratchPtr(), storageRequirement,
iterator, dev_indices,
dev_nvalid, n,
select, stream);
}
template <typename T>
static
void c_generate_index_list_gpu_batched_generic(
const int batch_size,
const T* dev_conditions, const int cond_stride,
const int startid, const int endid,
int* dev_indices, const int idx_stride,
int* dev_nvalid, gpuStream_t stream)
{
for (int i = 0; i < batch_size; i++)
c_generate_index_list_gpu_generic_device(
dev_conditions + cond_stride*i,
startid, endid,
dev_indices + idx_stride*i,
dev_nvalid + i, stream);
}
template <typename T>
static
void c_generate_index_list_gpu_generic(
const T* dev_conditions,
const int startid, const int endid,
int* dev_indices, int* ptr_nvalid,
bool copy_to_host, gpuStream_t stream)
{
int* local_dev_nvalid = nullptr;
c_generate_index_list_gpu_generic_device(
dev_conditions, startid, endid, dev_indices,
copy_to_host ? local_dev_nvalid : ptr_nvalid, stream);
if (copy_to_host) {
cudaMemcpyAsync(ptr_nvalid, local_dev_nvalid, sizeof(int), cudaMemcpyDeviceToHost, stream);
cudaStreamSynchronize(stream);
}
}
///
/// Exposed functions
///
/// Non-batched first
///
void c_generate_index_list_gpu_single(
const void* dev_conditions,
const int startid, const int endid,
int* dev_indices, int* nvalid,
int data_size, bool copy_to_host,
gpuStream_t stream)
{
switch (data_size) {
case 1:
c_generate_index_list_gpu_generic(
static_cast<const char*>(dev_conditions),
startid, endid, dev_indices, nvalid, copy_to_host, stream);
break;
case 4:
c_generate_index_list_gpu_generic(
static_cast<const int*> (dev_conditions),
startid, endid, dev_indices, nvalid, copy_to_host, stream);
break;
}
}
///
/// And now batched
///
void c_generate_index_list_gpu_batched(
const int batch_size,
const void* dev_conditions, const int cond_stride,
const int startid, const int endid,
int* dev_indices, const int idx_stride,
int* dev_nvalid, int data_size,
gpuStream_t stream)
{
switch (data_size) {
case 1:
c_generate_index_list_gpu_batched_generic(
batch_size,
static_cast<const char*>(dev_conditions),
cond_stride,
startid, endid,
dev_indices, idx_stride,
dev_nvalid, stream);
break;
case 4:
c_generate_index_list_gpu_batched_generic(
batch_size,
static_cast<const int*> (dev_conditions),
cond_stride,
startid, endid,
dev_indices, idx_stride,
dev_nvalid, stream);
break;
}
}