準備條件(基於linux環境)
- 這裏部署2個節點的集羣,準備2個linux環境,ip爲
192.168.2.165 worker節點(內存限制,本地安裝只有一個CPU)
192.168.2.166 head節點(內存限制,本地安裝只有一個CPU)
-
安裝 python3
yum install python3 -
Ray學習文檔地址:
https://ray.readthedocs.io/en/latest/index.html -
github地址:
https://github.com/ray-project/ray
Ray簡介
Ray是用於構建和運行分佈式應用程序的快速,簡單的框架。
Ray與以下庫打包在一起,以加快機器學習的工作量:
Tune:可伸縮超參數調整
RLlib:可擴展的強化學習
Distributed Training :分佈式培訓
Ray的優勢
- 更省時,高效
普通的執行方式是這樣的:
Ray:
Ray工作原理
該圖片引用來自參考文獻1:
安裝Ray(在所有節點上安裝)
pip3 install ray
部署Ray集羣
1.部署head節點
手動在head節點上啓動:
firewall-cmd --add-port=6379/tcp
ray start --head --redis-port=6379
在這個節點上啓動Ray。您可以通過調用向集羣添加其他節點:
(Started Ray on this node. You can add additional nodes to the cluster by calling)
ray start --redis-address 192.168.2.220:6379
您可以通過運行Python將一個驅動程序連接到集羣
(from the node you wish to add. You can connect a driver to the cluster from Python by running
)
import ray
ray.init(redis_address="192.168.2.220:6379")
如果從不同的機器連接有問題,請檢查防火牆的配置是否正確。如果希望終止已啓動的進程,請運行
(If you have trouble connecting from a different machine, check that your firewall is configured properly. If you wish to terminate the processes that have been started, run
)
ray stop
2.部署worker節點
firewall-cmd --add-port=6379/tcp
ray start --redis-address 192.168.2.220:6379
日誌存放目錄:
/tmp/ray
例子-python Ray集羣
# -*- coding: utf-8 -*-
import time
import ray
ray.init(redis_address="192.168.2.220:6379")
def f1():
time.sleep(1)
@ray.remote
def f2():
time.sleep(1)
#以下需要十秒。
time1=time.time()
[ f1() for _ in range(50)]
print(time.time()-time1)
#以下需要一秒(假設系統至少有10個CPU)。
time2=time.time()
ray.get([ f2.remote() for _ in range(50)])
print(time.time()-time2)
參考文獻
https://blog.csdn.net/luanpeng825485697/article/details/88242020