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