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);
168 this->d_data,
169 rhs.d_data);
170 }
171 return (*this);
172 }
173
174 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
175 MemoryStorage<ValueType, memorySpace> &
179 if (&rhs != this)
180 {
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];
200
201 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
204 operator[](const std::size_t i) const
205 {
206 // throwException<InvalidArgument>(
207 // memorySpace != dftfe::utils::MemorySpace::DEVICE,
208 // "[] operator return const reference to element not implemented for
209 // DEVICE");
210 return d_data[i];
211 }
212
213 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
214 void
217 {
218 ValueType * tempData = d_data;
219 const std::size_t tempSize = d_size;
220 d_data = rhs.d_data;
221 d_size = rhs.d_size;
222 rhs.d_data = tempData;
223 rhs.d_size = tempSize;
224 }
225
226 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
227 ValueType *
229 {
230 return d_data;
231 }
232 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
233 const ValueType *
235 {
236 return d_data;
237 }
238
239 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
240 template <dftfe::utils::MemorySpace memorySpaceDst>
241 void
243 MemoryStorage<ValueType, memorySpaceDst> &dstMemoryStorage) const
244 {
246 d_size <= dstMemoryStorage.size(),
247 "The allocated size of destination MemoryStorage is insufficient to "
248 "copy from the the MemoryStorage.");
250 d_size, dstMemoryStorage.begin(), this->begin());
251 }
252
253 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
254 template <dftfe::utils::MemorySpace memorySpaceDst>
255 void
258 const std::size_t N,
259 const std::size_t srcOffset,
260 const std::size_t dstOffset) const
261 {
263 srcOffset + N <= d_size,
264 "The offset and copy size specified for the source MemoryStorage"
265 " is out of range for it.");
266
268 dstOffset + N <= dstMemoryStorage.size(),
269 "The offset and size specified for the destination MemoryStorage"
270 " is out of range for it.");
271
273 N, dstMemoryStorage.begin() + dstOffset, this->begin() + srcOffset);
274 }
275
276
277 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
278 template <dftfe::utils::MemorySpace memorySpaceSrc>
279 void
281 const MemoryStorage<ValueType, memorySpaceSrc> &srcMemoryStorage)
282 {
284 srcMemoryStorage.size() <= d_size,
285 "The allocated size of the MemoryStorage is insufficient to "
286 " copy from the source MemoryStorage.");
288 srcMemoryStorage.size(), this->begin(), srcMemoryStorage.begin());
289 }
290
291 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
292 template <dftfe::utils::MemorySpace memorySpaceSrc>
293 void
295 const MemoryStorage<ValueType, memorySpaceSrc> &srcMemoryStorage,
296 const std::size_t N,
297 const std::size_t srcOffset,
298 const std::size_t dstOffset)
301 srcOffset + N <= srcMemoryStorage.size(),
302 "The offset and copy size specified for the source MemoryStorage"
303 " is out of range for it.");
304
306 dstOffset + N <= d_size,
307 "The offset and size specified for the destination MemoryStorage"
308 " is out of range for it.");
309
311 N, this->begin() + dstOffset, srcMemoryStorage.begin() + srcOffset);
312 }
313
314 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
315 template <dftfe::utils::MemorySpace memorySpaceDst>
316 void
323
324 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
325 template <dftfe::utils::MemorySpace memorySpaceDst>
326 void
328 ValueType * dst,
329 const std::size_t N,
330 const std::size_t srcOffset,
331 const std::size_t dstOffset) const
332 {
334 srcOffset + N <= d_size,
335 "The offset and copy size specified for the source MemoryStorage"
336 " is out of range for it.");
338 N, dst + dstOffset, this->begin() + srcOffset);
339 }
340
341
342 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
343 template <dftfe::utils::MemorySpace memorySpaceSrc>
344 void
346 {
348 this->begin(),
349 src);
350 }
351
352 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
353 template <dftfe::utils::MemorySpace memorySpaceSrc>
354 void
356 const std::size_t N,
357 const std::size_t srcOffset,
358 const std::size_t dstOffset)
359 {
361 dstOffset + N <= d_size,
362 "The offset and size specified for the destination MemoryStorage"
363 " is out of range for it.");
364
366 N, this->begin() + dstOffset, src + srcOffset);
368
369 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
370 void
372 std::vector<ValueType> &dst) const
373 {
374 if (dst.size() < d_size)
375 dst.resize(d_size);
376
378 d_size, dst.data(), this->begin());
379 }
380
381 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
382 void
384 std::vector<ValueType> &dst,
385 const std::size_t N,
386 const std::size_t srcOffset,
387 const std::size_t dstOffset) const
388 {
390 srcOffset + N <= d_size,
391 "The offset and copy size specified for the source MemoryStorage"
392 " is out of range for it.");
393 if (dst.size() < N + dstOffset)
394 dst.resize(N + dstOffset);
395
397 N, dst.data() + dstOffset, this->begin() + srcOffset);
398 }
399
400
401 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
402 void
404 const std::vector<ValueType> &src)
405 {
407 src.size() <= d_size,
408 "The allocated size of the MemoryStorage is insufficient to copy from "
409 "the source STL vector");
411 this->begin(),
412 src.data());
413 }
414
415 template <typename ValueType, dftfe::utils::MemorySpace memorySpace>
416 void
418 const std::vector<ValueType> &src,
419 const std::size_t N,
420 const std::size_t srcOffset,
421 const std::size_t dstOffset)
422 {
424 dstOffset + N <= d_size,
425 "The offset and size specified for the destination MemoryStorage"
426 " is out of range for it.");
427
429 srcOffset + N <= src.size(),
430 "The offset and size specified for the source STL vector "
431 " is out of range for it.");
432
434 N, this->begin() + dstOffset, src.data() + srcOffset);
435 }
436
437 template <typename ValueType, utils::MemorySpace memorySpaceDst>
439 memoryStorageFromSTL(const std::vector<ValueType> &src)
440 {
441 MemoryStorage<ValueType, memorySpaceDst> returnValue(src.size());
443 src.size(), returnValue.begin(), src.data());
444 return returnValue;
445 }
446
447 } // namespace utils
448} // 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:499
~MemoryStorage()
Destructor.
Definition MemoryStorage.t.cc:72
void swap(MemoryStorage &rhs)
Definition MemoryStorage.t.cc:215
std::size_t size() const
Returns the dimension of the Vector.
Definition MemoryStorage.t.cc:123
ValueType * d_data
Definition MemoryStorage.h:498
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:228
const ValueType & const_reference
Definition MemoryStorage.h:53
MemoryStorage & operator=(const MemoryStorage &rhs)
Copy assignment operator.
Definition MemoryStorage.t.cc:159
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:242
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:280
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:439
Definition pseudoPotentialToDftfeConverter.cc:34