2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
35 /*! \libinternal \file
36 * \brief Implements a GpuEventSynchronizer class for SYCL.
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
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).
46 * \author Erik Lindahl <erik.lindahl@gmail.com>
47 * \author Andrey Alekseenko <al42and@gmail.com>
50 #ifndef GMX_GPU_UTILS_DEVICE_EVENT_SYCL_H
51 #define GMX_GPU_UTILS_DEVICE_EVENT_SYCL_H
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"
68 doNotSynchronizeBetweenStreams_ = (std::getenv("GMX_GPU_SYCL_NO_SYNCHRONIZE") != nullptr);
71 //! A constructor from an existing event.
72 DeviceEvent(const cl::sycl::event& event) : events_{ event } {}
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;
81 /*! \brief Marks the synchronization point in the \p deviceStream.
82 * Should be called first and then followed by wait() or enqueueWait().
84 inline void mark(const DeviceStream& deviceStream)
87 // Relies on HIPSYCL_EXT_QUEUE_WAIT_LIST extension
88 events_ = deviceStream.stream().get_wait_list();
90 // Relies on SYCL_INTEL_enqueue_barrier
91 events_ = { deviceStream.stream().submit_barrier() };
95 //! Synchronizes the host thread on the marked event.
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!");
102 for (auto& event : events_)
104 event.wait_and_throw();
108 inline void enqueueWait(const DeviceStream& deviceStream)
110 if (!doNotSynchronizeBetweenStreams_)
112 # if GMX_SYCL_HIPSYCL
113 // Submit an empty kernel that depends on all the events recorded.
114 deviceStream.stream().single_task(events_, [=]() {});
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_);
123 //! Checks the completion of the underlying event.
124 inline bool isReady()
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);
134 //! Checks whether this object encapsulates an underlying event.
135 inline bool isMarked() { return !events_.empty(); }
137 //! Reset the event to unmarked state.
138 inline void reset() { events_.clear(); }
141 std::vector<cl::sycl::event> events_;
142 /*! \brief Dev. setting to no-op enqueueWait
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.
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.
150 bool doNotSynchronizeBetweenStreams_;
155 #endif // GMX_GPU_UTILS_DEVICE_EVENT_SYCL_H