赞
踩
功能:
主要用于创建指定数量的子进程,并行地执行目标函数。它与torch.distributed.launch的区别是,torch.distributed.launch启动的每个进程, 都运行整个Python 脚本。
主要参数有:
import torch.multiprocessing as mp
def worker(rank, world_size):
print(f"Hello from rank {rank} out of {world_size} ranks")
if __name__ == "__main__":
world_size = 4 # 启动四个进程
mp.spawn(worker, args=(world_size,), nprocs=world_size, join=True)
运行这个示例时,你会看到如下输出(进程的顺序可能不同):
Hello from rank 0 out of 4 ranks
Hello from rank 1 out of 4 ranks
Hello from rank 2 out of 4 ranks
Hello from rank 3 out of 4 ranks
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。