DFT-EFE
 
Loading...
Searching...
No Matches
MemoryStorage.h
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2021. *
3 * The Regents of the University of Michigan and DFT-EFE developers. *
4 * *
5 * This file is part of the DFT-EFE code. *
6 * *
7 * DFT-EFE is free software: you can redistribute it and/or modify *
8 * it under the terms of the Lesser GNU General Public License as *
9 * published by the Free Software Foundation, either version 3 of *
10 * the License, or (at your option) any later version. *
11 * *
12 * DFT-EFE is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
15 * See the Lesser GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License at the top level of DFT-EFE distribution. If not, see *
19 * <https://www.gnu.org/licenses/>. *
20 ******************************************************************************/
21
22/*
23 * @author Ian C. Lin, Sambit Das.
24 */
25
26#ifndef dftefeMemoryStorage_h
27#define dftefeMemoryStorage_h
28
29#include <utils/MemoryManager.h>
30#include <utils/TypeConfig.h>
31#include <vector>
32namespace dftefe
33{
34 namespace utils
35 {
36 template <typename ValueType, dftefe::utils::MemorySpace memorySpace>
38 {
51 //
52 // typedefs
53 //
54 public:
55 typedef ValueType value_type;
56 typedef ValueType * pointer;
57 typedef ValueType & reference;
58 typedef const ValueType &const_reference;
59 typedef ValueType * iterator;
60 typedef const ValueType *const_iterator;
61
62 public:
63 MemoryStorage() = default;
64
70
75 MemoryStorage(MemoryStorage &&u) noexcept;
76
82 explicit MemoryStorage(size_type size, ValueType initVal = ValueType());
83
88
93 void
94 setValue(const ValueType val);
95
100 void
101 setZero(size_type size, const size_type offset);
102
110 begin();
111
120 begin() const;
121
128 end();
129
137 end() const;
138
139
146 operator=(const MemoryStorage &rhs);
147
148
155 operator=(MemoryStorage &&rhs) noexcept;
156
157 // // This part does not work for GPU version, will work on this
158 // until
159 // // having cleaner solution.
160 // /**
161 // * @brief Operator to get a reference to a element of the Vector
162 // * @param[in] i is the index to the element of the Vector
163 // * @returns reference to the element of the Vector
164 // * @throws exception if i >= size of the Vector
165 // */
166 // reference
167 // operator[](size_type i);
168 //
169 // /**
170 // * @brief Operator to get a const reference to a element of the Vector
171 // * @param[in] i is the index to the element of the Vector
172 // * @returns const reference to the element of the Vector
173 // * @throws exception if i >= size of the Vector
174 // */
175 // const_reference
176 // operator[](size_type i) const;
177
184 void
185 resize(size_type size, ValueType initVal = ValueType());
186
192 size() const;
193
198 ValueType *
199 data() noexcept;
200
205 const ValueType *
206 data() const noexcept;
207
223 template <dftefe::utils::MemorySpace memorySpaceDst>
224 void
225 copyTo(MemoryStorage<ValueType, memorySpaceDst> &dstMemoryStorage) const;
226
253 template <dftefe::utils::MemorySpace memorySpaceDst>
254 void
255 copyTo(MemoryStorage<ValueType, memorySpaceDst> &dstMemoryStorage,
256 const size_type N,
257 const size_type srcOffset,
258 const size_type dstOffset) const;
259
274 template <dftefe::utils::MemorySpace memorySpaceSrc>
275 void
276 copyFrom(
277 const MemoryStorage<ValueType, memorySpaceSrc> &srcMemoryStorage);
278
304 template <dftefe::utils::MemorySpace memorySpaceSrc>
305 void
306 copyFrom(MemoryStorage<ValueType, memorySpaceSrc> &srcMemoryStorage,
307 const size_type N,
308 const size_type srcOffset,
309 const size_type dstOffset);
310
324 template <dftefe::utils::MemorySpace memorySpaceDst>
325 void
326 copyTo(ValueType *dst) const;
327
352 template <dftefe::utils::MemorySpace memorySpaceDst>
353 void
354 copyTo(ValueType * dst,
355 const size_type N,
356 const size_type srcOffset,
357 const size_type dstOffset) const;
358
372 template <dftefe::utils::MemorySpace memorySpaceSrc>
373 void
374 copyFrom(const ValueType *src);
375
399 template <dftefe::utils::MemorySpace memorySpaceSrc>
400 void
401 copyFrom(const ValueType *src,
402 const size_type N,
403 const size_type srcOffset,
404 const size_type dstOffset);
405
421 void
422 copyTo(std::vector<ValueType> &dst) const;
423
453 void
454 copyTo(std::vector<ValueType> &dst,
455 const size_type N,
456 const size_type srcOffset,
457 const size_type dstOffset) const;
458
471 void
472 copyFrom(const std::vector<ValueType> &src);
473
497 void
498 copyFrom(const std::vector<ValueType> &src,
499 const size_type N,
500 const size_type srcOffset,
501 const size_type dstOffset);
502
503
504 private:
505 ValueType *d_data = nullptr;
507 };
508
509 //
510 // helper functions
511 //
512
525 template <typename ValueType, utils::MemorySpace memorySpaceDst>
526 MemoryStorage<ValueType, memorySpaceDst>
527 memoryStorageFromSTL(const std::vector<ValueType> &src);
528
529 template <typename ValueType, dftefe::utils::MemorySpace memorySpace>
530 void
531 swap(MemoryStorage<ValueType, memorySpace> &X,
532 MemoryStorage<ValueType, memorySpace> &Y);
533
534 } // namespace utils
535} // end of namespace dftefe
536
537#include "MemoryStorage.t.cpp"
538
539#endif
Definition: MemoryStorage.h:38
void setZero(size_type size, const size_type offset)
Set all the entries to a given value.
Definition: MemoryStorage.t.cpp:101
const ValueType & const_reference
Definition: MemoryStorage.h:58
const ValueType * const_iterator
Definition: MemoryStorage.h:60
ValueType * data() noexcept
Return the raw pointer to the Vector.
Definition: MemoryStorage.t.cpp:208
size_type d_size
Definition: MemoryStorage.h:506
void resize(size_type size, ValueType initVal=ValueType())
Deallocates and then resizes Vector with new size and initial value arguments.
Definition: MemoryStorage.t.cpp:52
ValueType * iterator
Definition: MemoryStorage.h:59
iterator begin()
Return iterator pointing to the beginning of point data.
Definition: MemoryStorage.t.cpp:127
MemoryStorage & operator=(const MemoryStorage &rhs)
Copy assignment operator.
Definition: MemoryStorage.t.cpp:155
ValueType * pointer
Definition: MemoryStorage.h:56
void setValue(const ValueType val)
Set all the entries to a given value.
Definition: MemoryStorage.t.cpp:92
iterator end()
Return iterator pointing to the end of Vector data.
Definition: MemoryStorage.t.cpp:141
ValueType & reference
Definition: MemoryStorage.h:57
ValueType * d_data
Definition: MemoryStorage.h:505
size_type size() const
Returns the dimension of the Vector.
Definition: MemoryStorage.t.cpp:120
ValueType value_type
A class template to provide an interface that can act similar to STL vectors but with different Memor...
Definition: MemoryStorage.h:55
~MemoryStorage()
Destructor.
Definition: MemoryStorage.t.cpp:73
void copyFrom(const MemoryStorage< ValueType, memorySpaceSrc > &srcMemoryStorage)
Copies data from a MemoryStorage object in a different memory space. This provides a seamless interfa...
Definition: MemoryStorage.t.cpp:260
void copyTo(MemoryStorage< ValueType, memorySpaceDst > &dstMemoryStorage) const
Copies the data to a MemoryStorage object in a different memory space. This provides a seamless inter...
Definition: MemoryStorage.t.cpp:222
MemorySpace
Definition: MemorySpaceType.h:37
void swap(MemoryStorage< ValueType, memorySpace > &X, MemoryStorage< ValueType, memorySpace > &Y)
Definition: MemoryStorage.t.cpp:429
MemoryStorage< ValueType, memorySpaceDst > memoryStorageFromSTL(const std::vector< ValueType > &src)
Create a MemoryStorage object from an input C++ STL vector.
Definition: MemoryStorage.t.cpp:419
dealii includes
Definition: AtomFieldDataSpherical.cpp:31
unsigned int size_type
Definition: TypeConfig.h:8