Loading [MathJax]/extensions/tex2jax.js
ug4
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
vector_impl.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010-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
34#ifndef __H__UG__CPU_ALGEBRA__VECTOR_IMPL__
35#define __H__UG__CPU_ALGEBRA__VECTOR_IMPL__
36
37#include <fstream>
38#include <algorithm>
39#include "algebra_misc.h"
40#include "common/math/ugmath.h"
41#include "vector.h" // for urand
42
43#define prefetchReadWrite(a)
44
45namespace ug{
46template<typename value_type>
48{
49 UG_ASSERT(i < m_size, *this << ": tried to access element " << i);
50 return values[i];
51}
52
53template<typename value_type>
54inline const value_type &Vector<value_type>::operator [] (size_t i) const
55{
56 UG_ASSERT(i < m_size, *this << ": tried to access element " << i);
57 return values[i];
58}
59
60
61// energynorm2 = x*(A*x)
62/*inline double Vector<value_type>::energynorm2(const SparseMatrix &A) const
63{
64 double sum=0;
65 for(size_t i=0; i<m_size; i++) sum += (A[i] * (*this)) * values[i];
66 //FOR_UNROLL_FWD(i, 0, m_size, UNROLL, sum += A[i] * (*this) * values[i]);
67 return sum;
68}*/
69
70// dotprod
71template<typename value_type>
72inline double Vector<value_type>::dotprod(const Vector &w) //const
73{
74 UG_ASSERT(m_size == w.m_size, *this << " has not same size as " << w);
75
76 double sum=0;
77 for(size_t i=0; i<m_size; i++) sum += VecProd(values[i], w[i]);
78 return sum;
79}
80
81// assign double to whole Vector
82template<typename value_type>
83inline double Vector<value_type>::operator = (double d)
85 for(size_t i=0; i<m_size; i++)
86 values[i] = d;
87 return d;
88}
89
90template<typename value_type>
91inline void Vector<value_type>::set_random(double from, double to)
92{
93 for(size_t i=0; i<size(); i++)
94 for(size_t j=0; j<GetSize(values[i]); j++)
95 BlockRef(values[i], j) = urand(from, to);
97
98
99template<typename value_type>
101{
102 resize(v.size());
103 for(size_t i=0; i<m_size; i++)
104 values[i] = v[i];
105}
106
107template<typename value_type>
109{
110 UG_ASSERT(v.size() == size(), "vector sizes must match! (" << v.size() << " != " << size() << ")");
111 for(size_t i=0; i<m_size; i++)
112 values[i] += v[i];
113}
114
115template<typename value_type>
117{
118 UG_ASSERT(v.size() == size(), "vector sizes must match! (" << v.size() << " != " << size() << ")");
119 for(size_t i=0; i<m_size; i++)
120 values[i] -= v[i];
121}
122
126template<typename value_type>
127Vector<value_type>::Vector () : m_size(0), m_capacity(0), values(NULL)
128{
129 FORCE_CREATION { p(); } // force creation of this rountines for gdb.
130}
131
132template<typename value_type>
133Vector<value_type>::Vector(size_t size) : m_size(0), m_capacity(0), values(NULL)
134{
135 FORCE_CREATION { p(); } // force creation of this rountines for gdb.
136 create(size);
137}
138
139template<typename value_type>
142 destroy();
143}
145template<typename value_type>
147{
148 if(values)
149 {
150 delete [] values;
151 values = NULL;
152 }
153 m_size = 0;
156
157template<typename value_type>
160 if(m_size == size) return;
161 destroy();
162 m_size = size;
163 values = new value_type[size];
164 m_capacity = size;
165}
168template<typename value_type>
173
174template<typename value_type>
177 return SmartPtr<Vector<value_type> >(this->virtual_clone());
178}
180template<typename value_type>
185
186template<typename value_type>
188{
189 return SmartPtr<Vector<value_type> >(this->virtual_clone_without_values());
190}
191
192template<typename value_type>
193void Vector<value_type>::reserve_exactly(size_t newCapacity, bool bCopyValues)
194{
195 UG_ASSERT(newCapacity >= m_size, "use resize, then reserve_exactly");
196 value_type *new_values = new value_type[newCapacity];
197 // we cannot use memcpy here bcs of variable blocks.
198 if(values != NULL && bCopyValues)
199 {
200 for(size_t i=0; i<m_size; i++)
201 std::swap(new_values[i], values[i]);
202 for(size_t i=m_size; i<newCapacity; i++)
203 new_values[i] = 0.0;
204 }
205 if(values) delete [] values;
206 values = new_values;
207 m_capacity = newCapacity;
208}
209
210
211template<typename value_type>
212void Vector<value_type>::reserve_sloppy(size_t newCapacity, bool bCopyValues)
213{
214 if(newCapacity <= m_capacity) return;
215 reserve_exactly(newCapacity + m_capacity/2, bCopyValues);
216}
217
218template<typename value_type>
219void Vector<value_type>::resize_sloppy(size_t newSize, bool bCopyValues)
221 if(newSize > m_capacity)
222 {
223 size_t newCapacity = m_size/2 + newSize;
224 reserve_exactly(newCapacity, true);
225 }
226 m_size = newSize;
227}
228
229template<typename value_type>
230void Vector<value_type>::resize_exactly(size_t newSize, bool bCopyValues)
231{
232 if(newSize > m_capacity)
233 reserve_exactly(newSize, true);
234 m_size = newSize;
235}
236
237
238
239template<typename value_type>
241{
242 UG_ASSERT(m_size == 0, *this << " already created");
243 m_size = v.m_size;
244 values = new value_type[m_size];
245 m_capacity = m_size;
246
247 // we cannot use memcpy here bcs of variable blocks.
248 for(size_t i=0; i<m_size; i++)
249 values[i] = v.values[i];
250}
251
252
253// print
254template<typename value_type>
255void Vector<value_type>::print(const char * const text) const
256{
257
258 if(text) std::cout << " == " << text;
259 std::cout << " size: " << m_size << " =================" << std::endl;
260 for(size_t i=0; i<m_size; i++)
261 //cout << values[i] << " ";
262 std::cout << i << ": " << values[i] << std::endl;
263 std::cout << std::endl;
264}
265
266
267template<typename value_type>
268template<typename V>
270{
271 for(size_t i=0; i < u.size(); i++)
272 values[u.index(i)] += u[i];
273}
274
275template<typename value_type>
276template<typename V>
278{
279 for(size_t i=0; i < u.size(); i++)
280 values[u.index(i)] = u[i];
281}
282
283template<typename value_type>
284template<typename V>
286{
287 for(size_t i=0; i < u.size(); i++)
288 u[i] = values[u.index(i)];
289}
290
291
292
293
294template<typename value_type>
295void Vector<value_type>::add(const value_type *u, const size_t *indices, size_t nr)
296{
297 for(size_t i=0; i < nr; i++)
298 values[indices[i]] += u[i];
299}
300
301template<typename value_type>
302void Vector<value_type>::set(const value_type *u, const size_t *indices, size_t nr)
303{
304 for(size_t i=0; i < nr; i++)
305 values[indices[i]] = u[i];
306}
307
308template<typename value_type>
309void Vector<value_type>::get(value_type *u, const size_t *indices, size_t nr) const
310{
311 for(size_t i=0; i < nr; i++)
312 u[i] = values[indices[i]] ;
313}
314
315
316template<typename value_type>
318{
319 return x.T().dotprod(y);
320}
321
322template<typename value_type>
323inline double Vector<value_type>::norm() const
324{
325 double d=0;
326 for(size_t i=0; i<size(); ++i)
327 d+=BlockNorm2(values[i]);
328 return sqrt(d);
329}
330
331template<typename value_type>
332inline double Vector<value_type>::maxnorm() const
333{
334 double d=0;
335 for(size_t i=0; i<size(); ++i)
336 d = std::max(d, BlockMaxNorm(values[i]));
337 return d;
338}
339
340template<typename TValueType>
342{
343 dest.resize(src.size());
344}
345
346template<typename TValueType, class TOStream>
347void Serialize(TOStream &buf, const Vector<TValueType> &v)
348{
349 Serialize(buf, v.size());
350 for(size_t i=0; i < v.size(); i++)
351 Serialize(buf, v[i]);
352}
353
354template<typename TValueType, class TIStream>
355void Deserialize(TIStream &buf, Vector<TValueType> &v)
356{
357 size_t s = Deserialize<size_t>(buf);
358 v.resize(s);
359 for(size_t i=0; i < s; i++)
360 Deserialize(buf, v[i]);
361}
362
363}//namespace ug
364
365#endif
parameterString s
Definition smart_pointer.h:108
this helper class is a transposed of class A
Definition template_expressions.h:76
Definition vector.h:55
virtual ~Vector()
virtual destructor
Definition vector_impl.h:140
void resize_exactly(size_t newSize, bool bCopyValues=true)
resize the vector to be EXACTLY newSize big (no overhead)
Definition vector_impl.h:230
virtual vector_type * virtual_clone_without_values() const
virtual clone using covariant return type excluding values
Definition vector_impl.h:181
void set(double d)
assign double d to whole Vector
Definition vector.h:143
void resize_sloppy(size_t newSize, bool bCopyValues=true)
Definition vector_impl.h:219
void reserve_sloppy(size_t newCapacity, bool bCopyValues=true)
Definition vector_impl.h:212
TValueType value_type
Definition vector.h:57
void operator-=(const Vector &v)
Definition vector_impl.h:116
void set_random(double from, double to)
Definition vector_impl.h:91
void print(const char *const text=NULL) const
print vector to console
Definition vector_impl.h:255
void operator+=(const Vector &v)
Definition vector_impl.h:108
SmartPtr< vector_type > clone() const
clones the vector (deep-copy) including values
Definition vector_impl.h:175
void add(const V &u)
Definition vector_impl.h:269
virtual vector_type * virtual_clone() const
virtual clone using covariant return type
Definition vector_impl.h:169
double norm() const
return sqrt(sum values[i]^2) (euclidian norm)
Definition vector_impl.h:323
void destroy()
Definition vector_impl.h:146
void create(size_t size)
create a vector with specific size
Definition vector_impl.h:158
double dotprod(const Vector &w)
returns v.T w, that is the dotprod of this vector and w
Definition vector_impl.h:72
value_type & operator[](size_t i)
access element i of the vector
Definition vector_impl.h:47
void get(V &u) const
Definition vector_impl.h:285
void reserve_exactly(size_t newCapacity, bool bCopyValues)
Definition vector_impl.h:193
void resize(size_t newSize, bool bCopyValues=true)
Definition vector.h:109
double operator=(double d)
assign double d to whole Vector
Definition vector_impl.h:83
value_type * values
array where the values are stored, size m_size
Definition vector.h:227
void p()
gdb shortcut for print
Definition vector.h:187
double maxnorm() const
return max values[i] (max norm)
Definition vector_impl.h:332
size_t size() const
Definition vector.h:181
size_t m_size
size of the vector (vector is from 0..size-1)
Definition vector.h:225
SmartPtr< vector_type > clone_without_values() const
clones the vector (deep-copy) excluding values
Definition vector_impl.h:187
Vector()
constructor
Definition vector_impl.h:127
#define FORCE_CREATION
Definition algebra_misc.h:46
#define UG_ASSERT(expr, msg)
Definition assert.h:70
TNumber urand(TNumber lowerBound, TNumber upperBound)
uniform distributed random numbers in [lowerBound, upperBound[. Use srand to set a seed.
Definition math_util_impl.hpp:67
the ug namespace
void CloneVector(Vector< TValueType > &dest, const Vector< TValueType > &src)
Definition vector_impl.h:341
double VecProd(const double &a, const double &b)
returns scal<a, b>
Definition operations_vec.h:84
double & BlockRef(T &vec, size_t i)
Definition blocks.h:66
void Deserialize(TIStream &buf, ParallelVector< T > &v)
Deerialize for ParallelVector<T>
Definition restart_bridge.cpp:112
MatVec_Expression< L, R > operator*(const AlphaMat_Expression< L > &l, const R &r)
create a MatVec_Expression by (alpha*MATRIX) * VECTOR
Definition template_expressions.h:223
void Serialize(TOStream &buf, const ParallelVector< T > &v)
Serialize for ParallelVector<T>
Definition restart_bridge.cpp:103
number BlockMaxNorm(const T &t)
double BlockNorm2(const TYPE &v)
Definition blocks.h:51
size_t GetSize(const T &t)
bool resize(size_t newRows, size_t newCols)