Lindorm Ray node

更新时间:
复制 MD 格式

The Lindorm Ray node in DataWorks lets you develop and periodically schedule Python tasks with the Ray distributed computing framework.

Overview

The Lindorm compute engine provides a computing service based on the Ray distributed computing framework. It is compatible with open-source Ray APIs and supports the Python programming model for distributed computing, machine learning, and data processing. With the Lindorm Ray node in DataWorks, you can write Python code, configure Ray submission commands, and develop, debug, and schedule Ray jobs on a periodic basis.

Limitations

  • Resource groups: Lindorm Ray nodes can run only on a Serverless resource group.

  • Language: Lindorm Ray nodes support only Python.

  • Execution: You cannot run single lines or blocks of code. You must submit the entire script for execution.

Prerequisites

  • You have created a Lindorm instance and associated it with a DataWorks workspace. For more information, see Associate a Lindorm computing resource.

  • You have created a Ray resource group in the Lindorm console. For more information, see Use a RAY resource group.

  • (Optional, required for RAM accounts) The RAM account for development has been added to the corresponding workspace and granted the Developer or Workspace Administrator role. The Workspace Administrator role has extensive permissions, so assign it with caution. For instructions on how to add members, see Add workspace members.

    Note

    If you are using a primary account, you can skip this step.

Create a Lindorm Ray node

For instructions on how to create a node, see Create a Lindorm Ray node.

Develop a Lindorm Ray node

Write Python code in the code editor and configure the job submission command in the Ray submission command area.

Example 1: Direct code execution

This example uses the Monte Carlo method to estimate the value of pi with a Lindorm Ray node.

Step 1: Write the Python code

In the code editor for the Lindorm Ray node, enter the following Python code. The code uses the Ray @ray.remote decorator to distribute the computation across multiple workers for parallel execution, and estimates pi through random sampling.

import ray
import random
import time
import sys

ray.init()

@ray.remote
def compute_points_in_circle(num_points: int) -> int:
    """
    Remote function: Randomly generates points in a unit square and counts how many fall within the unit circle.
    """
    inside = 0
    for _ in range(num_points):
        x = random.uniform(0, 1)
        y = random.uniform(0, 1)
        if x * x + y * y <= 1:
            inside += 1
    return inside

def estimate_pi(total_points: int, num_workers: int = 4) -> float:
    """
    Use Ray to estimate pi in parallel.
    """
    start_time = time.time()
    points_per_worker = total_points // num_workers

    print(f"Estimating pi with total points: {total_points:,}, workers: {num_workers}")

    # Submit multiple remote tasks.
    tasks = [compute_points_in_circle.remote(points_per_worker) for _ in range(num_workers)]

    # Collect the results.
    results = ray.get(tasks)
    total_inside = sum(results)

    # Calculate pi.
    pi_estimate = 4.0 * total_inside / total_points

    duration = time.time() - start_time

    print("=" * 60)
    print("Ray Sample: Calculating π using Monte Carlo Method")
    print("=" * 60)
    print(f"Total samples: {total_points:,}")
    print(f"Points inside circle: {total_inside:,}")
    print(f"Estimated π: {pi_estimate:.10f}")
    print(f"Time taken: {duration:.4f} seconds")
    print(f"Workers used: {num_workers}")
    print("=" * 60)
    print("Calculation complete!")

    return pi_estimate

if __name__ == "__main__":
    estimated_pi = estimate_pi(total_points=int(sys.argv[1]), num_workers=4)
    ray.shutdown()

Step 2: Configure the Ray submission command

In the Ray submission command area of the node, configure the following submission command.

ray job submit \ 
--working-dir "." \
-- python calculate_pi.py 1000000
Important
  • If the --working-dir parameter is set to a local path, the filename in the -- python parameter must match the node name or the name of a resource file referenced in the Python code.

  • If --working-dir is a local path, the platform automatically controls the values of the --address, --submission-id, and --working-dir parameters during job submission. If you manually configure these parameters in the submission command, the platform overrides your settings. However, if the value you provide for --working-dir is a remote path that starts with https://, such as an OSS path, it is not overridden.

Node content configuration

Configuration area

Parameter

Description

Code editor

Python code

Python code that uses the Ray framework. Ray APIs such as ray.init() and the @ray.remote decorator are supported.

