/usr/include/deal.II/lac/sparse_vanka.templates.h is in libdeal.ii-dev 6.3.1-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 | //---------------------------------------------------------------------------
// $Id: sparse_vanka.templates.h 19894 2009-10-15 22:19:51Z kanschat $
// Version: $Name$
//
// Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by the deal.II authors
//
// This file is subject to QPL and may not be distributed
// without copyright and license information. Please refer
// to the file deal.II/doc/license.html for the text and
// further information on this license.
//
//---------------------------------------------------------------------------
#ifndef __deal2__sparse_vanka_templates_h
#define __deal2__sparse_vanka_templates_h
#include <base/memory_consumption.h>
#include <base/thread_management.h>
#include <lac/sparse_vanka.h>
#include <lac/full_matrix.h>
#include <lac/sparse_matrix.h>
#include <lac/vector.h>
#include <algorithm>
#include <map>
DEAL_II_NAMESPACE_OPEN
template<typename number>
SparseVanka<number>::SparseVanka(const SparseMatrix<number> &M,
const std::vector<bool> &selected,
const bool conserve_mem,
const unsigned int n_threads)
:
matrix (&M, typeid(*this).name()),
conserve_mem (conserve_mem),
selected (selected),
n_threads (n_threads),
inverses (M.m(), 0)
{
Assert (M.m() == M.n(), ExcNotQuadratic ());
Assert (M.m() == selected.size(), ExcDimensionMismatch(M.m(), selected.size()));
if (conserve_mem == false)
compute_inverses ();
}
template<typename number>
SparseVanka<number>::~SparseVanka()
{
typename std::vector<SmartPointer<FullMatrix<float>,SparseVanka<number> > >::iterator i;
for(i=inverses.begin(); i!=inverses.end(); ++i)
{
FullMatrix<float> *p = *i;
*i = 0;
if (p != 0) delete p;
}
}
template <typename number>
void
SparseVanka<number>::compute_inverses ()
{
if (!DEAL_II_USE_MT)
compute_inverses (0, matrix->m());
else
{
const unsigned int n_inverses = std::count (selected.begin(),
selected.end(),
true);
const unsigned int n_inverses_per_thread = std::max(n_inverses / n_threads,
1U);
// set up start and end index
// for each of the
// threads. note that we have
// to work somewhat to get this
// appropriate, since the
// indices for which inverses
// have to be computed may not
// be evenly distributed in the
// vector. as an extreme
// example consider numbering
// of DoFs by component, then
// all indices for which we
// have to do work will be
// consecutive, with other
// consecutive regions where we
// do not have to do something
std::vector<std::pair<unsigned int, unsigned int> > blocking (n_threads);
unsigned int c = 0;
unsigned int thread = 0;
blocking[0].first = 0;
for (unsigned int i=0; (i<matrix->m()) && (thread+1<n_threads); ++i)
{
if (selected[i] == true)
++c;
if (c == n_inverses_per_thread)
{
blocking[thread].second = i;
blocking[thread+1].first = i;
++thread;
c = 0;
};
};
blocking[n_threads-1].second = matrix->m();
typedef void (SparseVanka<number>::*FunPtr)(const unsigned int,
const unsigned int);
const FunPtr fun_ptr = &SparseVanka<number>::compute_inverses;
// Now spawn the threads
Threads::ThreadGroup<> threads;
for (unsigned int i=0; i<n_threads; ++i)
threads += Threads::new_thread (fun_ptr, *this,
blocking[i].first,
blocking[i].second);
threads.join_all ();
};
}
template <typename number>
void
SparseVanka<number>::compute_inverses (const unsigned int begin,
const unsigned int end)
{
// set-up the vector that will be used
// by the functions which we call
// below.
std::vector<unsigned int> local_indices;
// traverse all rows of the matrix
// which are selected
for (unsigned int row=begin; row<end; ++row)
if (selected[row] == true)
compute_inverse (row, local_indices);
}
template <typename number>
void
SparseVanka<number>::compute_inverse (const unsigned int row,
std::vector<unsigned int> &local_indices)
{
// first define an alias to the sparsity
// pattern of the matrix, since this
// will be used quite often
const SparsityPattern &structure
= matrix->get_sparsity_pattern();
const unsigned int row_length = structure.row_length(row);
FullMatrix<float> &this_inverse = *new FullMatrix<float> (row_length,
row_length);
inverses[row] = &this_inverse;
// collect the dofs that couple
// with @p row
local_indices.resize (row_length);
for (unsigned int i=0; i<row_length; ++i)
local_indices[i] = structure.column_number(row, i);
// Build local matrix
for (unsigned int i=0; i<row_length; ++i)
for (unsigned int j=0; j<row_length; ++j)
{
// if DoFs local_index[i] and
// local_index[j] couple with
// each other, then get the
// value from the global
// matrix. if not, then leave
// the value in the small
// matrix at zero
//
// the explicit use of operator()
// works around a bug in some gcc
// versions (see PR 18803)
const unsigned int global_entry =
structure.operator()(local_indices[i], local_indices[j]);
if (global_entry != SparsityPattern::invalid_entry)
// the explicit use of operator()
// works around a bug in some gcc
// versions (see PR 18803)
this_inverse.operator()(i,j) = matrix->global_entry(global_entry);
}
// Compute inverse
this_inverse.gauss_jordan();
}
template<typename number>
template<typename number2>
void
SparseVanka<number>::vmult (Vector<number2> &dst,
const Vector<number2> &src) const
{
// first set output vector to zero
dst = 0;
// then pass on to the function
// that actually does the work
apply_preconditioner (dst, src);
}
template<typename number>
template<typename number2>
void
SparseVanka<number>::apply_preconditioner (Vector<number2> &dst,
const Vector<number2> &src,
const std::vector<bool> *const dof_mask) const
{
Assert (dst.size() == src.size(),
ExcDimensionMismatch(dst.size(), src.size()));
Assert (dst.size() == matrix->m(),
ExcDimensionMismatch(dst.size(), src.size()));
// first define an alias to the sparsity
// pattern of the matrix, since this
// will be used quite often
const SparsityPattern &structure
= matrix->get_sparsity_pattern();
// store whether we shall work on
// the whole matrix, or only on
// blocks. this variable is used to
// optimize access to vectors a
// little bit.
const bool range_is_restricted = (dof_mask != 0);
// space to be used for local
// systems. allocate as much memory
// as is the maximum. this
// eliminates the need to
// re-allocate memory inside the
// loop.
FullMatrix<float> local_matrix (structure.max_entries_per_row(),
structure.max_entries_per_row());
Vector<float> b (structure.max_entries_per_row());
Vector<float> x (structure.max_entries_per_row());
std::map<unsigned int, unsigned int> local_index;
// traverse all rows of the matrix
// which are selected
const unsigned int n = matrix->m();
for (unsigned int row=0; row<n; ++row)
if ((selected[row] == true) &&
((range_is_restricted == false) || ((*dof_mask)[row] == true)))
{
const unsigned int row_length = structure.row_length(row);
// if we don't store the
// inverse matrices, then alias
// the entry in the global
// vector to the local matrix
// to be used
if (conserve_mem == true)
{
inverses[row] = &local_matrix;
inverses[row]->reinit (row_length, row_length);
};
b.reinit (row_length);
x.reinit (row_length);
// mapping between:
// 1 column number of all
// entries in this row, and
// 2 the position within this
// row (as stored in the
// SparsityPattern object
//
// since we do not explicitly
// consider nonsysmmetric sparsity
// patterns, the first element
// of each entry simply denotes
// all degrees of freedom that
// couple with @p row.
local_index.clear ();
for (unsigned int i=0; i<row_length; ++i)
local_index.insert(std::pair<unsigned int, unsigned int>
(structure.column_number(row, i), i));
// Build local matrix and rhs
for (std::map<unsigned int, unsigned int>::const_iterator is=local_index.begin();
is!=local_index.end(); ++is)
{
// irow loops over all DoFs that
// couple with the present DoF
const unsigned int irow = is->first;
// index of DoF irow in the matrix
// row corresponding to DoF @p row.
// runs between 0 and row_length
const unsigned int i = is->second;
// number of DoFs coupling to
// irow (including irow itself)
const unsigned int irow_length = structure.row_length(irow);
// copy rhs
b(i) = src(irow);
// for all the DoFs that irow
// couples with
for (unsigned int j=0; j<irow_length; ++j)
{
// col is the number of
// this dof
const unsigned int col = structure.column_number(irow, j);
// find out whether this DoF
// (that couples with @p irow,
// which itself couples with
// @p row) also couples with
// @p row.
const std::map<unsigned int, unsigned int>::const_iterator js
= local_index.find(col);
// if not, then still use
// this dof to modify the rhs
//
// note that if so, we already
// have copied the entry above
if (js == local_index.end())
{
if (!range_is_restricted ||
((*dof_mask)[col] == true))
b(i) -= matrix->raw_entry(irow,j) * dst(col);
}
else
// if so, then build the
// matrix out of it
if (conserve_mem == true)
(*inverses[row])(i,js->second) = matrix->raw_entry(irow,j);
};
};
// Compute new values
if (conserve_mem == true)
inverses[row]->gauss_jordan();
// apply preconditioner
inverses[row]->vmult(x,b);
// Distribute new values
for (std::map<unsigned int, unsigned int>::const_iterator is=local_index.begin();
is!=local_index.end(); ++is)
{
const unsigned int irow = is->first;
const unsigned int i = is->second;
if (!range_is_restricted ||
((*dof_mask)[irow] == true))
dst(irow) = x(i);
// do nothing if not in
// the range
};
// if we don't store the
// inverses, then unalias the
// local matrix
if (conserve_mem == true)
inverses[row] = 0;
};
}
template <typename number>
unsigned int
SparseVanka<number>::memory_consumption () const
{
unsigned int mem = (sizeof(*this) +
MemoryConsumption::memory_consumption (selected));
for (unsigned int i=0; i<inverses.size(); ++i)
mem += MemoryConsumption::memory_consumption (*inverses[i]);
return mem;
}
template <typename number>
SparseBlockVanka<number>::SparseBlockVanka (const SparseMatrix<number> &M,
const std::vector<bool> &selected,
const unsigned int n_blocks,
const BlockingStrategy blocking_strategy,
const bool conserve_memory,
const unsigned int n_threads)
:
SparseVanka<number> (M, selected, conserve_memory, n_threads),
n_blocks (n_blocks),
dof_masks (n_blocks,
std::vector<bool>(M.m(), false))
{
compute_dof_masks (M, selected, blocking_strategy);
}
template <typename number>
void
SparseBlockVanka<number>::compute_dof_masks (const SparseMatrix<number> &M,
const std::vector<bool> &selected,
const BlockingStrategy blocking_strategy)
{
Assert (n_blocks > 0, ExcInternalError());
const unsigned int n_inverses = std::count (selected.begin(),
selected.end(),
true);
const unsigned int n_inverses_per_block = std::max(n_inverses / n_blocks, 1U);
// precompute the splitting points
std::vector<std::pair<unsigned int, unsigned int> > intervals (n_blocks);
// set up start and end index for
// each of the blocks. note that
// we have to work somewhat to get
// this appropriate, since the
// indices for which inverses have
// to be computed may not be evenly
// distributed in the vector. as an
// extreme example consider
// numbering of DoFs by component,
// then all indices for which we
// have to do work will be
// consecutive, with other
// consecutive regions where we do
// not have to do something
if (true)
{
unsigned int c = 0;
unsigned int block = 0;
intervals[0].first = 0;
for (unsigned int i=0; (i<M.m()) && (block+1<n_blocks); ++i)
{
if (selected[i] == true)
++c;
if (c == n_inverses_per_block)
{
intervals[block].second = i;
intervals[block+1].first = i;
++block;
c = 0;
};
};
intervals[n_blocks-1].second = M.m();
};
// now transfer the knowledge on
// the splitting points into the
// vector<bool>s that the base
// class wants to see. the way how
// we do this depends on the
// requested blocking strategy
switch (blocking_strategy)
{
case index_intervals:
{
for (unsigned int block=0; block<n_blocks; ++block)
std::fill_n (dof_masks[block].begin()+intervals[block].first,
intervals[block].second - intervals[block].first,
true);
break;
};
case adaptive:
{
// the splitting points for
// the DoF have been computed
// above already, but we will
// only use them to split the
// Lagrange dofs into
// blocks. splitting the
// remaining dofs will be
// done now.
// first count how often the
// Lagrange dofs of each
// block access the different
// dofs
Table<2,unsigned int> access_count (n_blocks, M.m());
// set-up the map that will
// be used to store the
// indices each Lagrange dof
// accesses
std::map<unsigned int, unsigned int> local_index;
const SparsityPattern &structure = M.get_sparsity_pattern();
for (unsigned int row=0; row<M.m(); ++row)
if (selected[row] == true)
{
// first find out to
// which block the
// present row belongs
unsigned int block_number = 0;
while (row>=intervals[block_number].second)
++block_number;
Assert (block_number < n_blocks, ExcInternalError());
// now traverse the
// matrix structure to
// find out to which
// dofs number the
// present index wants
// to write
const unsigned int row_length = structure.row_length(row);
for (unsigned int i=0; i<row_length; ++i)
++access_count[block_number][structure.column_number(row, i)];
};
// now we know that block @p i
// wants to write to DoF @p j
// as often as
// <tt>access_count[i][j]</tt>
// times. Let @p j be allotted
// to the block which
// accesses it most often.
//
// if it is a Lagrange dof,
// the of course we leave it
// to the block we put it
// into in the first place
for (unsigned int row=0; row<M.m(); ++row)
if (selected[row] == true)
{
unsigned int block_number = 0;
while (row>=intervals[block_number].second)
++block_number;
dof_masks[block_number][row] = true;
}
else
{
// find out which block
// accesses this dof
// the most often
unsigned int max_accesses = 0;
unsigned int max_access_block = 0;
for (unsigned int block=0; block<n_blocks; ++block)
if (access_count[block][row] > max_accesses)
{
max_accesses = access_count[block][row];
max_access_block = block;
};
dof_masks[max_access_block][row] = true;
};
break;
};
default:
Assert (false, ExcInternalError());
};
}
template <typename number>
template <typename number2>
void SparseBlockVanka<number>::vmult (Vector<number2> &dst,
const Vector<number2> &src) const
{
dst = 0;
// if no blocking is required, pass
// down to the underlying class
if (n_blocks == 1)
this->apply_preconditioner (dst, src);
else
// otherwise: blocking requested
{
if (DEAL_II_USE_MT)
{
// spawn threads. since
// some compilers have
// trouble finding out
// which 'encapsulate'
// function to take of all
// those possible ones if
// we simply drop in the
// address of an overloaded
// template member
// function, make it
// simpler for the compiler
// by giving it the correct
// type right away:
typedef void (SparseVanka<number>::*mem_fun_p)
(Vector<number2> &,
const Vector<number2> &,
const std::vector<bool> * const) const;
const mem_fun_p comp
= &SparseVanka<number>::template apply_preconditioner<number2>;
Threads::ThreadGroup<> threads;
for (unsigned int block=0; block<n_blocks; ++block)
threads += Threads::new_thread (comp,
*static_cast<const SparseVanka<number>*>(this),
dst, src,&dof_masks[block]);
threads.join_all ();
}
else
for (unsigned int block=0; block<n_blocks; ++block)
this->apply_preconditioner (dst, src,
&dof_masks[block]);
}
}
template <typename number>
unsigned int
SparseBlockVanka<number>::memory_consumption () const
{
unsigned int mem = SparseVanka<number>::memory_consumption();
for (unsigned int i=0; i<dof_masks.size(); ++i)
mem += MemoryConsumption::memory_consumption (dof_masks[i]);
return mem;
}
DEAL_II_NAMESPACE_CLOSE
#endif
|