PeriDyno 1.0.0
Loading...
Searching...
No Matches
Array3D.inl
Go to the documentation of this file.
1#include "VkDeviceArray3D.h"
2#include "VkTransfer.h"
3
4namespace dyno {
5
6 template<typename T>
8 {
9 if (m_nx != src.size() || m_ny != src.size() || m_nz != src.size()) {
10 this->resize(src.nx(), src.ny(), src.nz());
11 }
12
13 vkTransfer(m_data, *src.handle());
14 //cuSafeCall(cudaMemcpy2D(m_data.data(), sizeof(T) * m_nx, src.begin(), src.pitch(), sizeof(T) * src.nx(), src.ny() * src.nz(), cudaMemcpyDeviceToHost));
15 }
16
17 template<typename T>
18 class Array3D<T, DeviceType::GPU>
19 {
20 public:
21 Array3D()
22 {};
23
24 Array3D(uint nx, uint ny, uint nz)
25 {
26 this->resize(nx, ny, nz);
27 };
28
32 ~Array3D() { };
33
34 void resize(const uint nx, const uint ny, const uint nz);
35
36 void reset();
37
38 void clear();
39
40 inline const VkDeviceArray3D<T>* handle() const { return &m_data; }
41 inline VkDeviceArray3D<T>* handle() { return &m_data; }
42
43 VkBuffer buffer() const { return m_data.bufferHandle(); }
44
45 uint32_t bufferSize() { return m_data.bufferSize(); }
46
47 inline T* begin() const { return m_data; }
48
49 inline uint nx() const { return m_nx; }
50 inline uint ny() const { return m_ny; }
51 inline uint nz() const { return m_nz; }
52// inline uint pitch() const { return m_pitch_x; }
53
54// DYN_FUNC inline T operator () (const int i, const int j, const int k) const
55// {
56// char* addr = (char*)m_data;
57// addr += (j * m_pitch_x + k * m_nxy);
58// return ((T*)addr)[i];
59// }
60//
61// DYN_FUNC inline T& operator () (const int i, const int j, const int k)
62// {
63// char* addr = (char*)m_data;
64// addr += (j * m_pitch_x + k * m_nxy);
65// return ((T*)addr)[i];
66// }
67//
68// DYN_FUNC inline T operator [] (const int id) const
69// {
70// return m_data[id];
71// }
72//
73// DYN_FUNC inline T& operator [] (const int id)
74// {
75// return m_data[id];
76// }
77
78 inline size_t index(const uint i, const uint j, const uint k) const
79 {
80 return i + j * m_nx + k * m_nx * m_ny;
81 }
82
83 inline size_t size() const { return m_nx * m_ny * m_nz; }
84 inline bool isCPU() const { return false; }
85 inline bool isGPU() const { return true; }
86
87 void assign(const std::vector<T>& src);
88 void assign(const Array3D<T, DeviceType::GPU>& src);
89 void assign(const Array3D<T, DeviceType::CPU>& src);
90
91 private:
92 uint m_nx = 0;
93
94 uint m_ny = 0;
95 uint m_nz = 0;
96 uint m_nxy = 0;
97 VkDeviceArray3D<T> m_data;
98 };
99
100 template<typename T>
102
103// typedef DArray3D<float> Grid1f;
104// typedef DArray3D<float3> Grid3f;
105// typedef DArray3D<bool> Grid1b;
106
107
108 template<typename T>
109 void Array3D<T, DeviceType::GPU>::resize(const uint nx, const uint ny, const uint nz)
110 {
111 uint total = nx * ny * nz;
112 if (m_data.size() == total) return;
113
114 if (total == 0) {
115 m_data.clear();
116 return;
117 }
118
119 //cuSafeCall(cudaMallocPitch((void**)&m_data, (size_t*)&m_pitch_x, (size_t)sizeof(T) * nx, (size_t)ny*nz));
120 m_data.resize(nx, ny, nz);
121
122 //TODO: check whether it has problem when m_pitch_x is not divisible by sizeof(T)
123 m_nx = nx; m_ny = ny; m_nz = nz;
124 m_nxy = m_nx * m_ny;
125 }
126
127 template<typename T>
129 {
130 //TODO:
131 //cuSafeCall(cudaMemset(m_data, 0, m_nxy * m_nz));
132 }
133
134 template<typename T>
136 {
137 m_data.clear();
138
139 m_nx = 0;
140 m_ny = 0;
141 m_nz = 0;
142 m_nxy = 0;
143 }
144
145 template<typename T>
146 void Array3D<T, DeviceType::GPU>::assign(const std::vector<T>& src)
147 {
148 assert(m_nx * m_ny * m_nz == src.size());
149
150 vkTransfer(m_data, src);
151 //cuSafeCall(cudaMemcpy2D(m_data, m_pitch_x, src.begin(), src.pitch(), sizeof(T) *src.nx(), src.ny()*src.nz(), cudaMemcpyDeviceToDevice));
152 }
153
154 template<typename T>
156 {
157 if (m_nx != src.nx() || m_ny != src.ny() || m_nz != src.nz()) {
158 this->resize(src.nx(), src.ny(), src.nz());
159 }
160
161 vkTransfer(m_data, *src.handle());
162 //cuSafeCall(cudaMemcpy2D(m_data, m_pitch_x, src.begin(), src.pitch(), sizeof(T) *src.nx(), src.ny()*src.nz(), cudaMemcpyDeviceToDevice));
163 }
164
165 template<typename T>
167 {
168 if (m_nx != src.nx() || m_ny != src.ny() || m_nz != src.nz()) {
169 this->resize(src.nx(), src.ny(), src.nz());
170 }
171
172 vkTransfer(m_data, *src.handle());
173 //cuSafeCall(cudaMemcpy2D(m_data, m_pitch_x, src.begin(), sizeof(T) *src.nx(), sizeof(T) *src.nx(), src.ny()*src.nz(), cudaMemcpyHostToDevice));
174 }
175}
assert(queueCount >=1)
#define T(t)
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
Array3D< T, DeviceType::GPU > DArray3D
Definition Array3D.inl:90
unsigned int uint
Definition VkProgram.h:14
bool vkTransfer(VkHostArray< T > &dst, const VkDeviceArray< T > &src)
Definition VkTransfer.inl:7