Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Kokkos_Future.hpp
1 /*
2 //@HEADER
3 // ************************************************************************
4 //
5 // Kokkos v. 2.0
6 // Copyright (2014) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Christian R. Trott (crtrott@sandia.gov)
39 //
40 // ************************************************************************
41 //@HEADER
42 */
43 
44 #ifndef KOKKOS_FUTURE_HPP
45 #define KOKKOS_FUTURE_HPP
46 
47 //----------------------------------------------------------------------------
48 
49 #include <Kokkos_Macros.hpp>
50 #if defined( KOKKOS_ENABLE_TASKDAG )
51 
52 #include <Kokkos_Core_fwd.hpp>
53 #include <Kokkos_TaskScheduler_fwd.hpp>
54 //----------------------------------------------------------------------------
55 
56 #include <impl/Kokkos_TaskQueue.hpp>
57 #include <impl/Kokkos_TaskResult.hpp>
58 #include <impl/Kokkos_TaskBase.hpp>
59 #include <Kokkos_Atomic.hpp>
60 
61 #include <Kokkos_Concepts.hpp> // is_space
62 
63 //----------------------------------------------------------------------------
64 //----------------------------------------------------------------------------
65 
66 namespace Kokkos {
67 
68 // For now, hack this in as a partial specialization
69 // TODO @tasking @cleanup Make this the "normal" class template and make the old code the specialization
70 template <typename ValueType, typename ExecutionSpace, typename QueueType>
71 class BasicFuture<ValueType, SimpleTaskScheduler<ExecutionSpace, QueueType>>
72 {
73 public:
74 
75  using value_type = ValueType;
76  using execution_space = ExecutionSpace;
77  using scheduler_type = SimpleTaskScheduler<ExecutionSpace, QueueType>;
78  using queue_type = typename scheduler_type::task_queue_type;
79 
80 
81 private:
82 
83  template <class, class>
84  friend class SimpleTaskScheduler;
85  template <class, class>
86  friend class BasicFuture;
87 
88  using task_base_type = typename scheduler_type::task_base_type;
89  using task_queue_type = typename scheduler_type::task_queue_type;
90 
91  using task_queue_traits = typename scheduler_type::task_queue_traits;
92  using task_scheduling_info_type = typename scheduler_type::task_scheduling_info_type;
93 
94  using result_storage_type =
95  Impl::TaskResultStorage<
96  ValueType,
97  Impl::SchedulingInfoStorage<
98  Impl::RunnableTaskBase<task_queue_traits>,
99  task_scheduling_info_type
100  >
101  >;
102 
103 
104 
105  OwningRawPtr<task_base_type> m_task = nullptr;
106 
107  KOKKOS_INLINE_FUNCTION
108  explicit
109  BasicFuture(task_base_type* task)
110  : m_task(task)
111  {
112  // Note: reference count starts at 2 to account for initial increment
113  // TODO @tasking @minor DSH verify reference count here and/or encapsulate starting reference count closer to here
114  }
115 
116 public:
117 
118  KOKKOS_INLINE_FUNCTION
119  BasicFuture() noexcept : m_task(nullptr) { }
120 
121  KOKKOS_INLINE_FUNCTION
122  BasicFuture(BasicFuture&& rhs) noexcept
123  : m_task(std::move(rhs.m_task))
124  {
125  rhs.m_task = nullptr;
126  }
127 
128  KOKKOS_INLINE_FUNCTION
129  BasicFuture(BasicFuture const& rhs)
130  // : m_task(rhs.m_task)
131  : m_task(nullptr)
132  {
133  *static_cast<task_base_type* volatile*>(&m_task) = rhs.m_task;
134  if(m_task) m_task->increment_reference_count();
135  }
136 
137  KOKKOS_INLINE_FUNCTION
138  BasicFuture& operator=(BasicFuture&& rhs) noexcept
139  {
140  if(m_task != rhs.m_task) {
141  clear();
142  //m_task = std::move(rhs.m_task);
143  *static_cast<task_base_type* volatile*>(&m_task) = rhs.m_task;
144  // rhs.m_task reference count is unchanged, since this is a move
145  }
146  else {
147  // They're the same, but this is a move, so 1 fewer references now
148  rhs.clear();
149  }
150  rhs.m_task = nullptr;
151  return *this ;
152  }
153 
154  KOKKOS_INLINE_FUNCTION
155  BasicFuture& operator=(BasicFuture const& rhs)
156  {
157  if(m_task != rhs.m_task) {
158  clear();
159  //m_task = rhs.m_task;
160  *static_cast<task_base_type* volatile*>(&m_task) = rhs.m_task;
161  }
162  if(m_task != nullptr) { m_task->increment_reference_count(); }
163  return *this;
164  }
165 
166  //----------------------------------------
167 
168  template <class T, class S>
169  KOKKOS_INLINE_FUNCTION
170  BasicFuture(BasicFuture<T, S>&& rhs) noexcept // NOLINT(google-explicit-constructor)
171  : m_task(std::move(rhs.m_task))
172  {
173  static_assert(
174  std::is_same<scheduler_type, void>::value ||
175  std::is_same<scheduler_type, S>::value,
176  "Moved Futures must have the same scheduler"
177  );
178 
179  static_assert(
180  std::is_same<value_type, void>::value ||
181  std::is_same<value_type, T>::value,
182  "Moved Futures must have the same value_type"
183  );
184 
185  // reference counts are unchanged, since this is a move
186  rhs.m_task = nullptr;
187  }
188 
189  template <class T, class S>
190  KOKKOS_INLINE_FUNCTION
191  BasicFuture(BasicFuture<T, S> const& rhs) // NOLINT(google-explicit-constructor)
192  //: m_task(rhs.m_task)
193  : m_task(nullptr)
194  {
195  static_assert(
196  std::is_same<scheduler_type, void>::value ||
197  std::is_same<scheduler_type, S>::value,
198  "Copied Futures must have the same scheduler"
199  );
200 
201  static_assert(
202  std::is_same<value_type, void>::value ||
203  std::is_same<value_type, T>::value,
204  "Copied Futures must have the same value_type"
205  );
206 
207  *static_cast<task_base_type* volatile*>(&m_task) = rhs.m_task;
208  if(m_task) m_task->increment_reference_count();
209  }
210 
211  template <class T, class S>
212  KOKKOS_INLINE_FUNCTION
213  BasicFuture&
214  operator=(BasicFuture<T, S> const& rhs)
215  {
216  static_assert(
217  std::is_same<scheduler_type, void>::value ||
218  std::is_same<scheduler_type, S>::value,
219  "Assigned Futures must have the same scheduler"
220  );
221 
222  static_assert(
223  std::is_same<value_type, void>::value ||
224  std::is_same<value_type, T>::value,
225  "Assigned Futures must have the same value_type"
226  );
227 
228  if(m_task != rhs.m_task) {
229  clear();
230  //m_task = rhs.m_task;
231  *static_cast<task_base_type* volatile*>(&m_task) = rhs.m_task;
232  if(m_task != nullptr) { m_task->increment_reference_count(); }
233  }
234  return *this;
235  }
236 
237  template<class T, class S>
238  KOKKOS_INLINE_FUNCTION
239  BasicFuture& operator=(BasicFuture<T, S>&& rhs)
240  {
241  static_assert(
242  std::is_same<scheduler_type, void>::value ||
243  std::is_same<scheduler_type, S>::value,
244  "Assigned Futures must have the same scheduler"
245  );
246 
247  static_assert(
248  std::is_same<value_type, void>::value ||
249  std::is_same<value_type, T>::value,
250  "Assigned Futures must have the same value_type"
251  );
252 
253  if(m_task != rhs.m_task) {
254  clear();
255  //m_task = std::move(rhs.m_task);
256  *static_cast<task_base_type* volatile*>(&m_task) = rhs.m_task;
257  // rhs.m_task reference count is unchanged, since this is a move
258  }
259  else {
260  // They're the same, but this is a move, so 1 fewer references now
261  rhs.clear();
262  }
263  rhs.m_task = nullptr;
264  return *this ;
265  }
266 
267  KOKKOS_INLINE_FUNCTION
268  ~BasicFuture() noexcept { clear(); }
269 
270  //----------------------------------------
271 
272  KOKKOS_INLINE_FUNCTION
273  void clear() noexcept {
274  if(m_task) {
275  bool should_delete = m_task->decrement_and_check_reference_count();
276  if(should_delete) {
277  static_cast<task_queue_type*>(m_task->ready_queue_base_ptr())
278  ->deallocate(std::move(*m_task));
279  }
280  }
281  //m_task = nullptr;
282  *static_cast<task_base_type* volatile*>(&m_task) = nullptr;
283  }
284 
285  KOKKOS_INLINE_FUNCTION
286  bool is_null() const noexcept {
287  return m_task == nullptr;
288  }
289 
290 
291  KOKKOS_INLINE_FUNCTION
292  bool is_ready() const noexcept {
293  return (m_task == nullptr) || m_task->wait_queue_is_consumed();
294  }
295 
296  KOKKOS_INLINE_FUNCTION
297  const typename Impl::TaskResult< ValueType >::reference_type
298  get() const
299  {
300  KOKKOS_EXPECTS(is_ready());
301  return static_cast<result_storage_type*>(m_task)->value_reference();
302  //return Impl::TaskResult<ValueType>::get(m_task);
303  }
304 
305 };
306 
308 // OLD CODE
310 
311 template <typename ValueType, typename Scheduler>
312 class BasicFuture {
313 private:
314 
315  template< typename , typename > friend class BasicTaskScheduler ;
316  template< typename , typename > friend class BasicFuture ;
317  friend class Impl::TaskBase ;
318  template< typename , typename , typename > friend class Impl::Task ;
319 
320 
321  //----------------------------------------
322 
323 public:
324 
325  //----------------------------------------
326 
327  using scheduler_type = Scheduler;
328  using queue_type = typename scheduler_type::queue_type;
329  using execution_space = typename scheduler_type::execution_space;
330  using value_type = ValueType;
331 
332  //----------------------------------------
333 
334 private:
335 
336  //----------------------------------------
337 
338  using task_base = Impl::TaskBase;
339 
340  task_base * m_task ;
341 
342  KOKKOS_INLINE_FUNCTION explicit
343  BasicFuture( task_base * task ) : m_task(0)
344  { if ( task ) queue_type::assign( & m_task , task ); }
345 
346  //----------------------------------------
347 
348 public:
349 
350  //----------------------------------------
351 
352  KOKKOS_INLINE_FUNCTION
353  bool is_null() const { return 0 == m_task ; }
354 
355  KOKKOS_INLINE_FUNCTION
356  int reference_count() const
357  { return 0 != m_task ? m_task->reference_count() : 0 ; }
358 
359  //----------------------------------------
360 
361  KOKKOS_INLINE_FUNCTION
362  void clear()
363  { if ( m_task ) queue_type::assign( & m_task , (task_base*)0 ); }
364 
365  //----------------------------------------
366 
367  KOKKOS_INLINE_FUNCTION
368  ~BasicFuture() { clear(); }
369 
370  //----------------------------------------
371 
372  KOKKOS_INLINE_FUNCTION
373  BasicFuture() noexcept : m_task(nullptr) { }
374 
375  KOKKOS_INLINE_FUNCTION
376  BasicFuture( BasicFuture && rhs ) noexcept
377  : m_task( rhs.m_task )
378  {
379  rhs.m_task = 0;
380  }
381 
382  KOKKOS_INLINE_FUNCTION
383  BasicFuture( const BasicFuture & rhs )
384  : m_task(0)
385  { if ( rhs.m_task ) queue_type::assign( & m_task , rhs.m_task ); }
386 
387  KOKKOS_INLINE_FUNCTION
388  BasicFuture& operator=(BasicFuture&& rhs) noexcept
389  {
390  clear();
391  m_task = rhs.m_task ;
392  rhs.m_task = 0 ;
393  return *this ;
394  }
395 
396  KOKKOS_INLINE_FUNCTION
397  BasicFuture& operator=(BasicFuture const& rhs)
398  {
399  if ( m_task || rhs.m_task ) queue_type::assign( & m_task , rhs.m_task );
400  return *this ;
401  }
402 
403  //----------------------------------------
404 
405  template <class T, class S>
406  KOKKOS_INLINE_FUNCTION
407  BasicFuture(BasicFuture<T, S>&& rhs) noexcept // NOLINT(google-explicit-constructor)
408  : m_task( rhs.m_task )
409  {
410  static_assert
411  ( std::is_same<scheduler_type, void>::value ||
412  std::is_same<scheduler_type, S>::value
413  , "Assigned Futures must have the same scheduler" );
414 
415  static_assert
416  ( std::is_same< value_type , void >::value ||
417  std::is_same<value_type, T>::value
418  , "Assigned Futures must have the same value_type" );
419 
420  rhs.m_task = 0 ;
421  }
422 
423  template <class T, class S>
424  KOKKOS_INLINE_FUNCTION
425  BasicFuture(BasicFuture<T, S> const& rhs) // NOLINT(google-explicit-constructor)
426  : m_task(nullptr)
427  {
428  static_assert
429  ( std::is_same<scheduler_type, void>::value ||
430  std::is_same<scheduler_type, S>::value
431  , "Assigned Futures must have the same scheduler" );
432 
433  static_assert
434  ( std::is_same< value_type , void >::value ||
435  std::is_same<value_type, T>::value
436  , "Assigned Futures must have the same value_type" );
437 
438  if ( rhs.m_task ) queue_type::assign( & m_task , rhs.m_task );
439  }
440 
441  template <class T, class S>
442  KOKKOS_INLINE_FUNCTION
443  BasicFuture&
444  operator=(BasicFuture<T, S> const& rhs)
445  {
446  static_assert
447  ( std::is_same<scheduler_type, void>::value ||
448  std::is_same<scheduler_type, S>::value
449  , "Assigned Futures must have the same scheduler" );
450 
451  static_assert
452  ( std::is_same< value_type , void >::value ||
453  std::is_same<value_type, T>::value
454  , "Assigned Futures must have the same value_type" );
455 
456  if ( m_task || rhs.m_task ) queue_type::assign( & m_task , rhs.m_task );
457  return *this ;
458  }
459 
460  template<class T, class S>
461  KOKKOS_INLINE_FUNCTION
462  BasicFuture& operator=(BasicFuture<T, S>&& rhs)
463  {
464  static_assert
465  ( std::is_same<scheduler_type, void>::value ||
466  std::is_same<scheduler_type, S>::value
467  , "Assigned Futures must have the same scheduler" );
468 
469  static_assert
470  ( std::is_same< value_type , void >::value ||
471  std::is_same<value_type, T>::value
472  , "Assigned Futures must have the same value_type" );
473 
474  clear();
475  m_task = rhs.m_task ;
476  rhs.m_task = 0 ;
477  return *this ;
478  }
479 
480  //----------------------------------------
481 
482  KOKKOS_INLINE_FUNCTION
483  int is_ready() const noexcept
484  { return ( 0 == m_task ) || ( ((task_base*) task_base::LockTag) == m_task->m_wait ); }
485 
486  KOKKOS_INLINE_FUNCTION
487  const typename Impl::TaskResult< ValueType >::reference_type
488  get() const
489  {
490  if ( 0 == m_task ) {
491  Kokkos::abort( "Kokkos:::Future::get ERROR: is_null()");
492  }
493  return Impl::TaskResult< ValueType >::get( m_task );
494  }
495 };
496 
497 // Is a Future with the given execution space
498 template< typename , typename ExecSpace = void >
499 struct is_future : public std::false_type {};
500 
501 template<typename ValueType, typename Scheduler, typename ExecSpace>
502 struct is_future<BasicFuture<ValueType, Scheduler>, ExecSpace>
503  : std::integral_constant<bool,
504  std::is_same<ExecSpace, typename Scheduler::execution_space>::value
505  || std::is_void<ExecSpace>::value
506  >
507 {};
508 
510 // END OLD CODE
512 
513 namespace Impl {
514 
515 template <class Arg1, class Arg2>
516 class ResolveFutureArgOrder {
517 private:
518  enum { Arg1_is_space = Kokkos::is_space<Arg1>::value };
519  enum { Arg2_is_space = Kokkos::is_space<Arg2>::value };
520  enum { Arg1_is_value = !Arg1_is_space && !std::is_same<Arg1, void>::value };
521  enum { Arg2_is_value = !Arg2_is_space && !std::is_same<Arg2, void>::value };
522 
523  static_assert(
524  ! ( Arg1_is_space && Arg2_is_space ),
525  "Future cannot be given two spaces"
526  );
527 
528  static_assert(
529  ! ( Arg1_is_value && Arg2_is_value ),
530  "Future cannot be given two value types"
531  );
532 
533  using value_type =
534  typename std::conditional<Arg1_is_value, Arg1,
535  typename std::conditional<Arg2_is_value, Arg2, void>::type
536  >::type;
537 
538  using execution_space =
539  typename std::conditional<Arg1_is_space, Arg1,
540  typename std::conditional<Arg2_is_space, Arg2, void>::type
541  >::type::execution_space;
542 
543 public:
544 
545  using type = BasicFuture<value_type, TaskScheduler<execution_space>>;
546 
547 };
548 
549 } // end namespace Impl
550 
558 template <class Arg1 = void, class Arg2 = void>
559 using Future = typename Impl::ResolveFutureArgOrder<Arg1, Arg2>::type;
560 
561 } // namespace Kokkos
562 
563 //----------------------------------------------------------------------------
564 //----------------------------------------------------------------------------
565 
566 #endif /* #if defined( KOKKOS_ENABLE_TASKDAG ) */
567 #endif /* #ifndef KOKKOS_FUTURE */
bool is_null(const boost::shared_ptr< T > &p)
Atomic functions.