cudaEvent_tとcudaStream_tのラッパを追加
[cuda.git] / libutils / cuda / Event.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_EVENT_H
19 #define FM7b5_CUDA_EVENT_H
20
21 #include <stdexcept>
22 #include <cuda_runtime.h>
23
24 class Event
25 {
26 public:
27         Event();
28         Event(unsigned int flags);
29         ~Event();
30         operator cudaEvent_t();
31
32         cudaError_t query() const;
33         cudaError_t record(cudaStream_t stream = (cudaStream_t)0) const;
34         cudaError_t synchronize() const;
35
36 protected:
37         cudaEvent_t m_event;
38 };
39
40
41 Event::Event()
42 {
43         cudaError_t status;
44
45         status = cudaEventCreate(&m_event);
46
47         if (status != cudaSuccess) {
48                 throw std::runtime_error(cudaGetErrorString(status));
49         }
50 }
51
52 Event::Event(unsigned int flags)
53 {
54         cudaError_t status;
55
56         status = cudaEventCreateWithFlags(&m_event, flags);
57
58         if (status != cudaSuccess) {
59                 throw std::runtime_error(cudaGetErrorString(status));
60         }
61 }
62
63 Event::~Event()
64 {
65         cudaError_t status;
66
67         status = cudaEventDestroy(m_event);
68
69         if (status != cudaSuccess) {
70                 throw std::runtime_error(cudaGetErrorString(status));
71         }
72 }
73
74 Event::operator cudaEvent_t()
75 {
76         return m_event;
77 }
78
79 cudaError_t
80 Event::query() const
81 {
82         return cudaEventQuery(m_event);
83 }
84
85 cudaError_t
86 Event::record(cudaStream_t stream) const
87 {
88         return cudaEventRecord(m_event, stream);
89 }
90
91 cudaError_t
92 Event::synchronize() const
93 {
94         return cudaEventSynchronize(m_event);
95 }
96
97 #endif /* FM7b5_CUDA_EVENT_H */