본문 바로가기
개발 공부/Language

[Python 분산/병렬처리] Ray 설치 및 실행, Cluster 구성 방법 (Windows)

by whatisthisblog 2022. 6. 6.
반응형

1. Python Ray 설치

분산/병렬 시스템 프레임워크인 Ray는 현재 Linux, MacOS, Windows 세개의 운영체제를 지원하며, Windows는 아직 베타 버전으로 Multi-Node Cluster 구성 및 사용에 잘 안되는 부분이 있을 수 있습니다.
본 포스팅에서는 Windows OS 에서 Ray 설치 및 단일 Ray Client 실행, Multi-Node Cluster Ray 실행 방법에 대해 알아보겠습니다.

 

Ray 공식 사이트 (설치 페이지)

Ray 관련 이슈 발생 시 참고 사이트 : https://github.com/ray-project/ray/issues

 

PIP를 사용한 설치 (latest version) 및 삭제

# minimal install
pip install -U ray

# To install Ray with support for the dashboard + cluster launcher
pip install -U "ray[default]"

# clean removal of previous install, otherwise version number may cause pip not to upgrade
pip uninstall -y ray

Ray의 Dashboard 및 Cluster 사용을 위해서는 "ray[default]" 로 설치가 필요합니다.

 

Ray는 Node 간 시스템 제어 및 메모리 공유를 위해 GCS (Global Control Storage)라는 기능을 사용하며, 이는 Redis 라는 In-Memory 기반 DBMS를 사용하기 때문에, Redis 설치가 필요합니다.

pip install redis

 

 

Ray가 정상적으로 설치되었는지는 아래 방법으로 확인이 가능합니다.

  • 명령프롬포트(cmd)로 Ray.exe 설치 위치인 Python 설치폴더의 Scripts 폴더로 이동하여 ray --version 명령어 입력

 

  • 파이썬 실행 후 import ray, ray 버전 및 실행 여부 확인
import ray

ray.__version__	# 설치된 ray 버전 확인
ray.init()	# Local Single Ray 실행

 

Wheel 설치 파일로 Ray 설치 (PIP 설치 불가시)

아래의 whl 설치 경로에서 파일 다운 후 pip install -U 파일 설치 경로 입력.

# minimal install
pip install -U LINK_TO_WHEEL.whl

# To install Ray with support for the dashboard + cluster launcher, run
pip install -U "ray[default] @ LINK_TO_WHEEL.whl"

 

OS 및 Python 버전별 .whl 설치 파일

Linux Python 3.9 MacOS Python 3.9 Windows Python 3.9
Linux Python 3.8 MacOS Python 3.8 Windows Python 3.8
Linux Python 3.7 MacOS Python 3.7 Windows Python 3.7
Linux Python 3.6 MacOS Python 3.6 Windows Python 3.6

 

 

2. Local에서 Single Ray 실행

사용자의 Local Device에서 python script 파일을 ray로 실행시키기 위해서는 script 시작 시 단순히 ray.init()를 통해 단일 ray node를 실행시킬 수 있습니다.
Ray의 Dashboard에서 할당된 Ray의 worker 수와 Device의 Resource 상태를 확인할 수 있습니다.

Ray 실행 시 사용하는 Resource는 별도 설정이 없으면 기본적으로 해당 Device의 Virtual CPU Core 수 및 감지된 GPU 수만큼 worker가 할당됩니다.
사용할 CPU Core 및 GPU 수를 제한하려면 ray.init(cpu_nums=x, gpu_nums=x)와 같이 실행하면 됩니다.

Windows에서는 ray 실행 시, Device IP를 자동으로 인식하지 못하고, localhost (127.0.0.1)로 실행됩니다.
따라서 IP 명시가 필요한 경우, ray.init(_node_ip_address='x.x.x.x')로 실행이 필요합니다.

Ray 실행 시 Dashboard가 자동으로 실행되며, 기본 주소는 http://localhost:8265 입니다.
Dashboard의 접속 주소와 Port 변경을 위해서는 ray.init(dashboard_host='x.x.x.x', dashboard_port=xxxx)로 실행해야 합니다.

import ray

# 현재 Device IP 명시, 사용할 Dashboard의 IP 및 Port 설정, 할당할 CPU Core 및 GPU 수 설정
ray.init(_node_ip_address='x.x.x.x', dashboard_host='x.x.x.x', dashboard_port=xxxx, cpu_nums=x, gpu_nums=x)


추가적인 Ray의 python API 는 https://docs.ray.io/en/latest/ray-core/package-ref.html#python-api 참고 부탁드립니다.

 

Dashboard에 들어가면 아래와 같이 현재 Ray가 실행 중인 PC의 Resource 정보와 Ray의 켜져있는 Worker들의 Resource 정보 확인이 가능합니다.

 

3. Local에서 Ray Cluster 구성하기

Local PC에서 Ray Node를 여러개 켜서 Cluster를 구성할 수 있고, 여러개의 Cluster를 운영할 수 있습니다.
Ray Cluster는 하나의 Head Node와 여러개의 Worker Node로 구성되며, Head Node 하나만으로 Cluster를 구성할 수 있습니다.
Cluster 기능 사용을 위해서는 ray 설치 시 "ray[default]"로 설치가 필요합니다.

 

