b2api
B2000++ API Reference Manual, VERSION 4.6
 
Loading...
Searching...
No Matches
b2linear_algebra_utils.H
1//------------------------------------------------------------------------
2// b2linear_algebra_utils.H --
3//
4// A framework for generic linear algebraic (matrix) computations.
5//
6// written by Mathias Doreille
7//
8// Copyright (c) 2004-2012 SMR Engineering & Development SA
9// 2502 Bienne, Switzerland
10//
11// All Rights Reserved. Proprietary source code. The contents of
12// this file may not be disclosed to third parties, copied or
13// duplicated in any form, in whole or in part, without the prior
14// written permission of SMR.
15//------------------------------------------------------------------------
16
17#ifndef __B2LINEAR_ALGEBRA_UTILS_H__
18#define __B2LINEAR_ALGEBRA_UTILS_H__
19
20#include <cassert>
21
22#include "b2linear_algebra_def.H"
23#include "b2ppconfig.h"
24#include "utils/b2logging.H"
25
26namespace b2000 {
27
28using std::size_t;
29
30namespace b2linalg {
31
32// ====================================
33// temporary buffer for sparse operation
34// ====================================
35
36#ifdef HAVE_LIB_TBB
37#define thread_local_storage __thread
38#else
39#define thread_local_storage
40#endif
41
43template <typename T>
45public:
50 static T* get(size_t size) {
51 if (buffer_size < size) {
52 while (buffer_size < size) { buffer_size = buffer_size ? buffer_size * 2 : 1; }
53 delete[] buffer;
54 buffer = new T[buffer_size];
55 std::fill_n(buffer, buffer_size, initialisation_value);
56 }
57 return buffer;
58 }
59
60private:
61 static thread_local_storage size_t buffer_size;
62 static thread_local_storage T* buffer;
63 static T initialisation_value;
64};
65
66template <typename T>
67thread_local_storage size_t TemporaryBuffer<T>::buffer_size = 0;
68
69template <typename T>
70thread_local_storage T* TemporaryBuffer<T>::buffer = 0;
71
72template <typename T>
74
75template <>
77
78// ====================================
79// Slice for sub Vector/Matrix
80// ====================================
81
82class Interval {
83public:
84 Interval(size_t start_, size_t end_) : start(start_), end(end_) {}
85
86 bool is_null() const { return this == &null; }
87
88 bool is_all() const { return this == &all; }
89
90 size_t size() const { return end - start; }
91
92 size_t operator[](size_t i) const { return start + i; }
93
94 size_t get_start() const { return start; }
95
96 size_t get_end() const { return end; }
97
98 std::pair<bool, size_t> find(size_t i) const {
99 if (i >= start && i < end) { return std::pair<bool, size_t>(true, i - start); }
100 return std::pair<bool, size_t>(false, 0);
101 }
102
103 static Interval null;
104 static Interval all;
105
106protected:
107 MVFRIEND;
108 friend class Index;
109 size_t start;
110 size_t end;
111};
112
113class Slice {
114public:
115 Slice(size_t start_, size_t end_ = 0, size_t step_ = 1)
116 : start(start_), end(end_ == 0 ? start_ + 1 : end_), step(step_) {}
117
118 bool is_null() const { return this == &null; }
119
120 bool is_all() const { return this == &all; }
121
122 size_t size() const { return (end - start) / step; }
123
124 size_t increment() const { return step; }
125
126 size_t operator[](size_t i) const { return start + i * step; }
127
128 std::pair<bool, size_t> find(size_t i) const {
129 if (i >= start && i < end) {
130 size_t r = i - start;
131 if ((r % step) == 0) { return std::pair<bool, size_t>(true, r / start); }
132 }
133 return std::pair<bool, size_t>(false, 0);
134 }
135
136 static Slice null;
137 static Slice all;
138
139protected:
140 MVFRIEND;
141 size_t start;
142 size_t end;
143 size_t step;
144};
145
146class Index : public std::vector<size_t> {
147public:
148 Index(size_t s = 0) : std::vector<size_t>(s) {}
149
150 Index(const Index::const_iterator& ibegin, const Index::const_iterator& iend)
151 : std::vector<size_t>(ibegin, iend) {}
152
153 Index(size_t start, size_t end) {
154 reserve(end - start);
155 while (start != end) { push_back(start++); }
156 }
157
158 bool is_null() const { return this == &null; }
159
160 bool is_all() const { return this == &all; }
161
162 void set_zero() { std::fill(begin(), end(), 0); }
163
164 std::pair<bool, size_t> find(size_t i) const {
165 std::vector<size_t>::const_iterator ii = std::lower_bound(begin(), end(), i);
166 if (*ii == i) { return std::pair<bool, size_t>(true, ii - begin()); }
167 return std::pair<bool, size_t>(false, 0);
168 }
169
170 bool sorted() const {
171 for (size_t i = 1; i != size(); ++i) {
172 if (operator[](i - 1) > operator[](i)) { return false; }
173 }
174 return true;
175 }
176
177 Index operator()(const Interval& i) { return Index(begin() + i.start, begin() + i.end); }
178
179 Index make_dual() const {
180 Index index(size());
181 for (size_t i = 0; i != size(); ++i) {
182 const size_t ii = operator[](i);
183 assert(ii < size());
184 index[ii] = i;
185 }
186 return index;
187 }
188
189 static Index null;
190 static Index all;
191
192 friend logging::Logger& operator<<(logging::Logger& l, const Index& v) {
193 l << "index vector ";
194 l.write(v.size(), &v[0], 1);
195 return l;
196 }
197};
198
199std::ostream& operator<<(std::ostream& out, const Index& index);
200
201} // namespace b2linalg
202} // namespace b2000
203
204#endif
Definition b2linear_algebra_utils.H:44
static T * get(size_t size)
Definition b2linear_algebra_utils.H:50
Contains the base classes for implementing Finite Elements.
Definition b2boundary_condition.H:32