decomposedBlockData.C 28 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
5
    \\  /    A nd           | www.openfoam.com
6
     \\/     M anipulation  |
OpenFOAM bot's avatar
OpenFOAM bot committed
7
-------------------------------------------------------------------------------
OpenFOAM bot's avatar
OpenFOAM bot committed
8
    Copyright (C) 2017-2018 OpenFOAM Foundation
9
    Copyright (C) 2020 OpenCFD Ltd.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM 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 General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "decomposedBlockData.H"
#include "OPstream.H"
#include "IPstream.H"
#include "PstreamBuffers.H"
Mark Olesen's avatar
Mark Olesen committed
33
#include "Fstream.H"
34 35
#include "dictionary.H"
#include "objectRegistry.H"
36 37 38
#include "SubList.H"
#include "labelPair.H"
#include "masterUncollatedFileOperation.H"
Mattijs Janssens's avatar
Mattijs Janssens committed
39
#include "IListStream.H"
40
#include "foamVersion.H"
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

// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

namespace Foam
{
    defineTypeNameAndDebug(decomposedBlockData, 0);
}

// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

Foam::decomposedBlockData::decomposedBlockData
(
    const label comm,
    const IOobject& io,
    const UPstream::commsTypes commsType
)
:
    regIOobject(io),
    commsType_(commsType),
    comm_(comm)
{
    // Temporary warning
    if (io.readOpt() == IOobject::MUST_READ_IF_MODIFIED)
    {
        WarningInFunction
            << "decomposedBlockData " << name()
            << " constructed with IOobject::MUST_READ_IF_MODIFIED"
            " but decomposedBlockData does not support automatic rereading."
            << endl;
    }
    if
    (
        (
            io.readOpt() == IOobject::MUST_READ
         || io.readOpt() == IOobject::MUST_READ_IF_MODIFIED
        )
     || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
    )
    {
        read();
    }
}


Foam::decomposedBlockData::decomposedBlockData
(
    const label comm,
    const IOobject& io,
    const UList<char>& list,
    const UPstream::commsTypes commsType
)
:
    regIOobject(io),
    commsType_(commsType),
    comm_(comm)
{
    // Temporary warning
    if (io.readOpt() == IOobject::MUST_READ_IF_MODIFIED)
    {
        WarningInFunction
            << "decomposedBlockData " << name()
            << " constructed with IOobject::MUST_READ_IF_MODIFIED"
            " but decomposedBlockData does not support automatic rereading."
            << endl;
    }

    if
    (
        (
            io.readOpt() == IOobject::MUST_READ
         || io.readOpt() == IOobject::MUST_READ_IF_MODIFIED
        )
     || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
    )
    {
        read();
    }
    else
    {
        List<char>::operator=(list);
    }
}


Foam::decomposedBlockData::decomposedBlockData
(
    const label comm,
    const IOobject& io,
129
    List<char>&& list,
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    const UPstream::commsTypes commsType
)
:
    regIOobject(io),
    commsType_(commsType),
    comm_(comm)
{
    // Temporary warning
    if (io.readOpt() == IOobject::MUST_READ_IF_MODIFIED)
    {
        WarningInFunction
            << "decomposedBlockData " << name()
            << " constructed with IOobject::MUST_READ_IF_MODIFIED"
            " but decomposedBlockData does not support automatic rereading."
            << endl;
    }

147
    List<char>::transfer(list);
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

    if
    (
        (
            io.readOpt() == IOobject::MUST_READ
         || io.readOpt() == IOobject::MUST_READ_IF_MODIFIED
        )
     || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
    )
    {
        read();
    }
}


// * * * * * * * * * * * * * * * Members Functions * * * * * * * * * * * * * //

bool Foam::decomposedBlockData::readMasterHeader(IOobject& io, Istream& is)
{
    if (debug)
    {
        Pout<< "decomposedBlockData::readMasterHeader:"
            << " stream:" << is.name() << endl;
    }

    // Master-only reading of header
    is.fatalCheck("read(Istream&)");

    List<char> data(is);
    is.fatalCheck("read(Istream&) : reading entry");

179 180 181 182
    UIListStream headerStream(data);
    headerStream.name() = is.name();

    return io.readHeader(headerStream);
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
}


void Foam::decomposedBlockData::writeHeader
(
    Ostream& os,
    const IOstream::versionNumber version,
    const IOstream::streamFormat format,
    const word& type,
    const string& note,
    const fileName& location,
    const word& name
)
{
    IOobject::writeBanner(os)
        << "FoamFile\n{\n"
        << "    version     " << version << ";\n"
        << "    format      " << format << ";\n"
        << "    class       " << type << ";\n";
202 203 204 205

    // This may be useful to have as well
    if (os.format() == IOstream::BINARY)
    {
206
        os  << "    arch        " << foamVersion::buildArch << ";\n";
207 208 209 210 211 212 213
    }

    if (Pstream::parRun())
    {
        os  << "    blocks      " << Pstream::nProcs() << ";\n";
    }

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
    if (note.size())
    {
        os  << "    note        " << note << ";\n";
    }

    if (location.size())
    {
        os  << "    location    " << location << ";\n";
    }

    os  << "    object      " << name << ";\n"
        << "}" << nl;

    IOobject::writeDivider(os) << nl;
}


Foam::autoPtr<Foam::ISstream> Foam::decomposedBlockData::readBlock
(
    const label blocki,
    Istream& is,
    IOobject& headerIO
)
{
    if (debug)
    {
        Pout<< "decomposedBlockData::readBlock:"
            << " stream:" << is.name() << " attempt to read block " << blocki
            << endl;
    }

    is.fatalCheck("read(Istream&)");

    autoPtr<ISstream> realIsPtr;

    if (blocki == 0)
    {
251
        List<char> data(is);
252 253
        is.fatalCheck("read(Istream&) : reading entry");

254 255
        realIsPtr.reset(new IListStream(std::move(data)));
        realIsPtr->name() = is.name();
256 257

        // Read header
258
        if (!headerIO.readHeader(*realIsPtr))
259
        {
260
            FatalIOErrorInFunction(*realIsPtr)
261 262 263 264 265 266 267
                << "problem while reading header for object "
                << is.name() << exit(FatalIOError);
        }
    }
    else
    {
        // Read master for header
268
        List<char> data(is);
269 270 271 272
        is.fatalCheck("read(Istream&) : reading entry");

        IOstream::versionNumber ver(IOstream::currentVersion);
        IOstream::streamFormat fmt;
273 274
        unsigned labelByteSize;
        unsigned scalarByteSize;
275
        {
276
            UIListStream headerStream(data);
277 278 279 280 281 282 283 284 285 286

            // Read header
            if (!headerIO.readHeader(headerStream))
            {
                FatalIOErrorInFunction(headerStream)
                    << "problem while reading header for object "
                    << is.name() << exit(FatalIOError);
            }
            ver = headerStream.version();
            fmt = headerStream.format();
287 288
            labelByteSize = headerStream.labelByteSize();
            scalarByteSize = headerStream.scalarByteSize();
289 290 291 292
        }

        for (label i = 1; i < blocki+1; i++)
        {
293
            // Read and discard data, only retain the last one
294 295 296
            is >> data;
            is.fatalCheck("read(Istream&) : reading entry");
        }
297 298
        realIsPtr.reset(new IListStream(std::move(data)));
        realIsPtr->name() = is.name();
299 300 301 302

        // Apply master stream settings to realIsPtr
        realIsPtr().format(fmt);
        realIsPtr().version(ver);
303 304
        realIsPtr().setLabelByteSize(labelByteSize);
        realIsPtr().setScalarByteSize(scalarByteSize);
305
    }
306

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
    return realIsPtr;
}


