Updated on 2026-11-06 GMT+08:00

Configuring Bin Packing

Before exploring bin packing, understand the two types of compute units in Ray—tasks and actors—and the nodes where they run.

Concept

Description

Task

The most basic stateless compute unit in Ray. A function's logic is executed once on a remote node, and its state is not retained after execution.

Actor

A long-running stateful execution unit. Class instances reside on remote nodes to share state across multiple invocations (such as model loading or counts).

Node

A server that provides compute resources and runs tasks and actors.

A single node can run multiple tasks and actors, which share its resources.

Bin packing concentrates tasks and actors (the two types of compute units) on feasible nodes with the highest resource utilization.

Bin Packing Overview

Bin packing is an optimization algorithm and an extension of the native Ray scheduling framework. It schedules tasks and actors (the two types of compute units). Bin packing aims to schedule as many tasks and actors as possible onto feasible nodes to maximize efficiency, thereby improving overall cluster resource utilization.

The primary goal of the Ray bin packing strategy is to concentrate tasks and actors onto feasible nodes with the highest resource utilization. By scoring nodes based on weighted resource utilization, this strategy selects the highest-scoring node to run new tasks, thereby maximizing resource packing density.

Unlike Ray's default hybrid scheduling strategy, the bin packing strategy does not spread tasks evenly across nodes. Instead, it fills active nodes first so idle nodes can be released to reduce costs.

Figure 1 Comparing hybrid scheduling and bin packing

How the Bin Packing Algorithm Works

When scoring nodes, the bin packing scheduler checks each node's resources (CPU, GPU, and memory) and calculates a score based on the weight of each resource type. The scoring process is as follows:

  1. Calculate the utilization of each resource type in sequence.

    Consider CPUs as an example. The CPU utilization on the candidate node is calculated as follows:

    util_cpu = 1 – (available – normal_task)/total, where:
    • util_cpu: CPU utilization. A higher value indicates a more heavily loaded node.
    • available: Available CPU resources on the current node (logical resource account balance).
    • normal_task: CPU resources that are allocated to normal tasks but are not yet in use because the tasks have not started running.
    • total: Total CPU resources on the current node.
  2. Multiply the utilization by the corresponding weight to calculate the resource's weighted score.
    score_cpu = util_cpu × CPU.weight

    A higher CPU weight gives CPU resources a larger share of the score. Higher resource utilization on a node results in a higher node score. The same rule applies to GPUs, memory, and object store memory.

  3. Use the highest weighted score among all resources as the node's final score. The candidate node with the highest score is selected to run the new task or actor.
    Score = max(score_cpu, score_gpu, score_mem, score_obj_mem)
    Score = max(util_r × weight_r)

Configuring a Bin Packing Strategy

You can configure the bin packing strategy by specifying scheduling_strategy="BINPACK" in your job script. For example, the text processing script ray-job-code.py configured with the bin packing strategy might look like this:

import ray
import random
import time
import sys

# Connect to a Ray cluster.
ray.init()

@ray.remote(scheduling_strategy="BINPACK")
def compute_points_in_circle(num_points: int) -> int:
    inside = 0
    for _ in range(num_points):
        x, y = random.random(), random.random()
        if x*x + y*y <= 1:
            inside += 1
    return inside

def estimate_pi(total_points: int, num_workers: int = 4) -> float:
    start = time.time()
    points_per_worker = total_points // num_workers
    # Run a task in distributed mode.
    tasks = [compute_points_in_circle.remote(points_per_worker) for _ in range(num_workers)]
    total_inside = sum(ray.get(tasks))
    pi = 4.0 * total_inside / total_points
    print(f"Estimated Pi: {pi:.6f}, Time taken: {time.time() - start:.2f} seconds")
    return pi

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

For details about submitting a job, see: