DFT-FE 1.1.0-pre
Density Functional Theory With Finite-Elements
Loading...
Searching...
No Matches
MemoryStorage.t.cc
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#include <MemoryManager.h>
22#include <Exceptions.h>
23#include <MemoryTransfer.h>
24
25namespace dftfe
26{
27 namespace utils
28 {
29 //
30 // Constructor
31 //
32 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
44
45 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
46 void
48 const ValueType initVal)
49 {
50 if (d_size > 0)
51 {
53 d_data);
54 }
55 d_size = size;
56 if (size > 0)
57 {
59 size, &d_data);
61 d_data,
62 initVal);
63 }
64 else
65 d_data = nullptr;
66 }
67
68 //
69 // Destructor
70 //
71 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
78 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
79 void
81 {
82 if (d_size > 0)
83 {
85 d_data);
86 }
87 d_size = 0;
88 d_data = nullptr;
89 }
90
91 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
94 : d_size(u.d_size)
95 {
97 &d_data);
99 d_data,
100 u.d_data);
101 }
102
103 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
104 void
106 {
108 d_data,
109 val);
110 }
111
112 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
115 : d_size(u.d_size)
116 , d_data(nullptr)
117 {
118 *this = std::move(u);
119 }
120
121 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
122 std::size_t
124 {
125 return d_size;
126 }
127
128 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
131 {
132 return d_data;
133 }
134
135 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
142 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
148
149 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
152 {
153 return (d_data + d_size);
154 }
155
156 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
160 {
161 if (&rhs != this)
162 {
163 if (rhs.d_size != d_size)
164 {
165 this->resize(rhs.d_size);
166 }
168 this->d_data,
169 rhs.d_data);
170 }
171 return (*this);
172 }
173
174 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
178 {
179 if (&rhs != this)
181 delete[] d_data;
182 d_data = rhs.d_data;
183 d_size = rhs.d_size;
184 rhs.d_size = 0;
185 rhs.d_data = nullptr;
186 }
187 return (*this);
188 }
189
190 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
193 {
194 // throwException<InvalidArgument>(
195 // memorySpace != dftfe::utils::MemorySpace::DEVICE,
196 // "[] operator return reference to element not implemented for
197 // DEVICE");
198 return d_data[i];
199 }
200
201 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
204 {
205 // throwException<InvalidArgument>(
206 // memorySpace != dftfe::utils::MemorySpace::DEVICE,
207 // "[] operator return const reference to element not implemented for
208 // DEVICE");
209 return d_data[i];
210 }
211
212 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
213 void
216 {
217 ValueType *tempData = d_data;
218 const std::size_t tempSize = d_size;
219 d_data = rhs.d_data;
221 rhs.d_data = tempData;
222 rhs.d_size = tempSize;
223 }
224
225 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
226 ValueType *
228 {
229 return d_data;
230 }
231 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
232 const ValueType *
234 {
235 return d_data;
236 }
237
238 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
239 template <dftfe::utils::MemorySpace memorySpaceDst>
240 void
242 MemoryStorage<ValueType, memorySpaceDst> &dstMemoryStorage) const
243 {
245 d_size <= dstMemoryStorage.size(),
246 "The allocated size of destination MemoryStorage is insufficient to "
247 "copy from the the MemoryStorage.");
249 d_size, dstMemoryStorage.begin(), this->begin());
251
252 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
253 template <dftfe::utils::MemorySpace memorySpaceDst>
254 void
257 const std::size_t N,
258 const std::size_t srcOffset,
259 const std::size_t dstOffset) const
260 {
262 srcOffset + N <= d_size,
263 "The offset and copy size specified for the source MemoryStorage"
264 " is out of range for it.");
265
267 dstOffset + N <= dstMemoryStorage.size(),
268 "The offset and size specified for the destination MemoryStorage"
269 " is out of range for it.");
270
272 N, dstMemoryStorage.begin() + dstOffset, this->begin() + srcOffset);
273 }
274
275
276 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
277 template <dftfe::utils::MemorySpace memorySpaceSrc>
278 void
280 const MemoryStorage<ValueType, memorySpaceSrc> &srcMemoryStorage)
281 {
283 srcMemoryStorage.size() <= d_size,
284 "The allocated size of the MemoryStorage is insufficient to "
285 " copy from the source MemoryStorage.");
287 srcMemoryStorage.size(), this->begin(), srcMemoryStorage.begin());
288 }
289
290 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
291 template <dftfe::utils::MemorySpace memorySpaceSrc>
292 void
294 const MemoryStorage<ValueType, memorySpaceSrc> &srcMemoryStorage,
295 const std::size_t N,
296 const std::size_t srcOffset,
297 const std::size_t dstOffset)
298 {
300 srcOffset + N <= srcMemoryStorage.size(),
301 "The offset and copy size specified for the source MemoryStorage"
302 " is out of range for it.");
303
305 dstOffset + N <= d_size,
306 "The offset and size specified for the destination MemoryStorage"
307 " is out of range for it.");
308
310 N, this->begin() + dstOffset, srcMemoryStorage.begin() + srcOffset);
311 }
312
313 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
314 template <dftfe::utils::MemorySpace memorySpaceDst>
315 void
322
323 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
324 template <dftfe::utils::MemorySpace memorySpaceDst>
325 void
327 ValueType *dst,
328 const std::size_t N,
329 const std::size_t srcOffset,
330 const std::size_t dstOffset) const
331 {
333 srcOffset + N <= d_size,
334 "The offset and copy size specified for the source MemoryStorage"
335 " is out of range for it.");
337 N, dst + dstOffset, this->begin() + srcOffset);
338 }
339
340
341 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
342 template <dftfe::utils::MemorySpace memorySpaceSrc>
343 void
350
351 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
352 template <dftfe::utils::MemorySpace memorySpaceSrc>
353 void
355 const std::size_t N,
356 const std::size_t srcOffset,
357 const std::size_t dstOffset)
358 {
360 dstOffset + N <= d_size,
361 "The offset and size specified for the destination MemoryStorage"
362 " is out of range for it.");
363
365 N, this->begin() + dstOffset, src + srcOffset);
366 }
367
368 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
369 void
371 std::vector<ValueType> &dst) const
372 {
373 if (dst.size() < d_size)
374 dst.resize(d_size);
375
377 d_size, dst.data(), this->begin());
378 }
379
380 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
381 void
383 std::vector<ValueType> &dst,
384 const std::size_t N,
385 const std::size_t srcOffset,
386 const std::size_t dstOffset) const
387 {
389 srcOffset + N <= d_size,
390 "The offset and copy size specified for the source MemoryStorage"
391 " is out of range for it.");
392 if (dst.size() < N + dstOffset)
393 dst.resize(N + dstOffset);
394
396 N, dst.data() + dstOffset, this->begin() + srcOffset);
397 }
398
399
400 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
401 void
403 const std::vector<ValueType> &src)
404 {
406 src.size() <= d_size,
407 "The allocated size of the MemoryStorage is insufficient to copy from "
408 "the source STL vector");
410 this->begin(),
411 src.data());
412 }
413
414 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
415 void
417 const std::vector<ValueType> &src,
418 const std::size_t N,
419 const std::size_t srcOffset,
420 const std::size_t dstOffset)
421 {
423 dstOffset + N <= d_size,
424 "The offset and size specified for the destination MemoryStorage"
425 " is out of range for it.");
426
428 srcOffset + N <= src.size(),
429 "The offset and size specified for the source STL vector "
430 " is out of range for it.");
431
433 N, this->begin() + dstOffset, src.data() + srcOffset);
434 }
435
436 template <typename ValueType, utils::MemorySpace memorySpaceDst>
438 memoryStorageFromSTL(const std::vector<ValueType> &src)
439 {
440 MemoryStorage<ValueType, memorySpaceDst> returnValue(src.size());
442 src.size(), returnValue.begin(), src.data());
443 return returnValue;
444 }
445
446 } // namespace utils
447} // namespace dftfe
static void deallocate(ValueType *ptr)
static void allocate(std::size_t size, ValueType **ptr)
static void set(std::size_t size, ValueType *ptr, ValueType val)
Definition MemoryStorage.h:33
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
std::size_t d_size
Definition MemoryStorage.h:501
~MemoryStorage()
Destructor.
Definition MemoryStorage.t.cc:72
void swap(MemoryStorage &rhs)
Definition MemoryStorage.t.cc:214
std::size_t size() const
Returns the dimension of the Vector.
Definition MemoryStorage.t.cc:123
ValueType * d_data
Definition MemoryStorage.h:500
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
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
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< ValueType, memorySpaceDst > &dstMemoryStorage) const
Copies the data to a MemoryStorage object in a different memory space. This provides a seamless inter...
Definition MemoryStorage.t.cc:241
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.cc:279
static void copy(std::size_t size, ValueType *dst, const ValueType *src)
Copy array from the memory space of source to the memory space of destination.
Definition Cell.h:36
void throwException(bool condition, std::string msg="")
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