bool Foam::decomposedBlockData::readBlocks
(
    const label comm,
    autoPtr<ISstream>& isPtr,
    List<char>& data,
    const UPstream::commsTypes commsType
)
{
    if (debug)
    {
        Pout<< "decomposedBlockData::readBlocks:"
322
            << " stream:" << (isPtr ? isPtr->name() : "invalid")
323 324 325 326 327 328 329 330 331 332
            << " commsType:" << Pstream::commsTypeNames[commsType]
            << " comm:" << comm << endl;
    }

    bool ok = false;

    if (commsType == UPstream::commsTypes::scheduled)
    {
        if (UPstream::master(comm))
        {
333
            Istream& is = *isPtr;
334 335 336 337 338 339 340 341 342
            is.fatalCheck("read(Istream&)");

            // Read master data
            {
                is >> data;
                is.fatalCheck("read(Istream&) : reading entry");
            }

            // Read slave data
343
            for (const int proci : UPstream::subProcs(comm))
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
            {
                List<char> elems(is);
                is.fatalCheck("read(Istream&) : reading entry");

                OPstream os
                (
                    UPstream::commsTypes::scheduled,
                    proci,
                    0,
                    UPstream::msgType(),
                    comm
                );
                os << elems;
            }

            ok = is.good();
        }
        else
        {
            IPstream is
            (
                UPstream::commsTypes::scheduled,
                UPstream::masterNo(),
                0,
                UPstream::msgType(),
                comm
            );
            is >> data;
        }
    }
    else
    {
        PstreamBuffers pBufs
        (
            UPstream::commsTypes::nonBlocking,
            UPstream::msgType(),
            comm
        );

        if (UPstream::master(comm))
        {
385
            Istream& is = *isPtr;
386 387 388 389 390 391 392 393 394
            is.fatalCheck("read(Istream&)");

            // Read master data
            {
                is >> data;
                is.fatalCheck("read(Istream&) : reading entry");
            }

            // Read slave data
395
            for (const int proci : UPstream::subProcs(comm))
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
            {
                List<char> elems(is);
                is.fatalCheck("read(Istream&) : reading entry");

                UOPstream os(proci, pBufs);
                os << elems;
            }
        }

        labelList recvSizes;
        pBufs.finishedSends(recvSizes);

        if (!UPstream::master(comm))
        {
            UIPstream is(UPstream::masterNo(), pBufs);
            is >> data;
        }
    }

    Pstream::scatter(ok, Pstream::msgType(), comm);

    return ok;
}


