PeriDyno 1.0.0
Loading...
Searching...
No Matches
Array.inl
Go to the documentation of this file.
1namespace dyno
2{
3 template<typename T>
5 {
6 if (mData.size() != src.size())
7 this->resize(src.size());
8
9 cuSafeCall(cudaMemcpy(this->begin(), src.begin(), src.size() * sizeof(T), cudaMemcpyDeviceToHost));
10 }
11
16 template<typename T>
17 class Array<T, DeviceType::GPU>
18 {
19 public:
20 Array()
21 {
22 };
23
24 Array(uint num)
25 {
26 this->resize(num);
27 }
28
32 ~Array() {};
33
34 void resize(const uint n);
35
39 void reset();
40
44 void clear();
45
46 DYN_FUNC inline const T* begin() const { return mData; }
47 DYN_FUNC inline T* begin() { return mData; }
48
49 DeviceType deviceType() { return DeviceType::GPU; }
50
51 GPU_FUNC inline T& operator [] (unsigned int id) {
52 return mData[id];
53 }
54
55 GPU_FUNC inline T& operator [] (unsigned int id) const {
56 return mData[id];
57 }
58
59 DYN_FUNC inline uint size() const { return mTotalNum; }
60 DYN_FUNC inline bool isCPU() const { return false; }
61 DYN_FUNC inline bool isGPU() const { return true; }
62 DYN_FUNC inline bool isEmpty() const { return mData == nullptr; }
63
64 void assign(const Array<T, DeviceType::GPU>& src);
65 void assign(const Array<T, DeviceType::CPU>& src);
66 void assign(const std::vector<T>& src);
67
68 void assign(const Array<T, DeviceType::GPU>& src, const uint count, const uint dstOffset = 0, const uint srcOffset = 0);
69 void assign(const Array<T, DeviceType::CPU>& src, const uint count, const uint dstOffset = 0, const uint srcOffset = 0);
70 void assign(const std::vector<T>& src, const uint count, const uint dstOffset = 0, const uint srcOffset = 0);
71
72 friend std::ostream& operator<<(std::ostream &out, const Array<T, DeviceType::GPU>& dArray)
73 {
74 Array<T, DeviceType::CPU> hArray;
75 hArray.assign(dArray);
76
77 out << hArray;
78
79 return out;
80 }
81
82 private:
83 T* mData = nullptr;
84 uint mTotalNum = 0;
85 uint mBufferNum = 0;
86 };
87
88 template<typename T>
90
91 template<typename T>
93 {
94 if (mTotalNum == n) return;
95
96 if (n == 0) {
97 clear();
98 return;
99 }
100
101 int exp = (int)std::ceil(std::log2(float(n)));
102
103 int bound = (int)std::pow(2, exp);
104
105 if (n > mBufferNum || n <= mBufferNum / 2) {
106 clear();
107
108 mTotalNum = n;
109 mBufferNum = bound;
110
111 cuSafeCall(cudaMalloc(&mData, bound * sizeof(T)));
112 }
113 else
114 mTotalNum = n;
115 }
116
117 template<typename T>
118 void Array<T, DeviceType::GPU>::clear()
119 {
120 if (mData != nullptr)
121 {
122 cuSafeCall(cudaFree((void*)mData));
123 }
124
125 mData = nullptr;
126 mTotalNum = 0;
127 mBufferNum = 0;
128 }
129
130 template<typename T>
132 {
133 cuSafeCall(cudaMemset((void*)mData, 0, mTotalNum * sizeof(T)));
134 }
135
136 template<typename T>
138 {
139 if (mTotalNum != src.size())
140 this->resize(src.size());
141
142 cuSafeCall(cudaMemcpy(mData, src.begin(), src.size() * sizeof(T), cudaMemcpyDeviceToDevice));
143 }
144
145 template<typename T>
147 {
148 if (mTotalNum != src.size())
149 this->resize(src.size());
150
151 cuSafeCall(cudaMemcpy(mData, src.begin(), src.size() * sizeof(T), cudaMemcpyHostToDevice));
152 }
153
154
155 template<typename T>
156 void Array<T, DeviceType::GPU>::assign(const std::vector<T>& src)
157 {
158 if (mTotalNum != src.size())
159 this->resize((uint)src.size());
160
161 cuSafeCall(cudaMemcpy(mData, src.data(), src.size() * sizeof(T), cudaMemcpyHostToDevice));
162 }
163
164 template<typename T>
165 void Array<T, DeviceType::GPU>::assign(const std::vector<T>& src, const uint count, const uint dstOffset, const uint srcOffset)
166 {
167 cuSafeCall(cudaMemcpy(mData + dstOffset, src.data() + srcOffset, count * sizeof(T), cudaMemcpyHostToDevice));
168 }
169
170 template<typename T>
171 void Array<T, DeviceType::GPU>::assign(const Array<T, DeviceType::CPU>& src, const uint count, const uint dstOffset, const uint srcOffset)
172 {
173 cuSafeCall(cudaMemcpy(mData + dstOffset, src.begin() + srcOffset, count * sizeof(T), cudaMemcpyHostToDevice));
174 }
175
176 template<typename T>
177 void Array<T, DeviceType::GPU>::assign(const Array<T, DeviceType::GPU>& src, const uint count, const uint dstOffset, const uint srcOffset)
178 {
179 cuSafeCall(cudaMemcpy(mData + dstOffset, src.begin() + srcOffset, count * sizeof(T), cudaMemcpyDeviceToDevice));
180 }
181}
This class is designed to be elegant, so it can be directly passed to GPU as parameters.
Definition Array.h:27
#define T(t)
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
Array< T, DeviceType::GPU > DArray
Definition Array.inl:89
DYN_FUNC Complex< Real > exp(const Complex< Real > &)
Definition Complex.inl:338
unsigned int uint
Definition VkProgram.h:14