MUDA
Loading...
Searching...
No Matches
field_entry.h
1#pragma once
2#include <string>
3#include <cinttypes>
4#include <muda/tools/string_pointer.h>
6#include <muda/ext/field/field_entry_type.h>
7#include <muda/ext/field/field_entry_base_data.h>
8#include <muda/ext/field/field_entry_view.h>
9#include <muda/tools/host_device_config.h>
10
11namespace muda
12{
13class SubField;
14class SubFieldInterface;
15template <FieldEntryLayout Layout>
16class SubFieldImpl;
17
19{
20 template <FieldEntryLayout layout>
21 friend class SubFieldImpl;
22
23 public:
25 FieldEntryLayoutInfo layout_info,
26 FieldEntryType type,
27 uint2 shape,
28 uint32_t m_elem_byte_size,
29 std::string_view name)
30 : m_field{field}
31 , m_name{name}
32 {
33 auto& info = m_core.m_info;
34 info.layout_info = layout_info;
35 info.type = type;
36 info.shape = shape;
37 info.elem_byte_size = m_elem_byte_size;
38
39 m_core.m_name =
40 m_field.m_field.m_string_cache[std::string{m_field.name()} + "." + m_name];
41 }
42 ~FieldEntryBase() = default;
43
44 protected:
45 friend class SubField;
46 friend class SubFieldInterface;
47 template <FieldEntryLayout Layout>
48 friend class SubFieldImpl;
49
50 virtual void async_copy_to_new_place(HostDeviceConfigView<FieldEntryCore> vfc) const = 0;
51
52 // delete copy
53 FieldEntryBase(const FieldEntryBase&) = delete;
54 FieldEntryBase& operator=(const FieldEntryBase&) = delete;
55
56 SubField& m_field;
57 std::string m_name;
58 // a parameter struct that can be copy between host and device.
59 FieldEntryCore m_core;
60 HostDeviceConfig<FieldEntryCore> m_host_device_core;
61
62 MUDA_GENERIC const auto& core() const { return m_core; }
63
64 public:
65 MUDA_GENERIC auto layout_info() const { return core().layout_info(); }
66 MUDA_GENERIC auto layout() const { return core().layout(); }
67 MUDA_GENERIC auto count() const { return core().count(); }
68 MUDA_GENERIC auto elem_byte_size() const { return core().elem_byte_size(); }
69 MUDA_GENERIC auto shape() const { return core().shape(); }
70 MUDA_GENERIC auto struct_stride() const { return core().struct_stride(); }
71 MUDA_GENERIC auto name() const { return std::string_view{m_name}; }
72};
73
74template <typename T, FieldEntryLayout Layout, int M, int N>
76{
77 static_assert(M > 0 && N > 0, "M and N must be positive");
78
79 public:
80 using ElementType = typename FieldEntryView<T, Layout, M, N>::ElementType;
81
82 FieldEntry(SubField& field, FieldEntryLayoutInfo layout, FieldEntryType type, std::string_view name)
83 : FieldEntryBase{field,
84 layout,
85 type,
86 make_uint2(static_cast<uint32_t>(M), static_cast<uint32_t>(N)),
87 sizeof(T),
88 name}
89 {
90 }
91 FieldEntry(SubField& field, FieldEntryLayoutInfo layout, FieldEntryType type, uint2 shape, std::string_view name)
92 : FieldEntryBase{field, layout, type, shape, sizeof(T), name}
93 {
94 }
95
97 {
98 MUDA_ASSERT(m_field.data_buffer() != nullptr, "Resize the field before you use it!");
100 m_host_device_core.view(), 0, static_cast<int>(m_core.count())};
101 }
102
104 {
105 MUDA_ASSERT(m_field.data_buffer() != nullptr, "Resize the field before you use it!");
107 m_host_device_core.view(), 0, static_cast<int>(m_core.count())};
108 }
109
110 auto view(int offset) { return view().subview(offset); }
111 auto view(int offset) const { return view().subview(offset); }
112
113 auto view(int offset, int count) { return view().subview(offset, count); }
114 auto view(int offset, int count) const
115 {
116 return view().subview(offset, count);
117 }
118
119 FieldEntryViewer<T, Layout, M, N> viewer() { return view().viewer(); }
121 {
122 return view().viewer();
123 }
124
125 void copy_to(DeviceBuffer<ElementType>& dst) const;
126 void copy_to(std::vector<ElementType>& dst) const;
127
128 void copy_from(const DeviceBuffer<ElementType>& src);
129 void copy_from(const std::vector<ElementType>& src);
130
131 template <FieldEntryLayout SrcLayout>
132 void copy_from(const FieldEntry<T, SrcLayout, M, N>& src);
133
134 virtual void async_copy_to_new_place(HostDeviceConfigView<FieldEntryCore> new_place) const override
135 {
136 using DstView = FieldEntryView<T, Layout, M, N>;
137 auto dst = DstView{new_place, 0, static_cast<int>(new_place->count())};
138
139 if(new_place->count() < this->count()) // shrinking
140 {
141 if(!m_field.allow_inplace_shrink()) // typically SoA don't allow inplace shrinking
142 {
143 BufferLaunch().resize(m_workpace, new_place->count());
144 FieldEntryLaunch().copy(m_workpace.view(),
145 std::as_const(*this).view(0, new_place->count())); // copy self to workspace
146 FieldEntryLaunch().copy(dst,
147 m_workpace.view()); // copy workspace to dst
148 }
149 // else do nothing, trivial shrink
150 }
151 else if(new_place->count() > this->count()) // expanding
152 {
153 // safe direct copy
154 FieldEntryLaunch().copy(dst.subview(0, this->count()),
155 std::as_const(*this).view());
156 }
157 else
158 {
159 // do thing
160 }
161 }
162
163 void fill(const ElementType& value);
164
165 private:
166 mutable DeviceBuffer<ElementType> m_workpace; // for data copy, if needed
167};
168} // namespace muda
169
170#include "details/field_entry.inl"
Definition buffer_launch.h:13
Definition field_entry_view_matrix.inl:110
Definition field_entry_viewer_matrix.inl:52
A std::vector like wrapper of cuda device memory, allows user to:
Definition device_buffer.h:46
Definition field_entry.h:19
Definition field_entry_core.h:15
Definition field_entry.h:76
Definition field_entry_launch.h:9
Definition field_entry_layout.h:24
Definition field_entry_view_matrix.inl:154
Definition field_entry_viewer_matrix.inl:77
Definition host_device_config.h:50
Definition host_device_config.h:12
Definition sub_field.h:18
Definition field.h:14
Definition sub_field_interface.h:15
A light-weight wrapper of cuda device memory. Like std::vector, allow user to resize,...