globalIndex.C 3.57 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
6
     \\/     M anipulation  | Copyright (C) 2018 OpenCFD Ltd.
7
8
9
10
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

11
12
13
14
    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.
15
16
17
18
19
20
21

    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
22
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
23
24
25
26
27
28
29

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

#include "globalIndex.H"

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

30
31
32
33
34
35
36
37
38
Foam::globalIndex::globalIndex(Istream& is)
{
    is >> offsets_;
}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

void Foam::globalIndex::reset
mattijs's avatar
mattijs committed
39
40
41
(
    const label localSize,
    const int tag,
42
    const label comm,
mattijs's avatar
mattijs committed
43
44
    const bool parallel
)
45
{
46
47
48
    offsets_.resize(Pstream::nProcs(comm)+1);

    labelList localSizes(Pstream::nProcs(comm), Zero);
49
    localSizes[Pstream::myProcNo(comm)] = localSize;
50

mattijs's avatar
mattijs committed
51
52
    if (parallel)
    {
53
54
        Pstream::gatherList(localSizes, tag, comm);
        Pstream::scatterList(localSizes, tag, comm);
mattijs's avatar
mattijs committed
55
    }
56

57
58
    label offset = 0;
    offsets_[0] = 0;
59
    for (label proci = 0; proci < Pstream::nProcs(comm); ++proci)
60
    {
61
        const label oldOffset = offset;
62
        offset += localSizes[proci];
63
64
65

        if (offset < oldOffset)
        {
66
67
            FatalErrorInFunction
                << "Overflow : sum of sizes " << localSizes
68
69
70
71
                << " exceeds capability of label (" << labelMax
                << "). Please recompile with larger datatype for label."
                << exit(FatalError);
        }
72
        offsets_[proci+1] = offset;
73
74
75
76
    }
}


77
void Foam::globalIndex::reset(const label localSize)
78
{
79
80
81
    offsets_.resize(Pstream::nProcs()+1);

    labelList localSizes(Pstream::nProcs(), Zero);
82
    localSizes[Pstream::myProcNo()] = localSize;
83

84
85
86
    Pstream::gatherList(localSizes, Pstream::msgType());
    Pstream::scatterList(localSizes, Pstream::msgType());

87
    label offset = 0;
mattijs's avatar
mattijs committed
88
    offsets_[0] = 0;
89
    for (label proci = 0; proci < Pstream::nProcs(); ++proci)
90
    {
91
        const label oldOffset = offset;
92
        offset += localSizes[proci];
93
94
95

        if (offset < oldOffset)
        {
96
            FatalErrorInFunction
97
98
99
100
101
                << "Overflow : sum of sizes " << localSizes
                << " exceeds capability of label (" << labelMax
                << "). Please recompile with larger datatype for label."
                << exit(FatalError);
        }
102
        offsets_[proci+1] = offset;
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    }
}


// * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //

Foam::Istream& Foam::operator>>(Istream& is, globalIndex& gi)
{
    return is >> gi.offsets_;
}


Foam::Ostream& Foam::operator<<(Ostream& os, const globalIndex& gi)
{
    return os << gi.offsets_;
}


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