/usr/include/trilinos/Tpetra_VbrMatrix_def.hpp is in libtrilinos-dev 10.4.0.dfsg-1ubuntu2.
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 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 | //@HEADER
// ************************************************************************
//
// Tpetra: Templated Linear Algebra Services Package
// Copyright (2008) Sandia Corporation
//
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
// license for use of this work by or on behalf of the U.S. Government.
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
//
// ************************************************************************
//@HEADER
#ifndef TPETRA_VBRMATRIX_DEF_HPP
#define TPETRA_VBRMATRIX_DEF_HPP
#include <algorithm>
#include <sstream>
#include <Kokkos_NodeHelpers.hpp>
#include <Teuchos_Array.hpp>
#include <Teuchos_CommHelpers.hpp>
#include <Teuchos_ScalarTraits.hpp>
#include <Teuchos_OrdinalTraits.hpp>
#include <Teuchos_TypeNameTraits.hpp>
#include <Tpetra_Vector.hpp>
#ifdef DOXYGEN_USE_ONLY
#include "Tpetra_VbrMatrix_decl.hpp"
#endif
namespace Tpetra {
//-----------------------------------------------------------------
template<class LocalOrdinal,class GlobalOrdinal,class Node>
Teuchos::RCP<const Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> >
convertBlockMapToPointMap(const Teuchos::RCP<const Tpetra::BlockMap<LocalOrdinal,GlobalOrdinal,Node> >& blockMap)
{
global_size_t numGlobalElems = Teuchos::OrdinalTraits<global_size_t>::invalid();
GlobalOrdinal indexBase = blockMap->getPointMap()->getIndexBase();
const Teuchos::RCP<const Teuchos::Comm<int> >& comm = blockMap->getPointMap()->getComm();
const Teuchos::RCP<Node>& node = blockMap->getPointMap()->getNode();
//Create a point-entry map where each point
//corresponds to a block in the block-map:
return Teuchos::rcp(new Map<LocalOrdinal,GlobalOrdinal,Node>(numGlobalElems, blockMap->getNodeBlockIDs(), indexBase, comm, node));
}
//-----------------------------------------------------------------
template<class LocalOrdinal,class GlobalOrdinal,class Node>
Teuchos::RCP<const Tpetra::BlockMap<LocalOrdinal,GlobalOrdinal,Node> >
makeBlockColumnMap(const Teuchos::RCP<const Tpetra::BlockMap<LocalOrdinal,GlobalOrdinal,Node> >& blockMap, const Teuchos::RCP<const Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> >& ptColMap)
{
global_size_t numGlobalBlocks = Teuchos::OrdinalTraits<global_size_t>::invalid();
Teuchos::ArrayView<const GlobalOrdinal> blockIDs = ptColMap->getNodeElementList();
Teuchos::ArrayRCP<const LocalOrdinal> firstPoints = blockMap->getNodeFirstPointInBlocks();
Teuchos::Array<GlobalOrdinal> points(firstPoints.size()-1);
Teuchos::Array<LocalOrdinal> blockSizes(firstPoints.size()-1);
typedef typename Teuchos::ArrayView<const LocalOrdinal>::size_type Tsize_t;
for(Tsize_t i=0; i<blockSizes.size(); ++i) {
points[i] = blockMap->getFirstGlobalPointInLocalBlock(i);
blockSizes[i] = firstPoints[i+1]-firstPoints[i];
}
//We will create a block-map where each block corresponds to a point in
//the input-map 'ptColMap', and where each block's size is obtained from
//the input-block-map 'blockMap'.
//if we are running on a single processor, then it's easy because
//we know that blockMap is distributed the same as ptColMap:
int numProcessors = ptColMap->getComm()->getSize();
if (numProcessors == 1) {
return Teuchos::rcp(new Tpetra::BlockMap<LocalOrdinal,GlobalOrdinal,Node>(
numGlobalBlocks, blockIDs, points(), blockSizes(),
ptColMap->getIndexBase(), ptColMap->getComm(), ptColMap->getNode()
));
}
//If we get to here, we're running on multiple processors, and blockMap
//is probably not distributed the same as ptColMap, so we have to do
//some communication to get the block-sizes from blockMap corresponding
//to the blockIDs we got from ptColMap.
//We also have to do communication to get the global first-points-in-block
//for the blocks in the new block-column-map.
//
//I think the simplest way to do this is to create vectors where the values
//are block-sizes (or first-points-in-block), and import one to the other.
typedef Tpetra::Vector<GlobalOrdinal,LocalOrdinal,GlobalOrdinal,Node> GOVec;
typedef Tpetra::Vector<LocalOrdinal,LocalOrdinal,GlobalOrdinal,Node> LOVec;
Teuchos::RCP<const Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> > blkPtMap =
convertBlockMapToPointMap(blockMap);
LOVec source_sizes(blkPtMap, blockSizes());
GOVec source_points(blkPtMap, points());
LOVec target_sizes(ptColMap);
GOVec target_points(ptColMap);
Tpetra::Import<LocalOrdinal,GlobalOrdinal,Node> importer(blkPtMap, ptColMap);
target_sizes.doImport(source_sizes, importer, REPLACE);
target_points.doImport(source_points, importer, REPLACE);
//now we can create our block-column-map:
return Teuchos::rcp(new Tpetra::BlockMap<LocalOrdinal,GlobalOrdinal,Node>(
numGlobalBlocks, blockIDs, target_points.get1dView()(), target_sizes.get1dView()(),
ptColMap->getIndexBase(), ptColMap->getComm(), ptColMap->getNode()
));
}
template<class Scalar,class Node>
void fill_device_ArrayRCP(Teuchos::RCP<Node>& node, Teuchos::ArrayRCP<Scalar>& ptr, Scalar value)
{
Kokkos::ReadyBufferHelper<Node> rbh(node);
Kokkos::InitOp<Scalar> wdp;
wdp.alpha = value;
rbh.begin();
wdp.x = rbh.addNonConstBuffer(ptr);
rbh.end();
node->template parallel_for<Kokkos::InitOp<Scalar> >(0, ptr.size(), wdp);
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::VbrMatrix(const Teuchos::RCP<const BlockMap<LocalOrdinal,GlobalOrdinal,Node> > &blkRowMap, size_t maxNumEntriesPerRow, ProfileType pftype)
: blkRowMap_(blkRowMap),
blkColMap_(Teuchos::null),
blkDomainMap_(Teuchos::null),
blkRangeMap_(Teuchos::null),
blkGraph_(),
lclMatrix_(blkRowMap->getNodeNumBlocks(), blkRowMap->getPointMap()->getNode()),
pbuf_values1D_(),
pbuf_bptr_(),
pbuf_bindx_(),
pbuf_indx_(),
lclMatVec_(blkRowMap->getPointMap()->getNode()),
importer_(),
exporter_(),
importedVec_(),
exportedVec_(),
col_ind_2D_global_(Teuchos::rcp(new Teuchos::Array<std::map<GlobalOrdinal,Teuchos::ArrayRCP<Scalar> > >)),
values2D_(Teuchos::rcp(new Teuchos::Array<Teuchos::Array<Teuchos::ArrayRCP<Scalar> > >)),
is_fill_completed_(false),
is_storage_optimized_(false)
{
//The graph of this VBR matrix will be a CrsGraph where each entry in the
//graph corresponds to a block-entry in the matrix.
//That is, you can think of a VBR matrix as a Crs matrix of dense
//submatrices...
//First create a point-entry map where each point corresponds to
//a block in our block-row-map:
Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> >
blkPointMap = convertBlockMapToPointMap(blkRowMap);
//now create the graph:
blkGraph_ = Teuchos::rcp(new CrsGraph<LocalOrdinal,GlobalOrdinal,Node>(blkPointMap, maxNumEntriesPerRow, pftype));
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::~VbrMatrix()
{
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::getDomainMap() const
{
return blkDomainMap_->getPointMap();
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::getRangeMap() const
{
return blkRangeMap_->getPointMap();
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
bool
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::isFillComplete() const
{
return is_fill_completed_;
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
template <class DomainScalar, class RangeScalar>
void
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::multiply(const MultiVector<DomainScalar,LocalOrdinal,GlobalOrdinal,Node> & X, MultiVector<RangeScalar,LocalOrdinal,GlobalOrdinal,Node> &Y, Teuchos::ETransp trans, RangeScalar alpha, RangeScalar beta) const
{
TEST_FOR_EXCEPTION(!isFillComplete(), std::runtime_error,
"Tpetra::VbrMatrix::multiply ERROR, multiply may only be called after fillComplete has been called.");
const Kokkos::MultiVector<Scalar,Node> *lclX = &X.getLocalMV();
Kokkos::MultiVector<Scalar,Node> *lclY = &Y.getLocalMVNonConst();
lclMatVec_.template multiply<Scalar,Scalar>(trans,alpha,*lclX,beta,*lclY);
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
void
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::updateImport(const MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& X) const
{
if (importer_ != Teuchos::null) {
if (importedVec_ == Teuchos::null || importedVec_->getNumVectors() != X.getNumVectors()) {
importedVec_ = Teuchos::rcp(new MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>(blkColMap_->getPointMap(), X.getNumVectors()));
}
importedVec_->doImport(X, *importer_, REPLACE);
}
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
void
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::updateExport(const MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& Y) const
{
if (exporter_ != Teuchos::null) {
if (exportedVec_ == Teuchos::null || exportedVec_->getNumVectors() != Y.getNumVectors()) {
exportedVec_ = Teuchos::rcp(new MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>(blkColMap_->getPointMap(), Y.getNumVectors()));
}
exportedVec_->doExport(Y, *exporter_, REPLACE);
}
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
void
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::apply(
const MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &X,
MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &Y,
Teuchos::ETransp trans,
Scalar alpha,
Scalar beta) const
{
if (trans == Teuchos::NO_TRANS) {
updateImport(X);
const MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& Xref = importedVec_ == Teuchos::null ? X : *importedVec_;
this->template multiply<Scalar,Scalar>(Xref, Y, trans, alpha, beta);
}
else if (trans == Teuchos::TRANS || trans == Teuchos::CONJ_TRANS) {
updateImport(Y);
MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& Yref = importedVec_ == Teuchos::null ? Y : *importedVec_;
this->template multiply<Scalar,Scalar>(X, Yref, trans, alpha, beta);
if (importedVec_ != Teuchos::null) {
Y.doExport(*importedVec_, *importer_, ADD);
}
}
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
bool
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::hasTransposeApply() const
{
return false;
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
const Teuchos::RCP<const BlockMap<LocalOrdinal,GlobalOrdinal,Node> > &
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::getBlockRowMap() const
{
return blkRowMap_;
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::getPointRowMap() const
{
return blkRowMap_->getPointMap();
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
const Teuchos::RCP<const BlockMap<LocalOrdinal,GlobalOrdinal,Node> > &
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::getBlockColMap() const
{
return blkColMap_;
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::getPointColMap() const
{
return blkColMap_->getPointMap();
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
void
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::getGlobalBlockEntryViewNonConst(
GlobalOrdinal globalBlockRow,
GlobalOrdinal globalBlockCol,
LocalOrdinal& numPtRows,
LocalOrdinal& numPtCols,
Teuchos::ArrayRCP<Scalar>& blockEntry)
{
//Return a non-const, read-write view of a block-entry (as an ArrayRCP),
//creating/allocating the block-entry if it doesn't already exist, (but only
//if fillComplete hasn't been called yet, and if the arguments numPtRows
//and numPtCols have been set on input).
if (is_storage_optimized_) {
TEST_FOR_EXCEPTION(!isFillComplete(), std::runtime_error,
"Tpetra::VbrMatrix::getGlobalBlockEntryViewNonConst internal ERROR, storage is optimized but isFillComplete() is false.");
LocalOrdinal localBlockRow = blkRowMap_->getLocalBlockID(globalBlockRow);
LocalOrdinal localBlockCol = blkColMap_->getLocalBlockID(globalBlockCol);
getLocalBlockEntryViewNonConst(localBlockRow, localBlockCol,
numPtRows, numPtCols, blockEntry);
return;
}
//If we get to here, fillComplete hasn't been called yet, and the matrix data
//is stored in un-packed '2D' form.
if (values2D_->size() == 0) {
LocalOrdinal numBlockRows = blkGraph_->getNodeNumRows();
values2D_->resize(numBlockRows);
col_ind_2D_global_->resize(numBlockRows);
}
//this acts as a range-check for globalBlockRow:
LocalOrdinal localBlockRow = blkRowMap_->getLocalBlockID(globalBlockRow);
TEST_FOR_EXCEPTION( localBlockRow == Teuchos::OrdinalTraits<LocalOrdinal>::invalid(),
std::runtime_error,
"Tpetra::VbrMatrix::getGlobalBlockEntryViewNonConst, globalBlockRow not on the local processor.");
MapGlobalArrayRCP& blkrow = (*col_ind_2D_global_)[localBlockRow];
typename MapGlobalArrayRCP::iterator col_iter = blkrow.find(globalBlockCol);
if (col_iter != blkrow.end()) {
blockEntry = col_iter->second;
}
else {
//blockEntry doesn't already exist, so we will create it.
//make sure block-size is specified:
TEST_FOR_EXCEPTION(numPtRows==0 || numPtCols==0, std::runtime_error,
"Tpetra::VbrMatrix::getGlobalBlockEntryViewNonConst ERROR: creating block-entry, but numPtRows and/or numPtCols is 0.");
Teuchos::RCP<Node> node = getNode();
size_t blockSize = numPtRows*numPtCols;
blockEntry = Teuchos::arcp(new Scalar[blockSize], 0, blockSize);
std::fill(blockEntry.begin(), blockEntry.end(), 0);
(*values2D_)[localBlockRow].push_back(blockEntry);
blkrow.insert(std::make_pair(globalBlockCol, blockEntry));
blkGraph_->insertGlobalIndices(globalBlockRow, Teuchos::ArrayView<GlobalOrdinal>(&globalBlockCol, 1));
}
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
void
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::getGlobalBlockEntryView(
GlobalOrdinal globalBlockRow,
GlobalOrdinal globalBlockCol,
LocalOrdinal& numPtRows,
LocalOrdinal& numPtCols,
Teuchos::ArrayRCP<const Scalar>& blockEntry) const
{
//This method returns a const-view of a block-entry (as an ArrayRCP).
//Throws an exception if the block-entry doesn't already exist.
if (is_storage_optimized_) {
TEST_FOR_EXCEPTION(!isFillComplete(), std::runtime_error,
"Tpetra::VbrMatrix::getGlobalBlockEntryView internal ERROR, storage is optimized but isFillComplete() is false.");
LocalOrdinal localBlockRow = blkRowMap_->getLocalBlockID(globalBlockRow);
LocalOrdinal localBlockCol = blkColMap_->getLocalBlockID(globalBlockCol);
getLocalBlockEntryView(localBlockRow, localBlockCol, numPtRows, numPtCols, blockEntry);
return;
}
TEST_FOR_EXCEPTION(values2D_->size() == 0, std::runtime_error,
"Tpetra::VbrMatrix::getGlobalBlockEntryView ERROR, matrix storage not yet allocated, can't return a const view.");
//this acts as a range-check for globalBlockRow:
LocalOrdinal localBlockRow = blkRowMap_->getLocalBlockID(globalBlockRow);
TEST_FOR_EXCEPTION( localBlockRow == Teuchos::OrdinalTraits<LocalOrdinal>::invalid(),
std::runtime_error,
"Tpetra::VbrMatrix::getGlobalBlockEntryView, globalBlockRow not on the local processor.");
MapGlobalArrayRCP& blkrow = (*col_ind_2D_global_)[localBlockRow];
typename MapGlobalArrayRCP::iterator col_iter = blkrow.find(globalBlockCol);
if (col_iter == blkrow.end()) {
throw std::runtime_error("Tpetra::VbrMatrix::getGlobalBlockEntryView ERROR, specified block-entry doesn't exist.");
}
numPtRows = blkRowMap_->getLocalBlockSize(localBlockRow);
TEST_FOR_EXCEPTION(numPtRows == 0, std::runtime_error,
"Tpetra::VbrMatrix::getGlobalBlockEntryView ERROR, numPtRows == 0.");
blockEntry = col_iter->second;
numPtCols = blockEntry.size() / numPtRows;
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
void
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::getLocalBlockEntryViewNonConst(
LocalOrdinal localBlockRow,
LocalOrdinal localBlockCol,
LocalOrdinal& numPtRows,
LocalOrdinal& numPtCols,
Teuchos::ArrayRCP<Scalar>& blockEntry)
{
typedef Teuchos::ArrayRCP<const LocalOrdinal> Host_View;
typedef typename Host_View::iterator ITER;
//This method returns a non-constant view of a block-entry (as an ArrayRCP).
TEST_FOR_EXCEPTION(isFillComplete() == false, std::runtime_error,
"Tpetra::VbrMatrix::getLocalBlockEntryViewNonConst ERROR, this method can only be called after fillComplete() has been called.");
TEST_FOR_EXCEPTION(is_storage_optimized_ == false, std::runtime_error,
"Tpetra::VbrMatrix::getLocalBlockEntryViewNonConst ERROR, this method can only be called if storage is optimized.");
Teuchos::RCP<Node> node = getNode();
Host_View bptr = node->template viewBuffer<LocalOrdinal>(2,pbuf_bptr_+localBlockRow);
LocalOrdinal bindx_offset = bptr[0];
LocalOrdinal length = bptr[1] - bindx_offset;
TEST_FOR_EXCEPTION( length < 1, std::runtime_error,
"Tpetra::VbrMatrix::getLocalBlockEntryViewNonConst ERROR, specified localBlockCol not found in localBlockRow.");
Host_View bindx = node->template viewBuffer<LocalOrdinal>(length, pbuf_bindx_+bindx_offset);
ITER bindx_beg = bindx.begin(), bindx_end = bindx.end();
ITER it = std::lower_bound(bindx_beg, bindx_end, localBlockCol);
TEST_FOR_EXCEPTION(it == bindx_end || *it != localBlockCol, std::runtime_error,
"Tpetra::VbrMatrix::getLocalBlockEntryViewNonConst ERROR, specified localBlockCol not found.");
numPtRows = blkRowMap_->getLocalBlockSize(localBlockRow);
numPtCols = blkColMap_->getLocalBlockSize(localBlockCol);
const LocalOrdinal blkSize = numPtRows*numPtCols;
Host_View indx = node->template viewBuffer<LocalOrdinal>(1,pbuf_indx_+bptr[0]+(it-bindx_beg));
const LocalOrdinal offset = indx[0];
blockEntry = node->template viewBufferNonConst<Scalar>(Kokkos::ReadWrite, blkSize, pbuf_values1D_ + offset);
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
void
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::getLocalBlockEntryView(
LocalOrdinal localBlockRow,
LocalOrdinal localBlockCol,
LocalOrdinal& numPtRows,
LocalOrdinal& numPtCols,
Teuchos::ArrayRCP<const Scalar>& blockEntry) const
{
typedef Teuchos::ArrayRCP<const LocalOrdinal> Host_View;
typedef typename Host_View::iterator ITER;
//This method returns a constant view of a block-entry (as an ArrayRCP).
TEST_FOR_EXCEPTION(isFillComplete() == false, std::runtime_error,
"Tpetra::VbrMatrix::getLocalBlockEntryView ERROR, this method can only be called after fillComplete() has been called.");
TEST_FOR_EXCEPTION(is_storage_optimized_ == false, std::runtime_error,
"Tpetra::VbrMatrix::getLocalBlockEntryView ERROR, this method can only be called if storage is optimized.");
Teuchos::RCP<Node> node = getNode();
Host_View bptr = node->template viewBuffer<LocalOrdinal>(2, pbuf_bptr_+localBlockRow);
LocalOrdinal bindx_offset = bptr[0];
LocalOrdinal length = bptr[1] - bindx_offset;
Host_View bindx = node->template viewBuffer<LocalOrdinal>(length, pbuf_bindx_+bindx_offset);
ITER bindx_beg = bindx.begin(), bindx_end = bindx.end();
ITER it = std::lower_bound(bindx_beg, bindx_end, localBlockCol);
TEST_FOR_EXCEPTION(it == bindx_end || *it != localBlockCol, std::runtime_error,
"Tpetra::VbrMatrix::getLocalBlockEntryView ERROR, specified localBlockCol not found.");
numPtRows = blkRowMap_->getLocalBlockSize(localBlockRow);
numPtCols = blkRowMap_->getLocalBlockSize(localBlockCol);
const LocalOrdinal blkSize = numPtRows*numPtCols;
Host_View indx = node->template viewBuffer<LocalOrdinal>(1, pbuf_indx_+bptr[0]+(it-bindx_beg));
const LocalOrdinal offset = indx[0];
blockEntry = node->template viewBuffer<Scalar>(blkSize, pbuf_values1D_ + offset);
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
Teuchos::RCP<Node>
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::getNode() const
{
return blkRowMap_->getPointMap()->getNode();
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
void
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::putScalar(Scalar s)
{
Teuchos::RCP<Node> node = getNode();
if (isFillComplete()) {
fill_device_ArrayRCP(node, pbuf_values1D_, s);
}
else {
typedef typename Teuchos::Array<Teuchos::Array<Teuchos::ArrayRCP<Scalar> > >::size_type Tsize_t;
Teuchos::Array<Teuchos::Array<Teuchos::ArrayRCP<Scalar> > >& pbuf_vals2D = *values2D_;
LocalOrdinal numBlockRows = blkGraph_->getNodeNumRows();
for(LocalOrdinal r=0; r<numBlockRows; ++r) {
for(Tsize_t c=0; c<pbuf_vals2D[r].size(); ++c) {
std::fill(pbuf_vals2D[r][c].begin(), pbuf_vals2D[r][c].end(), s);
}
}
}
}
//-------------------------------------------------------------------
template<class ArrayType1,class ArrayType2,class Ordinal>
void set_array_values(ArrayType1& dest, ArrayType2& src, Ordinal rows, Ordinal cols, Ordinal stride, Tpetra::CombineMode mode)
{
size_t offset = 0;
size_t src_offset = 0;
if (mode == ADD) {
for(Ordinal col=0; col<cols; ++col) {
for(Ordinal row=0; row<rows; ++row) {
dest[offset++] += src[src_offset + row];
}
src_offset += stride;
}
}
else {
for(Ordinal col=0; col<cols; ++col) {
for(Ordinal row=0; row<rows; ++row) {
dest[offset++] = src[src_offset + row];
}
src_offset += stride;
}
}
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
void
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::setGlobalBlockEntry(GlobalOrdinal globalBlockRow, GlobalOrdinal globalBlockCol, const Teuchos::SerialDenseMatrix<GlobalOrdinal,Scalar>& blockEntry)
{
//first get an ArrayRCP for the internal storage for this block-entry:
Teuchos::ArrayRCP<Scalar> internalBlockEntry;
LocalOrdinal blkRowSize = blockEntry.numRows();
LocalOrdinal blkColSize = blockEntry.numCols();
getGlobalBlockEntryViewNonConst(globalBlockRow,globalBlockCol, blkRowSize, blkColSize, internalBlockEntry);
//now copy the incoming block-entry into internal storage:
const Scalar* inputvalues = blockEntry.values();
set_array_values(internalBlockEntry, inputvalues, blkRowSize, blkColSize, blockEntry.stride(), REPLACE);
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
void
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::sumIntoGlobalBlockEntry(GlobalOrdinal globalBlockRow, GlobalOrdinal globalBlockCol, const Teuchos::SerialDenseMatrix<GlobalOrdinal,Scalar>& blockEntry)
{
//first get an ArrayRCP for the internal storage for this block-entry:
Teuchos::ArrayRCP<Scalar> internalBlockEntry;
LocalOrdinal blkRowSize = blockEntry.numRows();
LocalOrdinal blkColSize = blockEntry.numCols();
getGlobalBlockEntryViewNonConst(globalBlockRow,globalBlockCol, blkRowSize, blkColSize, internalBlockEntry);
//now sum (add) the incoming block-entry into internal storage:
const Scalar* inputvalues = blockEntry.values();
set_array_values(internalBlockEntry, inputvalues, blkRowSize, blkColSize, blockEntry.stride(), ADD);
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
void
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::setGlobalBlockEntry(GlobalOrdinal globalBlockRow, GlobalOrdinal globalBlockCol, LocalOrdinal blkRowSize, LocalOrdinal blkColSize, LocalOrdinal LDA, const Teuchos::ArrayView<const Scalar>& blockEntry)
{
//first get an ArrayRCP for the internal storage for this block-entry:
Teuchos::ArrayRCP<Scalar> internalBlockEntry;
getGlobalBlockEntryViewNonConst(globalBlockRow,globalBlockCol, blkRowSize, blkColSize, internalBlockEntry);
LocalOrdinal blk_size = blockEntry.size();
TEST_FOR_EXCEPTION(blkColSize*LDA > blk_size, std::runtime_error,
"Tpetra::VbrMatrix::setGlobalBlockEntry ERROR, inconsistent block-entry sizes.");
//copy the incoming block-entry into internal storage:
set_array_values(internalBlockEntry, blockEntry, blkRowSize, blkColSize, LDA, REPLACE);
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
void
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::sumIntoGlobalBlockEntry(GlobalOrdinal globalBlockRow, GlobalOrdinal globalBlockCol, LocalOrdinal blkRowSize, LocalOrdinal blkColSize, LocalOrdinal LDA, const Teuchos::ArrayView<const Scalar>& blockEntry)
{
//first get an ArrayRCP for the internal storage for this block-entry:
Teuchos::ArrayRCP<Scalar> internalBlockEntry;
getGlobalBlockEntryViewNonConst(globalBlockRow,globalBlockCol, blkRowSize, blkColSize, internalBlockEntry);
LocalOrdinal blk_size = blockEntry.size();
TEST_FOR_EXCEPTION(blkColSize*LDA > blk_size, std::runtime_error,
"Tpetra::VbrMatrix::setGlobalBlockEntry ERROR, inconsistent block-entry sizes.");
//copy the incoming block-entry into internal storage:
set_array_values(internalBlockEntry, blockEntry, blkRowSize, blkColSize, LDA, ADD);
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
void
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::optimizeStorage()
{
if (is_storage_optimized_ == true) return;
size_t num_block_rows = blkGraph_->getNodeNumRows();
size_t num_block_nonzeros = blkGraph_->getNodeNumEntries();
//need to count the number of point-entries:
size_t num_point_entries = 0;
typedef typename Teuchos::Array<Teuchos::Array<Teuchos::ArrayRCP<Scalar> > >::size_type Tsize_t;
for(Tsize_t r=0; r<values2D_->size(); ++r) {
Tsize_t rlen = (*values2D_)[r].size();
for(Tsize_t c=0; c<rlen; ++c) {
num_point_entries += (*values2D_)[r][c].size();
}
}
const Teuchos::RCP<Node>& node = blkRowMap_->getPointMap()->getNode();
//Don't change these allocation sizes unless you know what you're doing!
//The '+1's are in the right place. (Trust me, I'm a trained professional.)
pbuf_bptr_ = node->template allocBuffer<LocalOrdinal>(num_block_rows+1);
pbuf_bindx_= node->template allocBuffer<LocalOrdinal>(num_block_nonzeros);
pbuf_indx_ = node->template allocBuffer<LocalOrdinal>(num_block_nonzeros+1);
pbuf_values1D_ = node->template allocBuffer<Scalar>(num_point_entries);
Teuchos::ArrayRCP<LocalOrdinal> v_bptr = node->template viewBufferNonConst<LocalOrdinal>(Kokkos::WriteOnly, num_block_rows+1, pbuf_bptr_);
Teuchos::ArrayRCP<LocalOrdinal> v_bindx = node->template viewBufferNonConst<LocalOrdinal>(Kokkos::WriteOnly, num_block_nonzeros, pbuf_bindx_);
Teuchos::ArrayRCP<LocalOrdinal> v_indx = node->template viewBufferNonConst<LocalOrdinal>(Kokkos::WriteOnly, num_block_nonzeros+1, pbuf_indx_);
Teuchos::ArrayRCP<Scalar> v_values1D = node->template viewBufferNonConst<Scalar>(Kokkos::WriteOnly, num_point_entries, pbuf_values1D_);
size_t roffset = 0;
size_t ioffset = 0;
size_t offset = 0;
Teuchos::Array<Teuchos::Array<Teuchos::ArrayRCP<Scalar> > >& pbuf_vals2D = *values2D_;
for(Tsize_t r=0; r<pbuf_vals2D.size(); ++r) {
LocalOrdinal rsize = blkRowMap_->getLocalBlockSize(r);
v_bptr[r] = roffset;
Tsize_t rlen = pbuf_vals2D[r].size();
roffset += rlen;
Teuchos::ArrayRCP<const LocalOrdinal> blk_row_inds = blkGraph_->getLocalRowView(r);
MapGlobalArrayRCP& blk_row = (*col_ind_2D_global_)[r];
for(Tsize_t c=0; c<rlen; ++c) {
v_bindx[ioffset] = blk_row_inds[c];
v_indx[ioffset++] = offset;
LocalOrdinal csize = blkColMap_->getLocalBlockSize(blk_row_inds[c]);
Tsize_t blkSize = rsize*csize;
GlobalOrdinal global_col = blkGraph_->getColMap()->getGlobalElement(blk_row_inds[c]);
typename MapGlobalArrayRCP::iterator iter = blk_row.find(global_col);
TEST_FOR_EXCEPTION(iter == blk_row.end(), std::runtime_error,
"Tpetra::VbrMatrix::optimizeStorage ERROR, global_col not found in row.");
Teuchos::ArrayRCP<Scalar> vals = iter->second;
for(Tsize_t n=0; n<blkSize; ++n) {
v_values1D[offset+n] = vals[n];
}
offset += blkSize;
}
}
v_bptr[num_block_rows] = roffset;
v_indx[ioffset] = offset;
//Final step: release memory for the "2D" storage:
col_ind_2D_global_ = Teuchos::null;
values2D_ = Teuchos::null;
is_storage_optimized_ = true;
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
void VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::fillLocalMatrix()
{
//We insist that optimzeStorage has already been called.
//We don't care whether this function (fillLocalMatrix()) is being
//called for the first time or not.
TEST_FOR_EXCEPTION(is_storage_optimized_ != true, std::runtime_error,
"Tpetra::VbrMatrix::fillLocalMatrix ERROR, optimizeStorage is required to have already been called.");
lclMatrix_.setPackedValues(pbuf_values1D_,
blkRowMap_->getNodeFirstPointInBlocks_Device(),
blkColMap_->getNodeFirstPointInBlocks_Device(),
pbuf_bptr_,
pbuf_bindx_,
pbuf_indx_);
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
void VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::fillLocalMatVec()
{
//We insist that optimzeStorage has already been called.
//We don't care whether this function (fillLocalMatVec()) is being
//called for the first time or not.
TEST_FOR_EXCEPTION(is_storage_optimized_ != true, std::runtime_error,
"Tpetra::VbrMatrix::fillLocalMatrix ERROR, optimizeStorage is required to have already been called.");
lclMatVec_.initializeValues(lclMatrix_);
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
void
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::fillComplete(const Teuchos::RCP<const BlockMap<LocalOrdinal,GlobalOrdinal,Node> >& blockDomainMap, const Teuchos::RCP<const BlockMap<LocalOrdinal,GlobalOrdinal,Node> >& blockRangeMap)
{
if (isFillComplete()) return;
blkDomainMap_ = blockDomainMap;
blkRangeMap_ = blockRangeMap;
blkGraph_->fillComplete(convertBlockMapToPointMap(blockDomainMap),
convertBlockMapToPointMap(blockRangeMap));
//Now we need to take the point-column-map from blkGraph_ and create a
//corresponding block-column-map.
//Our block-column-map will have the same distribution as
//blkGraph_->getColMap, and we'll get block-size info from the
//blkDomainMap_. This will require some communication in cases
//where blkDomainMap is distributed differently.
blkColMap_ = makeBlockColumnMap(blkDomainMap_, blkGraph_->getColMap());
if (!blkDomainMap_->getPointMap()->isSameAs(*blkColMap_->getPointMap())) {
importer_ = Teuchos::rcp(new Tpetra::Import<LocalOrdinal,GlobalOrdinal,Node>(blkDomainMap_->getPointMap(), blkColMap_->getPointMap()));
}
if (!blkRangeMap_->getPointMap()->isSameAs(*blkRowMap_->getPointMap())) {
exporter_ = Teuchos::rcp(new Tpetra::Export<LocalOrdinal,GlobalOrdinal,Node>(blkRowMap_->getPointMap(), blkRangeMap_->getPointMap()));
}
is_fill_completed_ = true;
optimizeStorage();
fillLocalMatrix();
fillLocalMatVec();
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
void VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::fillComplete()
{
//In this case, our block-row-map will also be our domain-map and
//range-map.
fillComplete(getBlockRowMap(), getBlockRowMap());
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
std::string
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::description() const
{
std::ostringstream oss;
oss << Teuchos::Describable::description();
if (isFillComplete()) {
oss << "{status = fill complete, global num block rows = " << blkRowMap_->getGlobalNumBlocks() << "}";
}
else {
oss << "{status = fill not complete, global num block rows = " << blkRowMap_->getGlobalNumBlocks() << "}";
}
return oss.str();
}
//-------------------------------------------------------------------
template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class LocalMatOps>
void
VbrMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalMatOps>::describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
{
Teuchos::EVerbosityLevel vl = verbLevel;
if (vl == Teuchos::VERB_DEFAULT) vl = Teuchos::VERB_LOW;
if (vl == Teuchos::VERB_NONE) return;
Teuchos::RCP<const Teuchos::Comm<int> > comm = blkRowMap_->getPointMap()->getComm();
const int myProc = comm->getRank();
if (myProc == 0) out << "VbrMatrix::describe is under construction..." << std::endl;
}
}//namespace Tpetra
//
// Explicit instantiation macro
//
// Must be expanded from within the Tpetra namespace!
//
#define TPETRA_VBRMATRIX_INSTANT(SCALAR,LO,GO,NODE) \
\
template class VbrMatrix< SCALAR , LO , GO , NODE >;
#endif //TPETRA_VBRMATRIX_DEF_HPP
|