DFT-FE 1.1.0-pre
Density Functional Theory With Finite-Elements
Loading...
Searching...
No Matches
MemoryStorage.h
Go to the documentation of this file.
1// ---------------------------------------------------------------------
2//
3// Copyright (c) 2017-2025 The Regents of the University of Michigan and DFT-FE
4// authors.
5//
6// This file is part of the DFT-FE code.
7//
8// The DFT-FE code is free software; you can use it, redistribute
9// it, and/or modify it under the terms of the GNU Lesser General
10// Public License as published by the Free Software Foundation; either
11// version 2.1 of the License, or (at your option) any later version.
12// The full text of the license can be found in the file LICENSE at
13// the top level of the DFT-FE distribution.
14//
15// ---------------------------------------------------------------------
16
17/*
18 * @author Ian C. Lin, Sambit Das.
19 */
20
21#ifndef dftfeMemoryStorage_h
22#define dftfeMemoryStorage_h
23
24#include <MemoryManager.h>
25#include <TypeConfig.h>
26#include <vector>
27namespace dftfe
28{
29 namespace utils
30 {
31 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
33 {
34 /**
35 * @brief A class template to provide an interface that can act similar to
36 * STL vectors but with different MemorySpace---
37 * HOST (cpu) , DEVICE (gpu), etc,.
38 *
39 * @tparam ValueType The underlying value type for the MemoryStorage
40 * (e.g., dftfe::Int, double, complex<double>, etc.)
41 * @tparam memorySpace The memory space in which the MemoryStorage needs
42 * to reside
43 *
44 */
45
46 //
47 // typedefs
48 //
49 public:
50 typedef ValueType value_type;
51 typedef ValueType *pointer;
52 typedef ValueType &reference;
53 typedef const ValueType &const_reference;
54 typedef ValueType *iterator;
55 typedef const ValueType *const_iterator;
56
57 public:
58 MemoryStorage() = default;
59
60 /**
61 * @brief Copy constructor for a MemoryStorage
62 * @param[in] u MemoryStorage object to copy from
63 */
65
66 /**
67 * @brief Move constructor for a Vector
68 * @param[in] u Vector object to move from
69 */
71
72 /**
73 * @brief Constructor for Vector with size and initial value arguments
74 * @param[in] size size of the Vector
75 * @param[in] initVal initial value of elements of the Vector
76 */
77 explicit MemoryStorage(std::size_t size, ValueType initVal = 0);
78
79 /**
80 * @brief Destructor
81 */
83
84 /**
85 * @brief clear and set to d_data to nullptr
86 */
87 void
89
90
91 /**
92 * @brief Set all the entries to a given value
93 * @param[in] val The value to which the entries are to be set
94 */
95 void
96 setValue(const ValueType val);
97
98 /**
99 * @brief Return iterator pointing to the beginning of point
100 * data.
101 *
102 * @returns Iterator pointing to the beginning of Vector.
103 */
106
107 /**
108 * @brief Return iterator pointing to the beginning of Vector
109 * data.
110 *
111 * @returns Constant iterator pointing to the beginning of
112 * Vector.
113 */
115 begin() const;
116
117 /**
118 * @brief Return iterator pointing to the end of Vector data.
119 *
120 * @returns Iterator pointing to the end of Vector.
121 */
124
125 /**
126 * @brief Return iterator pointing to the end of Vector data.
127 *
128 * @returns Constant iterator pointing to the end of
129 * Vector.
130 */
132 end() const;
133
134
135 /**
136 * @brief Copy assignment operator
137 * @param[in] rhs the rhs Vector from which to copy
138 * @returns reference to the lhs Vector
139 */
142
143
144 /**
145 * @brief Move assignment constructor
146 * @param[in] rhs the rhs Vector from which to move
147 * @returns reference to the lhs Vector
148 */
150 operator=(MemoryStorage &&rhs) noexcept;
151
152 /**
153 * @brief Operator to get a reference to a element of the Vector
154 * @param[in] i is the index to the element of the Vector
155 * @returns reference to the element of the Vector
156 * @throws exception if i >= size of the Vector
157 */
159 operator[](std::size_t i);
160
161 /**
162 * @brief Operator to get a const reference to a element of the Vector
163 * @param[in] i is the index to the element of the Vector
164 * @returns const reference to the element of the Vector
165 * @throws exception if i >= size of the Vector
166 */
168 operator[](std::size_t i) const;
169
170 void
172
173 /**
174 * @brief Deallocates and then resizes Vector with new size
175 * and initial value arguments
176 * @param[in] size size of the Vector
177 * @param[in] initVal initial value of elements of the Vector
178 */
179 void
180 resize(std::size_t size, ValueType initVal = ValueType());
181
182 /**
183 * @brief Returns the dimension of the Vector
184 * @returns size of the Vector
185 */
186 std::size_t
187 size() const;
188
189 /**
190 * @brief Return the raw pointer to the Vector
191 * @return pointer to data
192 */
193 ValueType *
194 data() noexcept;
195
196 /**
197 * @brief Return the raw pointer to the Vector without modifying the values
198 * @return pointer to const data
199 */
200 const ValueType *
201 data() const noexcept;
202
203 /**
204 * @brief Copies the data to a MemoryStorage object in a different memory space.
205 * This provides a seamless interface to copy back and forth between
206 * memory spaces , including between the same memory spaces.
207 *
208 * @note The destination MemoryStorage must be pre-allocated appropriately
209 *
210 * @tparam memorySpaceDst memory space of the destination MemoryStorage
211 * @param[in] dstMemoryStorage reference to the destination
212 * MemoryStorage. It must be pre-allocated appropriately
213 * @param[out] dstMemoryStorage reference to the destination
214 * MemoryStorage with the data copied into it
215 * @throw utils::LengthError exception if the size of dstMemoryStorage is
216 * less than underlying MemoryStorage
217 */
218 template <dftfe::utils::MemorySpace memorySpaceDst>
219 void
220 copyTo(MemoryStorage<ValueType, memorySpaceDst> &dstMemoryStorage) const;
221
222 /**
223 * @brief Copies the data to a MemoryStorage object in a different memory space.
224 * This provides a seamless interface to copy back and forth between
225 * memory spaces , including between the same memory spaces. This is a
226 * more granular version of the above copyTo function as it provides
227 * transfer from a specific portion of the source MemoryStorage to a
228 * specific portion of the destination MemoryStorage.
229 *
230 * @note The destination MemoryStorage must be pre-allocated appropriately
231 *
232 * @tparam memorySpaceDst memory space of the destination MemoryStorage
233 * @param[in] dstMemoryStorage reference to the destination
234 * MemoryStorage. It must be pre-allocated appropriately
235 * @param[in] N number of entries of the source MemoryStorage
236 * that needs to be copied to the destination MemoryStorage
237 * @param[in] srcOffset offset relative to the start of the source
238 * MemoryStorage from which we need to copy data
239 * @param[in] dstOffset offset relative to the start of the destination
240 * MemoryStorage to which we need to copy data
241 * @param[out] dstMemoryStorage reference to the destination
242 * MemoryStorage with the data copied into it
243 * @throw utils::LengthError exception if the size of dstMemoryStorage is
244 * less than N + dstOffset
245 * @throw utils::LengthError exception if the size of underlying
246 * MemoryStorage is less than N + srcOffset
247 */
248 template <dftfe::utils::MemorySpace memorySpaceDst>
249 void
250 copyTo(MemoryStorage<ValueType, memorySpaceDst> &dstMemoryStorage,
251 const std::size_t N,
252 const std::size_t srcOffset,
253 const std::size_t dstOffset) const;
254
255 /**
256 * @brief Copies data from a MemoryStorage object in a different memory space.
257 * This provides a seamless interface to copy back and forth between
258 * memory spaces, including between the same memory spaces.
259 *
260 * @note The MemoryStorage must be pre-allocated appropriately
261 *
262 * @tparam memorySpaceSrc memory space of the source MemoryStorage
263 * from which to copy
264 * @param[in] srcMemoryStorage reference to the source
265 * MemoryStorage
266 * @throw utils::LengthError exception if the size of underlying
267 * MemoryStorage is less than size of srcMemoryStorage
268 */
269 template <dftfe::utils::MemorySpace memorySpaceSrc>
270 void
272 const MemoryStorage<ValueType, memorySpaceSrc> &srcMemoryStorage);
273
274 /**
275 * @brief Copies data from a MemoryStorage object in a different memory space.
276 * This provides a seamless interface to copy back and forth between
277 * memory spaces, including between the same memory spaces.
278 * This is a more granular version of the above copyFrom function as it
279 * provides transfer from a specific portion of the source MemoryStorage
280 * to a specific portion of the destination MemoryStorage.
281 *
282 * @note The MemoryStorage must be pre-allocated appropriately
283 *
284 * @tparam memorySpaceSrc memory space of the source MemoryStorage
285 * from which to copy
286 * @param[in] srcMemoryStorage reference to the source
287 * MemoryStorage
288 * @param[in] N number of entries of the source MemoryStorage
289 * that needs to be copied to the destination MemoryStorage
290 * @param[in] srcOffset offset relative to the start of the source
291 * MemoryStorage from which we need to copy data
292 * @param[in] dstOffset offset relative to the start of the destination
293 * MemoryStorage to which we need to copy data
294 * @throw utils::LengthError exception if the size of srcMemoryStorage is
295 * less than N + srcOffset
296 * @throw utils::LengthError exception if the size of underlying
297 * MemoryStorage is less than N + dstOffset
298 */
299 template <dftfe::utils::MemorySpace memorySpaceSrc>
300 void
301 copyFrom(const MemoryStorage<ValueType, memorySpaceSrc> &srcMemoryStorage,
302 const std::size_t N,
303 const std::size_t srcOffset,
304 const std::size_t dstOffset);
305
306 /**
307 * @brief Copies the data to a memory pointed by a raw pointer
308 * This provides a seamless interface to copy back and forth between
309 * memory spaces, including between the same memory spaces.
310 *
311 * @note The destination pointer must be pre-allocated to be
312 * at least of the size of the MemoryStorage
313 *
314 * @tparam memorySpaceDst memory space of the destination pointer
315 * @param[in] dst pointer to the destination. It must be pre-allocated
316 * appropriately
317 * @param[out] dst pointer to the destination with the data copied into it
318 */
319 template <dftfe::utils::MemorySpace memorySpaceDst>
320 void
321 copyTo(ValueType *dst) const;
322
323 /**
324 * @brief Copies the data to a memory pointer by a raw pointer.
325 * This provides a seamless interface to copy back and forth between
326 * memory spaces , including between the same memory spaces. This is a
327 * more granular version of the above copyTo function as it provides
328 * transfer from a specific portion of the source MemoryStorage to a
329 * specific portion of the destination pointer.
330 *
331 * @note The destination pointer must be pre-allocated to be at least
332 * of the size N + dstOffset
333 *
334 * @tparam memorySpaceDst memory space of the destination pointer
335 * @param[in] dst pointer to the destination. It must be pre-allocated
336 * appropriately
337 * @param[in] N number of entries of the source MemoryStorage
338 * that needs to be copied to the destination pointer
339 * @param[in] srcOffset offset relative to the start of the source
340 * MemoryStorage from which we need to copy data
341 * @param[in] dstOffset offset relative to the start of the destination
342 * pointer to which we need to copy data
343 * @param[out] dst pointer to the destination with the data copied into it
344 * @throw utils::LengthError exception if the size of the MemoryStorage is
345 * less than N + srcOffset
346 */
347 template <dftfe::utils::MemorySpace memorySpaceDst>
348 void
349 copyTo(ValueType *dst,
350 const std::size_t N,
351 const std::size_t srcOffset,
352 const std::size_t dstOffset) const;
353
354 /**
355 * @brief Copies data from a memory pointed by a raw pointer into
356 * the MemoryStorage object.
357 * This provides a seamless interface to copy back and forth between
358 * memory spaces, including between the same memory spaces.
359 *
360 * @note The src pointer must point to a memory chunk that is at least the
361 * size of the MemoryStorage
362 *
363 * @tparam memorySpaceSrc memory space of the source pointer
364 * from which to copy
365 * @param[in] src pointer to the source memory
366 */
367 template <dftfe::utils::MemorySpace memorySpaceSrc>
368 void
369 copyFrom(const ValueType *src);
370
371 /**
372 * @brief Copies data from a memory pointer by a raw pointer into the MemoryStorage object.
373 * This provides a seamless interface to copy back and forth between
374 * memory spaces, including between the same memory spaces.
375 * This is a more granular version of the above copyFrom function as it
376 * provides transfer from a specific portion of the source memory
377 * to a specific portion of the destination MemoryStorage.
378 *
379 * @note The src pointer must point to a memory chunk that is at least
380 * the size of N + srcOffset
381 *
382 * @tparam memorySpaceSrc memory space of the source pointer
383 * from which to copy
384 * @param[in] src pointer to the source memory
385 * @param[in] N number of entries of the source pointer
386 * that needs to be copied to the destination MemoryStorage
387 * @param[in] srcOffset offset relative to the start of the source
388 * pointer from which we need to copy data
389 * @param[in] dstOffset offset relative to the start of the destination
390 * MemoryStorage to which we need to copy data
391 * @throw utils::LengthError exception if the size of the MemoryStorage is
392 * less than N + dstOffset
393 */
394 template <dftfe::utils::MemorySpace memorySpaceSrc>
395 void
396 copyFrom(const ValueType *src,
397 const std::size_t N,
398 const std::size_t srcOffset,
399 const std::size_t dstOffset);
400
401 /**
402 * @brief Copies the data to a C++ STL vector, which always resides in
403 * the CPU. This provides a seamless interface to copy from any memory
404 * space to a C++ STL vector, including the case where source memory space
405 * is HOST (i.e., it resides on the CPU)
406 *
407 * @param[in] dst reference to the destination C++ STL vector to which
408 * the data needs to be copied.
409 * @param[out] dst reference to the destination C++ STL vector with
410 * the data copied into it
411 * @note If the size of the dst vector is less than the the size of
412 * the underlying MemoryStorage, it will be resized. Thus, for performance
413 * reasons, it is recommened that the dst STL vector be pre-allocated
414 * appropriately.
415 */
416 void
417 copyTo(std::vector<ValueType> &dst) const;
418
419 /**
420 * @brief Copies the data to a C++ STL vector, which always resides in
421 * the CPU. This provides a seamless interface to copy from any memory
422 * space to a C++ STL vector, including the case where source memory space
423 * is HOST (i.e., it resides on the CPU).
424 * This is a more granular version of the above copyToSTL function as it
425 * provides transfer from a specific portion of the MemoryStorage
426 * to a specific portion of the destination STL vector.
427 *
428 * @param[in] dst reference to the destination C++ STL vector to which
429 * the data needs to be copied.
430 * @note If the size of the dst vector is less than the the size of
431 * the underlying memory storage, it will be resized. Thus, for
432 * performance reasons it is recommened to should be allocated
433 * appropriately.
434 * @param[in] N number of entries of the source MemoryStorage
435 * that needs to be copied to the destination pointer
436 * @param[in] srcOffset offset relative to the start of the source
437 * MemoryStorage from which we need to copy data
438 * @param[in] dstOffset offset relative to the start of the STL vector
439 * to which we need to copy data
440 * @param[out] dst reference to the destination C++ STL vector with
441 * the data copied into it
442 * @throw utils::LengthError exception if the size of the MemoryStorage is
443 * less than N + srcOffset
444 * @note If the size of the dst vector is less N + srcOffset, it will be resized.
445 * Thus, for performance reasons, it is recommened that the dst STL
446 * vector be allocated appropriately.
447 */
448 void
449 copyTo(std::vector<ValueType> &dst,
450 const std::size_t N,
451 const std::size_t srcOffset,
452 const std::size_t dstOffset) const;
453
454 /**
455 * @brief Copies data from a C++ STL vector to the MemoryStorage object,
456 * which always resides on a CPU. This provides a seamless interface to
457 * copy from any memory space, including the case where the same memory
458 * spaces is HOST(i.e., the MemoryStorage is on CPU).
459 *
460 * @param[in] src const reference to the source C++ STL vector from which
461 * the data needs to be copied.
462 *
463 * @throw utils::LengthError exception if the size of the MemoryStorage is
464 * less than the size of the src
465 */
466 void
467 copyFrom(const std::vector<ValueType> &src);
468
469 /**
470 * @brief Copies data from a C++ STL vector to the MemoryStorage object,
471 * which always resides on a CPU. This provides a seamless interface to
472 * copy from any memory space, including the case where the same memory
473 * spaces is HOST(i.e., the MemoryStorage is on CPU). This is a more
474 * granular version of the above copyFromSTL function as it provides
475 * transfer from a specific portion of the source STL vector to to a
476 * specific portion of the destination MemoryStorage.
477 *
478 * @param[in] src const reference to the source C++ STL vector from which
479 * the data needs to be copied.
480 * @param[in] N number of entries of the source pointer
481 * that needs to be copied to the destination MemoryStorage
482 * @param[in] srcOffset offset relative to the start of the source STL
483 * vector from which we need to copy data
484 * @param[in] dstOffset offset relative to the start of the destination
485 * MemoryStorage to which we need to copy data
486 * @throw utils::LengthError exception if the size of src is less than
487 * N + srcOffset
488 * @throw utils::LengthError exception if the size of the MemoryStorage
489 * is less thant N + dstOffset
490 *
491 */
492 void
493 copyFrom(const std::vector<ValueType> &src,
494 const std::size_t N,
495 const std::size_t srcOffset,
496 const std::size_t dstOffset);
497
498
499 private:
500 ValueType *d_data = nullptr;
501 std::size_t d_size = 0;
502 };
503
504 //
505 // helper functions
506 //
507
508 /**
509 * @brief Create a MemoryStorage object from an input C++ STL vector
510 * @param[in] src Input C++ STL vector from which the MemoryStorage
511 * object is to be created
512 * @return A MemoryStorage object containing the data in the input C++
513 * STL vector
514 * @tparam ValueType Datatype of the underlying data in MemoryStorage
515 * as well as C++ STL vector (e.g., dftfe::Int, double, float,
516 * complex<double>, etc)
517 * @tparam memorySpaceDst MemorySpace (e.g. HOST, DEVICE, HOST_PINNED, etc)
518 * where the output MemoryStorage object should reside
519 */
520 template <typename ValueType, utils::MemorySpace memorySpaceDst>
521 MemoryStorage<ValueType, memorySpaceDst>
522 memoryStorageFromSTL(const std::vector<ValueType> &src);
523
524 } // namespace utils
525} // end of namespace dftfe
526
527#include "MemoryStorage.t.cc"
528
529#endif
ValueType * iterator
Definition MemoryStorage.h:54
ValueType & reference
Definition MemoryStorage.h:52
iterator end()
Return iterator pointing to the end of Vector data.
Definition MemoryStorage.t.cc:144
void resize(std::size_t size, ValueType initVal=ValueType())
Deallocates and then resizes Vector with new size and initial value arguments.
Definition MemoryStorage.t.cc:47
MemoryStorage & operator=(MemoryStorage &&rhs) noexcept
Move assignment constructor.
Definition MemoryStorage.t.cc:176
const_iterator begin() const
Return iterator pointing to the beginning of Vector data.
Definition MemoryStorage.t.cc:137
std::size_t d_size
Definition MemoryStorage.h:501
~MemoryStorage()
Destructor.
Definition MemoryStorage.t.cc:72
const_iterator end() const
Return iterator pointing to the end of Vector data.
Definition MemoryStorage.t.cc:151
void swap(MemoryStorage &rhs)
Definition MemoryStorage.t.cc:214
MemoryStorage(std::size_t size, ValueType initVal=0)
Constructor for Vector with size and initial value arguments.
Definition MemoryStorage.t.cc:33
dftfe::uInt * d_data
Definition MemoryStorage.h:500
ValueType * pointer
Definition MemoryStorage.h:51
ValueType value_type
A class template to provide an interface that can act similar to STL vectors but with different Memor...
Definition MemoryStorage.h:50
iterator begin()
Return iterator pointing to the beginning of point data.
Definition MemoryStorage.t.cc:130
void clear()
clear and set to d_data to nullptr
Definition MemoryStorage.t.cc:80
const_reference operator[](std::size_t i) const
Operator to get a const reference to a element of the Vector.
Definition MemoryStorage.t.cc:203
ValueType * data() noexcept
Return the raw pointer to the Vector.
Definition MemoryStorage.t.cc:227
const ValueType & const_reference
Definition MemoryStorage.h:53
MemoryStorage & operator=(const MemoryStorage &rhs)
Copy assignment operator.
Definition MemoryStorage.t.cc:158
void setValue(const ValueType val)
Set all the entries to a given value.
Definition MemoryStorage.t.cc:105
MemoryStorage(MemoryStorage &&u) noexcept
Move constructor for a Vector.
Definition MemoryStorage.t.cc:113
const ValueType * const_iterator
Definition MemoryStorage.h:55
reference operator[](std::size_t i)
Operator to get a reference to a element of the Vector.
Definition MemoryStorage.t.cc:192
void copyTo(MemoryStorage< dftfe::uInt, memorySpaceDst > &dstMemoryStorage) const
MemoryStorage(const MemoryStorage &u)
Copy constructor for a MemoryStorage.
Definition MemoryStorage.t.cc:92
void copyFrom(const MemoryStorage< dftfe::uInt, memorySpaceSrc > &srcMemoryStorage)
Definition Cell.h:36
MemorySpace
Definition MemorySpaceType.h:33
MemoryStorage< ValueType, memorySpaceDst > memoryStorageFromSTL(const std::vector< ValueType > &src)
Create a MemoryStorage object from an input C++ STL vector.
Definition MemoryStorage.t.cc:438
Definition pseudoPotentialToDftfeConverter.cc:34