Foam::autoPtr<Foam::ISstream> Foam::decomposedBlockData::readBlocks
(
    const label comm,
    const fileName& fName,
    autoPtr<ISstream>& isPtr,
    IOobject& headerIO,
    const UPstream::commsTypes commsType
)
{
    if (debug)
    {
        Pout<< "decomposedBlockData::readBlocks:"
433
            << " stream:" << (isPtr ? isPtr->name() : "invalid")
434 435 436 437 438 439 440 441 442 443 444 445
            << " commsType:" << Pstream::commsTypeNames[commsType] << endl;
    }

    bool ok = false;

    List<char> data;
    autoPtr<ISstream> realIsPtr;

    if (commsType == UPstream::commsTypes::scheduled)
    {
        if (UPstream::master(comm))
        {
446
            Istream& is = *isPtr;
447 448 449 450 451 452 453
            is.fatalCheck("read(Istream&)");

            // Read master data
            {
                is >> data;
                is.fatalCheck("read(Istream&) : reading entry");

454 455
                realIsPtr.reset(new IListStream(std::move(data)));
                realIsPtr->name() = fName;
Mattijs Janssens's avatar
Mattijs Janssens committed
456

457
                // Read header
458
                if (!headerIO.readHeader(*realIsPtr))
459
                {
460
                    FatalIOErrorInFunction(*realIsPtr)
461 462 463 464 465 466
                        << "problem while reading header for object "
                        << is.name() << exit(FatalIOError);
                }
            }

            // Read slave data
467
            for (const int proci : UPstream::subProcs(comm))
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
            {
                is >> data;
                is.fatalCheck("read(Istream&) : reading entry");

                OPstream os
                (
                    UPstream::commsTypes::scheduled,
                    proci,
                    0,
                    UPstream::msgType(),
                    comm
                );
                os << data;
            }

            ok = is.good();
        }
        else
        {
            IPstream is
            (
                UPstream::commsTypes::scheduled,
                UPstream::masterNo(),
                0,
                UPstream::msgType(),
                comm
            );
            is >> data;

497 498
            realIsPtr.reset(new IListStream(std::move(data)));
            realIsPtr->name() = fName;
499 500 501 502 503 504 505 506 507 508 509 510 511
        }
    }
    else
    {
        PstreamBuffers pBufs
        (
            UPstream::commsTypes::nonBlocking,
            UPstream::msgType(),
            comm
        );

        if (UPstream::master(comm))
        {
512
            Istream& is = *isPtr;
513 514 515 516 517 518 519
            is.fatalCheck("read(Istream&)");

            // Read master data
            {
                is >> data;
                is.fatalCheck("read(Istream&) : reading entry");

520 521
                realIsPtr.reset(new IListStream(std::move(data)));
                realIsPtr->name() = fName;
522 523

                // Read header
524
                if (!headerIO.readHeader(*realIsPtr))
525
                {
526
                    FatalIOErrorInFunction(*realIsPtr)
527 528 529 530 531 532
                        << "problem while reading header for object "
                        << is.name() << exit(FatalIOError);
                }
            }

            // Read slave data
533
            for (const int proci : UPstream::subProcs(comm))
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
            {
                List<char> elems(is);
                is.fatalCheck("read(Istream&) : reading entry");

                UOPstream os(proci, pBufs);
                os << elems;
            }

            ok = is.good();
        }

        labelList recvSizes;
        pBufs.finishedSends(recvSizes);

        if (!UPstream::master(comm))
        {
            UIPstream is(UPstream::masterNo(), pBufs);
            is >> data;

553 554
            realIsPtr.reset(new IListStream(std::move(data)));
            realIsPtr->name() = fName;
555 556 557 558 559
        }
    }

    Pstream::scatter(ok, Pstream::msgType(), comm);

560
    //- Set stream properties from realIsPtr on master
561

562
    // Scatter master header info
563 564
    string versionString;
    label formatValue;
565 566 567
    unsigned labelByteSize;
    unsigned scalarByteSize;
    if (UPstream::master(comm))
568
    {
569 570
        versionString = realIsPtr().version().str();
        formatValue = static_cast<label>(realIsPtr().format());
571 572
        labelByteSize = realIsPtr().labelByteSize();
        scalarByteSize = realIsPtr().scalarByteSize();
573
    }
574 575
    Pstream::scatter(versionString); //,  Pstream::msgType(), comm);
    Pstream::scatter(formatValue); //,  Pstream::msgType(), comm);
576 577 578
    Pstream::scatter(labelByteSize); //,  Pstream::msgType(), comm);
    Pstream::scatter(scalarByteSize); //,  Pstream::msgType(), comm);

579 580
    realIsPtr().version(IOstream::versionNumber(versionString));
    realIsPtr().format(IOstream::streamFormat(formatValue));
581 582
    realIsPtr().setLabelByteSize(labelByteSize);
    realIsPtr().setScalarByteSize(scalarByteSize);
583 584 585 586 587 588 589 590 591 592 593 594 595

    word name(headerIO.name());
    Pstream::scatter(name, Pstream::msgType(), comm);
    headerIO.rename(name);
    Pstream::scatter(headerIO.headerClassName(), Pstream::msgType(), comm);
    Pstream::scatter(headerIO.note(), Pstream::msgType(), comm);
    //Pstream::scatter(headerIO.instance(), Pstream::msgType(), comm);
    //Pstream::scatter(headerIO.local(), Pstream::msgType(), comm);

    return realIsPtr;
}


596 597 598 599 600 601 602 603 604 605 606 607
void Foam::decomposedBlockData::gather
(
    const label comm,
    const label data,
    labelList& datas
)
{
    const label nProcs = UPstream::nProcs(comm);
    datas.setSize(nProcs);

    char* data0Ptr = reinterpret_cast<char*>(datas.begin());

608 609
    List<int> recvOffsets;
    List<int> recvSizes;
610
    if (UPstream::master(comm))
611 612 613 614
    {
        recvOffsets.setSize(nProcs);
        forAll(recvOffsets, proci)
        {
615 616
            // Note: truncating long int to int since UPstream::gather limited
            // to ints
617
            recvOffsets[proci] =
618
                int(reinterpret_cast<char*>(&datas[proci]) - data0Ptr);
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
        }
        recvSizes.setSize(nProcs, sizeof(label));
    }

    UPstream::gather
    (
        reinterpret_cast<const char*>(&data),
        sizeof(label),
        data0Ptr,
        recvSizes,
        recvOffsets,
        comm
    );
}


