|
| 1 | +/*! |
| 2 | + * \file CGraphPartitioning.hpp |
| 3 | + * \brief Headers for the classes realted to the algorithms that are used |
| 4 | + to divide the matrix acyclic graph into parallel partitions. |
| 5 | + * \author A. Raj |
| 6 | + * \version 8.2.0 "Harrier" |
| 7 | + * |
| 8 | + * SU2 Project Website: https://su2code.github.io |
| 9 | + * |
| 10 | + * The SU2 Project is maintained by the SU2 Foundation |
| 11 | + * (http://su2foundation.org) |
| 12 | + * |
| 13 | + * Copyright 2012-2025, SU2 Contributors (cf. AUTHORS.md) |
| 14 | + * |
| 15 | + * SU2 is free software; you can redistribute it and/or |
| 16 | + * modify it under the terms of the GNU Lesser General Public |
| 17 | + * License as published by the Free Software Foundation; either |
| 18 | + * version 2.1 of the License, or (at your option) any later version. |
| 19 | + * |
| 20 | + * SU2 is distributed in the hope that it will be useful, |
| 21 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | + * Lesser General Public License for more details. |
| 24 | + * |
| 25 | + * You should have received a copy of the GNU Lesser General Public |
| 26 | + * License along with SU2. If not, see <http://www.gnu.org/licenses/>. |
| 27 | + */ |
| 28 | + |
| 29 | +#pragma once |
| 30 | + |
| 31 | +#include "../geometry/CGeometry.hpp" |
| 32 | + |
| 33 | +/*! |
| 34 | + * \class CGraphPartitioning |
| 35 | + * \brief Abstract base class for defining graph partitioning algorithms. |
| 36 | + * \author A. Raj |
| 37 | + * |
| 38 | + * In order to use certain parallel algorithms in the solution process - |
| 39 | + * whether with linear solvers or preconditioners - we require the matrix |
| 40 | + * to be partitioned into certain parallel divisions. These maybe in the form |
| 41 | + * of levels, blocks, colors and so on. Since a number of different algorithms |
| 42 | + * can be used to split the graph, we've introduced a base class containing the |
| 43 | + * "Partition" member function from which child classes of the specific |
| 44 | + * algorithm can be derived. Currently, we are only using direct declarations |
| 45 | + * of the derived classes in the code. However, this method was chosen as it |
| 46 | + * allows us to pass different child class algorithms to a single implementation |
| 47 | + * of the function that requires it - similar to the CMatrixVectorProduct class. |
| 48 | + */ |
| 49 | + |
| 50 | +template <class ScalarType> |
| 51 | + |
| 52 | +class CGraphPartitioning { |
| 53 | + public: |
| 54 | + virtual ~CGraphPartitioning() = 0; |
| 55 | + virtual void Partition(vector<ScalarType>& pointList, vector<ScalarType>& partitionOffsets, |
| 56 | + vector<ScalarType>& chainPtr, unsigned short chainLimit) = 0; |
| 57 | +}; |
| 58 | +template <class ScalarType> |
| 59 | +CGraphPartitioning<ScalarType>::~CGraphPartitioning() {} |
| 60 | + |
| 61 | +template <class ScalarType> |
| 62 | + |
| 63 | +class CLevelScheduling final : public CGraphPartitioning<ScalarType> { |
| 64 | + private: |
| 65 | + ScalarType nPointDomain; |
| 66 | + CPoint* nodes; |
| 67 | + |
| 68 | + public: |
| 69 | + ScalarType nLevels; |
| 70 | + ScalarType maxLevelWidth; |
| 71 | + vector<ScalarType> levels; |
| 72 | + |
| 73 | + /*! |
| 74 | + * \brief constructor of the class |
| 75 | + * \param[in] nPointDomain_ref - number of points associated with the problem |
| 76 | + * \param[in] nodes_ref - represents the relationships between the points |
| 77 | + */ |
| 78 | + inline CLevelScheduling<ScalarType>(ScalarType nPointDomain_ref, CPoint* nodes_ref) |
| 79 | + : nPointDomain(nPointDomain_ref), nodes(nodes_ref) { |
| 80 | + nLevels = 0ul; |
| 81 | + maxLevelWidth = 0ul; |
| 82 | + } |
| 83 | + |
| 84 | + CLevelScheduling() = delete; // Removing default constructor |
| 85 | + |
| 86 | + /*! |
| 87 | + * \brief Divides the levels into groups of chains depending on the preset GPU block and warp size. |
| 88 | + * \param[in] levelOffsets - Represents the vector array containing the ordered list of starting rows of each level. |
| 89 | + * \param[in] chainPtr - Represents the vector array containing the ordered list of starting levels of each chain. |
| 90 | + * \param[in] rowsPerBlock - Represents the maximum number of rows that can be accomodated per CUDA block. |
| 91 | + */ |
| 92 | + void CalculateChain(vector<ScalarType> levelOffsets, vector<ScalarType>& chainPtr, unsigned short rowsPerBlock) { |
| 93 | + ScalarType levelWidth = 0; |
| 94 | + unsigned short chainLength = chainPtr.capacity(); |
| 95 | + |
| 96 | + /*This is not a magic number. We are simply initializing |
| 97 | + the point array with its first element that is always zero.*/ |
| 98 | + chainPtr.push_back(0); |
| 99 | + |
| 100 | + for (ScalarType iLevel = 0ul; iLevel < nLevels; iLevel++) { |
| 101 | + levelWidth = levelOffsets[iLevel + 1] - levelOffsets[iLevel]; |
| 102 | + maxLevelWidth = std::max(levelWidth, maxLevelWidth); |
| 103 | + |
| 104 | + if (levelWidth > rowsPerBlock) { |
| 105 | + if (chainPtr.back() != iLevel) { |
| 106 | + chainPtr.push_back(iLevel); |
| 107 | + } |
| 108 | + |
| 109 | + chainPtr.push_back(iLevel + 1); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + chainPtr.push_back(nLevels); |
| 114 | + } |
| 115 | + |
| 116 | + /*! |
| 117 | + * \brief Reorders the points according to the levels |
| 118 | + * \param[in] pointList - Ordered array that contains the list of all mesh points. |
| 119 | + * \param[in] inversePointList - Array utilized to access the index of each point in pointList. |
| 120 | + * \param[in] levelOffsets - Vector array containing the ordered list of starting rows of each level. |
| 121 | + */ |
| 122 | + void Reorder(vector<ScalarType>& pointList, vector<ScalarType>& inversePointList, vector<ScalarType> levelOffsets) { |
| 123 | + for (auto localPoint = 0ul; localPoint < nPointDomain; ++localPoint) { |
| 124 | + const auto globalPoint = pointList[localPoint]; |
| 125 | + inversePointList[levelOffsets[levels[localPoint]]++] = globalPoint; |
| 126 | + } |
| 127 | + |
| 128 | + pointList = std::move(inversePointList); |
| 129 | + } |
| 130 | + |
| 131 | + /*! |
| 132 | + * \brief Reorders the points according to the levels |
| 133 | + * \param[in] pointList - Ordered array that contains the list of all mesh points. |
| 134 | + * \param[in] levelOffsets - Vector array containing the ordered list of starting rows of each level. |
| 135 | + * \param[in] chainPtr - Represents the vector array containing the ordered list of starting levels of each chain. |
| 136 | + * \param[in] rowsPerBlock - Represents the maximum number of rows that can be accomodated per CUDA block. |
| 137 | + */ |
| 138 | + void Partition(vector<ScalarType>& pointList, vector<ScalarType>& levelOffsets, vector<ScalarType>& chainPtr, |
| 139 | + unsigned short rowsPerBlock) override { |
| 140 | + vector<ScalarType> inversePointList; |
| 141 | + inversePointList.reserve(nPointDomain); |
| 142 | + levels.reserve(nPointDomain); |
| 143 | + |
| 144 | + for (auto point = 0ul; point < nPointDomain; point++) { |
| 145 | + inversePointList[pointList[point]] = point; |
| 146 | + levels[point] = 0; |
| 147 | + } |
| 148 | + |
| 149 | + // Local Point - Ordering of the points post the RCM ordering |
| 150 | + // Global Point - Original order of the points before the RCM ordering |
| 151 | + |
| 152 | + for (auto localPoint = 0ul; localPoint < nPointDomain; ++localPoint) { |
| 153 | + const auto globalPoint = pointList[localPoint]; |
| 154 | + |
| 155 | + for (auto adjPoints = 0u; adjPoints < nodes->GetnPoint(globalPoint); adjPoints++) { |
| 156 | + const auto adjGlobalPoint = nodes->GetPoint(globalPoint, adjPoints); |
| 157 | + |
| 158 | + if (adjGlobalPoint < nPointDomain) { |
| 159 | + const auto adjLocalPoint = inversePointList[adjGlobalPoint]; |
| 160 | + |
| 161 | + if (adjLocalPoint < localPoint) { |
| 162 | + levels[localPoint] = std::max(levels[localPoint], levels[adjLocalPoint] + 1); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + nLevels = std::max(nLevels, levels[localPoint] + 1); |
| 168 | + } |
| 169 | + |
| 170 | + levelOffsets.resize(nLevels + 1); |
| 171 | + for (auto iPoint = 0ul; iPoint < nPointDomain; iPoint++) { |
| 172 | + ++levelOffsets[levels[iPoint] + 1]; |
| 173 | + } |
| 174 | + |
| 175 | + for (auto iLevel = 2ul; iLevel <= nLevels; ++iLevel) { |
| 176 | + levelOffsets[iLevel] += levelOffsets[iLevel - 1]; |
| 177 | + } |
| 178 | + |
| 179 | + Reorder(pointList, inversePointList, levelOffsets); |
| 180 | + |
| 181 | + CalculateChain(levelOffsets, chainPtr, rowsPerBlock); |
| 182 | + } |
| 183 | +}; |
0 commit comments