CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
chunk.hpp
1/*
2 * CoDiPack, a Code Differentiation Package
3 *
4 * Copyright (C) 2015-2024 Chair for Scientific Computing (SciComp), University of Kaiserslautern-Landau
5 * Homepage: http://scicomp.rptu.de
6 * Contact: Prof. Nicolas R. Gauger (codi@scicomp.uni-kl.de)
7 *
8 * Lead developers: Max Sagebaum, Johannes Blühdorn (SciComp, University of Kaiserslautern-Landau)
9 *
10 * This file is part of CoDiPack (http://scicomp.rptu.de/software/codi).
11 *
12 * CoDiPack is free software: you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * CoDiPack is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty
19 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * See the GNU General Public License for more details.
22 * You should have received a copy of the GNU
23 * General Public License along with CoDiPack.
24 * If not, see <http://www.gnu.org/licenses/>.
25 *
26 * For other licensing options please contact us.
27 *
28 * Authors:
29 * - SciComp, University of Kaiserslautern-Landau:
30 * - Max Sagebaum
31 * - Johannes Blühdorn
32 * - Former members:
33 * - Tim Albring
34 */
35#pragma once
36
37#include <cstddef>
38
39#include "../../config.h"
40#include "../../misc/fileIo.hpp"
41#include "../../misc/macros.hpp"
42
44namespace codi {
45
76 struct ChunkBase {
77 public:
78
79 /*******************************************************************************/
82
83 static size_t constexpr EntrySize = CODI_UNDEFINED_VALUE;
84
86 /*******************************************************************************/
89
90 template<typename... Data>
91 CODI_INLINE void pushData(Data&&... dataEntries);
93
94 template<typename... Pointers>
95 CODI_INLINE void dataPointer(size_t const& index,
96 Pointers*&... pointers);
98
99 virtual void erase(size_t const& start, size_t const& end) = 0;
100
102 /*******************************************************************************/
105
106 CODI_INLINE virtual void allocateData() = 0;
107 CODI_INLINE virtual void deleteData() = 0;
108 CODI_INLINE virtual void readData(FileIo& handle) = 0;
109 CODI_INLINE virtual void writeData(FileIo& handle) const = 0;
110
112 /*******************************************************************************/
115
117
119 protected:
120 size_t size;
121 size_t usedSize;
122
123 public:
124
126 CODI_INLINE explicit ChunkBase(size_t const& size) : size(size), usedSize(0) {}
127
130
131 /*******************************************************************************/
134
136 CODI_INLINE size_t getSize() const {
137 return size;
138 }
139
141 CODI_INLINE size_t getUnusedSize() const {
142 return size - usedSize;
143 }
144
146 CODI_INLINE size_t getUsedSize() const {
147 return usedSize;
148 }
149
152 usedSize = 0;
153 }
154
156 CODI_INLINE void resize(size_t newSize) {
157 deleteData();
158 size = newSize;
159 usedSize = 0;
160 allocateData();
161 }
162
164 CODI_INLINE void setUsedSize(size_t const& usage) {
165 usedSize = usage;
166 }
167
169
170 protected:
171
174 std::swap(size, other.size);
175 std::swap(usedSize, other.usedSize);
176 }
177 };
178
184 template<typename Data1>
185 struct Chunk1 final : public ChunkBase {
186 public:
187
188 using Base = ChunkBase;
189
190 private:
191
192 Data1* data1;
193
194 public:
195
197 CODI_INLINE Chunk1(size_t const& size) : ChunkBase(size), data1(nullptr) {
198 allocateData();
199 }
200
205
206 /*******************************************************************************/
209
210 static size_t constexpr EntrySize = sizeof(Data1);
211
214 if (nullptr == data1) {
215 data1 = new Data1[size];
216 }
217 }
218
220 CODI_INLINE void dataPointer(size_t const& index, Data1*& pointer1) {
221 codiAssert(index <= ChunkBase::size);
222 pointer1 = &data1[index];
223 }
224
227 if (nullptr != data1) {
228 delete[] data1;
229 data1 = nullptr;
230 }
231 }
232
234 void erase(size_t const& start, size_t const& end) {
235 codiAssert(start <= end);
236 codiAssert(start < usedSize);
237 codiAssert(end <= usedSize);
238
239 if (start != end) {
240 for (size_t i = 0; i < usedSize - end; ++i) {
241 data1[start + i] = data1[end + i];
242 }
243 usedSize -= end - start;
244 }
245 }
246
248 CODI_INLINE void pushData(Data1 const& value1) {
250 data1[usedSize] = value1;
251 usedSize += 1;
252 }
253
256 allocateData();
257
258 handle.readData(data1, size);
259 }
260
263 Base::swap(other);
264
265 std::swap(data1, other.data1);
266 }
267
269 CODI_INLINE void writeData(FileIo& handle) const {
270 handle.writeData(data1, size);
271 }
272
274 };
275
282 template<typename Data1, typename Data2>
283 struct Chunk2 final : public ChunkBase {
284 public:
285
286 using Base = ChunkBase;
287
288 private:
289
290 Data1* data1;
291 Data2* data2;
292
293 public:
294
296 CODI_INLINE Chunk2(size_t const& size) : ChunkBase(size), data1(nullptr), data2(nullptr) {
297 allocateData();
298 }
299
304
305 /*******************************************************************************/
308
309 static size_t constexpr EntrySize = sizeof(Data1) + sizeof(Data2);
310
313 if (nullptr == data1) {
314 data1 = new Data1[size];
315 }
316
317 if (nullptr == data2) {
318 data2 = new Data2[size];
319 }
320 }
321
323 CODI_INLINE void dataPointer(size_t const& index, Data1*& pointer1, Data2*& pointer2) {
324 codiAssert(index <= ChunkBase::size);
325 pointer1 = &data1[index];
326 pointer2 = &data2[index];
327 }
328
331 if (nullptr != data1) {
332 delete[] data1;
333 data1 = nullptr;
334 }
335
336 if (nullptr != data2) {
337 delete[] data2;
338 data2 = nullptr;
339 }
340 }
341
343 void erase(size_t const& start, size_t const& end) {
344 codiAssert(start <= end);
345 codiAssert(start < usedSize);
346 codiAssert(end <= usedSize);
347
348 if (start != end) {
349 for (size_t i = 0; i < usedSize - end; ++i) {
350 data1[start + i] = data1[end + i];
351 }
352 for (size_t i = 0; i < usedSize - end; ++i) {
353 data2[start + i] = data2[end + i];
354 }
355 usedSize -= end - start;
356 }
357 }
358
360 CODI_INLINE void pushData(Data1 const& value1, Data2 const& value2) {
362 data1[usedSize] = value1;
363 data2[usedSize] = value2;
364 usedSize += 1;
365 }
366
369 allocateData();
370
371 handle.readData(data1, size);
372 handle.readData(data2, size);
373 }
374
377 Base::swap(other);
378
379 std::swap(data1, other.data1);
380 std::swap(data2, other.data2);
381 }
382
384 CODI_INLINE void writeData(FileIo& handle) const {
385 handle.writeData(data1, size);
386 handle.writeData(data2, size);
387 }
388
390 };
391
399 template<typename Data1, typename Data2, typename Data3>
400 struct Chunk3 final : public ChunkBase {
401 public:
402
403 using Base = ChunkBase;
404
405 private:
406
407 Data1* data1;
408 Data2* data2;
409 Data3* data3;
410
411 public:
412
414 CODI_INLINE Chunk3(size_t const& size) : ChunkBase(size), data1(nullptr), data2(nullptr), data3(nullptr) {
415 allocateData();
416 }
417
422
423 /*******************************************************************************/
426
427 static size_t constexpr EntrySize =
428 sizeof(Data1) + sizeof(Data2) + sizeof(Data3);
429
432 if (nullptr == data1) {
433 data1 = new Data1[size];
434 }
435
436 if (nullptr == data2) {
437 data2 = new Data2[size];
438 }
439
440 if (nullptr == data3) {
441 data3 = new Data3[size];
442 }
443 }
444
446 CODI_INLINE void dataPointer(size_t const& index, Data1*& pointer1, Data2*& pointer2, Data3*& pointer3) {
447 codiAssert(index <= ChunkBase::size);
448 pointer1 = &data1[index];
449 pointer2 = &data2[index];
450 pointer3 = &data3[index];
451 }
452
455 if (nullptr != data1) {
456 delete[] data1;
457 data1 = nullptr;
458 }
459
460 if (nullptr != data2) {
461 delete[] data2;
462 data2 = nullptr;
463 }
464
465 if (nullptr != data3) {
466 delete[] data3;
467 data3 = nullptr;
468 }
469 }
470
472 void erase(size_t const& start, size_t const& end) {
473 codiAssert(start <= end);
474 codiAssert(start < usedSize);
475 codiAssert(end <= usedSize);
476
477 if (start != end) {
478 for (size_t i = 0; i < usedSize - end; ++i) {
479 data1[start + i] = data1[end + i];
480 }
481 for (size_t i = 0; i < usedSize - end; ++i) {
482 data2[start + i] = data2[end + i];
483 }
484 for (size_t i = 0; i < usedSize - end; ++i) {
485 data3[start + i] = data3[end + i];
486 }
487 usedSize -= end - start;
488 }
489 }
490
492 CODI_INLINE void pushData(Data1 const& value1, Data2 const& value2, Data3 const& value3) {
494 data1[usedSize] = value1;
495 data2[usedSize] = value2;
496 data3[usedSize] = value3;
497 usedSize += 1;
498 }
499
502 allocateData();
503
504 handle.readData(data1, size);
505 handle.readData(data2, size);
506 handle.readData(data3, size);
507 }
508
511 Base::swap(other);
512
513 std::swap(data1, other.data1);
514 std::swap(data2, other.data2);
515 std::swap(data3, other.data3);
516 }
517
519 CODI_INLINE void writeData(FileIo& handle) const {
520 handle.writeData(data1, size);
521 handle.writeData(data2, size);
522 handle.writeData(data3, size);
523 }
524
526 };
527
536 template<typename Data1, typename Data2, typename Data3, typename Data4>
537 struct Chunk4 final : public ChunkBase {
538 public:
539
540 using Base = ChunkBase;
541
542 private:
543
544 Data1* data1;
545 Data2* data2;
546 Data3* data3;
547 Data4* data4;
548
549 public:
550
552 CODI_INLINE Chunk4(size_t const& size)
553 : ChunkBase(size), data1(nullptr), data2(nullptr), data3(nullptr), data4(nullptr) {
554 allocateData();
555 }
556
561
562 /*******************************************************************************/
565
566 static size_t constexpr EntrySize =
567 sizeof(Data1) + sizeof(Data2) + sizeof(Data3) + sizeof(Data4);
568
571 if (nullptr == data1) {
572 data1 = new Data1[size];
573 }
574
575 if (nullptr == data2) {
576 data2 = new Data2[size];
577 }
578
579 if (nullptr == data3) {
580 data3 = new Data3[size];
581 }
582
583 if (nullptr == data4) {
584 data4 = new Data4[size];
585 }
586 }
587
589 CODI_INLINE void dataPointer(size_t const& index, Data1*& pointer1, Data2*& pointer2, Data3*& pointer3,
590 Data4*& pointer4) {
591 codiAssert(index <= ChunkBase::size);
592 pointer1 = &data1[index];
593 pointer2 = &data2[index];
594 pointer3 = &data3[index];
595 pointer4 = &data4[index];
596 }
597
600 if (nullptr != data1) {
601 delete[] data1;
602 data1 = nullptr;
603 }
604
605 if (nullptr != data2) {
606 delete[] data2;
607 data2 = nullptr;
608 }
609
610 if (nullptr != data3) {
611 delete[] data3;
612 data3 = nullptr;
613 }
614
615 if (nullptr != data4) {
616 delete[] data4;
617 data4 = nullptr;
618 }
619 }
620
622 void erase(size_t const& start, size_t const& end) {
623 codiAssert(start <= end);
624 codiAssert(start < usedSize);
625 codiAssert(end <= usedSize);
626
627 if (start != end) {
628 for (size_t i = 0; i < usedSize - end; ++i) {
629 data1[start + i] = data1[end + i];
630 }
631 for (size_t i = 0; i < usedSize - end; ++i) {
632 data2[start + i] = data2[end + i];
633 }
634 for (size_t i = 0; i < usedSize - end; ++i) {
635 data3[start + i] = data3[end + i];
636 }
637 for (size_t i = 0; i < usedSize - end; ++i) {
638 data4[start + i] = data4[end + i];
639 }
640 usedSize -= end - start;
641 }
642 }
643
645 CODI_INLINE void pushData(Data1 const& value1, Data2 const& value2, Data3 const& value3, Data4 const& value4) {
647 data1[usedSize] = value1;
648 data2[usedSize] = value2;
649 data3[usedSize] = value3;
650 data4[usedSize] = value4;
651 usedSize += 1;
652 }
653
656 allocateData();
657
658 handle.readData(data1, size);
659 handle.readData(data2, size);
660 handle.readData(data3, size);
661 handle.readData(data4, size);
662 }
663
666 Base::swap(other);
667
668 std::swap(data1, other.data1);
669 std::swap(data2, other.data2);
670 std::swap(data3, other.data3);
671 std::swap(data4, other.data4);
672 }
673
675 CODI_INLINE void writeData(FileIo& handle) const {
676 handle.writeData(data1, size);
677 handle.writeData(data2, size);
678 handle.writeData(data3, size);
679 handle.writeData(data4, size);
680 }
681
683 };
684
685}
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:457
#define codiAssert(x)
See codi::Config::EnableAssert.
Definition config.h:432
#define CODI_IMPLEMENTATION
Used in interface declarations to indicate the type of the implementing class.
Definition macros.hpp:105
#define CODI_UNDEFINED_VALUE
Used in interface declarations for variables that have to be defined in the specializations.
Definition macros.hpp:117
CoDiPack - Code Differentiation Package.
Definition codi.hpp:90
Definition chunk.hpp:185
static size_t constexpr EntrySize
Total size of all data in one entry.
Definition chunk.hpp:210
~Chunk1()
Destructor.
Definition chunk.hpp:202
void pushData(Data1 const &value1)
provided.
Definition chunk.hpp:248
void erase(size_t const &start, size_t const &end)
Erase data items start, ..., end - 1.
Definition chunk.hpp:234
void writeData(FileIo &handle) const
Write data to the FileIo handle.
Definition chunk.hpp:269
void swap(Chunk1< Data1 > &other)
Swap data with other chunk of the same type.
Definition chunk.hpp:262
void readData(FileIo &handle)
Read data from the FileIo handle.
Definition chunk.hpp:255
void deleteData()
Delete the allocated data.
Definition chunk.hpp:226
void dataPointer(size_t const &index, Data1 *&pointer1)
Definition chunk.hpp:220
Chunk1(size_t const &size)
Constructor.
Definition chunk.hpp:197
void allocateData()
Allocated the data if it was deallocated before.
Definition chunk.hpp:213
Definition chunk.hpp:283
void writeData(FileIo &handle) const
Write data to the FileIo handle.
Definition chunk.hpp:384
void swap(Chunk2< Data1, Data2 > &other)
Swap data with other chunk of the same type.
Definition chunk.hpp:376
void dataPointer(size_t const &index, Data1 *&pointer1, Data2 *&pointer2)
Definition chunk.hpp:323
~Chunk2()
Destructor.
Definition chunk.hpp:301
void deleteData()
Delete the allocated data.
Definition chunk.hpp:330
Chunk2(size_t const &size)
Constructor.
Definition chunk.hpp:296
static size_t constexpr EntrySize
Total size of all data in one entry.
Definition chunk.hpp:309
void pushData(Data1 const &value1, Data2 const &value2)
provided.
Definition chunk.hpp:360
void readData(FileIo &handle)
Read data from the FileIo handle.
Definition chunk.hpp:368
void allocateData()
Allocated the data if it was deallocated before.
Definition chunk.hpp:312
void erase(size_t const &start, size_t const &end)
Erase data items start, ..., end - 1.
Definition chunk.hpp:343
Definition chunk.hpp:400
void dataPointer(size_t const &index, Data1 *&pointer1, Data2 *&pointer2, Data3 *&pointer3)
Definition chunk.hpp:446
void readData(FileIo &handle)
Read data from the FileIo handle.
Definition chunk.hpp:501
void erase(size_t const &start, size_t const &end)
Erase data items start, ..., end - 1.
Definition chunk.hpp:472
void pushData(Data1 const &value1, Data2 const &value2, Data3 const &value3)
provided.
Definition chunk.hpp:492
void writeData(FileIo &handle) const
Write data to the FileIo handle.
Definition chunk.hpp:519
void swap(Chunk3< Data1, Data2, Data3 > &other)
Swap data with other chunk of the same type.
Definition chunk.hpp:510
void deleteData()
Delete the allocated data.
Definition chunk.hpp:454
static size_t constexpr EntrySize
Total size of all data in one entry.
Definition chunk.hpp:427
Chunk3(size_t const &size)
Constructor.
Definition chunk.hpp:414
~Chunk3()
Destructor.
Definition chunk.hpp:419
void allocateData()
Allocated the data if it was deallocated before.
Definition chunk.hpp:431
Definition chunk.hpp:537
void readData(FileIo &handle)
Read data from the FileIo handle.
Definition chunk.hpp:655
static size_t constexpr EntrySize
Total size of all data in one entry.
Definition chunk.hpp:566
void deleteData()
Delete the allocated data.
Definition chunk.hpp:599
void writeData(FileIo &handle) const
Write data to the FileIo handle.
Definition chunk.hpp:675
void pushData(Data1 const &value1, Data2 const &value2, Data3 const &value3, Data4 const &value4)
provided.
Definition chunk.hpp:645
void swap(Chunk4< Data1, Data2, Data3, Data4 > &other)
Swap data with other chunk of the same type.
Definition chunk.hpp:665
void dataPointer(size_t const &index, Data1 *&pointer1, Data2 *&pointer2, Data3 *&pointer3, Data4 *&pointer4)
Definition chunk.hpp:589
~Chunk4()
Destructor.
Definition chunk.hpp:558
void erase(size_t const &start, size_t const &end)
Erase data items start, ..., end - 1.
Definition chunk.hpp:622
Chunk4(size_t const &size)
Constructor.
Definition chunk.hpp:552
void allocateData()
Allocated the data if it was deallocated before.
Definition chunk.hpp:570
A chunk stores a contiguous block of data in CoDiPack.
Definition chunk.hpp:76
size_t size
Maximum size of arrays.
Definition chunk.hpp:120
size_t usedSize
Currently used size.
Definition chunk.hpp:121
virtual void allocateData()=0
Allocated the data if it was deallocated before.
virtual void deleteData()=0
Delete the allocated data.
ChunkBase(size_t const &size)
Constructor.
Definition chunk.hpp:126
virtual void readData(FileIo &handle)=0
Read data from the FileIo handle.
virtual void erase(size_t const &start, size_t const &end)=0
Erase data items start, ..., end - 1.
size_t getUnusedSize() const
Number of unused data items.
Definition chunk.hpp:141
void swap(ChunkBase &other)
Swap the entries of this base class.
Definition chunk.hpp:173
virtual void writeData(FileIo &handle) const =0
Write data to the FileIo handle.
void setUsedSize(size_t const &usage)
Set the used size.
Definition chunk.hpp:164
virtual ~ChunkBase()
Destructor.
Definition chunk.hpp:129
void swap(ImplProxy &other)
Swap data with other chunk of the same type.
void resize(size_t newSize)
Resize the allocated data. Stored data is lost. Used size is set to zero.
Definition chunk.hpp:156
static size_t constexpr EntrySize
Total size of all data in one entry.
Definition chunk.hpp:83
void pushData(Data &&... dataEntries)
provided.
size_t getSize() const
Get the allocated size.
Definition chunk.hpp:136
void dataPointer(size_t const &index, Pointers *&... pointers)
void reset()
Sets the number of used items to zero.
Definition chunk.hpp:151
size_t getUsedSize() const
Number of used data items.
Definition chunk.hpp:146
Helper structure for writing binary data.
Definition fileIo.hpp:84
void writeData(Data const *data, size_t const length)
Definition fileIo.hpp:119
void readData(Data *data, size_t const length)
Definition fileIo.hpp:134