void Foam::decomposedBlockData::gatherSlaveData
(
    const label comm,
    const UList<char>& data,
    const labelUList& recvSizes,

    const label startProc,
    const label nProcs,

    List<int>& sliceOffsets,
    List<char>& recvData
)
{
    // Calculate master data
    List<int> sliceSizes;
    if (UPstream::master(comm))
    {
        const label numProcs = UPstream::nProcs(comm);

        sliceSizes.setSize(numProcs, 0);
        sliceOffsets.setSize(numProcs+1, 0);

        int totalSize = 0;
        label proci = startProc;
        for (label i = 0; i < nProcs; i++)
        {
            sliceSizes[proci] = int(recvSizes[proci]);
            sliceOffsets[proci] = totalSize;
            totalSize += sliceSizes[proci];
664
            ++proci;
665 666 667 668 669 670 671 672 673 674 675 676 677
        }
        sliceOffsets[proci] = totalSize;
        recvData.setSize(totalSize);
    }

    int nSend = 0;
    if
    (
       !UPstream::master(comm)
     && (UPstream::myProcNo(comm) >= startProc)
     && (UPstream::myProcNo(comm) < startProc+nProcs)
    )
    {
678 679
        // Note: UPstream::gather limited to int
        nSend = int(data.byteSize());
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
    }

    UPstream::gather
    (
        data.begin(),
        nSend,

        recvData.begin(),
        sliceSizes,
        sliceOffsets,
        comm
    );
}


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
Foam::label Foam::decomposedBlockData::calcNumProcs
(
    const label comm,
    const off_t maxBufferSize,
    const labelUList& recvSizes,
    const label startProci
)
{
    const label nProcs = UPstream::nProcs(comm);

    label nSendProcs = -1;
    if (UPstream::master(comm))
    {
        off_t totalSize = recvSizes[startProci];
        label proci = startProci+1;
        while (proci < nProcs && (totalSize+recvSizes[proci] < maxBufferSize))
        {
            totalSize += recvSizes[proci];
            proci++;
        }

        nSendProcs = proci-startProci;
    }

    // Scatter nSendProcs
    label n;
    UPstream::scatter
    (
        reinterpret_cast<const char*>(&nSendProcs),
        List<int>(nProcs, sizeof(nSendProcs)),
725
        List<int>(nProcs, Zero),
726 727 728 729 730 731 732 733 734
        reinterpret_cast<char*>(&n),
        sizeof(n),
        comm
    );

    return n;
}


