LCOV - code coverage report
Current view: top level - include/dash - Atomic.h (source / functions) Hit Total Coverage
Test: coverage.info.cleaned Lines: 38 38 100.0 %
Date: 2016-06-29 12:30:40 Functions: 13 13 100.0 %

          Line data    Source code
       1             : #ifndef DASH__ATOMIC_H__INCLUDED
       2             : #define DASH__ATOMIC_H__INCLUDED
       3             : 
       4             : #include <dash/dart/if/dart_types.h>
       5             : #include <dash/dart/if/dart_communication.h>
       6             : 
       7             : #include <dash/Exception.h>
       8             : #include <dash/GlobPtr.h>
       9             : #include <dash/GlobRef.h>
      10             : #include <dash/algorithm/Operation.h>
      11             : 
      12             : #include <dash/internal/Logging.h>
      13             : 
      14             : namespace dash {
      15             : 
      16             : template<typename ValueType>
      17             : class Atomic
      18             : {
      19             : 
      20             : public:
      21             :   typedef ValueType                                  value_type;
      22             :   typedef size_t                                      size_type;
      23             :   typedef size_t                                difference_type;
      24             : 
      25             :   typedef       GlobRef<value_type>                   reference;
      26             :   typedef const GlobRef<value_type>             const_reference;
      27             : 
      28             :   typedef       GlobPtr<value_type>                     pointer;
      29             :   typedef const GlobPtr<value_type>               const_pointer;
      30             : 
      31             : public:
      32             :   /**
      33             :    * Default constructor.
      34             :    * Sets underlying global pointer to \c DART_GPTR_NULL.
      35             :    */
      36          96 :   Atomic()
      37             :   : _gptr(DART_GPTR_NULL),
      38          96 :     _team(nullptr)
      39          96 :   { }
      40             : 
      41             :   /**
      42             :    * Constructor, creates a new instance of \c dash::Atomic from a DART
      43             :    * pointer.
      44             :    */
      45         151 :   Atomic(
      46             :     dart_gptr_t  gptr,
      47          43 :     dash::Team & team = dash::Team::All())
      48             :   : _gptr(gptr),
      49         151 :     _team(&team)
      50         151 :   { }
      51             : 
      52             :   /**
      53             :    * Constructor, creates a new instance of \c dash::Atomic from any object
      54             :    * that is convertible to \c dart_gptr_t.
      55             :    */
      56             :   template<class GlobalType>
      57          12 :   Atomic(
      58             :     const GlobalType & global,
      59          12 :     dash::Team       & team   = dash::Team::All())
      60          12 :   : Atomic(global.dart_gptr(), team)
      61          12 :   { }
      62             : 
      63             :   /**
      64             :    * Set the value of the shared atomic variable.
      65             :    */
      66             :   void set(ValueType val)
      67             :   {
      68             :     DASH_LOG_DEBUG_VAR("Atomic.set()", val);
      69             :     DASH_LOG_TRACE_VAR("Atomic.set",   _gptr);
      70             :     *(reference(_gptr)) = val;
      71             :     DASH_LOG_DEBUG("Atomic.set >");
      72             :   }
      73             : 
      74             :   /**
      75             :    * Get a reference on the shared atomic value.
      76             :    */
      77           4 :   reference get()
      78             :   {
      79             :     DASH_LOG_DEBUG("Atomic.get()");
      80             :     DASH_LOG_TRACE_VAR("Atomic.get", _gptr);
      81           4 :     DASH_ASSERT(
      82             :       !DART_GPTR_EQUAL(
      83             :         _gptr, DART_GPTR_NULL));
      84           4 :     reference ref(_gptr);
      85           4 :     DASH_LOG_DEBUG_VAR("Atomic.get >", static_cast<ValueType>(ref));
      86           4 :     return ref;
      87             :   }
      88             : 
      89             :   /**
      90             :    * Get a const reference on the shared atomic value.
      91             :    */
      92             :   const_reference get() const
      93             :   {
      94             :     DASH_LOG_DEBUG("Atomic.cget()");
      95             :     DASH_LOG_TRACE_VAR("Atomic.cget", _gptr);
      96             :     DASH_ASSERT(
      97             :       !DART_GPTR_EQUAL(
      98             :         _gptr, DART_GPTR_NULL));
      99             :     const_reference cref(_gptr);
     100             :     DASH_LOG_DEBUG_VAR("Atomic.cget >", static_cast<ValueType>(cref));
     101             :     return cref;
     102             :   }
     103             : 
     104             :   /**
     105             :    * Atomically executes specified operation on the referenced shared value.
     106             :    */
     107             :   template<typename BinaryOp>
     108           8 :   void op(
     109             :     BinaryOp  binary_op,
     110             :     /// Value to be added to global atomic variable.
     111             :     ValueType value)
     112             :   {
     113             :     DASH_LOG_DEBUG_VAR("Atomic.add()", value);
     114             :     DASH_LOG_TRACE_VAR("Atomic.add",   _gptr);
     115           8 :     DASH_ASSERT(_team != nullptr);
     116           8 :     DASH_ASSERT(
     117             :       !DART_GPTR_EQUAL(
     118             :         _gptr, DART_GPTR_NULL));
     119           8 :     value_type acc = value;
     120           8 :     dart_ret_t ret = dart_accumulate(
     121             :                        _gptr,
     122             :                        reinterpret_cast<char *>(&acc),
     123             :                        1,
     124             :                        dash::dart_datatype<ValueType>::value,
     125             :                        binary_op.dart_operation(),
     126          16 :                        _team->dart_id());
     127           8 :     DASH_ASSERT_EQ(DART_OK, ret, "dart_accumulate failed");
     128           8 :     dart_flush(_gptr);
     129             :     DASH_LOG_DEBUG_VAR("Atomic.add >", acc);
     130           8 :   }
     131             : 
     132             :   /**
     133             :    * Atomic fetch-and-op operation on the referenced shared value.
     134             :    *
     135             :    * \return  The value of the referenced shared variable before the
     136             :    *          operation.
     137             :    */
     138             :   template<typename BinaryOp>
     139          51 :   ValueType fetch_and_op(
     140             :     BinaryOp  op,
     141             :     /// Value to be added to global atomic variable.
     142             :     ValueType val)
     143             :   {
     144             :     DASH_LOG_DEBUG_VAR("Atomic.fetch_and_op()", val);
     145             :     DASH_LOG_TRACE_VAR("Atomic.fetch_and_op",   _gptr);
     146          51 :     DASH_ASSERT(_team != nullptr);
     147          51 :     DASH_ASSERT(
     148             :       !DART_GPTR_EQUAL(
     149             :         _gptr, DART_GPTR_NULL));
     150             :     value_type acc;
     151          51 :     dart_ret_t ret = dart_fetch_and_op(
     152             :                        _gptr,
     153             :                        reinterpret_cast<void *>(&val),
     154             :                        reinterpret_cast<void *>(&acc),
     155             :                        dash::dart_datatype<ValueType>::value,
     156             :                        op.dart_operation(),
     157         102 :                        _team->dart_id());
     158          51 :     DASH_ASSERT_EQ(DART_OK, ret, "dart_accumulate failed");
     159          51 :     dart_flush(_gptr);
     160             :     DASH_LOG_DEBUG_VAR("Atomic.fetch_and_op >", acc);
     161          51 :     return acc;
     162             :   }
     163             : 
     164             :   /**
     165             :    * Atomic add operation on the referenced shared value.
     166             :    */
     167           8 :   void add(
     168             :     ValueType value)
     169             :   {
     170           8 :     op(dash::plus<ValueType>(), value);
     171           8 :   }
     172             : 
     173             :   /**
     174             :    * Atomic subtract operation on the referenced shared value.
     175             :    */
     176             :   void sub(
     177             :     ValueType value)
     178             :   {
     179             :     op(dash::plus<ValueType>(), -value);
     180             :   }
     181             : 
     182             :   /**
     183             :    * Atomic fetch-and-add operation on the referenced shared value.
     184             :    *
     185             :    * \return  The value of the referenced shared variable before the
     186             :    *          operation.
     187             :    */
     188          51 :   ValueType fetch_and_add(
     189             :     /// Value to be added to global atomic variable.
     190             :     ValueType val)
     191             :   {
     192          51 :     return fetch_and_op(dash::plus<ValueType>(), val);
     193             :   }
     194             : 
     195             :   /**
     196             :    * Atomic fetch-and-sub operation on the referenced shared value.
     197             :    *
     198             :    * \return  The value of the referenced shared variable before the
     199             :    *          operation.
     200             :    */
     201             :   ValueType fetch_and_sub(
     202             :     /// Value to be subtracted from global atomic variable.
     203             :     ValueType val)
     204             :   {
     205             :     return fetch_and_op(dash::plus<ValueType>(), -val);
     206             :   }
     207             : 
     208             : private:
     209             :   /// The atomic value's underlying global pointer.
     210             :   dart_gptr_t   _gptr = DART_GPTR_NULL;
     211             :   /// Team of units associated with the shared atomic variable.
     212             :   dash::Team  * _team = nullptr;
     213             : 
     214             : }; // class Atomic
     215             : 
     216             : } // namespace dash
     217             : 
     218             : #endif // DASH__ATOMIC_H__INCLUDED

Generated by: LCOV version 1.12