PeriDyno 1.0.0
Loading...
Searching...
No Matches
ArrayList.inl
Go to the documentation of this file.
1#include "ArrayTools.h"
2#include "Algorithm/Scan.h"
4
5namespace dyno
6{
7 template<class ElementType>
8 class ArrayList<ElementType, DeviceType::GPU>
9 {
10 public:
11 ArrayList()
12 {
13 };
14
18 ~ArrayList() {};
19
27 bool resize(const DArray<uint>& counts);
28 bool resize(const uint arraySize, const uint eleSize);
29
30
31 bool resize(uint num);
32
33 template<typename ET2>
34 bool resize(const ArrayList<ET2, DeviceType::GPU>& src);
35
36 DYN_FUNC inline uint size() const { return mLists.size(); }
37 DYN_FUNC inline uint elementSize() const { return mElements.size(); }
38
39 GPU_FUNC inline List<ElementType>& operator [] (unsigned int id) {
40 return mLists[id];
41 }
42
43 GPU_FUNC inline List<ElementType>& operator [] (unsigned int id) const {
44 return mLists[id];
45 }
46
47 DYN_FUNC inline bool isCPU() const { return false; }
48 DYN_FUNC inline bool isGPU() const { return true; }
49 DYN_FUNC inline bool isEmpty() const { return mIndex.size() == 0; }
50
51 void clear();
52
53 void assign(const ArrayList<ElementType, DeviceType::GPU>& src);
54 void assign(const ArrayList<ElementType, DeviceType::CPU>& src);
55 void assign(const std::vector<std::vector<ElementType>>& src);
56
57 friend std::ostream& operator<<(std::ostream& out, const ArrayList<ElementType, DeviceType::GPU>& aList)
58 {
59 ArrayList<ElementType, DeviceType::CPU> hList;
60 hList.assign(aList);
61 out << hList;
62
63 return out;
64 }
65
66 const DArray<uint>& index() const { return mIndex; }
67 const DArray<ElementType>& elements() const { return mElements; }
68 const DArray<List<ElementType>>& lists() const { return mLists; }
69
73 ArrayList<ElementType, DeviceType::GPU>& operator=(const ArrayList<ElementType, DeviceType::GPU>&) = delete;
74
75 private:
76 DArray<uint> mIndex;
77 DArray<ElementType> mElements;
78
80 };
81
82 template<typename ElementType>
84
85 template<class ElementType>
87 {
88 mIndex.assign(src.index());
89 mElements.assign(src.elements());
90
91 mLists.assign(src.lists());
92
93 //redirect the element address
94 for (int i = 0; i < src.size(); i++)
95 {
96 mLists[i].reserve(mElements.begin() + mIndex[i], mLists[i].size());
97 }
98 }
99
100 template<class ElementType>
102 {
103 mIndex.clear();
104 mElements.clear();
105 mLists.clear();
106 }
107
108 template<class ElementType>
109 bool ArrayList<ElementType, DeviceType::GPU>::resize(const DArray<uint>& counts)
110 {
111 if (counts.size() == 0)
112 {
113 mIndex.clear();
114 mElements.clear();
115 mLists.clear();
116
117 return false;
118 }
119
120 if (mIndex.size() != counts.size())
121 {
122 mIndex.resize(counts.size());
123 mLists.resize(counts.size());
124 }
125
126 mIndex.assign(counts);
127
128 Reduction<uint> reduce;
129 uint total_num = reduce.accumulate(mIndex.begin(), mIndex.size());
130
131 Scan<uint> scan;
132 scan.exclusive(mIndex);
133
134 //printf("total num 2 = %d\n", total_num);
135
136 mElements.resize(total_num);
137
138 parallel_allocate_for_list<sizeof(ElementType)>(mLists.begin(), mElements.begin(), mElements.size(), mIndex);
139
140 return true;
141 }
142
143
144 template<class ElementType>
145 bool ArrayList<ElementType, DeviceType::GPU>::resize(const uint arraySize, const uint eleSize)
146 {
147 assert(arraySize > 0);
148 assert(eleSize > 0);
149
150 if (mIndex.size() != arraySize)
151 {
152 mIndex.resize(arraySize);
153 mLists.resize(arraySize);
154 }
155
156 CArray<uint> hIndex;
157 hIndex.resize(arraySize);
158 int accNum = 0;
159 for (size_t i = 0; i < arraySize; i++)
160 {
161 hIndex[i] = (uint)accNum;
162 accNum += eleSize;
163 }
164
165 mIndex.assign(hIndex);
166
167 mElements.resize(arraySize*eleSize);
168
169 parallel_allocate_for_list<sizeof(ElementType)>(mLists.begin(), mElements.begin(), mElements.size(), mIndex);
170
171 return true;
172 }
173
174 template<typename ElementType>
175 template<typename ET2>
177 uint arraySize = src.size();
178 if (mIndex.size() != arraySize)
179 {
180 mIndex.resize(arraySize);
181 mLists.resize(arraySize);
182 }
183
184 mIndex.assign(src.index());
185 mElements.resize(src.elementSize());
186
187 parallel_allocate_for_list<sizeof(ElementType)>(mLists.begin(), mElements.begin(), mElements.size(), mIndex);
188
189 return true;
190 }
191
192 template<class ElementType>
194 {
195 mIndex.assign(src.index());
196 mElements.assign(src.elements());
197
198 mLists.assign(src.lists());
199
200 //redirect the element address
201 parallel_init_for_list<sizeof(ElementType)>(mLists.begin(), mElements.begin(), mElements.size(), mIndex);
202 }
203
204 template<class ElementType>
206 {
207 mIndex.assign(src.index());
208 mElements.assign(src.elements());
209
210 mLists.assign(src.lists());
211
212 //redirect the element address
213 parallel_init_for_list<sizeof(ElementType)>(mLists.begin(), mElements.begin(), mElements.size(), mIndex);
214 }
215
216 template<class ElementType>
217 void ArrayList<ElementType, DeviceType::GPU>::assign(const std::vector<std::vector<ElementType>>& src)
218 {
219 size_t indNum = src.size();
220 CArray<uint> hIndex(indNum);
221
222 CArray<ElementType> hElements;
223
224 size_t eleNum = 0;
225 for (int i = 0; i < src.size(); i++)
226 {
227 hIndex[i] = (uint)eleNum;
228 eleNum += src[i].size();
229
230 for (int j = 0; j < src[i].size(); j++)
231 {
232 hElements.pushBack(src[i][j]);
233 }
234 }
235
237 lists.resize(indNum);
238
239 mIndex.assign(hIndex);
240 mElements.assign(hElements);
241 ElementType* stAdr = mElements.begin();
242
243 eleNum = 0;
244 for (int i = 0; i < src.size(); i++)
245 {
246 size_t num_i = src[i].size();
248 lst.assign(stAdr + eleNum, num_i, num_i);
249 lists[i] = lst;
250
251 eleNum += src[i].size();
252 }
253
254 mLists.assign(lists);
255 }
256
257 template<class ElementType>
259 {
260 assert(num > 0);
261
262 mIndex.resize(num);
263 mLists.resize(num);
264
265 return true;
266 }
267
268 template<class ElementType>
270 {
271 assert(num > 0);
272
273 mIndex.resize(num);
274 mLists.resize(num);
275
276 return true;
277 }
278}
assert(queueCount >=1)
CArray< List< ElementType > > mLists
Definition ArrayList.h:106
Be aware do not use this structure on GPU if the data size is large.
Definition List.h:21
DYN_FUNC void assign(T *beg, int num, int buffer_size)
Definition List.h:45
void exclusive(T *output, const T *input, size_t length, bool bcao=true)
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
Array< T, DeviceType::GPU > DArray
Definition Array.inl:89
void parallel_allocate_for_list(void *lists, void *elements, size_t ele_size, DArray< uint > &index)
void parallel_init_for_list(void *lists, void *elements, size_t ele_size, DArray< uint > &index)
ArrayList< ElementType, DeviceType::GPU > DArrayList
Definition ArrayList.inl:83
Array< T, DeviceType::CPU > CArray
Definition Array.h:151
unsigned int uint
Definition VkProgram.h:14