735 736 737 738 739 740
bool Foam::decomposedBlockData::writeBlocks
(
    const label comm,
    autoPtr<OSstream>& osPtr,
    List<std::streamoff>& start,
    const UList<char>& data,
741 742

    const labelUList& recvSizes,
743
    const PtrList<SubList<char>>& slaveData,
744

745 746 747 748 749 750 751
    const UPstream::commsTypes commsType,
    const bool syncReturnState
)
{
    if (debug)
    {
        Pout<< "decomposedBlockData::writeBlocks:"
752
            << " stream:" << (osPtr ? osPtr->name() : "invalid")
753
            << " data:" << data.size()
754
            << " (master only) slaveData:" << slaveData.size()
755 756 757
            << " commsType:" << Pstream::commsTypeNames[commsType] << endl;
    }

758 759
    const label nProcs = UPstream::nProcs(comm);

760 761
    bool ok = true;

762
    if (slaveData.size())
763 764 765
    {
        // Already have gathered the slave data. communicator only used to
        // check who is the master
766

767 768
        if (UPstream::master(comm))
        {
769
            OSstream& os = *osPtr;
770 771 772 773 774 775 776 777 778 779 780 781 782 783

            start.setSize(nProcs);

            // Write master data
            {
                os << nl << "// Processor" << UPstream::masterNo() << nl;
                start[UPstream::masterNo()] = os.stdStream().tellp();
                os << data;
            }

            // Write slaves

            label slaveOffset = 0;

784
            for (label proci = 1; proci < nProcs; ++proci)
785 786 787 788
            {
                os << nl << nl << "// Processor" << proci << nl;
                start[proci] = os.stdStream().tellp();

789
                os << slaveData[proci];
790 791 792 793 794 795 796
                slaveOffset += recvSizes[proci];
            }

            ok = os.good();
        }
    }
    else if (commsType == UPstream::commsTypes::scheduled)
797 798 799
    {
        if (UPstream::master(comm))
        {
800
            start.setSize(nProcs);
801

802
            OSstream& os = *osPtr;
803 804 805 806 807 808 809 810 811

            // Write master data
            {
                os << nl << "// Processor" << UPstream::masterNo() << nl;
                start[UPstream::masterNo()] = os.stdStream().tellp();
                os << data;
            }
            // Write slaves
            List<char> elems;
812
            for (label proci = 1; proci < nProcs; ++proci)
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
            {
                elems.setSize(recvSizes[proci]);
                IPstream::read
                (
                    UPstream::commsTypes::scheduled,
                    proci,
                    elems.begin(),
                    elems.size(),
                    Pstream::msgType(),
                    comm
                );

                os << nl << nl << "// Processor" << proci << nl;
                start[proci] = os.stdStream().tellp();
                os << elems;
            }

            ok = os.good();
        }
        else
        {
            UOPstream::write
            (
                UPstream::commsTypes::scheduled,
                UPstream::masterNo(),
                data.begin(),
                data.byteSize(),
                Pstream::msgType(),
                comm
            );
        }
    }
    else
    {
847 848
        // Write master data
        if (UPstream::master(comm))
849
        {
850 851
            start.setSize(nProcs);

852
            OSstream& os = *osPtr;
853 854 855 856

            os << nl << "// Processor" << UPstream::masterNo() << nl;
            start[UPstream::masterNo()] = os.stdStream().tellp();
            os << data;
857 858 859
        }


860 861
        // Find out how many processor can be received into
        // maxMasterFileBufferSize
862

863
        // Starting slave processor and number of processors
864 865
        label startProc = 1;
        label nSendProcs = nProcs-1;
866

867
        while (nSendProcs > 0 && startProc < nProcs)
868
        {
869
            nSendProcs = calcNumProcs
870
            (
871 872 873 874 875 876 877 878
                comm,
                off_t
                (
                    fileOperations::masterUncollatedFileOperation::
                    maxMasterFileBufferSize
                ),
                recvSizes,
                startProc
879
            );
880

881
            if (nSendProcs == 0)
882
            {
883
                break;
884 885 886
            }


887 888 889 890 891 892 893 894 895
            // Gather data from (a slice of) the slaves
            List<int> sliceOffsets;
            List<char> recvData;
            gatherSlaveData
            (
                comm,
                data,
                recvSizes,

896 897
                startProc,    // startProc,
                nSendProcs,    // nProcs,
898 899 900 901 902 903

                sliceOffsets,
                recvData
            );

            if (UPstream::master(comm))
904
            {
905
                OSstream& os = *osPtr;
906

907 908 909
                // Write slaves
                for
                (
910 911 912
                    label proci = startProc;
                    proci < startProc+nSendProcs;
                    proci++
913
                )
914
                {
915 916 917 918 919 920 921 922 923 924
                    os << nl << nl << "// Processor" << proci << nl;
                    start[proci] = os.stdStream().tellp();

                    os <<
                        SubList<char>
                        (
                            recvData,
                            sliceOffsets[proci+1]-sliceOffsets[proci],
                            sliceOffsets[proci]
                        );
925 926 927
                }
            }

928
            startProc += nSendProcs;
929
        }
930

931 932
        if (UPstream::master(comm))
        {
933
            ok = osPtr->good();
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
        }
    }

    if (syncReturnState)
    {
        //- Enable to get synchronised error checking. Is the one that keeps
        //  slaves as slow as the master (which does all the writing)
        Pstream::scatter(ok, Pstream::msgType(), comm);
    }

    return ok;
}


bool Foam::decomposedBlockData::read()
{
    autoPtr<ISstream> isPtr;
    fileName objPath(fileHandler().filePath(false, *this, word::null));
    if (UPstream::master(comm_))
    {
        isPtr.reset(new IFstream(objPath));
955
        IOobject::readHeader(*isPtr);
956 957 958 959 960 961 962 963 964 965 966 967
    }

    List<char>& data = *this;
    return readBlocks(comm_, isPtr, data, commsType_);
}


