-
Notifications
You must be signed in to change notification settings - Fork 457
Expand file tree
/
Copy pathblocksparse.cu
More file actions
297 lines (245 loc) · 10 KB
/
blocksparse.cu
File metadata and controls
297 lines (245 loc) · 10 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
*
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto. Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
*/
#include <random>
#include <memory>
#include <vector>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <cuda_runtime.h>
#include <cutensor.h>
// Handle cuTENSOR errors
#define HANDLE_ERROR(x) \
{ \
const cutensorStatus_t err = (x); \
if ( err != CUTENSOR_STATUS_SUCCESS ) \
{ throw std::runtime_error { std::string { cutensorGetErrorString(err) } }; } \
};
// Handle CUDA errors.
#define HANDLE_CUDA_ERROR(x) \
{ \
const cudaError_t err = (x); \
if ( err != cudaSuccess ) \
{ throw std::runtime_error { std::string { cudaGetErrorString(err) } }; } \
};
template <typename T>
using cuda_ptr = std::unique_ptr<T,decltype(&cudaFree)>;
template <typename T>
cuda_ptr<T> cuda_alloc( size_t count )
{
void* result;
cudaError_t err = cudaMalloc( &result, sizeof(T)*count );
if ( err != cudaSuccess ) throw std::bad_alloc {};
else return cuda_ptr<T> { reinterpret_cast<T*>(result), &cudaFree };
}
// Useful for automatic clearing of resources.
template <typename F>
class Guard
{
F f;
bool invoke;
public:
explicit Guard(F x) noexcept: f(std::move(x)), invoke(true) {}
Guard(Guard &&g) noexcept: f(std::move(g.f)), invoke(g.invoke) { g.invoke=false; }
Guard(const Guard&) = delete;
Guard& operator=(const Guard&) = delete;
~Guard() noexcept
{
if (invoke) f();
}
};
template <class F>
inline Guard<F> finally(const F& f) noexcept
{
return Guard<F>(f);
}
template <class F>
inline Guard<F> finally(F&& f) noexcept
{
return Guard<F>(std::forward<F>(f));
}
std::mt19937 get_seeded_random_engine()
{
using rand_type = std::random_device::result_type;
using mersenne_type = std::mt19937::result_type;
constexpr size_t N = std::mt19937::state_size * sizeof(mersenne_type);
constexpr size_t M = 1 + (N-1)/sizeof(rand_type);
rand_type random_data[M];
std::random_device source;
std::generate(random_data,random_data+M,std::ref(source));
std::seed_seq seed(random_data,random_data+M);
return std::mt19937( seed );
}
int main()
try
{
using ModeType = int32_t;
using ExtentType = int32_t;
using StrideType = int64_t;
using SectionExtentType = int64_t;
// Random number generator.
std::mt19937 eng = get_seeded_random_engine();
std::uniform_real_distribution<double> dist(0.,1.);
auto rand = [&eng,&dist]() { return dist(eng); };
// Initialise the library.
cutensorHandle_t handle;
HANDLE_ERROR(cutensorCreate(&handle));
auto guardHandle = finally( [&handle]() { cutensorDestroy(handle); } );
//////////////////////////////////////
// Example: //
// We compute C_i = A_{kil}B_{kl} //
//////////////////////////////////////
std::vector<ModeType> modeA {'k','i','l'};
std::vector<ModeType> modeB {'k','l'};
std::vector<ModeType> modeC {'i'};
std::unordered_map<ModeType, std::vector<SectionExtentType>> sectionExtents;
sectionExtents['k'] = {10, 10, 15};
sectionExtents['i'] = {20, 20, 25};
sectionExtents['l'] = {30, 30, 35};
// Helper-λ to allocate and initialise block-sparse tensors with random
// data. In this example we use 64-bit double precision numbers.
cutensorDataType_t dataType = CUTENSOR_R_64F;
auto initTensor = [handle,§ionExtents,&rand,dataType]
(
const std::vector<ModeType> &modes,
const std::vector<ExtentType> &nonZeroCoordinates,
cutensorBlockSparseTensorDescriptor_t &desc,
std::vector<void*> &dev
) -> cuda_ptr<double>
{
uint32_t numModes = modes.size();
uint64_t numNonZeroBlocks = nonZeroCoordinates.size() / numModes;
std::vector<uint32_t> numSections;
std::vector<SectionExtentType> extents;
for ( ModeType mode: modes )
{
const std::vector<SectionExtentType> &modeExtents = sectionExtents.at(mode);
numSections.push_back(modeExtents.size());
extents.insert(extents.end(),modeExtents.begin(),modeExtents.end());
}
// We assume packed contiguous storage, column-major order.
// This means that we may pass nullptr for the strides array later.
// The offets are used to set the pointers in the dev vector.
std::vector<StrideType> offsets(numNonZeroBlocks+1); offsets[0]=0;
for ( uint64_t i = 0; i < numNonZeroBlocks; ++i )
{
StrideType size = 1;
for ( uint32_t j = 0; j < numModes; ++j )
size *= sectionExtents.at( modes[j] ).at( nonZeroCoordinates[i*numModes+j] );
offsets[i+1]=offsets[i]+size;
}
const StrideType totalSize { offsets[numNonZeroBlocks] };
cuda_ptr<double> buf { cuda_alloc<double>(totalSize) };
std::vector<double> tmp( totalSize );
std::generate( tmp.begin(), tmp.end(), rand );
HANDLE_CUDA_ERROR(cudaMemcpy(buf.get(),tmp.data(),totalSize*sizeof(double),cudaMemcpyHostToDevice));
tmp.clear();
dev.resize(numNonZeroBlocks);
for ( uint64_t i = 0; i < numNonZeroBlocks; ++i )
dev[i] = buf.get() + offsets[i];
HANDLE_ERROR(cutensorCreateBlockSparseTensorDescriptor
(
handle, &desc,
numModes, numNonZeroBlocks, numSections.data(), extents.data(),
nonZeroCoordinates.data(), nullptr, dataType
));
return buf;
};
//////////////
// Tensor A //
//////////////
std::vector<void*> devA;
cutensorBlockSparseTensorDescriptor_t descA = nullptr;
// Order-3 Tensor ("box"). E.g., one block in each corner.
const std::vector<ExtentType> nonZeroCoordinatesA
{
0, 0, 0, // Block 0.
2, 0, 0, // Block 1.
0, 2, 0, // Block 2.
2, 2, 0, // Block 3.
0, 0, 2, // Block 4.
2, 0, 2, // Block 5.
0, 2, 2, // Block 6.
2, 2, 2 // Block 7.
};
cuda_ptr<double> bufA = initTensor(modeA,nonZeroCoordinatesA,descA,devA);
auto guardDescA = finally( [&descA]() { cutensorDestroyBlockSparseTensorDescriptor(descA); } );
//////////////
// Tensor B //
//////////////
std::vector<void*> devB;
cutensorBlockSparseTensorDescriptor_t descB = nullptr;
// Order-2 Tensor ("matrix"), two blocks
const std::vector<ExtentType> nonZeroCoordinatesB =
{
0, 0, // Block 0.
1, 2 // Block 1.
};
cuda_ptr<double> bufB = initTensor(modeB,nonZeroCoordinatesB,descB,devB);
auto guardDescB = finally( [&descB]() { cutensorDestroyBlockSparseTensorDescriptor(descB); } );
//////////////
// Tensor C //
//////////////
std::vector<void*> devC;
cutensorBlockSparseTensorDescriptor_t descC = nullptr;
// Order-1 Tensor ("vector"), three blocks
// Actually not sparse, we specify full.
const std::vector<ExtentType> nonZeroCoordinatesC =
{
0, // Block 0.
1, // Block 1.
2 // Block 2.
};
cuda_ptr<double> bufC = initTensor(modeC,nonZeroCoordinatesC,descC,devC);
auto guardDescC = finally( [&descC]() { cutensorDestroyBlockSparseTensorDescriptor(descC); } );
///////////////////////////////
// Block-sparse Contraction. //
///////////////////////////////
cutensorOperationDescriptor_t desc;
HANDLE_ERROR(cutensorCreateBlockSparseContraction(handle, &desc,
descA, modeA.data(), CUTENSOR_OP_IDENTITY,
descB, modeB.data(), CUTENSOR_OP_IDENTITY,
descC, modeC.data(), CUTENSOR_OP_IDENTITY,
descC, modeC.data(),
CUTENSOR_COMPUTE_DESC_64F));
auto guardOpDesc = finally( [&desc]() { cutensorDestroyOperationDescriptor(desc); } );
// Currently, block-sparse contraction plans only support default settings.
cutensorPlanPreference_t planPref = nullptr;
// Query workspace estimate. For block-sparse contraction plans, this is estimate is exact.
uint64_t workspaceSize = 0;
const cutensorWorksizePreference_t workspacePref = CUTENSOR_WORKSPACE_DEFAULT;
HANDLE_ERROR(cutensorEstimateWorkspaceSize(handle,desc,planPref,workspacePref,&workspaceSize));
cuda_ptr<char> work = cuda_alloc<char>(workspaceSize);
// Create Contraction Plan
cutensorPlan_t plan;
HANDLE_ERROR(cutensorCreatePlan(handle,&plan,desc,planPref,workspaceSize));
auto guardPlan = finally( [&plan]() { cutensorDestroyPlan(plan); } );
// Execute
cudaStream_t stream;
HANDLE_CUDA_ERROR(cudaStreamCreate(&stream));
auto guardStream = finally( [&stream]() { cudaStreamDestroy(stream); } );
double alpha = 1., beta = 0.;
HANDLE_ERROR(cutensorBlockSparseContract(handle, plan,
(void*) &alpha, (const void *const *) devA.data(), (const void *const *) devB.data(),
(void*) &beta, (const void *const *) devC.data(), ( void *const *) devC.data(),
(void*) work.get(), workspaceSize, stream));
return EXIT_SUCCESS;
}
catch ( std::exception &ex )
{
std::cerr << "Exception caught! Exiting." << std::endl;
std::cerr << ex.what() << std::endl;
return EXIT_FAILURE;
}
catch ( ... )
{
std::cerr << "Unknown exception caught! Exiting." << std::endl;
return EXIT_FAILURE;
}