mirror of
https://github.com/wassname/ray.git
synced 2026-06-27 21:38:18 +08:00
[java] Remove multi-return API (#2724)
This commit is contained in:
committed by
Robert Nishihara
parent
dbba7f2a53
commit
4f4bea086a
@@ -22,10 +22,4 @@ To run a exercise case, set the ``RAY_CONFIG`` env variable and run the followin
|
||||
|
||||
`Exercise 4 <https://github.com/ray-project/ray/tree/master/java/tutorial/src/main/java/org/ray/exercise/Exercise04.java>`_: Use ``Ray.wait`` to ignore stragglers.
|
||||
|
||||
`Exercise 5 <https://github.com/ray-project/ray/tree/master/java/tutorial/src/main/java/org/ray/exercise/Exercise05.java>`_: Use multiple heterogeneous return values.
|
||||
|
||||
`Exercise 6 <https://github.com/ray-project/ray/tree/master/java/tutorial/src/main/java/org/ray/exercise/Exercise06.java>`_: Usage of ``RayList<T>``.
|
||||
|
||||
`Exercise 7 <https://github.com/ray-project/ray/tree/master/java/tutorial/src/main/java/org/ray/exercise/Exercise07.java>`_: Usage of ``RayMap<L, T>``.
|
||||
|
||||
`Exercise 8 <https://github.com/ray-project/ray/tree/master/java/tutorial/src/main/java/org/ray/exercise/Exercise08.java>`_: Actor Support of create Actor and call Actor method.
|
||||
`Exercise 5 <https://github.com/ray-project/ray/tree/master/java/tutorial/src/main/java/org/ray/exercise/Exercise08.java>`_: Actor Support of create Actor and call Actor method.
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package org.ray.exercise;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import java.util.List;
|
||||
import org.ray.api.Ray;
|
||||
import org.ray.api.RayList;
|
||||
import org.ray.api.RayObject;
|
||||
import org.ray.api.RayRemote;
|
||||
import org.ray.api.WaitResult;
|
||||
@@ -44,26 +45,24 @@ public class Exercise04 {
|
||||
public static void main(String[] args) throws Exception {
|
||||
try {
|
||||
Ray.init();
|
||||
RayObject<String> o1 = Ray.call(Exercise04::f1);
|
||||
RayObject<String> o2 = Ray.call(Exercise04::f2);
|
||||
RayObject<String> o3 = Ray.call(Exercise04::f3);
|
||||
RayList<String> rayList = new RayList<>();
|
||||
rayList.add(o1);
|
||||
rayList.add(o2);
|
||||
rayList.add(o3);
|
||||
List<RayObject<String>> waitList = ImmutableList.of(
|
||||
Ray.call(Exercise04::f1),
|
||||
Ray.call(Exercise04::f2),
|
||||
Ray.call(Exercise04::f3)
|
||||
);
|
||||
// Ray.wait will block until specified number of results are ready
|
||||
// or specified timeout have passed.
|
||||
// In this case, the result of f3 will be ignored.
|
||||
WaitResult<String> waitResult = Ray.wait(rayList, 2, 3000);
|
||||
RayList<String> readyOnes = waitResult.getReadyOnes();
|
||||
RayList<String> remainOnes = waitResult.getRemainOnes();
|
||||
WaitResult<String> waitResult = Ray.wait(waitList, 2, 3000);
|
||||
List<RayObject<String>> readyOnes = waitResult.getReadyOnes();
|
||||
List<RayObject<String>> remainOnes = waitResult.getRemainOnes();
|
||||
System.out.println("Number of readyOnes: " + readyOnes.size());
|
||||
for (int i = 0; i < readyOnes.size(); i++) {
|
||||
System.out.println("The value of readyOnes " + i + " is " + readyOnes.get(i));
|
||||
System.out.println("The value of readyOnes " + i + " is " + readyOnes.get(i).get());
|
||||
}
|
||||
System.out.println("Number of remainOnes: " + remainOnes.size());
|
||||
for (int i = 0; i < remainOnes.size(); i++) {
|
||||
System.out.println("The value of remainOnes " + i + " is " + remainOnes.get(i));
|
||||
System.out.println("The value of remainOnes " + i + " is " + remainOnes.get(i).get());
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
|
||||
@@ -1,26 +1,26 @@
|
||||
package org.ray.exercise;
|
||||
|
||||
import org.ray.api.Ray;
|
||||
import org.ray.api.RayActor;
|
||||
import org.ray.api.RayObject;
|
||||
import org.ray.api.RayRemote;
|
||||
import org.ray.api.returns.MultipleReturns2;
|
||||
import org.ray.api.returns.RayObjects2;
|
||||
import org.ray.core.RayRuntime;
|
||||
|
||||
/**
|
||||
* Use multiple heterogeneous return values
|
||||
* Java worker support at most four heterogeneous return values,
|
||||
* To call such remote functions, use {@code Ray.call_X} as follows.
|
||||
* Show usage of actors.
|
||||
*/
|
||||
public class Exercise05 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
Ray.init();
|
||||
RayObjects2<Integer, String> refs = Ray.call_2(Exercise05::sayMultiRet);
|
||||
Integer obj1 = refs.r0().get();
|
||||
String obj2 = refs.r1().get();
|
||||
System.out.println(obj1);
|
||||
System.out.println(obj2);
|
||||
// `Ray.create` creates an actor instance.
|
||||
RayActor<Adder> adder = Ray.create(Adder.class);
|
||||
// Use `Ray.call(actor, parameters)` to call an actor method.
|
||||
RayObject<Integer> result1 = Ray.call(Adder::add, adder, 1);
|
||||
System.out.println(result1.get());
|
||||
RayObject<Integer> result2 = Ray.call(Adder::add, adder, 10);
|
||||
System.out.println(result2.get());
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
} finally {
|
||||
@@ -29,10 +29,20 @@ public class Exercise05 {
|
||||
}
|
||||
|
||||
/**
|
||||
* A remote function that returns multiple heterogeneous values.
|
||||
* An example actor.
|
||||
*/
|
||||
// `@RayRemote` annotation also converts a normal class to an actor.
|
||||
@RayRemote
|
||||
public static MultipleReturns2<Integer, String> sayMultiRet() {
|
||||
return new MultipleReturns2<Integer, String>(123, "123");
|
||||
public static class Adder {
|
||||
|
||||
public Adder() {
|
||||
sum = 0;
|
||||
}
|
||||
|
||||
public int add(int n) {
|
||||
return sum += n;
|
||||
}
|
||||
|
||||
private int sum;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
package org.ray.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.ray.api.Ray;
|
||||
import org.ray.api.RayList;
|
||||
import org.ray.api.RayObject;
|
||||
import org.ray.api.RayRemote;
|
||||
import org.ray.core.RayRuntime;
|
||||
|
||||
/**
|
||||
* Show usage of RayList.
|
||||
* RayList is a list of {@code RayObject}s, inherited from {@code List}.
|
||||
* It can be used as the type for both return values and parameters.
|
||||
*
|
||||
*/
|
||||
public class Exercise06 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
Ray.init();
|
||||
// The result is a `RayList`.
|
||||
RayList<Integer> ns = Ray.call_n(Exercise06::sayList, 10, 10);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
RayObject<Integer> obj = ns.Get(i);
|
||||
System.out.println(obj.get());
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
} finally {
|
||||
RayRuntime.getInstance().cleanUp();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A remote function that returns a list.
|
||||
*/
|
||||
@RayRemote
|
||||
public static List<Integer> sayList(Integer count) {
|
||||
ArrayList<Integer> rets = new ArrayList<>();
|
||||
for (int i = 0; i < count; i++) {
|
||||
rets.add(i);
|
||||
}
|
||||
return rets;
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package org.ray.exercise;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.ray.api.Ray;
|
||||
import org.ray.api.RayMap;
|
||||
import org.ray.api.RayObject;
|
||||
import org.ray.api.RayRemote;
|
||||
import org.ray.core.RayRuntime;
|
||||
|
||||
/**
|
||||
* Show usage of RayMap.
|
||||
* {@code RayMap} is a map of {@code RayObject}s, inherited from {@code Map}.
|
||||
* It can be used as the type for both return values and parameters.
|
||||
*/
|
||||
public class Exercise07 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
Ray.init();
|
||||
RayMap<Integer, String> ns = Ray.call_n(Exercise07::sayMap,
|
||||
Arrays.asList(1, 2, 4, 3), "n_futures_");
|
||||
for (Map.Entry<Integer, RayObject<String>> ne : ns.EntrySet()) {
|
||||
Integer key = ne.getKey();
|
||||
RayObject<String> obj = ne.getValue();
|
||||
System.out.println(obj.get());
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
} finally {
|
||||
RayRuntime.getInstance().cleanUp();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A remote function that returns a map.
|
||||
*/
|
||||
@RayRemote()
|
||||
public static Map<Integer, String> sayMap(Collection<Integer> ids, String prefix) {
|
||||
Map<Integer, String> ret = new HashMap<>();
|
||||
for (int id : ids) {
|
||||
ret.put(id, prefix + id);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package org.ray.exercise;
|
||||
|
||||
import org.ray.api.Ray;
|
||||
import org.ray.api.RayActor;
|
||||
import org.ray.api.RayObject;
|
||||
import org.ray.api.RayRemote;
|
||||
import org.ray.core.RayRuntime;
|
||||
|
||||
/**
|
||||
* Show usage of actors.
|
||||
*/
|
||||
public class Exercise08 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
Ray.init();
|
||||
// `Ray.create` creates an actor instance.
|
||||
RayActor<Adder> adder = Ray.create(Adder.class);
|
||||
// Use `Ray.call(actor, parameters)` to call an actor method.
|
||||
RayObject<Integer> result1 = Ray.call(Adder::add, adder, 1);
|
||||
System.out.println(result1.get());
|
||||
RayObject<Integer> result2 = Ray.call(Adder::add, adder, 10);
|
||||
System.out.println(result2.get());
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
} finally {
|
||||
RayRuntime.getInstance().cleanUp();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An example actor.
|
||||
*/
|
||||
// `@RayRemote` annotation also converts a normal class to an actor.
|
||||
@RayRemote
|
||||
public static class Adder {
|
||||
|
||||
public Adder() {
|
||||
sum = 0;
|
||||
}
|
||||
|
||||
public int add(int n) {
|
||||
return sum += n;
|
||||
}
|
||||
|
||||
private int sum;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user