cudaEvent_tとcudaStream_tのラッパを追加
[cuda.git] / libutils / cuda / Stream.h
1 /*
2         Copyright (C) 2012, 2013  fmaj7b5.info
3
4         This program is free software: you can redistribute it and/or modify
5         it under the terms of the GNU General Public License as published by
6         the Free Software Foundation, either version 2 of the License, or
7         (at your option) any later version.
8
9         This program is distributed in the hope that it will be useful,
10         but WITHOUT ANY WARRANTY; without even the implied warranty of
11         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12         GNU General Public License for more details.
13
14         You should have received a copy of the GNU General Public License
15         along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #ifndef FM7b5_CUDA_STREAM_H
19 #define FM7b5_CUDA_STREAM_H
20
21 #include <stdexcept>
22 #include <cuda_runtime.h>
23
24 class Stream
25 {
26 public:
27         Stream();
28         ~Stream();
29         operator cudaStream_t();
30
31         cudaError_t query() const;
32         cudaError_t synchronize() const;
33         cudaError_t wait_event(cudaEvent_t event, unsigned int flags) const;
34
35 protected:
36         cudaStream_t m_stream;
37 };
38
39 Stream::Stream()
40 {
41         cudaError_t status;
42
43         status = cudaStreamCreate(&m_stream);
44
45         if (status != cudaSuccess) {
46                 throw std::runtime_error(cudaGetErrorString(status));
47         }
48 }
49
50 Stream::~Stream()
51 {
52         cudaError_t status;
53
54         status = cudaStreamDestroy(m_stream);
55
56         if (status != cudaSuccess) {
57                 throw std::runtime_error(cudaGetErrorString(status));
58         }
59 }
60
61 Stream::operator cudaStream_t()
62 {
63         return m_stream;
64 }
65
66 cudaError_t
67 Stream::query() const
68 {
69         return cudaStreamQuery(m_stream);
70 }
71
72 cudaError_t
73 Stream::synchronize() const
74 {
75         return cudaStreamSynchronize(m_stream);
76
77 }
78
79 cudaError_t
80 Stream::wait_event(cudaEvent_t event, unsigned int flags) const
81 {
82         return cudaStreamWaitEvent(m_stream, event, flags);
83 }
84
85 #endif /* FM7b5_CUDA_STREAM_H */