bool Foam::decomposedBlockData::writeData(Ostream& os) const
{
    const List<char>& data = *this;

    IOobject io(*this);
Mattijs Janssens's avatar
Mattijs Janssens committed
968 969

    // Re-read my own data to find out the header information
970
    if (Pstream::master(comm_))
971
    {
972 973
        UIListStream headerStream(data);
        io.readHeader(headerStream);
974 975 976 977 978
    }

    // Scatter header information

    string versionString(os.version().str());
979
    Pstream::scatter(versionString, Pstream::msgType(), comm_);
980

981 982
    label formatValue(os.format());
    Pstream::scatter(formatValue, Pstream::msgType(), comm_);
983 984

    //word masterName(name());
985
    //Pstream::scatter(masterName, Pstream::msgType(), comm_);
986

987 988
    Pstream::scatter(io.headerClassName(), Pstream::msgType(), comm_);
    Pstream::scatter(io.note(), Pstream::msgType(), comm_);
989 990 991 992
    //Pstream::scatter(io.instance(), Pstream::msgType(), comm);
    //Pstream::scatter(io.local(), Pstream::msgType(), comm);

    fileName masterLocation(instance()/db().dbDir()/local());
993
    Pstream::scatter(masterLocation, Pstream::msgType(), comm_);
994

995
    if (!Pstream::master(comm_))
996 997 998 999
    {
        writeHeader
        (
            os,
1000
            IOstream::versionNumber(versionString),
1001
            IOstream::streamFormat(formatValue),
1002 1003 1004 1005 1006 1007 1008
            io.headerClassName(),
            io.note(),
            masterLocation,
            name()
        );
    }

1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
    // Write the character data
    if (isA<OFstream>(os))
    {
        // Serial file output - can use writeRaw()
        os.writeRaw(data.cdata(), data.byteSize());
    }
    else
    {
        // Other cases are less fortunate, and no std::string_view
        std::string str(data.cdata(), data.byteSize());
        os.writeQuoted(str, false);
    }
1021

1022
    if (!Pstream::master(comm_))
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
    {
        IOobject::writeEndDivider(os);
    }

    return os.good();
}


bool Foam::decomposedBlockData::writeObject
(
1033
    IOstreamOption streamOpt,
1034 1035 1036
    const bool valid
) const
{
1037 1038 1039
    // Always write BINARY
    streamOpt.format(IOstream::BINARY);

1040 1041 1042
    autoPtr<OSstream> osPtr;
    if (UPstream::master(comm_))
    {
1043 1044 1045
        // Note: always write binary. These are strings so readable anyway.
        //       They have already be tokenised on the sending side.
        osPtr.reset(new OFstream(objectPath(), streamOpt));
1046
        IOobject::writeHeader(*osPtr);
1047
    }
1048 1049

    labelList recvSizes;
1050
    gather(comm_, label(this->byteSize()), recvSizes);
1051

1052
    List<std::streamoff> start;
1053
    PtrList<SubList<char>> slaveData;  // dummy slave data
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
    return writeBlocks
    (
        comm_,
        osPtr,
        start,
        *this,
        recvSizes,
        slaveData,
        commsType_
    );
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
}


Foam::label Foam::decomposedBlockData::numBlocks(const fileName& fName)
{
    label nBlocks = 0;

    IFstream is(fName);
    is.fatalCheck("decomposedBlockData::numBlocks(const fileName&)");

    if (!is.good())
    {
        return nBlocks;
    }

1079
    // FoamFile header
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
    token firstToken(is);

    if
    (
        is.good()
     && firstToken.isWord()
     && firstToken.wordToken() == "FoamFile"
    )
    {
        dictionary headerDict(is);
1090
        is.version(headerDict.get<token>("version"));
1091
        is.format(headerDict.get<word>("format"));
1092 1093 1094 1095 1096 1097

        // Obtain number of blocks directly
        if (headerDict.readIfPresent("blocks", nBlocks))
        {
            return nBlocks;
        }
1098 1099
    }

1100
    // Fallback to brute force read of each data block
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
    List<char> data;
    while (is.good())
    {
        token sizeToken(is);
        if (!sizeToken.isLabel())
        {
            return nBlocks;
        }
        is.putBack(sizeToken);

        is >> data;
        nBlocks++;
    }

    return nBlocks;
}


// ************************************************************************* //