devicescratch.h
Go to the documentation of this file.
1 #ifndef TTG_PARSEC_DEVICESCRATCH_H
2 #define TTG_PARSEC_DEVICESCRATCH_H
3 
4 // TODO: replace with short vector
5 #define TTG_PARSEC_MAX_NUM_DEVICES 4
6 
7 #include <array>
8 #include <parsec.h>
9 #include <parsec/data_internal.h>
10 #include <parsec/mca/device/device.h>
11 #include <parsec/mca/device/device_gpu.h>
12 #include <ttg/devicescope.h>
13 #include "ttg/parsec/task.h"
14 
15 namespace ttg_parsec {
16 
17 namespace detail {
18  // fwd decl
19  template<typename T>
20  parsec_data_t* get_parsec_data(const ttg_parsec::devicescratch<T>&);
21 } // namespace detail
22 
28 template<typename T>
29 struct devicescratch {
30 
31  using element_type = std::decay_t<T>;
32 
33  static_assert(std::is_trivially_copyable_v<element_type>,
34  "Only trivially copyable types are supported for devices.");
35  static_assert(std::is_default_constructible_v<element_type>,
36  "Only default constructible types are supported for devices.");
37 
38 private:
39 
40  parsec_data_t* m_data = nullptr;
41  ttg::scope m_scope;
42 
43  static parsec_data_t* create_parsec_data(void *ptr, size_t count) {
44 
45  parsec_data_t *data = parsec_data_create_with_type(nullptr, 0, ptr,
46  sizeof(element_type)*count,
47  parsec_datatype_int8_t);
48  data->device_copies[0]->flags |= PARSEC_DATA_FLAG_PARSEC_MANAGED;
49  data->device_copies[0]->coherency_state = PARSEC_DATA_COHERENCY_SHARED;
50  return data;
51  }
52 
53  friend parsec_data_t* detail::get_parsec_data<T>(const ttg_parsec::devicescratch<T>&);
54 
55 public:
56 
57  /* Constructing a devicescratch using application-managed memory.
58  * The memory pointed to by ptr must be accessible during
59  * the life-time of the devicescratch. */
61  : m_data(create_parsec_data(ptr, count))
62  , m_scope(scope)
63  {
64  if (ttg::scope::SyncIn == scope) {
65  /* increment the version to force the first initial transfer */
66  m_data->device_copies[0]->version = 1;
67  }
68  }
69 
70  /* don't allow moving */
72 
73  /* don't allow copying */
74  devicescratch(const devicescratch& db) = delete;
75 
76  /* don't allow moving */
78 
79  /* don't allow copying */
80  devicescratch& operator=(const devicescratch& db) = delete;
81 
83  /* remove data from flow */
84  if (nullptr != m_data) {
85  parsec_data_discard(m_data);
86  }
87  m_data = nullptr;
88  }
89 
90  /* get the current device pointer */
92  assert(is_valid());
93  return static_cast<element_type*>(m_data->device_copies[m_data->owner_device]->device_private);
94  }
95 
96  /* get the current device pointer */
97  const element_type* device_ptr() const {
98  assert(is_valid());
99  return static_cast<element_type*>(m_data->device_copies[m_data->owner_device]->device_private);
100  }
101 
102  bool is_valid() const {
103  // TODO: how to get the current device
104  // return (m_data->owner_device == parsec_current_device);
105  return true;
106  }
107 
108  ttg::scope scope() const {
109  return m_scope;
110  }
111 
112  std::size_t size() const {
113  return (m_data->nb_elts / sizeof(element_type));
114  }
115 
116 };
117 
118 namespace detail {
119  template<typename T>
120  parsec_data_t* get_parsec_data(const ttg_parsec::devicescratch<T>& scratch) {
121  return const_cast<parsec_data_t*>(scratch.m_data);
122  }
123 } // namespace detail
124 
125 } // namespace ttg_parsec
126 
127 #endif // TTG_PARSEC_DEVICESCRATCH_H
constexpr auto data(C &c) -> decltype(c.data())
Definition: span.h:189
parsec_data_t * get_parsec_data(const ttg_parsec::Buffer< T, A > &db)
Definition: buffer.h:545
this contains PaRSEC-based TTG functionality
Definition: fwd.h:18
scope
Definition: devicescope.h:5
std::decay_t< T > element_type
Definition: devicescratch.h:31
devicescratch & operator=(devicescratch &&)=delete
devicescratch(devicescratch &&)=delete
ttg::scope scope() const
devicescratch(const devicescratch &db)=delete
devicescratch(element_type *ptr, ttg::scope scope=ttg::scope::SyncIn, std::size_t count=1)
Definition: devicescratch.h:60
devicescratch & operator=(const devicescratch &db)=delete
element_type * device_ptr()
Definition: devicescratch.h:91
const element_type * device_ptr() const
Definition: devicescratch.h:97
std::size_t size() const