Ray 为构建分布式应用程序提供了一个简单、通用的 API。
通过以下方式完成了这一使命:
- 为构建和运行分布式应用程序提供简单的原语。
- 使最终用户能够并行化单个机器代码,而代码更改几乎为零。
- 在核心 Ray 之上包括一个由应用程序、库和工具组成的大型生态系统,以支持复杂的应用程序。
在Ray Core之上是几个用于解决机器学习问题的库:
以及用于将 ML 和分布式应用程序投入生产的库:
还有许多与 Ray 的社区集成,包括Dask、MARS、Modin、Horovod、Hugging Face、Scikit-learn等。
Ray 提供 Python、Java 和EXPERIMENTAL C++ API。Ray 使用任务(函数)和Actors(类)来允许您并行化您的代码。
Java代码:
import io.ray.api.ActorHandle; import io.ray.api.ObjectRef; import io.ray.api.Ray; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors;
public class RayDemo {
public static int square(int x) { return x * x; }
public static class Counter {
private int value = 0;
public void increment() { this.value += 1; }
public int read() { return this.value; } }
public static void main(String[] args) { // Intialize Ray runtime. Ray.init(); { List<ObjectRef<Integer>> objectRefList = new ArrayList<>(); // Invoke the `square` method 4 times remotely as Ray tasks. // The tasks will run in parallel in the background. for (int i = 0; i < 4; i++) { objectRefList.add(Ray.task(RayDemo::square, i).remote()); } // Get the actual results of the tasks with `get`. System.out.println(Ray.get(objectRefList)); // [0, 1, 4, 9] }
{ List<ActorHandle<Counter>> counters = new ArrayList<>(); // Create 4 actors from the `Counter` class. // They will run in remote worker processes. for (int i = 0; i < 4; i++) { counters.add(Ray.actor(Counter::new).remote()); }
// Invoke the `increment` method on each actor. // This will send an actor task to each remote actor. for (ActorHandle<Counter> counter : counters) { counter.task(Counter::increment).remote(); } // Invoke the `read` method on each actor, and print the results. List<ObjectRef<Integer>> objectRefList = counters.stream() .map(counter -> counter.task(Counter::read).remote()) .collect(Collectors.toList()); System.out.println(Ray.get(objectRefList)); // [1, 1, 1, 1] } } }
|