Line data Source code
1 : /*
2 : * Copyright (c) 2009-2015: G-CSC, Goethe University Frankfurt
3 : * Author: Martin Rupp
4 : *
5 : * This file is part of UG4.
6 : *
7 : * UG4 is free software: you can redistribute it and/or modify it under the
8 : * terms of the GNU Lesser General Public License version 3 (as published by the
9 : * Free Software Foundation) with the following additional attribution
10 : * requirements (according to LGPL/GPL v3 §7):
11 : *
12 : * (1) The following notice must be displayed in the Appropriate Legal Notices
13 : * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 : *
15 : * (2) The following notice must be displayed at a prominent place in the
16 : * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 : *
18 : * (3) The following bibliography is recommended for citation and must be
19 : * preserved in all covered files:
20 : * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 : * parallel geometric multigrid solver on hierarchically distributed grids.
22 : * Computing and visualization in science 16, 4 (2013), 151-164"
23 : * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 : * flexible software system for simulating pde based models on high performance
25 : * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 : *
27 : * This program is distributed in the hope that it will be useful,
28 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 : * GNU Lesser General Public License for more details.
31 : */
32 :
33 : #ifndef __H__UG__CPU_ALGEBRA__VECTOR__
34 : #define __H__UG__CPU_ALGEBRA__VECTOR__
35 :
36 : #include "sparsematrix.h"
37 :
38 : #include "../common/template_expressions.h"
39 : #include "../common/operations.h"
40 : #include "common/util/smart_pointer.h"
41 : #include <vector>
42 : //#include "../vector_interface/ivector.h"
43 :
44 : namespace ug{
45 : ///////////////////////////////////////////////////////////////////
46 : // Vector
47 : ///////////////////////////////////////////////////////////////////
48 :
49 : /// \addtogroup cpu_algebra
50 : /// \{
51 :
52 : //!
53 : template <typename TValueType>
54 : class Vector //: public IVector
55 : {
56 : public:
57 : typedef TValueType value_type;
58 : //typedef subvector<value_type> subvector_type;
59 : typedef Vector<TValueType> vector_type;
60 :
61 : // IVECTOR_TO_VEC_FUNCTIONS(vector_type)
62 :
63 : //! constructor
64 : Vector();
65 :
66 : //! constructor with size
67 : Vector(size_t size);
68 :
69 : //! virtual destructor
70 : virtual ~Vector();
71 :
72 0 : Vector(const vector_type & v)
73 0 : {
74 0 : m_capacity = 0;
75 0 : m_size = 0; values = NULL;
76 0 : create(v.m_size);
77 0 : operator =(v);
78 0 : }
79 :
80 : public:
81 : //! create a vector with specific size
82 : void create(size_t size);
83 : //! create as a copy of other vector
84 : void create(const Vector &v);
85 :
86 : //! clones the vector (deep-copy) including values
87 : SmartPtr<vector_type> clone() const;
88 :
89 : //! clones the vector (deep-copy) excluding values
90 : SmartPtr<vector_type> clone_without_values() const;
91 :
92 : //! resize vector.
93 : //! if bigger than capacity, new capacity is newSize+oldSize/2
94 : //! so growth factor is 1.5 (this is to keep memory overhead small)
95 : //! \see also https://github.com/facebook/folly/blob/master/folly/docs/FBVector.md
96 : void resize_sloppy(size_t newSize, bool bCopyValues=true);
97 :
98 : //! resize the vector to be EXACTLY newSize big (no overhead)
99 : void resize_exactly(size_t newSize, bool bCopyValues=true);
100 :
101 : //! reserve will allocate EXACTLY newCapacity
102 : //! assertion if newCapacity < size
103 : void reserve_exactly(size_t newCapacity, bool bCopyValues);
104 :
105 : //! reserve capacity in vector.
106 : //! if bigger than capacity, new capacity is newCapacity+oldCapacity/2
107 : void reserve_sloppy(size_t newCapacity, bool bCopyValues=true);
108 :
109 : void resize(size_t newSize, bool bCopyValues=true)
110 : {
111 : resize_exactly(newSize, bCopyValues);
112 0 : }
113 : void reserve(size_t newCapacity, bool bCopyValues=true)
114 : {
115 : reserve_exactly(newCapacity, bCopyValues);
116 : }
117 :
118 : size_t capacity() const
119 : {
120 : return m_capacity;
121 : }
122 :
123 : //! access element i of the vector
124 : inline value_type &operator [] (size_t i);
125 : inline const value_type &operator [] (size_t i) const;
126 :
127 :
128 : //! returns v.T w, that is the dotprod of this vector and w
129 : double dotprod(const Vector &w); //const;
130 :
131 : // deprecated, use x.T() * y.
132 : //inline double operator *(const Vector &w); ///< shortcut for .dotprod(w)
133 :
134 : //double energynorm2(const SparseMatrix &A) const;
135 : /*double energynorm(const SparseMatrix &A) const
136 : {
137 : return sqrt(energynorm2(A));
138 : }*/
139 :
140 : //! assign double d to whole Vector
141 : double operator = (double d);
142 : //! assign double d to whole Vector
143 0 : void set(double d) { operator = (d);}
144 : void set_random(double from, double to);
145 :
146 : /** add/set/get a local vector
147 : *
148 : * The local vector type must provide the following members:
149 : * - size() - length of local vector
150 : * - index(size_t i) - global index for component i
151 : * - operator[](size_t i) - access to value of component i
152 : */
153 : template <typename V> void add(const V& u);
154 : template <typename V> void set(const V& u);
155 : template <typename V> void get(V& u) const;
156 :
157 :
158 : void add(const value_type *u, const size_t *indices, size_t nr);
159 : void set(const value_type *u, const size_t *indices, size_t nr);
160 : void get(value_type *u, const size_t *indices, size_t nr) const;
161 :
162 :
163 : //template<typename T> inline void apply(Operation_type op, const T &t);
164 :
165 : //! assign other vector v
166 : inline void operator = (const Vector &v);
167 : inline void operator += (const Vector &v);
168 : inline void operator -= (const Vector &v);
169 :
170 : inline void operator *= (const number &a)
171 : {
172 0 : for(size_t i=0; i<size(); i++) values[i] *= a;
173 : }
174 :
175 : //! return sqrt(sum values[i]^2) (euclidian norm)
176 : inline double norm() const;
177 :
178 : //! return max values[i] (max norm)
179 : inline double maxnorm() const;
180 :
181 0 : size_t size() const { return m_size; }
182 :
183 :
184 : public: // output functions
185 : //! print vector to console
186 : void print(const char * const text = NULL) const;
187 0 : void p() {print(); } ///< gdb shortcut for print
188 :
189 : //! ostream << operator
190 : friend std::ostream &operator<<(std::ostream &output, const Vector &v)
191 : {
192 : output << "Vector " << "[" << v.m_size << "]";
193 : return output;
194 : }
195 :
196 : size_t defragment() { return true; }
197 :
198 : public:
199 : /*size_t begin_index() { return 0;}
200 : size_t end_index() { return size();}
201 :
202 : value_type *begin() { return values + begin_index(); }
203 : value_type *end() { return values + end_index(); }*/
204 :
205 : protected:
206 : //! virtual clone using covariant return type
207 : /**
208 : * This should be used instead of the copy constructor at the places
209 : * where the additional information stored in the object of the derived
210 : * class (like the geometry or the topology of the grid) should be kept.
211 : */
212 : virtual vector_type* virtual_clone() const;
213 :
214 : //! virtual clone using covariant return type excluding values
215 : /**
216 : * This should be used instead of the copy constructor at the places
217 : * where the additional information stored in the object of the derived
218 : * class (like the geometry or the topology of the grid) should be kept.
219 : */
220 : virtual vector_type* virtual_clone_without_values() const;
221 :
222 : private:
223 : void destroy();
224 :
225 : size_t m_size; ///< size of the vector (vector is from 0..size-1)
226 : size_t m_capacity; ///< size of the vector (vector is from 0..size-1)
227 : value_type *values; ///< array where the values are stored, size m_size
228 :
229 : //mutable vector_mode dist_mode;
230 : };
231 :
232 : // end group cpu_algebra
233 : /// \}
234 :
235 : } // namespace ug
236 :
237 : #include "vector_impl.h"
238 :
239 : #endif
|