• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""
16management api
17"""
18import mindspore.communication.management as D
19
20
21def has_raise_error(func, x):
22    try:
23        # pylint:disable=eval-used
24        if x is None:
25            func()
26        else:
27            func(x)
28        print("x:{}".format(x))
29    except (TypeError, ValueError, RuntimeError):
30        return True
31    else:
32        return False
33
34
35def create_backend(name):
36    D.Backend(name)
37
38
39def get_group_size_int(group):
40    D.get_group_size(group)
41
42
43def create_group0(x):
44    D.GlobalComm.BACKEND = D.Backend.HCCL
45    D.create_group('0-1', x)
46
47
48def create_group1(x):
49    D.GlobalComm.BACKEND = D.Backend.HCCL
50    D.create_group('0-1', x)
51
52
53def create_group2(x):
54    D.GlobalComm.BACKEND = D.Backend.HCCL
55    D.create_group('0-1', x)
56
57
58def create_group3(x):
59    D.GlobalComm.BACKEND = D.Backend.UNDEFINED
60    D.create_group('0-1', x)
61
62
63def create_group4(x):
64    D.GlobalComm.BACKEND = D.Backend.HCCL
65    D.create_group('0-1', x)
66
67
68def get_world_rank_from_group_rank0():
69    D.GlobalComm.BACKEND = D.Backend.HCCL
70    D.get_world_rank_from_group_rank(D.HCCL_WORLD_COMM_GROUP, 0)
71
72
73def get_world_rank_from_group_rank1():
74    D.GlobalComm.BACKEND = D.Backend.HCCL
75    D.get_world_rank_from_group_rank('0-1', '0')
76
77
78def get_world_rank_from_group_rank2():
79    D.GlobalComm.BACKEND = D.Backend.UNDEFINED
80    D.get_world_rank_from_group_rank('0-1', 0)
81
82
83def get_group_rank_from_world_rank0():
84    D.GlobalComm.BACKEND = D.Backend.HCCL
85    D.get_group_rank_from_world_rank(0, D.HCCL_WORLD_COMM_GROUP)
86
87
88def get_group_rank_from_world_rank1():
89    D.GlobalComm.BACKEND = D.Backend.HCCL
90    D.get_group_rank_from_world_rank('0', '0-1')
91
92
93def get_group_rank_from_world_rank2():
94    D.GlobalComm.BACKEND = D.Backend.UNDEFINED
95    D.get_group_rank_from_world_rank(0, '0-1')
96
97
98def destroy_group0(x):
99    D.GlobalComm.BACKEND = D.Backend.UNDEFINED
100    D.destroy_group(x)
101
102
103def destroy_group1():
104    D.GlobalComm.BACKEND = D.Backend.HCCL
105    D.destroy_group(D.HCCL_WORLD_COMM_GROUP)
106
107
108def destroy_group2(x):
109    D.GlobalComm.BACKEND = D.Backend.HCCL
110    D.destroy_group(x)
111
112
113def test_raise_error_funcs():
114    """test raise error funcs"""
115    assert has_raise_error(create_backend, 123) is True
116    assert has_raise_error(create_backend, 'hccl') is False
117    assert has_raise_error(create_backend, 'nccl') is False
118    assert has_raise_error(get_group_size_int, 123) is True
119    assert has_raise_error(create_group0, (0, 1)) is True
120    assert has_raise_error(create_group1, [0]) is False
121    assert has_raise_error(create_group2, [0, 0, 1]) is True
122    assert has_raise_error(create_group3, [0, 1]) is True
123    assert has_raise_error(create_group4, [0, 1]) is True
124    assert has_raise_error(get_world_rank_from_group_rank0, None) is True
125    assert has_raise_error(get_world_rank_from_group_rank1, None) is True
126    assert has_raise_error(get_world_rank_from_group_rank2, None) is True
127    assert has_raise_error(get_group_rank_from_world_rank0, None) is True
128    assert has_raise_error(get_group_rank_from_world_rank1, None) is True
129    assert has_raise_error(get_group_rank_from_world_rank2, None) is True
130    assert has_raise_error(destroy_group0, '0-1') is True
131    assert has_raise_error(destroy_group1, None) is True
132    assert has_raise_error(destroy_group2, '0-1') is False
133
134
135def test_group_funs():
136    D.GlobalComm.BACKEND = D.Backend.HCCL
137    assert D.get_group_size('2-abcd') == 2
138    assert D.get_world_rank_from_group_rank('0-1', 0) == 0
139    assert D.get_group_rank_from_world_rank(0, '0-1') == 0
140