Nektar++
Loading...
Searching...
No Matches
LibUtilities/BasicUtils/Field/Field.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: Field.hpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description:
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#pragma once
36
39
40#include <memory>
41
43{
44
45template <typename TData, FieldState TState> class MultiField;
46
47/**
48 * @brief A Field represents expansion data to be operated on.
49 *
50 * @tparam TData The floating-point representation used by the field.
51 * @tparam TState A FieldState value representing the state of the field.
52 */
53template <typename TData, FieldState TState> class Field
54{
55 template <typename TDataField, FieldState TStateField>
56 friend class BlockAccessor;
57
58public:
59 Field() = default;
61 {
63 }
64
65 /**
66 * @brief Construct a new Field object.
67 *
68 * @param name - Name of the field object.
69 * @param blockAttr - Block attributes.
70 * @param num_components - Number of components.
71 * @param num_homo_modes - Number of homogeneous modes.
72 * @param memAllocType - [ eHostPageable, eHostPinned, eDeviceMemoryPool,
73 * eHostPinnedDeviceMemoryPool]
74 * @param alignment - Memory alignment to use.
75 */
76 Field(const std::string name,
77 const std::vector<BlockAttributes<TState>> blockAttr,
78 const unsigned int num_components, const unsigned int num_homo_modes,
79 const MemAllocType &memAllocType = eHostPageable,
80 const size_t alignment = NektarSpaces::host_memory_alignment)
81 : m_instantiated(true), m_name(name), m_component_names(num_components),
82 m_num_homo_modes(num_homo_modes), m_memAllocType(memAllocType),
83 m_alignment(alignment)
84 {
85 for (unsigned int blk = 0; blk < blockAttr.size(); ++blk)
86 {
87 auto nsize =
88 blockAttr[blk].CompSize() * num_components * num_homo_modes;
89 auto mr = MemoryRegion<TData>(name + std::to_string(blk), nsize,
90 memAllocType, alignment);
91 this->GetBlocks().push_back(
92 BlockAccessor(blockAttr[blk], std::move(mr), this,
93 num_components, num_homo_modes));
94 }
95 }
96
97 /**
98 * @brief Construct a new Field object.
99 *
100 * @param blockAttr - Block attributes.
101 * @param num_components - Number of components.
102 * @param num_homo_modes - Number of homogeneous modes.
103 * @param memAllocType - [ eHostPageable, eHostPinned, eDeviceMemoryPool,
104 * eHostPinnedDeviceMemoryPool]
105 * @param alignment - Memory alignment to use.
106 */
107 Field(const std::vector<BlockAttributes<TState>> blockAttr,
108 const unsigned int num_components, const unsigned int num_homo_modes,
109 const MemAllocType &memAllocType = eHostPageable,
110 const size_t alignment = NektarSpaces::host_memory_alignment)
111 : Field<TData, TState>("", blockAttr, num_components, num_homo_modes,
112 memAllocType, alignment)
113 {
114 }
115
116 /**
117 * @brief Construct a new Field object.
118 *
119 * @param name - Name of the field object.
120 * @param blockAttr - Block attributes.
121 * @param components - Names of components for vector field.
122 * @param num_homo_modes - Number of homogeneous modes.
123 * @param memAllocType - [ eHostPageable, eHostPinned, eDeviceMemoryPool,
124 * eHostPinnedDeviceMemoryPool]
125 * @param alignment - Memory alignment to use.
126 */
127 Field(const std::string name,
128 const std::vector<BlockAttributes<TState>> blockAttr,
129 const std::vector<std::string> components,
130 const unsigned int num_homo_modes,
131 const MemAllocType &memAllocType = eHostPageable,
132 const size_t alignment = NektarSpaces::host_memory_alignment)
133 : m_instantiated(true), m_name(name), m_component_names(components),
134 m_num_homo_modes(num_homo_modes), m_memAllocType(memAllocType),
135 m_alignment(alignment)
136 {
137 for (unsigned int blk = 0; blk < blockAttr.size(); ++blk)
138 {
139 auto nsize =
140 blockAttr[blk].CompSize() * components.size() * num_homo_modes;
141 auto mr = MemoryRegion<TData>(name + std::to_string(blk), nsize,
142 memAllocType, alignment);
143 this->GetBlocks().push_back(
144 BlockAccessor(blockAttr[blk], std::move(mr), this,
145 components.size(), num_homo_modes));
146 }
147 }
148
149 /**
150 * @brief Construct a new Field object.
151 *
152 * @param blockAttr - Block attributes.
153 * @param components - Names of components for vector field.
154 * @param num_homo_modes - Number of homogeneous modes.
155 * @param memAllocType - [ eHostPageable, eHostPinned, eDeviceMemoryPool,
156 * eHostPinnedDeviceMemoryPool]
157 * @param alignment - Memory alignment to use.
158 */
159 Field(const std::vector<BlockAttributes<TState>> blockAttr,
160 const std::vector<std::string> components,
161 const unsigned int num_homo_modes,
162 const MemAllocType &memAllocType = eHostPageable,
163 const size_t alignment = NektarSpaces::host_memory_alignment)
164 : Field<TData, TState>("", blockAttr, components, num_homo_modes,
165 memAllocType, alignment)
166 {
167 }
168
169 /**
170 * @brief Construct a new Field object by moving storage from an
171 * existing Field object.
172 *
173 * @param rhs
174 */
176 : m_instantiated(std::move(rhs.m_instantiated)),
177 m_name(std::move(rhs.m_name)),
180 m_host(std::move(rhs.m_host)), m_device(std::move(rhs.m_device)),
183 m_alignment(std::move(rhs.m_alignment)),
186 {
187 for (auto &blocks : m_block_accessors)
188 {
189 blocks.m_field = this;
190 }
191 rhs.m_instantiated = false;
192 rhs.m_name = "";
193 rhs.m_component_names.clear();
194 rhs.m_num_homo_modes = 1;
195 rhs.m_host = nullptr;
196 rhs.m_device = nullptr;
197 rhs.m_block_accessors.clear();
198 rhs.m_memAllocType = eHostPageable;
199 rhs.m_alignment = NektarSpaces::host_memory_alignment;
200 rhs.m_sharedStorageOffset = 0;
201 }
202
203 /**
204 * @brief Constructor methods - No copy methods
205 *
206 */
207 Field(const Field &) = delete;
208 Field &operator=(const Field &) = delete;
209
210 /**
211 * @brief Move assignment operator.
212 *
213 * @param rhs
214 *
215 * @return Field&
216 */
218 {
219 if (this == &rhs)
220 {
221 return *this;
222 }
223
224 // The contiguous storage this field already owns is released before
225 // it takes over the storage of rhs; its blocks only point into it, so
226 // replacing the blocks and pointers alone would leak it. This has to
227 // come before the blocks are replaced: the device size is taken from
228 // them.
230
231 m_instantiated = std::move(rhs.m_instantiated);
232 m_name = std::move(rhs.m_name);
233 m_component_names = std::move(rhs.m_component_names);
234 m_num_homo_modes = std::move(rhs.m_num_homo_modes);
235 m_host = std::move(rhs.m_host);
236 m_device = std::move(rhs.m_device);
237 m_memAllocType = std::move(rhs.m_memAllocType);
238 m_block_accessors = std::move(rhs.m_block_accessors);
239 m_alignment = std::move(rhs.m_alignment);
240 m_sharedStorage = std::move(rhs.m_sharedStorage);
241 m_sharedStorageOffset = std::move(rhs.m_sharedStorageOffset);
242 for (auto &blocks : m_block_accessors)
243 {
244 blocks.m_field = this;
245 }
246
247 rhs.m_instantiated = false;
248 rhs.m_name = "";
249 rhs.m_component_names.clear();
250 rhs.m_num_homo_modes = 1;
251 rhs.m_host = nullptr;
252 rhs.m_device = nullptr;
253 rhs.m_block_accessors.clear();
254 rhs.m_memAllocType = eHostPageable;
255 rhs.m_alignment = NektarSpaces::host_memory_alignment;
256 rhs.m_sharedStorageOffset = 0;
257 return *this;
258 }
259
260 /**
261 * @brief Whether the field has been given storage by a custom
262 * constructor.
263 *
264 * A default-constructed or moved-from field is not instantiated, and
265 * every other accessor errors on it - this is the one query that is
266 * valid either way, so a caller can test with `if (field)` before
267 * touching.
268 */
269 explicit operator bool() const
270 {
271 return m_instantiated;
272 }
273
274 std::vector<unsigned int> GetInterleaveWidth(void) const
275 {
276 if (!m_instantiated)
277 {
279 "Field::GetInterleaveWidth - Object has not been "
280 "instantiated by custom constructor.");
281 }
282
283 std::vector<unsigned int> interleaveWidths;
284 for (unsigned int blk = 0; blk < this->m_block_accessors.size(); ++blk)
285 {
286 interleaveWidths.push_back(
288 }
289 return interleaveWidths;
290 }
291
292 void SetInterleaveWidth(const unsigned int &interleaveWidth)
293 {
294 for (unsigned int blk = 0; blk < this->GetBlocks().size(); ++blk)
295 {
296 this->GetBlocks()[blk].template SetInterleaveWidth<TData>(
297 interleaveWidth);
298 }
299 }
300
301 void SetInterleaveWidth(const std::vector<unsigned int> &interleaveWidths)
302 {
303 ASSERTL1(interleaveWidths.size() == this->GetBlocks().size(),
304 "Number of interleave widths does not match number of "
305 "blocks.");
306 for (unsigned int blk = 0; blk < this->GetBlocks().size(); ++blk)
307 {
308 this->GetBlocks()[blk].template SetInterleaveWidth<TData>(
309 interleaveWidths[blk]);
310 }
311 }
312
314 {
315 for (unsigned int blk = 0; blk < this->GetBlocks().size(); ++blk)
316 {
317 this->GetBlocks()[blk].template SetInterleaveWidth<TData>(
318 field.GetBlocks()[blk].GetInterleaveWidth());
319 }
320 }
321
322 void ReshapeStorage(const unsigned int &interleaveWidth,
323 const std::string &execSpace)
324 {
325 for (unsigned int blk = 0; blk < this->GetBlocks().size(); ++blk)
326 {
327 const unsigned int streamID = blk + 1;
328 this->GetBlocks()[blk].ReshapeStorage(interleaveWidth, execSpace,
329 streamID);
330 }
331 }
332
333 void ReshapeStorage(const std::vector<unsigned int> &interleaveWidths,
334 const std::string &execSpace)
335 {
336 ASSERTL1(interleaveWidths.size() == this->GetBlocks().size(),
337 "Number of interleave widths does not match number of "
338 "blocks.");
339 for (unsigned int blk = 0; blk < this->GetBlocks().size(); ++blk)
340 {
341 const unsigned int streamID = blk + 1;
342 this->GetBlocks()[blk].ReshapeStorage(interleaveWidths[blk],
343 execSpace, streamID);
344 }
345 }
346
347 /**
348 * @brief Templated initialize method: set every entry to @p val, but the
349 * padding, which is zero.
350 *
351 * @param val - Value to set.
352 * @param streamID - Stream every block is set on. 0, the default, sets
353 * each block on its own stream (block index + 1).
354 */
355 template <typename MemSpace>
356 void Initialize(const TData val, const unsigned int streamID = 0)
357 {
358 if (!m_instantiated)
359 {
361 "Field::Initialize - Object has not been "
362 "instantiated by custom constructor.");
363 }
364
365 // If not yet allocated, allocate contiguous host OR device memory
366 // across all MemoryRegion objects within current Field object before
367 // data initialization.
368 AllocateFieldStorage<MemSpace>(this);
369
370 for (unsigned int blk = 0; blk < this->GetBlocks().size(); ++blk)
371 {
372 const unsigned int blkStreamID = streamID == 0 ? blk + 1 : streamID;
373 auto &block = this->GetBlocks()[blk];
374 block.m_memory_region.template Initialize<MemSpace>(val, 0, 0,
375 blkStreamID);
376 if (val != TData(0))
377 {
378 ZeroPadding<MemSpace>(block, blkStreamID);
379 }
380 }
381 }
382
383 /**
384 * @brief Copy the data to a std::vector, component by component and
385 * element by element, without padding, whatever the interleave width of
386 * each block.
387 *
388 * @return std::vector
389 */
390 template <typename TDataOut = TData, class Alloc = std::allocator<TDataOut>>
391 std::vector<TDataOut, Alloc> ToVector()
392 {
393 if (!m_instantiated)
394 {
396 "Field::ToVector - Object has not been "
397 "instantiated by custom constructor.");
398 }
399
400 // If not yet allocated, allocate contiguous host memory across all
401 // MemoryRegion objects within current Field objects before data copy to
402 // vector.
403 AllocateFieldStorage<NektarSpaces::HostSpace>(this);
404
405 unsigned int compSize = 0;
406 for (auto &block : this->GetBlocks())
407 {
408 compSize += block.GetNumData() * block.GetNumElements();
409 }
410
411 std::vector<TDataOut, Alloc> array(compSize * this->GetNumComponents() *
412 this->GetNumHomoModes());
413
414 DeInterleaveToHostPtr(array.data(), compSize);
415
416 return array;
417 }
418
419 /**
420 * @brief Copy the data to a Nektar::Array, component by component and
421 * element by element, without padding, whatever the interleave width of
422 * each block.
423 *
424 * @return Array<Nektar::OneD, TDataOut>
425 */
426 template <typename TDataOut = TData>
428 {
429 if (!m_instantiated)
430 {
432 "Field::ToArray - Object has not been "
433 "instantiated by custom constructor.");
434 }
435
436 // If not yet allocated, allocate contiguous host memory across all
437 // MemoryRegion objects within current Field objects before data copy to
438 // NekArray.
439 AllocateFieldStorage<NektarSpaces::HostSpace>(this);
440
441 unsigned int compSize = 0;
442 for (auto &block : this->GetBlocks())
443 {
444 compSize += block.GetNumData() * block.GetNumElements();
445 }
446
448 compSize * this->GetNumComponents() * this->GetNumHomoModes());
449
450 DeInterleaveToHostPtr(array.data(), compSize);
451
452 return array;
453 }
454
455 /**
456 * @brief Templated copy method. This method copies data from a
457 * Field
458 *
459 * @param field - Field to copy from
460 *
461 */
462 template <typename MemSpace> void Copy(Field &field)
463 {
464 if (!m_instantiated)
465 {
467 "Field::Copy - Object has not been "
468 "instantiated by custom constructor.");
469 }
470
471 // If not yet allocated, allocate contiguous host OR device memory
472 // across all MemoryRegion objects within the input argument field
473 // object before data copy.
474 AllocateFieldStorage<MemSpace>(&field);
475
476 // If not yet allocated, allocate contiguous host OR device memory
477 // across all MemoryRegion objects within current Field object before
478 // data copy from input argument field object.
479 AllocateFieldStorage<MemSpace>(this);
480
481 if (this->GetBlocks().size() != field.GetBlocks().size())
482 {
483 std::stringstream msg;
484
485 msg << "Field::Copy - "
486 << "Block number mismatch between (" << field.GetName()
487 << ") and (" << this->GetName() << ").";
489 }
490
491 if (this->size() != field.size())
492 {
493 std::stringstream msg;
494
495 msg << "Field::Copy - "
496 << "Memory size mismatch between (" << field.GetName()
497 << ") and (" << this->GetName() << ").";
499 }
500
501 for (unsigned int blk = 0; blk < this->GetBlocks().size(); ++blk)
502 {
503 const unsigned int streamID = blk + 1;
504 this->GetBlocks()[blk].m_memory_region.template Copy<MemSpace>(
505 field.GetBlocks()[blk].m_memory_region, streamID);
506 this->GetBlocks()[blk].template SetInterleaveWidth<TData>(
507 field.GetBlocks()[blk].GetInterleaveWidth());
508 }
509 }
510
511 /**
512 * @brief Templated copy method. This method copies data from a
513 * std::vector laid out as ToVector() returns it, and interleaves
514 * it to the width of each block.
515 *
516 * @param array - std::vector to copy from
517 *
518 */
519 template <typename MemSpace, typename TDataIn,
520 class Alloc = std::allocator<TDataIn>>
521 void CopyVector(const std::vector<TDataIn, Alloc> &array)
522 {
523 if (!m_instantiated)
524 {
526 "Field::CopyVector - Object has not been "
527 "instantiated by custom constructor.");
528 }
529
530 // If not yet allocated, allocate contiguous host memory across all
531 // MemoryRegion objects within current Field object before data copy
532 // from vector.
533 AllocateFieldStorage<MemSpace>(this);
534
535 unsigned int compSize = 0;
536 for (auto &block : this->GetBlocks())
537 {
538 compSize += block.GetNumData() * block.GetNumElements();
539 }
540
541 if (compSize * this->GetNumComponents() * this->GetNumHomoModes() !=
542 array.size())
543 {
544 std::stringstream msg;
545
546 msg << "Field::CopyVector - "
547 << "Memory size mismatch between (std::vector) and ("
548 << this->GetName() << ").";
550 }
551
552 InterleaveFromHostPtr<MemSpace>(array.data(), compSize);
553 }
554
555 /**
556 * @brief Templated copy method. This method copies data from a
557 * Nektar::Array<Nektar::OneD, TDataIn> laid out as ToArray()
558 * returns it, and interleaves it to the width of each block.
559 *
560 * @param array - Nektar::Array to copy from
561 *
562 */
563 template <typename MemSpace, typename TDataIn>
565 {
566 if (!m_instantiated)
567 {
569 "Field::CopyArray - Object has not been "
570 "instantiated by custom constructor.");
571 }
572
573 // If not yet allocated, allocate contiguous host memory across all
574 // MemoryRegion objects within current Field object before data copy
575 // from NekArray.
576 AllocateFieldStorage<MemSpace>(this);
577
578 unsigned int compSize = 0;
579 for (auto &block : this->GetBlocks())
580 {
581 compSize += block.GetNumData() * block.GetNumElements();
582 }
583
584 if (compSize * this->GetNumComponents() * this->GetNumHomoModes() !=
585 array.size())
586 {
587 std::stringstream msg;
588
589 msg << "Field::CopyArray - "
590 << "Memory size mismatch between (Nektar::array) and ("
591 << this->GetName() << ").";
593 }
594
595 InterleaveFromHostPtr<MemSpace>(array.data(), compSize);
596 }
597
598 /**
599 * @brief Get BlockAccessors for the field.
600 *
601 * @return std::vector<BlockAccessor>
602 */
603 std::vector<BlockAccessor<TData, TState>> &GetBlocks()
604 {
605 if (!m_instantiated)
606 {
608 "Field::GetBlocks - Object has not been "
609 "instantiated by custom constructor.");
610 }
611
612 return m_block_accessors;
613 }
614
615 /**
616 * @brief Gets the size of the field.
617 *
618 * @return size_t
619 */
620 size_t size() const
621 {
622 if (!m_instantiated)
623 {
625 "Field::size - Object has not been "
626 "instantiated by custom constructor.");
627 }
628
629 size_t nSize = 0;
630 for (unsigned int blk = 0; blk < this->m_block_accessors.size(); ++blk)
631 {
632 nSize += this->m_block_accessors[blk].m_memory_region.size();
633 }
634 return nSize;
635 }
636
637 /**
638 * @brief Gets the name of the field.
639 *
640 * @return std::string
641 */
642 std::string GetName(void) const
643 {
644 if (!m_instantiated)
645 {
647 "Field::GetName - Object has not been "
648 "instantiated by custom constructor.");
649 }
650
651 return m_name;
652 }
653
654 /**
655 * @brief Gets the number of components for a vector field.
656 *
657 * @return unsigned int
658 */
659 unsigned int GetNumComponents() const
660 {
661 if (!m_instantiated)
662 {
664 "Field::GetNumComponents - Object has not been "
665 "instantiated by custom constructor.");
666 }
667
668 return m_component_names.size();
669 }
670
671 /**
672 * @brief Gets a copy of the components names for a vector field.
673 *
674 * @return std::vector<std::string>
675 */
676 std::vector<std::string> GetComponentNames() const
677 {
678 if (!m_instantiated)
679 {
681 "Field::GetComponentNames - Object has not been "
682 "instantiated by custom constructor.");
683 }
684
685 return m_component_names;
686 }
687
688 /**
689 * @brief Gets the number of homogeneous modes.
690 *
691 * @return unsigned int
692 */
693 unsigned int GetNumHomoModes() const
694 {
695 if (!m_instantiated)
696 {
698 "Field::GetNumHomoModes - Object has not been "
699 "instantiated by custom constructor.");
700 }
701
702 return m_num_homo_modes;
703 }
704
705 typedef TData value_type;
706
707protected:
708 /**
709 * @brief Free the contiguous host and device storage this field owns,
710 * allocated by AllocateFieldStorage(), and mark the field uninstantiated.
711 *
712 * The blocks' memory regions only point into this storage and do not own
713 * it; storage a block allocated for itself is freed with the block.
714 * Shared storage is not freed here, only released by this field.
715 */
717 {
718 if (m_sharedStorage)
719 {
720 m_sharedStorage.reset();
721 }
722 else
723 {
724 if (m_device)
725 {
726 const unsigned int streamID = 0;
727 deviceFree(m_device, this->size() * sizeof(TData), streamID,
729 nekStreamSynchronize(streamID);
730 }
731
732 if (m_host)
733 {
735 {
737 }
738 else if (m_memAllocType == eHostPinned)
739 {
741 }
742 }
743 }
744
745 m_instantiated = false;
746 m_host = nullptr;
747 m_device = nullptr;
750 }
751
752 /**
753 * @brief Allocate contiguous host OR device memory accross all MemoryRegion
754 * objects belonging to a Field object pointer.
755 *
756 * @param field - Field object pointer.
757 */
758 template <typename MemSpace>
760 {
761 if constexpr (std::is_same_v<MemSpace, NektarSpaces::HostSpace>)
762 {
763 size_t alignment_offset = 0;
764
765 // Point the blocks into the shared storage, if any.
766 if (!field->m_host && field->m_sharedStorage)
767 {
768 auto src = field->m_sharedStorage->template GetPtr<MemSpace>() +
769 field->m_sharedStorageOffset;
770 field->m_host = src;
771
772 for (unsigned int blk = 0; blk < field->GetBlocks().size();
773 ++blk)
774 {
775 field->GetBlocks()[blk].m_memory_region.SetHostStorage(src);
776 src += field->GetBlocks()[blk].m_memory_region.size();
777 }
778 }
779
780 // Allocate contiguous host memory accross all MemoryRegions of the
781 // field object.
782 if (!field->m_host)
783 {
784 if (field->m_memAllocType == eHostPageable)
785 {
786 hostMalloc(&field->m_host, field->size() * sizeof(TData),
787 field->m_alignment);
788 }
789 else if (field->m_memAllocType == eHostPinned)
790 {
791 // Add extra bytes for alignment provision
792 size_t aligned_bytes_size =
793 field->size() * sizeof(TData) + field->m_alignment;
794 hostMallocPinned(&field->m_host, aligned_bytes_size);
795 // Compute offset in bytes for non-aligned memory.
796 if ((size_t)field->m_host % field->m_alignment)
797 {
798 alignment_offset =
799 field->m_alignment -
800 (size_t)field->m_host % field->m_alignment;
801 }
802 }
803 // Get aligned memory pointer.
804 auto src = (TData *)((size_t)field->m_host + alignment_offset);
805 std::memset((void *)src, 0, field->size() * sizeof(TData));
806
807 for (unsigned int blk = 0; blk < field->GetBlocks().size();
808 ++blk)
809 {
810 field->GetBlocks()[blk].m_memory_region.SetHostStorage(src);
811 src += field->GetBlocks()[blk].m_memory_region.size();
812 }
813 }
814 }
815 else if constexpr (std::is_same_v<MemSpace, NektarSpaces::DeviceSpace>)
816 {
817 // Point the blocks into the shared storage, if any.
818 if (!field->m_device && field->m_sharedStorage)
819 {
820 auto src = field->m_sharedStorage->template GetPtr<MemSpace>() +
821 field->m_sharedStorageOffset;
822 field->m_device = src;
823
824 for (unsigned int blk = 0; blk < field->GetBlocks().size();
825 ++blk)
826 {
827 field->GetBlocks()[blk].m_memory_region.SetDeviceStorage(
828 src);
829 src += field->GetBlocks()[blk].m_memory_region.size();
830 }
831 }
832
833 // Allocate contiguous device memory accross all MemoryRegions of
834 // the field object.
835 if (!field->m_device)
836 {
837 const unsigned int streamID = 0;
838 deviceMalloc(&field->m_device, field->size() * sizeof(TData),
839 streamID, field->m_memAllocType);
840 deviceMemset(field->m_device, 0, field->size() * sizeof(TData),
841 streamID);
842 nekStreamSynchronize(streamID);
843
844 auto src = field->m_device;
845 for (unsigned int blk = 0; blk < field->GetBlocks().size();
846 ++blk)
847 {
848 field->GetBlocks()[blk].m_memory_region.SetDeviceStorage(
849 src);
850 src += field->GetBlocks()[blk].m_memory_region.size();
851 }
852 }
853 }
854 }
855
856 /**
857 * @brief Copy the data to host memory, component by component and element
858 * by element, without padding. Each block is de-interleaved from its own
859 * interleave width during the copy; the field itself is left unchanged.
860 *
861 * @param dst - host memory to copy to
862 * @param compSize - number of values of one component in @p dst
863 */
864 template <typename TDataOut>
865 void DeInterleaveToHostPtr(TDataOut *dst, const size_t compSize)
866 {
867 const unsigned int nCompHomo =
868 this->GetNumComponents() * this->GetNumHomoModes();
869 for (auto &block : this->GetBlocks())
870 {
871 auto src =
872 block.template GetPtr<NektarSpaces::HostSpace, ReadOnly>();
873 const size_t nSize = block.CompSize();
874 const size_t nElmts = block.GetNumElements();
875 const unsigned int npts = block.GetNumData();
876 const unsigned int interleaveWidth = block.GetInterleaveWidth();
877 const unsigned int elmtGroupSize = npts * interleaveWidth;
878 const size_t numElmtGroups =
879 (nElmts + interleaveWidth - 1) / interleaveWidth;
880 for (unsigned int n = 0; n < nCompHomo; ++n)
881 {
882 if (interleaveWidth == 1)
883 {
884 std::copy(src, src + nElmts * npts, dst + n * compSize);
885 }
886 else
887 {
888 for (size_t e = 0; e < numElmtGroups; ++e)
889 {
890 for (unsigned int vecElem = 0;
891 vecElem < interleaveWidth &&
892 e * interleaveWidth + vecElem < nElmts;
893 ++vecElem)
894 {
895 const TData *elmtSrc =
896 src + e * elmtGroupSize + vecElem;
897 TDataOut *elmtDst =
898 dst + n * compSize +
899 (e * interleaveWidth + vecElem) * npts;
900 for (unsigned int idx = 0; idx < npts; ++idx)
901 {
902 elmtDst[idx] = elmtSrc[idx * interleaveWidth];
903 }
904 }
905 }
906 }
907 src += nSize;
908 }
909 dst += nElmts * npts;
910 }
911 }
912
913 /**
914 * @brief Copy data from host memory, laid out component by component and
915 * element by element without padding, to the storage in @p MemSpace. Each
916 * block is copied with an interleave width of 1 and its storage is then
917 * reshaped back to its own interleave width.
918 *
919 * @param src - host memory to copy from
920 * @param compSize - number of values of one component in @p src
921 */
922 template <typename MemSpace, typename TDataIn>
923 void InterleaveFromHostPtr(const TDataIn *src, const size_t compSize)
924 {
925 const unsigned int nCompHomo =
926 this->GetNumComponents() * this->GetNumHomoModes();
927 const std::string execSpace =
928 std::is_same_v<MemSpace, NektarSpaces::HostSpace> ? "Serial"
929 : "Device";
930 for (unsigned int blk = 0; blk < this->GetBlocks().size(); ++blk)
931 {
932 const unsigned int streamID = blk + 1;
933 auto &block = this->GetBlocks()[blk];
934 const size_t nSize = block.CompSize();
935 const size_t nElmts = block.GetNumElements();
936 const unsigned int npts = block.GetNumData();
937 const unsigned int interleaveWidth = block.GetInterleaveWidth();
938 for (unsigned int n = 0; n < nCompHomo; ++n)
939 {
940 block.m_memory_region.template CopyFromHostPtr<MemSpace>(
941 src + n * compSize, nElmts * npts, n * nSize, streamID);
942 }
943 if (interleaveWidth != 1)
944 {
945 block.template SetInterleaveWidth<TData>(1);
946 block.ReshapeStorage(interleaveWidth, execSpace, streamID);
947 }
948 src += nElmts * npts;
949 }
950 }
951
952 // Member variables:
953 bool m_instantiated = false; ///< Flag indicating if the current object has
954 ///< been instantiated.
955 std::string m_name;
956 std::vector<std::string> m_component_names;
957 unsigned int m_num_homo_modes = 1;
958 TData *m_host = nullptr;
959 TData *m_device = nullptr;
960 std::vector<BlockAccessor<TData, TState>> m_block_accessors;
963 /// Shared storage the field points into, if any.
964 std::shared_ptr<SharedFieldStorage<TData>> m_sharedStorage;
965 /// Offset of the field in m_sharedStorage.
967
968private:
969 /**
970 * @brief Zero the padding of @p block in place, in @p MemSpace.
971 */
972 template <typename MemSpace>
974 const unsigned int streamID)
975 {
976 auto ptr = block.m_memory_region.template GetPtr<MemSpace, ReadWrite>(
977 streamID);
978 const unsigned int ncomp =
979 block.GetNumComponents() * block.GetNumHomoModes();
980 if constexpr (std::is_same_v<MemSpace, NektarSpaces::HostSpace>)
981 {
982 hostZeroPadding(ptr, ncomp, block.CompSize(),
983 block.GetNumElements(), block.GetNumData(),
984 block.GetInterleaveWidth());
985 }
986 else
987 {
988 deviceZeroPadding(ptr, ncomp, block.CompSize(),
989 block.GetNumElements(), block.GetNumData(),
990 block.GetInterleaveWidth(), streamID);
991 }
992 }
993
994 // MultiField is the only class allowed to use the shared-storage
995 // constructor below.
996 friend class MultiField<TData, TState>;
997
998 /**
999 * @brief Construct a new Field object in storage shared with other
1000 * fields. For use by MultiField only.
1001 *
1002 * The field holds no storage of its own: its blocks point into @p storage
1003 * from @p offset onwards, which must leave room for the whole field.
1004 *
1005 * @warning This constructor must only be used by MultiField, which owns
1006 * the shared storage and lays its columns out in it. It is private, with
1007 * MultiField as friend, so that no other code can build a Field on
1008 * storage it does not manage.
1009 *
1010 * @param name - Name of the field object.
1011 * @param blockAttr - Block attributes.
1012 * @param components - Names of components for vector field.
1013 * @param num_homo_modes - Number of homogeneous modes.
1014 * @param storage - Storage the field points into.
1015 * @param offset - Offset of the field in @p storage.
1016 */
1017 Field(const std::string name,
1018 const std::vector<BlockAttributes<TState>> blockAttr,
1019 const std::vector<std::string> components,
1020 const unsigned int num_homo_modes,
1021 const std::shared_ptr<SharedFieldStorage<TData>> &storage,
1022 const size_t offset)
1023 : Field<TData, TState>(name, blockAttr, components, num_homo_modes,
1024 storage->GetMemAllocType(),
1025 storage->GetAlignment())
1026 {
1027 if (offset + this->size() > storage->size())
1028 {
1030 "Field - Shared storage too small for the field.");
1031 }
1032
1033 m_sharedStorage = storage;
1034 m_sharedStorageOffset = offset;
1035 }
1036};
1037
1038} // namespace Nektar::LibUtilities
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Get the BlockAttributes for a given field state from an ExpList. This method basically captures ident...
Definition Block.hpp:144
unsigned int GetNumComponents() const
Gets the number of components.
Definition Block.hpp:249
unsigned int GetNumHomoModes() const
Gets the number of homogeneous modes.
Definition Block.hpp:259
MemoryRegion< TData > m_memory_region
Definition Block.hpp:291
A block means a group of elements of identical shape, basis and order. BlockAttributes stores the mos...
Definition Block.hpp:50
unsigned int GetInterleaveWidth(void) const
Definition Block.hpp:103
unsigned int GetNumData(void) const
Definition Block.hpp:93
size_t GetNumElements(void) const
Definition Block.hpp:83
A Field represents expansion data to be operated on.
void InterleaveFromHostPtr(const TDataIn *src, const size_t compSize)
Copy data from host memory, laid out component by component and element by element without padding,...
Field(const std::string name, const std::vector< BlockAttributes< TState > > blockAttr, const std::vector< std::string > components, const unsigned int num_homo_modes, const std::shared_ptr< SharedFieldStorage< TData > > &storage, const size_t offset)
Construct a new Field object in storage shared with other fields. For use by MultiField only.
static void AllocateFieldStorage(Field< TData, TState > *field)
Allocate contiguous host OR device memory accross all MemoryRegion objects belonging to a Field objec...
Field(const std::vector< BlockAttributes< TState > > blockAttr, const unsigned int num_components, const unsigned int num_homo_modes, const MemAllocType &memAllocType=eHostPageable, const size_t alignment=NektarSpaces::host_memory_alignment)
Construct a new Field object.
void CopyArray(const Nektar::Array< Nektar::OneD, TDataIn > &array)
Templated copy method. This method copies data from a Nektar::Array<Nektar::OneD, TDataIn> laid out a...
void CopyVector(const std::vector< TDataIn, Alloc > &array)
Templated copy method. This method copies data from a std::vector laid out as ToVector() returns it,...
size_t size() const
Gets the size of the field.
void ReleaseStorage()
Free the contiguous host and device storage this field owns, allocated by AllocateFieldStorage(),...
static void ZeroPadding(BlockAccessor< TData, TState > &block, const unsigned int streamID)
Zero the padding of block in place, in MemSpace.
void ReshapeStorage(const unsigned int &interleaveWidth, const std::string &execSpace)
Field(const Field &)=delete
Constructor methods - No copy methods.
std::vector< BlockAccessor< TData, TState > > & GetBlocks()
Get BlockAccessors for the field.
std::shared_ptr< SharedFieldStorage< TData > > m_sharedStorage
Shared storage the field points into, if any.
Field(Field &&rhs)
Construct a new Field object by moving storage from an existing Field object.
size_t m_sharedStorageOffset
Offset of the field in m_sharedStorage.
std::vector< unsigned int > GetInterleaveWidth(void) const
Field & operator=(Field &&rhs)
Move assignment operator.
bool m_instantiated
Flag indicating if the current object has been instantiated.
std::string GetName(void) const
Gets the name of the field.
void ReshapeStorage(const std::vector< unsigned int > &interleaveWidths, const std::string &execSpace)
std::vector< TDataOut, Alloc > ToVector()
Copy the data to a std::vector, component by component and element by element, without padding,...
Field & operator=(const Field &)=delete
Nektar::Array< Nektar::OneD, TDataOut > ToArray()
Copy the data to a Nektar::Array, component by component and element by element, without padding,...
void Copy(Field &field)
Templated copy method. This method copies data from a Field.
std::vector< std::string > GetComponentNames() const
Gets a copy of the components names for a vector field.
unsigned int GetNumHomoModes() const
Gets the number of homogeneous modes.
void Initialize(const TData val, const unsigned int streamID=0)
Templated initialize method: set every entry to val, but the padding, which is zero.
void SetInterleaveWidth(const unsigned int &interleaveWidth)
unsigned int GetNumComponents() const
Gets the number of components for a vector field.
void DeInterleaveToHostPtr(TDataOut *dst, const size_t compSize)
Copy the data to host memory, component by component and element by element, without padding....
Field(const std::string name, const std::vector< BlockAttributes< TState > > blockAttr, const std::vector< std::string > components, const unsigned int num_homo_modes, const MemAllocType &memAllocType=eHostPageable, const size_t alignment=NektarSpaces::host_memory_alignment)
Construct a new Field object.
Field(const std::vector< BlockAttributes< TState > > blockAttr, const std::vector< std::string > components, const unsigned int num_homo_modes, const MemAllocType &memAllocType=eHostPageable, const size_t alignment=NektarSpaces::host_memory_alignment)
Construct a new Field object.
void SetInterleaveWidth(const std::vector< unsigned int > &interleaveWidths)
std::vector< BlockAccessor< TData, TState > > m_block_accessors
Field(const std::string name, const std::vector< BlockAttributes< TState > > blockAttr, const unsigned int num_components, const unsigned int num_homo_modes, const MemAllocType &memAllocType=eHostPageable, const size_t alignment=NektarSpaces::host_memory_alignment)
Construct a new Field object.
A MemoryRegion represents a memory region.
A set of fields stored as the columns of a column-major matrix.
Contiguous host and device storage shared by several Field objects.
static constexpr size_t host_memory_alignment
Definition Backends.hpp:178
void deviceFree(TData *src, const size_t size, const unsigned int streamID, const MemAllocType memAllocType=eHostPageable)
void hostFreePinned(TData *src)
void hostFree(TData *src, const size_t alignment)
void deviceZeroPadding(TData *dst, const unsigned int ncomp, const size_t compSize, const size_t nelmt, const unsigned int ndata, const unsigned int width, const unsigned int streamID)
void deviceMemset(TData *dst, const int val, const size_t size, const unsigned int streamID)
void hostZeroPadding(TData *dst, const unsigned int ncomp, const size_t compSize, const size_t nelmt, const unsigned int ndata, const unsigned int width)
static void nekStreamSynchronize(const unsigned int streamID)
Definition Backends.hpp:428
void deviceMalloc(TData **src, const size_t size, const unsigned int streamID, const MemAllocType memAllocType=eHostPageable)
@ eHostPageable
void hostMallocPinned(TData **src, const size_t size)
void hostMalloc(TData **src, const size_t size, const size_t alignment)
STL namespace.