Ray submission command

Submission command

The submission command for the Ray job. Format: ray job submit [options] -- python script.py [args].

runtime-env-json

Optional. Configures the runtime environment. For example, use the pip field to install additional Python dependency packages. Example: --runtime-env-json '{"pip": ["numpy", "pandas"]}'.

Parameters

Parameters to pass to your code. You can use the ${var} format to configure dynamic parameters.

Example 2: Code execution with resource files

If your Ray job uses multiple Python modules, upload them as DataWorks resource files and reference them in your main program with the ##@resource_reference syntax.

Step 1: Create and upload resource files

Create the following two Python resource files in DataWorks.

Resource file 1: circle_utils.py

"""
Utility module for calculating points within a circle.
"""
import random

def compute_points_in_circle(num_points: int) -> int:
    """
    Randomly generates points in a unit square and counts how many fall within the unit circle.
    """
    inside = 0
    for _ in range(num_points):
        x = random.uniform(0, 1)
        y = random.uniform(0, 1)
        if x * x + y * y <= 1:
            inside += 1
    return inside

Resource file 2: pi_estimator.py

"""
Utility module for estimating pi.
"""
import time

def estimate_pi(results, total_points: int) -> float:
    """
    Estimates pi based on the number of points inside the circle.
    """
    start_time = time.time()
    total_inside = sum(results)
    pi_estimate = 4.0 * total_inside / total_points
    duration = time.time() - start_time

    print(f"Total points: {total_points:,}")
    print(f"Points inside circle: {total_inside:,}")
    print(f"Estimated pi ≈ {pi_estimate:.6f}")
    print(f"Time taken: {duration:.2f} seconds")

    return pi_estimate

Step 2: Write the main program code

In the code editor of the Lindorm Ray node, reference the uploaded resource files with ##@resource_reference and write the main program code.

##@resource_reference{"circle_utils.py"}
##@resource_reference{"pi_estimator.py"}
"""
Main program to calculate pi in parallel by using Ray.
"""
import sys
import ray
from circle_utils import compute_points_in_circle
from pi_estimator import estimate_pi

@ray.remote
def compute_points_in_circle_remote(num_points: int) -> int:
    """
    Remote function: A wrapper for the calculation function.
    """
    return compute_points_in_circle(num_points)

if __name__ == "__main__":
    ray.init()

    total_points = int(sys.argv[1])
    num_workers = 4
    points_per_worker = total_points // num_workers

    print(f"Estimating pi with total points: {total_points:,}, workers: {num_workers}")

    # Submit multiple remote tasks.
    tasks = [compute_points_in_circle_remote.remote(points_per_worker) for _ in range(num_workers)]

    # Get the results.
    results = ray.get(tasks)

    # Calculate pi.
    estimated_pi = estimate_pi(results, total_points)

    # Shut down Ray.
    ray.shutdown()

Step 3: Configure the Ray submission command

In the Ray submission command area of the node, configure the following submission command.

ray job submit \
--working-dir "." \
-- python main.py 1000000
Note

When you reference DataWorks resource files, the platform automatically downloads them to the working directory. Setting --working-dir "." specifies the current working directory so that the referenced modules can be imported correctly.

Debug a Lindorm Ray node

  1. Configure run properties.

    In the Run Configuration pane on the right side of the node, configure the Compute Resource, Lindorm Resource Group, and Resource Group. The following table describes these parameters.

    Parameter

    Description

    Compute Resource

    Select the associated Lindorm compute resource.

    Lindorm Resource Group

    Select the Ray resource group you created in the Lindorm console.

    Resource Group

    Select a Serverless resource group that has passed the network connectivity test. Lindorm Ray nodes support only Serverless resource groups.

    Script parameters

    If you define variables using the ${Parameter Name} format when configuring the node, configure the Parameter name and Parameter Value in the Script Parameters section. The task then dynamically replaces the variables with their values at runtime. For more information, see Scheduling parameter sources and their expressions.

  2. Run and debug the node.

    To execute the node task, click Save and then click Run.

Next steps

  • Configure node scheduling: To run the node on a periodic schedule, configure the scheduling policy in the Scheduling pane on the right.

  • Deploy the node: To run the task in the production environment, deploy the node by clicking the image icon. Nodes run periodically only after they are deployed.