Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
AlephCudaVector.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7#include "arcane/aleph/AlephArcane.h"
8#include "arcane/aleph/cuda/AlephCuda.h"
9
10//---------------------------------------------------------------------------//
11
12namespace Arcane
13{
14
15template <class T> CNC_Vector<T>::CNC_Vector(unsigned int size, unsigned int alignment)
16{
17 data_ = NULL;
18 base_mem_ = NULL;
19 size_ = 0;
20 allocate(size, alignment);
21}
22
23template <class T> CNC_Vector<T>::~CNC_Vector()
24{
25 deallocate();
26}
27
28//---------------------------------------------------------------------------//
29
30/** does not preserve previous values stored in the array */
31template <class T> void CNC_Vector<T>::allocate(unsigned int size, unsigned int alignment)
32{
33 deallocate();
34 if (size != 0) {
35 base_mem_ = (char*)malloc(size * sizeof(T) + alignment - 1);
36 char* p = base_mem_;
37 // GMY 20090825 original: while(unsigned __int64(p) % alignment) { p++ ; }
38 while (((unsigned long long)p) % alignment) {
39 ++p;
40 }
41 data_ = (T*)p;
42 for (unsigned int i = 0; i < size; i++) {
43 // Direct call to the constructor, see dlist.h for more explanations.
44 new (&data_[i]) T();
45 }
46 }
47 size_ = size;
48}
49
50//---------------------------------------------------------------------------//
51
52template <class T> void CNC_Vector<T>::set_all(const T& value)
53{
54 for (unsigned int i = 0; i < size_; i++) {
55 data_[i] = value;
56 }
57}
58
59//---------------------------------------------------------------------------//
60
61template <class T> T& CNC_Vector<T>::operator()(unsigned int i)
62{
63 return data_[i];
64}
65
66//---------------------------------------------------------------------------//
67
68template <class T> const T& CNC_Vector<T>::operator()(unsigned int i) const
69{
70 return data_[i];
71}
73//---------------------------------------------------------------------------//
74
75template <class T> T& CNC_Vector<T>::operator[](unsigned int index)
76{
77 return data_[index];
78}
79
80//---------------------------------------------------------------------------//
81
82template <class T> const T& CNC_Vector<T>::operator[](unsigned int index) const
83{
84 return data_[index];
85}
86
87//---------------------------------------------------------------------------//
88
89template <class T> T& CNC_Vector<T>::from_linear_index(unsigned int index)
90{
91 return data_[index];
92}
93
94//---------------------------------------------------------------------------//
95
96template <class T> const T& CNC_Vector<T>::from_linear_index(unsigned int index) const
97{
98 return data_[index];
99}
100
101//---------------------------------------------------------------------------//
102
103template <class T> unsigned int CNC_Vector<T>::size() const
104{
105 return size_;
106}
107
108//---------------------------------------------------------------------------//
109
110template <class T> unsigned int CNC_Vector<T>::alignment() const
111{
112 return alignment_;
113}
114
115//---------------------------------------------------------------------------//
116
117template <class T> void CNC_Vector<T>::clear()
118{
119 allocate(0);
120}
121
122//---------------------------------------------------------------------------//
123
125template <class T> const T* CNC_Vector<T>::data() const
126{
127 return data_;
128}
129
130//---------------------------------------------------------------------------//
131
133template <class T> T* CNC_Vector<T>::data()
134{
135 return data_;
136}
137
138//---------------------------------------------------------------------------//
139
140template <class T> unsigned int CNC_Vector<T>::mem_usage() const
141{
142 return size_ * sizeof(T) + sizeof(thisclass);
143}
144
145//---------------------------------------------------------------------------//
146
147template <class T> void CNC_Vector<T>::print() const
148{
149 for (unsigned int index = 0; index < size_; index++) {
150 //printf("\t[%d]=%ld\n", index, data_[index]);
151 }
152}
153
154//---------------------------------------------------------------------------//
155
156template <class T> void CNC_Vector<T>::deallocate()
157{
158 if (size_ != 0) {
159 for (unsigned int i = 0; i < size_; i++) {
160 // direct call to the destructor
161 data_[i].~T();
162 }
163 free(base_mem_);
164 data_ = NULL;
165 base_mem_ = NULL;
166 size_ = 0;
167 }
168}
169
170template class CNC_Vector<double>;
171template class CNC_Vector<long>;
173
174} // namespace Arcane
void allocate(unsigned int size, unsigned int alignment=1)
const T * data() const
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --