-
Notifications
You must be signed in to change notification settings - Fork 3
/
stream.cu
67 lines (52 loc) · 1.48 KB
/
stream.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
#include <cuda.h>
#include <map>
#include <memory>
#include <exception>
#include <stdexcept>
#include "stream.h"
using std::runtime_error;
typedef std::map<int, StreamPtr> StreamMap;
static void deleteStream(cudaStream_t* stream)
{
cudaStreamSynchronize(*stream);
cudaStreamDestroy(*stream);
delete stream;
}
static StreamPtr createStream()
{
cudaStream_t* stream = new cudaStream_t;
cudaError_t err = cudaStreamCreateWithFlags(stream, cudaStreamNonBlocking);
//cudaError_t err = cudaStreamCreateWithFlags(stream, cudaStreamDefault);
if (err != cudaSuccess)
{
delete stream;
throw runtime_error(cudaGetErrorString(err));
}
return StreamPtr(stream, &deleteStream);
}
StreamPtr StreamManager::retrieveStream(int device)
{
if (mode != perTransfer)
{
if (mode == singleStream)
{
device = -1;
}
// Try to find stream in map
StreamMap::iterator lowerBound = streams.lower_bound(device);
if (lowerBound != streams.end() && !(streams.key_comp()(device, lowerBound->first)))
{
return lowerBound->second;
}
// Stream was not found in map, create it and return it
StreamPtr stream = createStream();
streams.insert(lowerBound, StreamMap::value_type(device, stream));
return stream;
}
// Create a new stream every time
return createStream();
}
StreamManager::StreamManager(StreamSharingMode mode)
: mode(mode)
{
}