Ray 环境搭建和示例

本文介绍了如何在macOS环境下搭建Ray分布式计算环境,包括安装Python 3、创建虚拟环境、安装Ray 0.7.0版本,并详细阐述了以Cluster模式运行Ray的步骤,分别在Head和Node机器上进行配置。最后,通过运行hello_world.py示例验证环境的正确性。

Ray 环境搭建和示例

Ray 环境设置

本次实验采用 2 台 Mac,系统 macOS Mojave Version 10.14.3。

安装 Python 3

brew install python

安装 Python 虚拟环境

pip3 install virtualenv
cd ~/Tools
virtualenv -p /usr/local/bin/python3 git_ray_env
source ./git_ray/env/bin/active	

然后,查看虚拟环境 Python 的版本

$ python -V
Python 3.7.3

注意:确保两台机器 Python 版本一致。

安装 Ray

pip install ray

Ray 的版本是 0.7.0

以 Cluster 模式运行 Ray

两台机器IP分别为:192.168.1.6192.168.1.9

192.168.1.6 作为 Head,另一台作为 Node。

在 Head 机器上执行:

ray start --head --redis-port=6379

启动输入如下:

2019-06-23 15:04:18,601	INFO scripts.py:289 -- Using IP address 192.168.1.6 for this node.
2019-06-23 15:04:18,602	INFO node.py:497 -- Process STDOUT and STDERR is being redirected to /tmp/ray/session_2019-06-23_15-04-18_601521_88785/logs.
2019-06-23 15:04:18,710	INFO services.py:409 -- Waiting for redis server at 127.0.0.1:6379 to respond...
2019-06-23 15:04:18,835	INFO services.py:409 -- Waiting for redis server at 127.0.0.1:25445 to respond...
2019-06-23 15:04:18,840	INFO services.py:806 -- Starting Redis shard with 3.44 GB max memory.
2019-06-23 15:04:18,855	INFO node.py:511 -- Process STDOUT and STDERR is being redirected to /tmp/ray/session_2019-06-23_15-04-18_601521_88785/logs.
2019-06-23 15:04:18,856	INFO services.py:1441 -- Starting the Plasma object store with 5.15 GB memory using /tmp.
2019-06-23 15:04:18,877	INFO scripts.py:319 --
Started Ray on this node. You can add additional nodes to the cluster by calling

    ray start --redis-address 192.168.1.6:6379

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.1.6: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

在 Node 机器上执行:

ray start --redis-address=192.168.1.6:6379

启动输入如下:

2019-06-23 15:08:37,474	INFO services.py:409 -- Waiting for redis server at 192.168.1.6:6379 to respond...
2019-06-23 15:08:37,503	INFO scripts.py:363 -- Using IP address 192.168.1.9 for this node.
2019-06-23 15:08:37,551	INFO node.py:511 -- Process STDOUT and STDERR is being redirected to /tmp/ray/session_2019-06-23_15-04-18_601521_88785/logs.
2019-06-23 15:08:37,551	INFO services.py:1441 -- Starting the Plasma object store with 5.15 GB memory using /tmp.
2019-06-23 15:08:37,566	INFO scripts.py:371 --
Started Ray on this node. If you wish to terminate the processes that have been started, run

    ray stop

示例

hello_world.py

import ray

ray.init(redis_address="192.168.1.6:6379")

@ray.remote
def hello():
    return "Hello"

@ray.remote
def world():
    return "world!"

@ray.remote
def hello_world(a, b):
    return a + " " + b

a_id = hello.remote()
b_id = world.remote()
c_id = hello_world.remote(a_id, b_id)

hello = ray.get(c_id)

print(hello)

运行:python hello_world.py

输出:

Hello world!

参考

  1. 伯克利AI分布式框架Ray,兼容TensorFlow、PyTorch与MXNet
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值