f750776bcdaadc30ca193e299ae15b3f22117071
[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 <vector>
54
55 #include "gromacs/gpu_utils/device_stream.h"
56 #include "gromacs/gpu_utils/gmxsycl.h"
57 #include "gromacs/utility/exceptions.h"
58 #include "gromacs/utility/gmxassert.h"
59
60 #ifndef DOXYGEN
61
62 class DeviceEvent
63 {
64 public:
65     //! A constructor.
66     DeviceEvent()
67     {
68         doNotSynchronizeBetweenStreams_ = (std::getenv("GMX_GPU_SYCL_NO_SYNCHRONIZE") != nullptr);
69         events_.reserve(1);
70     }
71     //! A constructor from an existing event.
72     DeviceEvent(const cl::sycl::event& event) : events_{ event } {}
73     //! A destructor.
74     ~DeviceEvent() = default;
75     // Disable copy, move, and assignment. They all can be allowed, but not needed yet.
76     DeviceEvent& operator=(const DeviceEvent&) = delete;
77     DeviceEvent(const DeviceEvent&)            = delete;
78     DeviceEvent& operator=(DeviceEvent&&) = delete;
79     DeviceEvent(DeviceEvent&&)            = delete;
80
81     /*! \brief Marks the synchronization point in the \p deviceStream.
82      * Should be called first and then followed by wait() or enqueueWait().
83      */
84     inline void mark(const DeviceStream& deviceStream)
85     {
86 #    if GMX_SYCL_HIPSYCL
87         // Relies on HIPSYCL_EXT_QUEUE_WAIT_LIST extension
88         events_ = deviceStream.stream().get_wait_list();
89 #    else
90         // Relies on SYCL_INTEL_enqueue_barrier
91         events_ = { deviceStream.stream().submit_barrier() };
92 #    endif
93     }
94
95     //! Synchronizes the host thread on the marked event.
96     inline void wait()
97     {
98 #    if GMX_SYCL_DPCPP
99         // Note: this is not to prevent use-before-marking, but for checking the DPC++ vs hipSYCL consistency
100         GMX_ASSERT(events_.size() <= 1, "One event expected in DPC++, but we have several!");
101 #    endif
102         for (auto& event : events_)
103         {
104             event.wait_and_throw();
105         }
106     }
107
108     inline void enqueueWait(const DeviceStream& deviceStream)
109     {
110         if (!doNotSynchronizeBetweenStreams_)
111         {
112 #    if GMX_SYCL_HIPSYCL
113             // Submit an empty kernel that depends on all the events recorded.
114             deviceStream.stream().single_task(events_, [=]() {});
115 #    else
116             GMX_ASSERT(events_.size() <= 1, "One event expected in DPC++, but we have several!");
117             // Relies on SYCL_INTEL_enqueue_barrier extensions
118             deviceStream.stream().submit_barrier(events_);
119 #    endif
120         }
121     }
122
123     //! Checks the completion of the underlying event.
124     inline bool isReady()
125     {
126         bool allReady = std::all_of(events_.begin(), events_.end(), [](cl::sycl::event& event) {
127             auto info       = event.get_info<cl::sycl::info::event::command_execution_status>();
128             bool isComplete = (info == cl::sycl::info::event_command_status::complete);
129             return isComplete;
130         });
131         return allReady;
132     }
133
134     //! Checks whether this object encapsulates an underlying event.
135     inline bool isMarked() { return !events_.empty(); }
136
137     //! Reset the event to unmarked state.
138     inline void reset() { events_.clear(); }
139
140 private:
141     std::vector<cl::sycl::event> events_;
142     /*! \brief Dev. setting to no-op enqueueWait
143      *
144      * In SYCL, dependencies between the GPU tasks are managed by the runtime, so manual
145      * synchronization between GPU streams should be redundant, but we keep it on by default.
146      *
147      * Setting this to \c true via \c GMX_GPU_SYCL_NO_SYNCHRONIZE environment variable will
148      * immediately return from \ref enqueueWaitEvent, without placing a barrier into the stream.
149      */
150     bool doNotSynchronizeBetweenStreams_;
151 };
152
153 #endif // DOXYGEN
154
155 #endif // GMX_GPU_UTILS_DEVICE_EVENT_SYCL_H