Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-13 08:27:51

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file accel/detail/OffloadWriter.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <memory>
0010 #include <mutex>
0011 
0012 #include "celeritas/io/EventIOInterface.hh"
0013 
0014 namespace celeritas
0015 {
0016 namespace detail
0017 {
0018 //---------------------------------------------------------------------------//
0019 /*!
0020  * Dump primaries to a shared file, one event per flush.
0021  *
0022  * This thin class simply adds a mutex to the output writer for thread safety.
0023  */
0024 class OffloadWriter
0025 {
0026   public:
0027     //!@{
0028     //! \name Type aliases
0029     using UPWriter = std::unique_ptr<EventWriterInterface>;
0030     using argument_type = EventWriterInterface::argument_type;
0031     //!@}
0032 
0033   public:
0034     // Construct from a writer interface
0035     inline explicit OffloadWriter(UPWriter&& writer);
0036 
0037     // Write primaries
0038     inline void operator()(argument_type);
0039 
0040   private:
0041     std::mutex write_mutex_;
0042     UPWriter write_event_;
0043 };
0044 
0045 //---------------------------------------------------------------------------//
0046 // INLINE DEFINITIONS
0047 //---------------------------------------------------------------------------//
0048 /*!
0049  * Construct from a writer.
0050  */
0051 OffloadWriter::OffloadWriter(UPWriter&& writer)
0052     : write_event_{std::move(writer)}
0053 {
0054 }
0055 
0056 //---------------------------------------------------------------------------//
0057 /*!
0058  * Write primaries using a mutex.
0059  */
0060 void OffloadWriter::operator()(argument_type primaries)
0061 {
0062     std::lock_guard scoped_lock{write_mutex_};
0063     (*write_event_)(primaries);
0064 }
0065 
0066 //---------------------------------------------------------------------------//
0067 }  // namespace detail
0068 }  // namespace celeritas