/usr/include/trilinos/Tpetra_Details_ReadTriples.hpp is in libtrilinos-tpetra-dev 12.12.1-5.
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 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 | // @HEADER
// ***********************************************************************
//
// Tpetra: Templated Linear Algebra Services Package
// Copyright (2008) Sandia Corporation
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
//
// ************************************************************************
// @HEADER
#ifndef TPETRA_DETAILS_READTRIPLES_HPP
#define TPETRA_DETAILS_READTRIPLES_HPP
/// \file Tpetra_Details_ReadTriples.hpp
/// \brief Declaration and definition of
/// Tpetra::Details::readAndDealOutTriples, which reads a Matrix
/// Market file or input stream on one process, and distributes the
/// resulting sparse matrix entries to the other processes.
///
/// \warning This is an implementation detail of Tpetra.
/// Users must not rely on this file or its contents.
#include "TpetraCore_config.h"
#include "Tpetra_Details_PackTriples.hpp"
#include "Kokkos_ArithTraits.hpp"
#include "Teuchos_MatrixMarket_generic.hpp"
#include "Teuchos_CommHelpers.hpp"
#include <iostream>
#include <typeinfo> // for debugging
namespace Tpetra {
namespace Details {
//
// Search for "SKIP DOWN TO HERE" (omit quotes) for the "public"
// interface. I put "public" in quotes because it's public only for
// Tpetra developers, NOT for Tpetra users.
//
namespace Impl {
// mfh 01 Feb 2017: Unfortunately,
// Teuchos::MatrixMarket::readComplexData requires Teuchos to have
// complex arithmetic support enabled. To avoid this issue, I
// reimplement the function here. It's not very long.
/// \brief Read "<rowIndex> <colIndex> <realPart> <imagPart>" from a line.
///
/// Matrix Market files that store a sparse matrix with complex values
/// do so with one sparse matrix entry per line. It is stored as
/// space-delimited ASCII text: the row index, the column index, the
/// real part, and the imaginary part, in that order. Both the row
/// and column indices are 1-based. This function attempts to read
/// one line from the given input stream istr, extract the row and
/// column indices and the real and imaginary parts, and write them to
/// the corresponding output variables.
///
/// \param istr [in/out] Input stream from which to attempt to read
/// one line.
/// \param rowIndex [out] On output: if successful, the row index read
/// from the line.
/// \param colIndex [out] On output: if successful, the column index
/// read from the line.
/// \param realPart [out] On output: if successful, the real part of
/// the matrix entry's value read from the line.
/// \param imagPart [out] On output: if successful, the imaginary part
/// of the matrix entry's value read from the line.
/// \param lineNumber [in] The current line number. Used only for
/// diagnostic error messages.
/// \param tolerant [in] Whether to parse tolerantly. In tolerant
/// mode, if this function fails in any way to read any of the data,
/// it will return false without throwing an exception. Otherwise,
/// this function will either throw an exception or return true.
///
/// \return True if this function successfully read the line from istr
/// and extracted all the output data, false otherwise. If
/// tolerant==false, this function never returns false; it either
/// returns true or throws an exception.
template<class OrdinalType, class RealType>
bool
readComplexData (std::istream& istr,
OrdinalType& rowIndex,
OrdinalType& colIndex,
RealType& realPart,
RealType& imagPart,
const std::size_t lineNumber,
const bool tolerant)
{
using ::Teuchos::MatrixMarket::readRealData;
RealType the_realPart, the_imagPart;
if (! readRealData (istr, rowIndex, colIndex, the_realPart, lineNumber, tolerant)) {
if (tolerant) {
return false;
}
else {
std::ostringstream os;
os << "Failed to read pattern data and/or real value from line "
<< lineNumber << " of input";
throw std::invalid_argument(os.str());
}
}
if (istr.eof ()) {
if (tolerant) {
return false;
}
else {
std::ostringstream os;
os << "No more data after real value on line "
<< lineNumber << " of input";
throw std::invalid_argument (os.str ());
}
}
istr >> the_imagPart;
if (istr.fail ()) {
if (tolerant) {
return false;
}
else {
std::ostringstream os;
os << "Failed to get imaginary value from line "
<< lineNumber << " of input";
throw std::invalid_argument (os.str ());
}
}
realPart = the_realPart;
imagPart = the_imagPart;
return true;
}
/// \brief Implementation of the readLine stand-alone function in this
/// namespace (see below).
///
/// Implementations are specialized on whether or not SC is a
/// complex-valued type.
///
/// \tparam SC The type of the value of each matrix entry.
/// \tparam GO The type of each (global) index of each matrix entry.
/// \tparam isComplex Whether SC is a complex-valued type.
template<class SC,
class GO,
const bool isComplex = ::Kokkos::Details::ArithTraits<SC>::is_complex>
struct ReadLine {
/// \brief Take a line from the Matrix Market file or input stream,
/// and process the sparse matrix entry in that line.
///
/// \param processTriple [in] Closure, generally with side effects,
/// that takes in and stores off a sparse matrix entry. First
/// argument is the (global) row index, second argument is the
/// (global) column index, and third argument is the value of the
/// entry. The closure must NOT do MPI communication. Return
/// value is an error code, that is zero if and only if the
/// closure succeeded.
/// \param line [in] Current line of the Matrix Market file or input
/// stream to read.
/// \param lineNumber [in] Current line number in the file or input
/// stream.
/// \param tolerant [in] Whether to read tolerantly.
/// \param errStrm [in] If not NULL, print any error messages to
/// this stream.
/// \param debug [in] If true, print debug messages to \c *errStrm.
///
/// \return Error code; 0 if and only if success.
static int
readLine (std::function<int (const GO, const GO, const SC&)> processTriple,
const std::string& line,
const std::size_t lineNumber,
const bool tolerant = false,
std::ostream* errStrm = NULL,
const bool debug = false);
};
/// \brief Complex-arithmetic partial specialization of ReadLine.
///
/// This helps implement the readLine stand-alone function in this
/// namespace (see below).
///
/// \tparam SC The type of the value of each matrix entry.
/// \tparam GO The type of each (global) index of each matrix entry.
template<class SC, class GO>
struct ReadLine<SC, GO, true> {
/// \brief Take a line from the Matrix Market file or input stream,
/// and process the sparse matrix entry in that line.
///
/// \param processTriple [in] Closure, generally with side effects,
/// that takes in and stores off a sparse matrix entry. First
/// argument is the (global) row index, second argument is the
/// (global) column index, and third argument is the value of the
/// entry. The closure must NOT do MPI communication. Return
/// value is an error code, that is zero if and only if the
/// closure succeeded.
/// \param line [in] Current line of the Matrix Market file or input
/// stream to read.
/// \param lineNumber [in] Current line number in the file or input
/// stream.
/// \param tolerant [in] Whether to read tolerantly.
/// \param errStrm [in] If not NULL, print any error messages to
/// this stream.
/// \param debug [in] If true, print debug messages to \c *errStrm.
///
/// \return Error code; 0 if and only if success.
static int
readLine (std::function<int (const GO, const GO, const SC&)> processTriple,
const std::string& line,
const std::size_t lineNumber,
const bool tolerant = false,
std::ostream* errStrm = NULL,
const bool debug = false)
{
using ::Teuchos::MatrixMarket::checkCommentLine;
typedef typename ::Kokkos::Details::ArithTraits<SC>::mag_type real_type;
using std::endl;
GO rowInd, colInd;
real_type realPart, imagPart;
std::istringstream istr (line);
bool success = true;
try {
// Use the version of this function in this file, not the
// version in Teuchos_MatrixMarket_generic.hpp, because the
// latter only exists if HAVE_TEUCHOS_COMPLEX is defined.
success = readComplexData (istr, rowInd, colInd, realPart, imagPart,
lineNumber, tolerant);
}
catch (std::exception& e) {
success = false;
if (errStrm != NULL) {
std::ostringstream os;
os << "readLine: readComplexData threw an exception: " << e.what ()
<< endl;
*errStrm << os.str ();
}
}
if (success) {
// if (debug && errStrm != NULL) {
// std::ostringstream os;
// os << "readLine: Got entry: row=" << rowInd << ", col=" << colInd
// << ", realPart=" << realPart << ", imagPart=" << imagPart
// << std::endl;
// *errStrm << os.str ();
// }
// This line may have side effects.
const int errCode =
processTriple (rowInd, colInd, SC (realPart, imagPart));
if (errCode != 0 && errStrm != NULL) {
std::ostringstream os;
os << "readLine: processTriple returned " << errCode << " != 0."
<< endl;
*errStrm << os.str ();
}
return errCode;
}
else {
return -1;
}
}
};
/// \brief Real-arithmetic partial specialization of ReadLine.
///
/// This helps implement the readLine stand-alone function in this
/// namespace (see below).
///
/// \tparam SC The type of the value of each matrix entry.
/// \tparam GO The type of each (global) index of each matrix entry.
template<class SC, class GO>
struct ReadLine<SC, GO, false> {
/// \brief Take a line from the Matrix Market file or input stream,
/// and process the sparse matrix entry in that line.
///
/// \param processTriple [in] Closure, generally with side effects,
/// that takes in and stores off a sparse matrix entry. First
/// argument is the (global) row index, second argument is the
/// (global) column index, and third argument is the value of the
/// entry. The closure must NOT do MPI communication. Return
/// value is an error code, that is zero if and only if the
/// closure succeeded.
/// \param line [in] Current line of the Matrix Market file or input
/// stream to read.
/// \param lineNumber [in] Current line number in the file or input
/// stream.
/// \param tolerant [in] Whether to read tolerantly.
/// \param errStrm [in] If not NULL, print any error messages to
/// this stream.
/// \param debug [in] If true, print debug messages to \c *errStrm.
///
/// \return Error code; 0 if and only if success.
static int
readLine (std::function<int (const GO, const GO, const SC&)> processTriple,
const std::string& line,
const std::size_t lineNumber,
const bool tolerant = false,
std::ostream* errStrm = NULL,
const bool debug = false)
{
using ::Teuchos::MatrixMarket::checkCommentLine;
using ::Teuchos::MatrixMarket::readRealData;
using std::endl;
GO rowInd, colInd;
SC val;
std::istringstream istr (line);
bool success = true;
try {
success = readRealData (istr, rowInd, colInd, val,
lineNumber, tolerant);
}
catch (std::exception& e) {
success = false;
if (errStrm != NULL) {
std::ostringstream os;
os << "readLine: readRealData threw an exception: " << e.what ()
<< endl;
*errStrm << os.str ();
}
}
if (success) {
if (debug && errStrm != NULL) {
std::ostringstream os;
os << "readLine: Got entry: row=" << rowInd << ", col=" << colInd
<< ", val=" << val << std::endl;
*errStrm << os.str ();
}
// This line may have side effects.
const int errCode = processTriple (rowInd, colInd, val);
if (errCode != 0 && errStrm != NULL) {
std::ostringstream os;
os << "readLine: processTriple returned " << errCode << " != 0."
<< endl;
*errStrm << os.str ();
}
return errCode;
}
else {
return -1;
}
}
};
/// \brief Take a line from the Matrix Market file or input stream,
/// and process the sparse matrix entry in that line.
///
/// The line must be a valid Matrix Market line, not a comment.
///
/// \tparam SC The type of the value of each matrix entry.
/// \tparam GO The type of each (global) index of each matrix entry.
///
/// \param processTriple [in] Closure, generally with side effects,
/// that takes in and stores off a sparse matrix entry. First
/// argument is the (global) row index, second argument is the
/// (global) column index, and third argument is the value of the
/// entry. The closure must NOT do MPI communication. Return value
/// is an error code, that is zero if and only if the closure
/// succeeded.
/// \param line [in] The line from the Matrix Market file or input
/// stream to read.
/// \param lineNumber [in] Current line number in the file or input
/// stream.
/// \param tolerant [in] Whether to read tolerantly.
/// \param errStrm [in] If not NULL, print any error messages to this
/// stream.
/// \param debug [in] If true, print debug messages to \c *errStrm.
///
/// \return Error code; 0 if and only if success.
template<class SC, class GO>
int
readLine (std::function<int (const GO, const GO, const SC&)> processTriple,
const std::string& line,
const std::size_t lineNumber,
const bool tolerant = false,
std::ostream* errStrm = NULL,
const bool debug = false)
{
return ReadLine<SC, GO>::readLine (processTriple, line, lineNumber,
tolerant, errStrm, debug);
}
/// \brief Read at most numTriplesToRead triples from the given Matrix
/// Market input stream, and pass along any resulting matrix entries
/// to the given closure.
///
/// The line must be a valid Matrix Market line, not a comment.
///
/// \tparam SC The type of the value of each matrix entry.
/// \tparam GO The type of each (global) index of each matrix entry.
///
/// \param inputStream [in/out] Input stream from which to read.
/// \param curLineNum [in/out] Current line number in the input stream.
/// \param numTriplesRead [out] On output: Number of matrix triples
/// (row index, column index, value) successfully read from the
/// input stream on this call to the function.x
/// \param processTriple [in] Closure, generally with side effects,
/// that takes in and stores off a sparse matrix entry. First
/// argument is the (global) row index, second argument is the
/// (global) column index, and third argument is the value of the
/// entry. The closure must NOT do MPI communication. Return value
/// is an error code, that is zero if and only if the closure
/// succeeded.
/// \param maxNumTriplesToRead [in] Maximum number of triples to read
/// from the input stream on this call of the function. This is a
/// strict upper bound for numTriplesRead (see above).
/// \param tolerant [in] Whether to read tolerantly.
/// \param errStrm [in] If not NULL, print any error messages to this
/// stream.
/// \param debug [in] If true, print debug messages to \c *errStrm.
///
/// \return Error code; 0 if and only if success.
template<class SC, class GO>
int
readTriples (std::istream& inputStream,
std::size_t& curLineNum,
std::size_t& numTriplesRead,
std::function<int (const GO, const GO, const SC&)> processTriple,
const std::size_t maxNumTriplesToRead,
const bool tolerant = false,
std::ostream* errStrm = NULL,
const bool debug = false)
{
using Teuchos::MatrixMarket::checkCommentLine;
using std::endl;
using std::size_t;
numTriplesRead = 0; // output argument only
if (inputStream.eof ()) {
return 0; // no error, just nothing left to read
}
else if (inputStream.fail ()) {
if (errStrm != NULL) {
*errStrm << "Input stream reports a failure (not the same as "
"end-of-file)." << endl;
}
return -1;
}
std::string line;
std::vector<size_t> badLineNumbers;
int errCode = 0; // 0 means success
bool inputStreamCanStillBeRead = std::getline (inputStream, line).good ();
++curLineNum; // we read the line; we can't put it back
while (inputStreamCanStillBeRead && numTriplesRead < maxNumTriplesToRead) {
// if (debug && errStrm != NULL) {
// std::ostringstream os;
// os << "readTriples: Got line: \"" << line << "\"" << std::endl;
// *errStrm << os.str ();
// }
size_t start, size;
const bool isCommentLine =
checkCommentLine (line, start, size, curLineNum, tolerant);
if (isCommentLine) {
// Move on to the next line, if there is a next line.
inputStreamCanStillBeRead = std::getline (inputStream, line).good ();
++curLineNum; // we read another line; we can't put it back
continue; // move on to the next line
}
else { // not a comment line; should have a sparse matrix entry
const std::string theLine = line.substr (start, size);
// If the line has a valid sparse matrix entry, extract it and
// hand it off to the processTriple closure.
const int curErrCode =
readLine (processTriple, theLine, curLineNum, tolerant, errStrm, debug);
if (curErrCode != 0) {
errCode = curErrCode;
badLineNumbers.push_back (curLineNum);
}
else {
++numTriplesRead;
}
if (numTriplesRead < maxNumTriplesToRead) {
inputStreamCanStillBeRead = std::getline (inputStream, line).good ();
}
}
} // while there are lines to read and we need more triples
if (errCode != 0 && errStrm != NULL) {
const size_t numBadLines = badLineNumbers.size ();
*errStrm << "Encountered " << numBadLines << " bad line"
<< (numBadLines != size_t (1) ? "s" : "")
<< ": [";
for (size_t k = 0; k < numBadLines; ++k) {
*errStrm << badLineNumbers[k];
if (k + 1 < numBadLines) {
*errStrm << ", ";
}
}
*errStrm << "]" << endl;
}
if (! inputStream.eof () && inputStream.fail ()) {
if (errCode == 0) {
errCode = -1;
}
if (errStrm != NULL) {
*errStrm << "The input stream is not at end-of-file, "
"but is in a bad state." << endl;
}
}
return errCode;
}
/// \brief Read at most maxNumEntPerMsg sparse matrix entries from the
/// input stream, and send them to the process with rank destRank.
///
/// To be called only by the sending process.
///
/// \tparam SC The type of the value of each matrix entry.
/// \tparam GO The type of each (global) index of each matrix entry.
///
/// \param inputStream [in/out] Input stream from which to read.
/// \param curLineNum [in/out] Current line number in the input stream.
/// \param numEntRead [out] Number of matrix entries successfully read
/// from the input stream on this call of the function.
/// \param sizeBuf [in/out] Array of length 1, for sending message size.
/// \param msgBuf [in/out] Message buffer; to be resized as needed.
/// \param rowInds [out] Row indices read from the file.
/// \param colInds [out] Column indices read from the file.
/// \param vals [out] Matrix values read from the file.
/// \param maxNumEntPerMsg [in] Maximum number of sparse matrix
/// entries to read from the input stream on this call to the
/// function.
/// \param destRank [in] Rank of the process where to send the triples.
/// \param comm [in] Communicator to use for sending the triples.
/// \param tolerant [in] Whether to read tolerantly.
/// \param errStrm [in] If not NULL, print any error messages to this
/// stream.
template<class SC, class GO>
int
readAndSendOneBatchOfTriples (std::istream& inputStream,
std::size_t& curLineNum,
std::size_t& numEntRead,
::Teuchos::ArrayRCP<int>& sizeBuf,
::Teuchos::ArrayRCP<char>& msgBuf,
std::vector<GO>& rowInds,
std::vector<GO>& colInds,
std::vector<SC>& vals,
const std::size_t maxNumEntPerMsg,
const int destRank,
const ::Teuchos::Comm<int>& comm,
const bool tolerant = false,
std::ostream* errStrm = NULL,
const bool debug = false)
{
using ::Tpetra::Details::countPackTriplesCount;
using ::Tpetra::Details::countPackTriples;
using ::Tpetra::Details::packTriplesCount;
using ::Tpetra::Details::packTriples;
using ::Teuchos::isend;
using std::endl;
using ::Kokkos::ArithTraits;
// constexpr int sizeTag = 42 + (ArithTraits<SC>::is_complex ? 100 : 0);
// constexpr int msgTag = 43 + (ArithTraits<SC>::is_complex ? 100 : 0);
constexpr int sizeTag = 42;
constexpr int msgTag = 43;
//constexpr int srcRank = 0;
int errCode = 0;
// This doesn't actually deallocate memory; it just changes the size
// back to zero, so that push_back starts over from the beginning.
rowInds.resize (0);
colInds.resize (0);
vals.resize (0);
// Closure that adds the new matrix entry to the above temp arrays.
auto processTriple = [&rowInds, &colInds, &vals]
(const GO rowInd, const GO colInd, const SC& val) {
try {
rowInds.push_back (rowInd);
colInds.push_back (colInd);
vals.push_back (val);
}
catch (...) {
return -1;
}
return 0;
};
numEntRead = 0; // output argument
errCode = readTriples<SC, GO> (inputStream, curLineNum, numEntRead,
processTriple, maxNumEntPerMsg, tolerant,
errStrm, debug);
if (debug && errStrm != NULL) {
std::ostringstream os;
os << "Proc " << comm.getRank () << ", SC=" << typeid (SC).name ()
<< ", GO=" << typeid (GO).name () << ": "
<< "readAndSendOneBatchOfTriples: readTriples read "
<< numEntRead << " matrix entries, and returned errCode="
<< errCode << "." << std::endl;
*errStrm << os.str ();
}
if (numEntRead != rowInds.size () ||
numEntRead != colInds.size () ||
numEntRead != vals.size ()) {
if (errStrm != NULL) {
*errStrm << "readTriples size results are not consistent. "
<< "numEntRead = " << numEntRead
<< ", rowInds.size() = " << rowInds.size ()
<< ", colInds.size() = " << colInds.size ()
<< ", and vals.size() = " << vals.size () << "."
<< std::endl;
}
if (errCode == 0) {
errCode = -1;
}
}
// We don't consider reading having "failed" if we've reached
// end-of-file before reading maxNumEntPerMsg entries. It's OK if
// we got fewer triples than that. Furthermore, we have to send at
// least one message to the destination process, even if the read
// from the file failed.
if (numEntRead == 0 || errCode != 0) {
// Send a message size of zero to the receiving process, to tell
// it that we have no triples to send, or that there was an error
// reading. The latter just means that we "go through the
// motions," then broadcast the error code.
sizeBuf[0] = 0;
if (debug && errStrm != NULL) {
std::ostringstream os;
os << "Proc " << comm.getRank () << ", SC=" << typeid (SC).name ()
<< ", GO=" << typeid (GO).name () << ": "
<< "Post send (size=0, errCode=" << errCode << ") "
<< "to " << destRank << " with tag " << sizeTag << endl;
*errStrm << os.str ();
}
send (sizeBuf.getRawPtr (), 1, destRank, sizeTag, comm);
return errCode;
}
else { // we read a nonzero # of triples, without error
const int numEnt = static_cast<int> (numEntRead);
int countSize = 0; // output argument
int triplesSize = 0; // output argument
errCode = countPackTriplesCount (comm, countSize, errStrm);
// countSize should never be nonpositive, since we have to pack an
// integer size.
if (countSize <= 0 && errCode == 0) {
errCode = -1;
}
if (errCode != 0) {
// Send zero to the receiving process, to tell it about the error.
sizeBuf[0] = 0;
if (debug && errStrm != NULL) {
std::ostringstream os;
os << "Proc " << comm.getRank () << ", SC=" << typeid (SC).name ()
<< ", GO=" << typeid (GO).name () << ": "
<< "Post send (size=0, error case) to " << destRank
<< " with tag " << sizeTag << endl;
*errStrm << os.str ();
}
send (sizeBuf.getRawPtr (), 1, destRank, sizeTag, comm);
return errCode;
}
else { // countPackTriplesCount succeeded
errCode = countPackTriples<SC, GO> (numEnt, comm, triplesSize, errStrm);
if (errCode != 0) {
// Send a message size of zero to the receiving process, to
// tell it that there was an error counting.
sizeBuf[0] = 0;
if (debug && errStrm != NULL) {
std::ostringstream os;
os << "Proc " << comm.getRank () << ", SC=" << typeid (SC).name ()
<< ", GO=" << typeid (GO).name () << ": "
<< "Post send (size=0, error case) to " << destRank
<< " with tag " << sizeTag << endl;
*errStrm << os.str ();
}
send (sizeBuf.getRawPtr (), 1, destRank, sizeTag, comm);
return errCode;
}
else { // countPackTriples succeeded; message packed & ready to send
// Send the message size (in bytes). We can use a nonblocking
// send here, and try to overlap with message packing.
const int outBufSize = countSize + triplesSize;
sizeBuf[0] = outBufSize;
if (debug && errStrm != NULL) {
std::ostringstream os;
os << "Proc " << comm.getRank () << ", SC=" << typeid (SC).name ()
<< ", GO=" << typeid (GO).name () << ": "
<< "Post isend (size=" << sizeBuf[0] << ") to " << destRank
<< " with tag " << sizeTag << endl;
*errStrm << os.str ();
}
auto sizeReq = isend<int, int> (sizeBuf, destRank, sizeTag, comm);
msgBuf.resize (outBufSize);
char* outBuf = msgBuf.getRawPtr ();
// If anything goes wrong with packing, send the pack buffer
// anyway, since the receiving process expects a message.
int outBufCurPos = 0; // input/output argument
errCode = packTriplesCount (numEnt, outBuf, outBufSize,
outBufCurPos, comm, errStrm);
if (errCode == 0) {
errCode = packTriples<SC, GO> (rowInds.data (), colInds.data (),
vals.data (), numEnt, outBuf,
outBufSize, outBufCurPos, comm,
errStrm);
}
if (debug && errStrm != NULL) {
std::ostringstream os;
os << "Proc " << comm.getRank () << ", SC=" << typeid (SC).name ()
<< ", GO=" << typeid (GO).name () << ": "
<< "Post isend (packed data) to " << destRank
<< " with tag " << msgTag << endl;
*errStrm << os.str ();
}
auto msgReq = isend<int, char> (msgBuf, destRank, msgTag, comm);
// Wait on the two messages. It doesn't matter in what order
// we send them, because they have different tags. The
// receiving process will wait on the first message first, in
// order to get the size of the second message.
if (debug && errStrm != NULL) {
std::ostringstream os;
os << "Proc " << comm.getRank () << ", SC=" << typeid (SC).name ()
<< ", GO=" << typeid (GO).name () << ": "
<< "Wait on isend (size)" << endl;
*errStrm << os.str ();
}
sizeReq->wait ();
if (debug && errStrm != NULL) {
std::ostringstream os;
os << "Proc " << comm.getRank () << ", SC=" << typeid (SC).name ()
<< ", GO=" << typeid (GO).name () << ": "
<< "Wait on isend (packed data)" << endl;
*errStrm << os.str ();
}
msgReq->wait ();
// This doesn't actually deallocate; it just resets sizes to zero.
rowInds.clear ();
colInds.clear ();
vals.clear ();
}
}
}
return errCode;
}
/// \brief Read at most maxNumEntPerMsg sparse matrix entries from the
/// input stream, and send them to the process with rank destRank.
///
/// To be called only by the sending process.
///
/// \tparam SC The type of the value of each matrix entry.
/// \tparam GO The type of each (global) index of each matrix entry.
/// \tparam CommRequestPtr The type of a (smart) pointer to a
/// "communication request" returned by, e.g., ::Teuchos::ireceive.
/// It must implement operator= and operator->, and the thing to
/// which it points must implement <tt>void wait()</tt>. A model
/// for this is ::Teuchos::RCP< ::Teuchos::CommRequest<int> >.
///
/// \param rowInds [out] Row indices to receive. Will be resized as
/// needed.
/// \param colInds [out] Column indices to receive. Will be resized
/// as needed.
/// \param vals [out] Matrix values to receive. Will be resized as
/// needed.
/// \param numEnt [out] Number of matrix entries (triples) received.
/// \param sizeBuf [in/out] Array of length 1, for receiving message
/// size (size in bytes of \c msgBuf).
/// \param msgBuf [in/out] Message buffer; to be resized as needed.
/// \param sizeReq [in/out] Preposed receive request for message size.
/// After waiting on this, you may read the contents of sizeBuf.
/// This is a nonconst (smart) pointer reference, so that we can
/// assign to it. A model for this is
/// ::Teuchos::RCP< ::Teuchos::CommRequest<int> >&.
/// \param srcRank [in] Rank of the process from which to receive the
/// matrix entries (triples).
/// \param comm [in] Communicator to use for receiving the triples.
/// \param tolerant [in] Whether to read tolerantly.
/// \param errStrm [in] If not NULL, print any error messages to this
/// stream.
template<class SC, class GO, class CommRequestPtr>
int
recvOneBatchOfTriples (std::vector<GO>& rowInds,
std::vector<GO>& colInds,
std::vector<SC>& vals,
int& numEnt,
::Teuchos::ArrayRCP<int>& sizeBuf,
::Teuchos::ArrayRCP<char>& msgBuf,
CommRequestPtr& sizeReq,
const int srcRank,
const ::Teuchos::Comm<int>& comm,
const bool tolerant = false,
std::ostream* errStrm = NULL,
const bool debug = false)
{
using ::Tpetra::Details::unpackTriplesCount;
using ::Tpetra::Details::unpackTriples;
using ::Kokkos::ArithTraits;
////constexpr int sizeTag = 42 + (ArithTraits<SC>::is_complex ? 100 : 0);
//constexpr int msgTag = 43 + (ArithTraits<SC>::is_complex ? 100 : 0);
//constexpr int sizeTag = 42;
constexpr int msgTag = 43;
int errCode = 0; // return value
numEnt = 0; // output argument
// Wait on the ireceive we preposted before calling this function.
if (debug && errStrm != NULL) {
std::ostringstream os;
os << "Proc " << comm.getRank () << ", SC=" << typeid (SC).name ()
<< ", GO=" << typeid (GO).name () << ": "
<< "Wait on irecv (size)" << std::endl;
*errStrm << os.str ();
}
sizeReq->wait ();
sizeReq = CommRequestPtr (NULL);
const int inBufSize = sizeBuf[0];
if (debug && errStrm != NULL) {
std::ostringstream os;
os << "Proc " << comm.getRank () << ", SC=" << typeid (SC).name ()
<< ", GO=" << typeid (GO).name () << ": "
<< "Received size: sizeBuf[0]=" << sizeBuf[0] << std::endl;
*errStrm << os.str ();
}
if (inBufSize == 0) {
numEnt = 0;
rowInds.resize (0);
colInds.resize (0);
vals.resize (0);
}
else {
msgBuf.resize (inBufSize);
char* inBuf = msgBuf.getRawPtr ();
if (debug && errStrm != NULL) {
std::ostringstream os;
os << "Proc " << comm.getRank () << ", SC=" << typeid (SC).name ()
<< ", GO=" << typeid (GO).name () << ": "
<< "Post irecv (packed data) " << "from " << srcRank
<< " with tag " << msgTag << std::endl;
*errStrm << os.str ();
}
auto msgReq = ::Teuchos::ireceive (msgBuf, srcRank, msgTag, comm);
if (debug && errStrm != NULL) {
std::ostringstream os;
os << "Proc " << comm.getRank () << ", SC=" << typeid (SC).name ()
<< ", GO=" << typeid (GO).name () << ": "
<< "Wait on irecv (packed data)" << std::endl;
*errStrm << os.str ();
}
msgReq->wait ();
int inBufCurPos = 0; // output argument
errCode = unpackTriplesCount (inBuf, inBufSize, inBufCurPos,
numEnt, comm, errStrm);
if (errCode == 0) {
rowInds.resize (numEnt);
colInds.resize (numEnt);
vals.resize (numEnt);
errCode = unpackTriples<SC, GO> (inBuf, inBufSize, inBufCurPos,
rowInds.data (), colInds.data (),
vals.data (), numEnt, comm, errStrm);
}
}
return errCode;
}
} // namespace Impl
//
// SKIP DOWN TO HERE FOR "PUBLIC" INTERFACE
//
/// \brief On Process 0 in the given communicator, read sparse matrix
/// entries (in chunks of at most maxNumEntPerMsg entries at a time)
/// from the input stream, and "deal them out" to all other
/// processes in the communicator.
///
/// This is a collective over the communicator.
///
/// \tparam SC The type of the value of each matrix entry.
/// \tparam GO The type of each (global) index of each matrix entry.
///
/// \param inputStream [in/out] Input stream from which to read Matrix
/// Market - format matrix entries ("triples"). Only Process 0 in
/// the communicator needs to be able to access this.
/// \param curLineNum [in/out] On both input and output, the
/// current line number in the input stream. (In the Matrix Market
/// format, sparse matrix entries cannot start until at least line 3
/// of the file.) This is only valid on Process 0.
/// \param totalNumEntRead [out] Total number of matrix entries
/// (triples) read on Process 0. This is only valid on Process 0.
/// \param processTriple [in] Closure, generally with side effects,
/// that takes in and stores off a sparse matrix entry. First
/// argument is the (global) row index, second argument is the
/// (global) column index, and third argument is the value of the
/// entry. The closure must NOT do MPI communication. Return value
/// is an error code, that is zero if and only if the closure
/// succeeded. We intend for you to use this to call
/// CooMatrix::insertEntry.
/// \param comm [in] Communicator to use for receiving the triples.
/// \param tolerant [in] Whether to read tolerantly.
/// \param errStrm [in] If not NULL, print any error messages to this
/// stream.
///
/// \return Error code; 0 if and only if success.
template<class SC, class GO>
int
readAndDealOutTriples (std::istream& inputStream, // only valid on Proc 0
std::size_t& curLineNum, // only valid on Proc 0
std::size_t& totalNumEntRead, // only valid on Proc 0
std::function<int (const GO, const GO, const SC&)> processTriple,
const std::size_t maxNumEntPerMsg,
const ::Teuchos::Comm<int>& comm,
const bool tolerant = false,
std::ostream* errStrm = NULL,
const bool debug = false)
{
using Kokkos::ArithTraits;
using std::endl;
using std::size_t;
constexpr int srcRank = 0;
//constexpr int sizeTag = 42 + (ArithTraits<SC>::is_complex ? 100 : 0);
////constexpr int msgTag = 43 + (ArithTraits<SC>::is_complex ? 100 : 0);
constexpr int sizeTag = 42;
//constexpr int msgTag = 43;
const int myRank = comm.getRank ();
const int numProcs = comm.getSize ();
int errCode = 0;
::Teuchos::ArrayRCP<int> sizeBuf (1);
::Teuchos::ArrayRCP<char> msgBuf; // to be resized as needed
// Temporary storage for reading & packing (on Process srcRank) or
// unpacking (every other process) triples.
std::vector<GO> rowInds;
std::vector<GO> colInds;
std::vector<SC> vals;
rowInds.reserve (maxNumEntPerMsg);
colInds.reserve (maxNumEntPerMsg);
vals.reserve (maxNumEntPerMsg);
totalNumEntRead = 0;
if (myRank == srcRank) {
// Loop around through all the processes, including this one, over
// and over until we reach the end of the file, or an error occurs.
int destRank = 0;
bool lastMessageWasLegitZero = false;
for ( ;
! inputStream.eof () && errCode == 0;
destRank = (destRank + 1) % numProcs) {
size_t curNumEntRead = 0; // output argument of below
if (destRank == srcRank) {
// We can read and process the triples directly. We don't
// need to use intermediate storage, because we don't need to
// pack and send the triples.
const int readErrCode =
Impl::readTriples<SC, GO> (inputStream, curLineNum, curNumEntRead,
processTriple, maxNumEntPerMsg, tolerant,
errStrm, debug);
if (debug && errStrm != NULL) {
std::ostringstream os;
os << "Proc " << comm.getRank () << ", SC=" << typeid (SC).name ()
<< ", GO=" << typeid (GO).name () << ": "
<< "(dest=src) readTriples returned curNumEntRead="
<< curNumEntRead << ", errCode=" << readErrCode << endl;
*errStrm << os.str ();
}
errCode = (readErrCode != 0) ? readErrCode : errCode;
}
else {
if (false && debug && errStrm != NULL) {
std::ostringstream os;
os << "Proc " << comm.getRank () << ", SC=" << typeid (SC).name ()
<< ", GO=" << typeid (GO).name () << ": "
<< "Calling readAndSend... with destRank=" << destRank << endl;
*errStrm << os.str ();
}
// Read, pack, and send the triples to destRank.
const int readAndSendErrCode =
Impl::readAndSendOneBatchOfTriples<SC, GO> (inputStream, curLineNum,
curNumEntRead,
sizeBuf, msgBuf,
rowInds, colInds, vals,
maxNumEntPerMsg, destRank,
comm, tolerant, errStrm,
debug);
totalNumEntRead += curNumEntRead;
if (debug && errStrm != NULL) {
std::ostringstream os;
os << "Proc " << comm.getRank () << ", SC=" << typeid (SC).name ()
<< ", GO=" << typeid (GO).name () << ": "
<< "readAndSend... with destRank=" << destRank
<< " returned curNumEntRead=" << curNumEntRead
<< ", errCode=" << readAndSendErrCode << endl;
*errStrm << os.str ();
}
errCode = (readAndSendErrCode != 0) ? readAndSendErrCode : errCode;
if (readAndSendErrCode == 0 && curNumEntRead == 0) {
lastMessageWasLegitZero = true;
if (debug && errStrm != NULL) {
std::ostringstream os;
os << "Proc " << comm.getRank () << ", SC=" << typeid (SC).name ()
<< ", GO=" << typeid (GO).name () << ": "
<< "Last send to " << destRank << " with tag " << sizeTag
<< " was legit zero, counts as termination" << endl;
*errStrm << os.str ();
}
}
}
} // loop around through processes until done reading file, or error
// Loop around through the remaining processes, and tell them that
// we're done, by sending zero. If the last message we sent to
// destRank was zero, then skip that process, since it only
// expects one message of size zero. Note that destRank got
// incremented mod numProcs at end of loop, so we have to
// decrement it mod numProcs.
destRank = (destRank - 1) % numProcs;
if (destRank < 0) { // C mod operator does not promise positivity
destRank = destRank + numProcs;
}
const int startRank = lastMessageWasLegitZero ? (destRank+1) : destRank;
for (int outRank = startRank; outRank < numProcs; ++outRank) {
if (outRank != srcRank) {
if (debug && errStrm != NULL) {
std::ostringstream os;
os << "Proc " << comm.getRank () << ", SC=" << typeid (SC).name ()
<< ", GO=" << typeid (GO).name () << ": "
<< "Post send (size, termination msg) to " << outRank
<< " with tag " << sizeTag << "(was last message legit zero? "
<< (lastMessageWasLegitZero ? "true" : "false") << ")" << endl;
*errStrm << os.str ();
}
sizeBuf[0] = 0;
::Teuchos::send (sizeBuf.getRawPtr (), 1, outRank, sizeTag, comm);
}
}
}
else {
while (true) {
// Prepost a message to receive the size (in bytes) of the
// incoming packet.
sizeBuf[0] = 0; // superfluous, but safe
if (debug && errStrm != NULL) {
std::ostringstream os;
os << "Proc " << comm.getRank () << ", SC=" << typeid (SC).name ()
<< ", GO=" << typeid (GO).name () << ": "
<< "Post irecv (size) from " << srcRank
<< " with tag " << sizeTag << std::endl;
*errStrm << os.str ();
}
auto sizeReq = ::Teuchos::ireceive (sizeBuf, srcRank, sizeTag, comm);
int numEnt = 0; // output argument
const int recvErrCode =
Impl::recvOneBatchOfTriples (rowInds, colInds, vals, numEnt, sizeBuf,
msgBuf, sizeReq, srcRank, comm, tolerant,
errStrm, debug);
if (debug && errStrm != NULL) {
std::ostringstream os;
os << "Proc " << comm.getRank () << ", SC=" << typeid (SC).name ()
<< ", GO=" << typeid (GO).name () << ": "
<< "recvOneBatchOfTriples returned numEnt=" << numEnt
<< ", errCode=" << recvErrCode << endl;
*errStrm << os.str ();
}
errCode = (recvErrCode != 0) ? recvErrCode : errCode;
if (numEnt != static_cast<int> (rowInds.size ()) ||
numEnt != static_cast<int> (colInds.size ()) ||
numEnt != static_cast<int> (vals.size ())) {
errCode = (errCode == 0) ? -1 : errCode;
if (errStrm != NULL) {
*errStrm << "recvOneBatchOfTriples produced inconsistent data sizes. "
<< "numEnt = " << numEnt
<< ", rowInds.size() = " << rowInds.size ()
<< ", colInds.size() = " << colInds.size ()
<< ", vals.size() = " << vals.size () << "."
<< endl;
}
} // if sizes inconsistent
// Sending zero items is how Process srcRank tells this process
// that it (Process srcRank) is done sending out data.
if (numEnt == 0) {
break;
}
for (int k = 0; k < numEnt && errCode == 0; ++k) {
const int curErrCode = processTriple (rowInds[k], colInds[k], vals[k]);
errCode = (curErrCode == 0) ? errCode : curErrCode;
}
} // while we still get messages from srcRank
}
if (debug && errStrm != NULL) {
std::ostringstream os;
os << "Proc " << comm.getRank () << ", SC=" << typeid (SC).name ()
<< ", GO=" << typeid (GO).name () << ": "
<< "Done with send/recv loop" << endl;
*errStrm << os.str ();
}
// Do a bitwise OR to get an error code that is nonzero if and only
// if any process' local error code is nonzero.
using ::Teuchos::outArg;
using ::Teuchos::REDUCE_BOR;
using ::Teuchos::reduceAll;
const int lclErrCode = errCode;
reduceAll<int, int> (comm, REDUCE_BOR, lclErrCode, outArg (errCode));
return errCode;
}
} // namespace Details
} // namespace Tpetra
#endif // TPETRA_DETAILS_READTRIPLES_HPP
|