打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Ray 环境搭建和示例

文章目录

  • Ray 环境搭建和示例
    • Ray 环境设置
      • 安装 Python 3
      • 安装 Python 虚拟环境
      • 安装 Ray
      • 以 Cluster 模式运行 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,601INFO scripts.py:289 -- Using IP address 192.168.1.6 for this node.
2019-06-23 15:04:18,602INFO 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,710INFO services.py:409 -- Waiting for redis server at 127.0.0.1:6379 to respond...
2019-06-23 15:04:18,835INFO services.py:409 -- Waiting for redis server at 127.0.0.1:25445 to respond...
2019-06-23 15:04:18,840INFO services.py:806 -- Starting Redis shard with 3.44 GB max memory.
2019-06-23 15:04:18,855INFO 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,856INFO services.py:1441 -- Starting the Plasma object store with 5.15 GB memory using /tmp.
2019-06-23 15:04:18,877INFO 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,474INFO services.py:409 -- Waiting for redis server at 192.168.1.6:6379 to respond...
2019-06-23 15:08:37,503INFO scripts.py:363 -- Using IP address 192.168.1.9 for this node.
2019-06-23 15:08:37,551INFO 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,551INFO services.py:1441 -- Starting the Plasma object store with 5.15 GB memory using /tmp.
2019-06-23 15:08:37,566INFO 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
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Ray
6.面向大数据 AI的语言
取代Python多进程!伯克利开源分布式框架Ray
Redis服务之常用配置(三)
redis 集群(文档整理)
令人抓狂的redis和rediscluster Python驱动包的安装
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服