Ray Cluster의 기본 구성인 Head Node(필수)와 Worker Node (출처 : Ray)

 

Ray를 Cluster로 구성하기 위해서는 먼저 커맨드 창에서 Ray Node 실행 및 구성이 필요합니다.
Ray Cluster는 한 개의 head node가 필수로 있어야하며, ray start --head 로 실행 가능합니다.
이때도 마찬가지로 Windows 환경에서는 Ray가 Local Device의 IP를 자동으로 인식하지 못 합니다.
따라서 --node-ip-address=x.x.x.x 옵션을 통해 IP를 명시해주어야 Cluster 구성 시 오류가 발생하지 않습니다.

 

Ray Cluster의 Head Node 를 구성하고 추가적으로 Worker Node를 연결하기 위해서는 ray start --address=<head node의 address>:<port> --node-ip-address=x.x.x.x 로 실행시키면 됩니다.
Head Node의 기본 접속 포트는 6379 이며, 다른 Port 사용을 원하면 Head Node 실행 시 --port=xxxx 명령어를 추가해 변경 가능합니다.

추가적인 Ray Comman-Line API는 https://docs.ray.io/en/latest/ray-core/package-ref.html#the-ray-command-line-api 를 참고 부탁드립니다.

이때 주의점은 Windows의 명령프롬포트로 명령어 실행 시 ip address에 따옴표를 제외한 주소만 입력해주어야 합니다.

 

Head Node에 Worker Node 하나를 붙여 Cluster를 구성하면 Dashboard에 아래와 같이 표시됩니다.
Cluster를 구성했을 때는 Worker가 아직 할당되지 않습니다.

 

 

이후 위 Cluster가 구성된 Device와 동일한 Device에서 Python Script를 Ray Cluster에 실행시키기 위해서는 Script에 ray.init(address='auto')를 추가해 Cluster에 추가시킬 수 있습니다.

 

4. 외부의 Ray Cluster에 접속하기

Ray Cluster의 가장 Powerful한 기능은 여러 서버를 묶어 Ray Cluster로 구성하고 Cluster 외부에서 Cluster에 접속해 대규모 병렬/분산처리가 가능하다는 것 입니다.

이때 외부(Python Script)에서 Ray Cluster에 접속하도록 하는 API를 Ray Client라고 합니다.

위의 3번 내용의 방법으로 여러 서버를 묶어 Ray Cluster를 구성한 후 Local PC의 python script에 ray.init(address='ray://<head_node_host>:<port>')를 추가합니다.

이때 기본 접속 포트는 10001 입니다.
위 외부 접속 포트를 변경하기 위해서는 Cluster의 Head Node 실행 시 --ray-client-server-port=xxxx 옵션을 추가해 변경 가능합니다.

Windows 환경에서 외부 Ray Cluster에 정상적으로 접속하기 위해서는 Head Node 실행 시 반드시 --node-ip-address=x.x.x.x 옵션을 추가해 Node의 IP 주소를 명시해주어야 합니다.

 

# You can run this code outside of the Ray cluster!
import ray

# Starting the Ray client. This connects to a remote Ray cluster.
# If you're using a version of Ray prior to 1.5, use the 1.4.1 example
# instead: https://docs.ray.io/en/releases-1.4.1/cluster/ray-client.html
ray.init("ray://<head_node_host>:10001")

# Normal Ray code follows
@ray.remote
def do_work(x):
    return x ** x

do_work.remote(2)
#....

 

Ray Client 실행 관련 추가 내용은 https://docs.ray.io/en/latest/cluster/ray-client.html#ray-client 를 참고 부탁드립니다.

 

 

Ray Client(Ray를 실행하는 Python Script)에서 외부 Ray Cluster의 Head Node에 접속하면 위와 같이 두개의 Wokrer가 연결됩니다.
(왜 두 개 인지 이유가 파악되면 내용 추가 예정입니다.)

이후 Ray Client에서 Ray가 실행되면 Ray Cluster의 모든 Node에 필요한 만큼 Worker가 자동으로 할당되어 병렬 및 분산처리가 진행됩니다.

 

Python Script API 실행 방법 비교

  • ray.init()
    Ray Cluster를 사용하지 않고 Local PC 에서 직접 하나의 Ray Node를 만들어서 실행.

  • ray.init(address='auto' or address='<localhost>:<port>')
    Local PC에 구성된 Ray Cluster에 접속.
    사전에 Ray Cluster가 구성되어 있어야 함 (ray start --head)
  • ray.init(address='ray://<head_node_host>:<port>')
    Remote 서버에 구성된 Ray Cluster에 접속.
    사전에 Ray Cluster가 구성되어 있어야 함 (ray start --head)
    기본 포트번호는 10001

 

 

반응형

'개발 공부 > Language' 카테고리의 다른 글

[Bitmap] C# 비트맵 픽셀 처리  (5) 2021.08.11

댓글