Unify handling of GMX_ENABLE_GPU_TIMING and GMX_DISABLE_GPU_TIMING
[alexxy/gromacs.git] / src / gromacs / gpu_utils / device_event_sycl.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2020,2021, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \libinternal \file
36  *  \brief Implements a GpuEventSynchronizer class for SYCL.
37  *
38  *  This implementation relies on SYCL_INTEL_enqueue_barrier proposal,
39  *  https://github.com/intel/llvm/blob/sycl/sycl/doc/extensions/EnqueueBarrier/enqueue_barrier.asciidoc
40  *
41  *  Using event-based synchronization is not recommended for SYCL.
42  *  SYCL queues are out-of-order and rely on data dependencies, allowing only to wait
43  *  for a specific kernel (by capturing the \c event returned from \c queue.submit) or for all
44  *  the tasks in the queue (\c queue.wait).
45  *
46  *  \author Erik Lindahl <erik.lindahl@gmail.com>
47  *  \author Andrey Alekseenko <al42and@gmail.com>
48  * \inlibraryapi
49  */
50 #ifndef GMX_GPU_UTILS_DEVICE_EVENT_SYCL_H
51 #define GMX_GPU_UTILS_DEVICE_EVENT_SYCL_H
52
53 #include <algorithm>
54 #include <vector>
55
56 #include "gromacs/gpu_utils/device_stream.h"
57 #include "gromacs/gpu_utils/gmxsycl.h"
58 #include "gromacs/utility/exceptions.h"
59 #include "gromacs/utility/gmxassert.h"
60
61 #ifndef DOXYGEN
62
63 class DeviceEvent
64 {
65 public:
66     //! A constructor.
67     DeviceEvent()
68     {
69         doNotSynchronizeBetweenStreams_ = (std::getenv("GMX_GPU_SYCL_NO_SYNCHRONIZE") != nullptr);
70         events_.reserve(1);
71     }
72     //! A constructor from an existing event.
73     DeviceEvent(const cl::sycl::event& event) : events_{ event } {}
74     //! A destructor.
75     ~DeviceEvent() = default;
76     // Disable copy, move, and assignment. They all can be allowed, but not needed yet.
77     DeviceEvent& operator=(const DeviceEvent&) = delete;
78     DeviceEvent(const DeviceEvent&)            = delete;
79     DeviceEvent& operator=(DeviceEvent&&) = delete;
80     DeviceEvent(DeviceEvent&&)            = delete;
81
82     /*! \brief Marks the synchronization point in the \p deviceStream.
83      * Should be called first and then followed by wait() or enqueueWait().
84      */
85     inline void mark(const DeviceStream& deviceStream)
86     {
87 #    if GMX_SYCL_HIPSYCL
88         // Relies on HIPSYCL_EXT_QUEUE_WAIT_LIST extension
89         events_   = deviceStream.stream().get_wait_list();
90         isMarked_ = true;
91 #    else
92         // Relies on SYCL_INTEL_enqueue_barrier
93         events_ = { deviceStream.stream().submit_barrier() };
94 #    endif
95     }
96
97     //! Synchronizes the host thread on the marked event.
98     inline void wait()
99     {
100 #    if GMX_SYCL_DPCPP
101         // Note: this is not to prevent use-before-marking, but for checking the DPC++ vs hipSYCL consistency
102         GMX_ASSERT(events_.size() <= 1, "One event expected in DPC++, but we have several!");
103 #    endif
104         for (auto& event : events_)
105         {
106             event.wait_and_throw();
107         }
108     }
109
110     inline void enqueueWait(const DeviceStream& deviceStream)
111     {
112         if (!doNotSynchronizeBetweenStreams_)
113         {
114 #    if GMX_SYCL_HIPSYCL
115             // Submit an empty kernel that depends on all the events recorded.
116             deviceStream.stream().single_task(events_, [=]() {});
117 #    else
118             GMX_ASSERT(events_.size() <= 1, "One event expected in DPC++, but we have several!");
119             // Relies on SYCL_INTEL_enqueue_barrier extensions
120             deviceStream.stream().submit_barrier(events_);
121 #    endif
122         }
123     }
124
125     //! Checks the completion of the underlying event.
126     inline bool isReady()
127     {
128         bool allReady = std::all_of(events_.begin(), events_.end(), [](cl::sycl::event& event) {
129             auto info       = event.get_info<cl::sycl::info::event::command_execution_status>();
130             bool isComplete = (info == cl::sycl::info::event_command_status::complete);
131             return isComplete;
132         });
133         return allReady;
134     }
135
136     //! Checks whether this object encapsulates an underlying event.
137     inline bool isMarked() const
138     {
139 #    if GMX_SYCL_HIPSYCL
140         return isMarked_;
141 #    else
142         return !events_.empty();
143 #    endif
144     }
145
146     //! Reset the event to unmarked state.
147     inline void reset()
148     {
149         events_.clear();
150 #    if GMX_SYCL_HIPSYCL
151         isMarked_ = false;
152 #    endif
153     }
154
155 private:
156     std::vector<cl::sycl::event> events_;
157 #    if GMX_SYCL_HIPSYCL
158     /*! \brief Flag to track event marking in hipSYCL.
159      *
160      * In hipSYCL, we can have empty \ref events_ after marking if there were no pending tasks in
161      * the queue. So, we use an explicit flag to check the event state. */
162     bool isMarked_ = false;
163 #    endif
164     /*! \brief Dev. setting to no-op enqueueWait
165      *
166      * In SYCL, dependencies between the GPU tasks are managed by the runtime, so manual
167      * synchronization between GPU streams should be redundant, but we keep it on by default.
168      *
169      * Setting this to \c true via \c GMX_GPU_SYCL_NO_SYNCHRONIZE environment variable will
170      * immediately return from \ref enqueueWaitEvent, without placing a barrier into the stream.
171      */
172     bool doNotSynchronizeBetweenStreams_;
173 };
174
175 #endif // DOXYGEN
176
177 #endif // GMX_GPU_UTILS_DEVICE_EVENT_SYCL_H