1/**
2 * Copyright (c) Glow Contributors. See CONTRIBUTORS file.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef GLOW_PARTITIONER_PARTITIONEROPTIMIZER_H
17#define GLOW_PARTITIONER_PARTITIONEROPTIMIZER_H
18
19#include "glow/Partitioner/PartitionerTypes.h"
20
21namespace glow {
22/// By using heuristic algorithm to move nodes among \p partitions, optimize the
23/// total communication cost of running a module and keep the memory usage of
24/// each partition within \p availableMemory.
25void optimizeCommunicationCost(NodeToFunctionMap &partitions,
26 FunctionToNodesMap &nodesSet, Module *mod,
27 uint64_t availableMemory);
28
29/// Combine partitions according to the following rules: Rule 1 :if all outside
30/// uses of the nodes in partition1 is in partition2, and the sum of memory
31/// consumption of partition1 and partition2 is less than availableMemory,
32/// combine partition1 and partition2.
33void partitionsCombine(NodeToFunctionMap &partitions,
34 FunctionToNodesMap &nodesSet, Module *mod,
35 uint64_t availableMemory);
36
37/// Assign the logicalDevice ID to each partition. The partitions with the same
38/// logicalDevice ID will be assigned on the same physical devices. E.g: there
39/// are 3 partitions node1(6GB) -> node2(14GB) -> node3(6GB). But we only have 2
40/// devices with 16GB memory. The logicalDevice ID assigning rules are:
41/// 1. For each type of backend, if the number of available physical devices is
42/// equal or larger than the number of partitions, different partitions are
43/// assigned with a different logicalDevice ID(i.e. each partition will be put
44/// on a different physical device for execution). E.g. we have 3 partitions
45/// node1->node2->node3, and 3 devices, the logicalDevice ID for each partition
46/// with be (node1, 0), (node2, 1), and (node3, 2).
47/// 2. For each type of backend, if the number of available physical devices is
48/// smaller than the number of partitions, and we can find a way to put all
49/// partitions on those pysical devices, this assignment will be applied and the
50/// partitions on the same physical devices will be assigned the same
51/// logicalDevice ID. E.g: there are 3 partitions node1(6GB) -> node2(14GB) ->
52/// node3(6GB). But we only have 2 devices with 16GB memory. The assignment will
53/// be : (node1, 0), (node2, 1), (node3, 0).
54/// 3. For each type of backend, if the number of available physical devices is
55/// smaller than the number of partitions, and we can not find a way to put all
56/// partitions on those pysical devices, we assign defferent partitions with
57/// different logicalDevice ID. E.g: there are 3 partitions node1(6GB) ->
58/// node2(14GB) -> node3(6GB). But we only have 1 device with 16GB memory. The
59/// assignment will be : (node1, 0), (node2, 1), (node3, 2). That is, even we
60/// can put node1 and node3 on the same device, we won't do it.
61DeviceIDTy
62assignLogicalDeviceID(NodeToFunctionMap &mapping,
63 const std::map<std::string, BackendInfo> &backendMap);
64} // namespace glow
65#endif // GLOW_PARTITIONER_PARTITIONEROPTIMIZER_H
66