Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
61b2bb57
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
61b2bb57
编写于
8月 14, 2023
作者:
张
张春乔
提交者:
GitHub
8月 14, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[xdoctest] reformat example code with google style in No. 231 (#56213)
上级
8e58fa57
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
117 addition
and
117 deletion
+117
-117
python/paddle/distributed/parallel_with_gloo.py
python/paddle/distributed/parallel_with_gloo.py
+117
-117
未找到文件。
python/paddle/distributed/parallel_with_gloo.py
浏览文件 @
61b2bb57
...
@@ -54,44 +54,44 @@ def gloo_init_parallel_env(rank_id, rank_num, server_endpoint):
...
@@ -54,44 +54,44 @@ def gloo_init_parallel_env(rank_id, rank_num, server_endpoint):
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle
>>>
import paddle
import multiprocessing
>>>
import multiprocessing
from contextlib import closing
>>>
from contextlib import closing
import socket
>>>
import socket
port_set = set()
>>>
port_set = set()
def find_free_port():
>>>
def find_free_port():
def _free_port():
...
def _free_port():
with closing(socket.socket(socket.AF_INET,
...
with closing(socket.socket(socket.AF_INET,
socket.SOCK_STREAM)) as s:
...
socket.SOCK_STREAM)) as s:
s.bind(('', 0))
...
s.bind(('', 0))
return s.getsockname()[1]
...
return s.getsockname()[1]
while True:
...
while True:
port = _free_port()
...
port = _free_port()
if port not in port_set:
...
if port not in port_set:
port_set.add(port)
...
port_set.add(port)
return port
...
return port
def test_gloo_init(id, rank_num, server_endpoint):
>>>
def test_gloo_init(id, rank_num, server_endpoint):
paddle.distributed.gloo_init_parallel_env(
...
paddle.distributed.gloo_init_parallel_env(
id, rank_num, server_endpoint)
...
id, rank_num, server_endpoint)
def test_gloo_init_with_multiprocess(num_of_ranks):
>>>
def test_gloo_init_with_multiprocess(num_of_ranks):
jobs = []
...
jobs = []
server_endpoint = "127.0.0.1:%s" % (find_free_port())
...
server_endpoint = "127.0.0.1:%s" % (find_free_port())
for id in range(num_of_ranks):
...
for id in range(num_of_ranks):
p = multiprocessing.Process(
...
p = multiprocessing.Process(
target=test_gloo_init,
...
target=test_gloo_init,
args=(id, num_of_ranks, server_endpoint))
...
args=(id, num_of_ranks, server_endpoint))
jobs.append(p)
...
jobs.append(p)
p.start()
...
p.start()
for proc in jobs:
...
for proc in jobs:
proc.join()
...
proc.join()
if __name__ == '__main__':
>>>
if __name__ == '__main__':
# Arg: number of ranks (processes)
...
# Arg: number of ranks (processes)
test_gloo_init_with_multiprocess(2)
...
test_gloo_init_with_multiprocess(2)
"""
"""
assert
(
assert
(
...
@@ -149,45 +149,45 @@ def gloo_barrier():
...
@@ -149,45 +149,45 @@ def gloo_barrier():
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle
>>>
import paddle
import multiprocessing
>>>
import multiprocessing
from contextlib import closing
>>>
from contextlib import closing
import socket
>>>
import socket
port_set = set()
>>>
port_set = set()
def find_free_port():
>>>
def find_free_port():
def _free_port():
...
def _free_port():
with closing(socket.socket(socket.AF_INET,
...
with closing(socket.socket(socket.AF_INET,
socket.SOCK_STREAM)) as s:
...
socket.SOCK_STREAM)) as s:
s.bind(('', 0))
...
s.bind(('', 0))
return s.getsockname()[1]
...
return s.getsockname()[1]
while True:
...
while True:
port = _free_port()
...
port = _free_port()
if port not in port_set:
...
if port not in port_set:
port_set.add(port)
...
port_set.add(port)
return port
...
return port
def test_gloo_barrier(id, rank_num, server_endpoint):
>>>
def test_gloo_barrier(id, rank_num, server_endpoint):
paddle.distributed.gloo_init_parallel_env(
...
paddle.distributed.gloo_init_parallel_env(
id, rank_num, server_endpoint)
...
id, rank_num, server_endpoint)
paddle.distributed.gloo_barrier()
...
paddle.distributed.gloo_barrier()
def test_gloo_barrier_with_multiprocess(num_of_ranks):
>>>
def test_gloo_barrier_with_multiprocess(num_of_ranks):
jobs = []
...
jobs = []
server_endpoint = "127.0.0.1:%s" % (find_free_port())
...
server_endpoint = "127.0.0.1:%s" % (find_free_port())
for id in range(num_of_ranks):
...
for id in range(num_of_ranks):
p = multiprocessing.Process(
...
p = multiprocessing.Process(
target=test_gloo_barrier,
...
target=test_gloo_barrier,
args=(id, num_of_ranks, server_endpoint))
...
args=(id, num_of_ranks, server_endpoint))
jobs.append(p)
...
jobs.append(p)
p.start()
...
p.start()
for proc in jobs:
...
for proc in jobs:
proc.join()
...
proc.join()
if __name__ == '__main__':
>>>
if __name__ == '__main__':
# Arg: number of ranks (processes)
...
# Arg: number of ranks (processes)
test_gloo_barrier_with_multiprocess(2)
...
test_gloo_barrier_with_multiprocess(2)
"""
"""
assert
_global_gloo_ctx
is
not
None
,
"gloo context is not initialzed."
assert
_global_gloo_ctx
is
not
None
,
"gloo context is not initialzed."
...
@@ -207,46 +207,46 @@ def gloo_release():
...
@@ -207,46 +207,46 @@ def gloo_release():
Examples:
Examples:
.. code-block:: python
.. code-block:: python
import paddle
>>>
import paddle
import multiprocessing
>>>
import multiprocessing
from contextlib import closing
>>>
from contextlib import closing
import socket
>>>
import socket
port_set = set()
>>>
port_set = set()
def find_free_port():
>>>
def find_free_port():
def _free_port():
...
def _free_port():
with closing(socket.socket(socket.AF_INET,
...
with closing(socket.socket(socket.AF_INET,
socket.SOCK_STREAM)) as s:
...
socket.SOCK_STREAM)) as s:
s.bind(('', 0))
...
s.bind(('', 0))
return s.getsockname()[1]
...
return s.getsockname()[1]
while True:
...
while True:
port = _free_port()
...
port = _free_port()
if port not in port_set:
...
if port not in port_set:
port_set.add(port)
...
port_set.add(port)
return port
...
return port
def test_gloo_release(id, rank_num, server_endpoint):
>>>
def test_gloo_release(id, rank_num, server_endpoint):
paddle.distributed.gloo_init_parallel_env(
...
paddle.distributed.gloo_init_parallel_env(
id, rank_num, server_endpoint)
...
id, rank_num, server_endpoint)
paddle.distributed.gloo_barrier()
...
paddle.distributed.gloo_barrier()
paddle.distributed.gloo_release()
...
paddle.distributed.gloo_release()
def test_gloo_release_with_multiprocess(num_of_ranks):
>>>
def test_gloo_release_with_multiprocess(num_of_ranks):
jobs = []
...
jobs = []
server_endpoint = "127.0.0.1:%s" % (find_free_port())
...
server_endpoint = "127.0.0.1:%s" % (find_free_port())
for id in range(num_of_ranks):
...
for id in range(num_of_ranks):
p = multiprocessing.Process(
...
p = multiprocessing.Process(
target=test_gloo_release,
...
target=test_gloo_release,
args=(id, num_of_ranks, server_endpoint))
...
args=(id, num_of_ranks, server_endpoint))
jobs.append(p)
...
jobs.append(p)
p.start()
...
p.start()
for proc in jobs:
...
for proc in jobs:
proc.join()
...
proc.join()
if __name__ == '__main__':
>>>
if __name__ == '__main__':
# Arg: number of ranks (processes)
...
# Arg: number of ranks (processes)
test_gloo_release_with_multiprocess(2)
...
test_gloo_release_with_multiprocess(2)
"""
"""
if
_global_gloo_ctx
is
not
None
:
if
_global_gloo_ctx
is
not
None
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录