mirror of
https://github.com/wassname/ray.git
synced 2026-07-03 10:36:19 +08:00
[java] Remove multi-return API (#2724)
This commit is contained in:
committed by
Robert Nishihara
parent
dbba7f2a53
commit
4f4bea086a
@@ -1,181 +0,0 @@
|
||||
package org.ray.api.experiment.mr;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.ray.api.Ray;
|
||||
import org.ray.api.RayList;
|
||||
import org.ray.api.RayObject;
|
||||
import org.ray.api.RayRemote;
|
||||
|
||||
/**
|
||||
* mimic the MapReduce interface atop of Ray API (in memory version).
|
||||
*/
|
||||
public class MemoryMapReduce<TInputT, TMapKeyT, TMapValueT, TReduceValueT> {
|
||||
|
||||
public List<Pair<TMapKeyT, TMapValueT>> map(TInputT input) throws Exception {
|
||||
throw new Exception("not implemented");
|
||||
}
|
||||
|
||||
public TReduceValueT reduce(TMapKeyT k, List<TMapValueT> values) throws Exception {
|
||||
throw new Exception("not implemented");
|
||||
}
|
||||
|
||||
//
|
||||
// main logic to execute this map-reduce with remote mappers and reducers
|
||||
//
|
||||
// @param inputs - given input file segments each containing List<TInput>
|
||||
// @return output file segments each containing SortedMap<TMapKey, TReduceValue>
|
||||
//
|
||||
public SortedMap<TMapKeyT, TReduceValueT> run(List<List<TInputT>> inputs, int mapperCount,
|
||||
Integer reducerCount) {
|
||||
// start all mappers
|
||||
ArrayList<RayList<SortedMap<TMapKeyT, List<TMapValueT>>>> mappers = new ArrayList<>();
|
||||
|
||||
int inputCountPerMap = inputs.size() / mapperCount;
|
||||
int index = 0;
|
||||
for (int i = 0; i < mapperCount; i++) {
|
||||
if (index >= inputs.size()) {
|
||||
break;
|
||||
}
|
||||
|
||||
List<List<TInputT>> perMapInputs = new ArrayList<>();
|
||||
for (int j = 0; j < inputCountPerMap && index < inputs.size(); j++) {
|
||||
perMapInputs.add(inputs.get(index++));
|
||||
}
|
||||
|
||||
mappers.add(Ray.call_n(MemoryMapReduce::internalMap, reducerCount, reducerCount, perMapInputs,
|
||||
this.getClass().getName()));
|
||||
}
|
||||
|
||||
// BSP barrier for all mappers to be finished
|
||||
// this is unnecessary as later on we call map.get() to wait for mappers to be completed
|
||||
// Ray.wait(mappers.toArray(new RayObject<?>[0]), mappers.size(), 0);
|
||||
|
||||
// start all reducers
|
||||
ArrayList<RayObject<SortedMap<TMapKeyT, TReduceValueT>>> reducers = new ArrayList<>();
|
||||
for (int i = 0; i < reducerCount; i++) {
|
||||
// collect states from mappers for this reducer
|
||||
RayList<SortedMap<TMapKeyT, List<TMapValueT>>> fromMappers = new RayList();
|
||||
for (int j = 0; j < mapperCount; j++) {
|
||||
assert (mappers.get(j).size() == reducerCount);
|
||||
fromMappers.add(mappers.get(j).Get(i));
|
||||
}
|
||||
|
||||
// start this reducer with given input
|
||||
reducers.add(Ray.call(MemoryMapReduce::internalReduce,
|
||||
(List<SortedMap<TMapKeyT, List<TMapValueT>>>) fromMappers, this.getClass().getName()));
|
||||
}
|
||||
|
||||
// BSP barrier for all reducers to be finished
|
||||
// this is unnecessary coz we will call reducer.get() to wait for their completion
|
||||
// Ray.wait(reducers.toArray(new RayObject<?>[0]), reducers.size(), 0);
|
||||
|
||||
// collect outputs
|
||||
TreeMap<TMapKeyT, TReduceValueT> outputs = new TreeMap<>();
|
||||
for (RayObject<SortedMap<TMapKeyT, TReduceValueT>> r : reducers) {
|
||||
r.get().forEach(outputs::put);
|
||||
}
|
||||
return outputs;
|
||||
}
|
||||
|
||||
//
|
||||
// given a set of input files, output another set of files for reducers
|
||||
//
|
||||
// @param inputs - for each input file, it contains List<TInput>
|
||||
// @return %recuderCount% number of files for the reducers numbering from 0 to reducerCount - 1.
|
||||
// for each output file, it contains SortedMap<TMapKey, List<TMapValue>>
|
||||
//
|
||||
@RayRemote
|
||||
public static <TInputT, TMapKeyT, TMapValueT, TReduceValueT> List<SortedMap<TMapKeyT,
|
||||
List<TMapValueT>>> internalMap(
|
||||
Integer reducerCount,
|
||||
List<List<TInputT>> inputs,
|
||||
String mrClassName) {
|
||||
MemoryMapReduce<TInputT, TMapKeyT, TMapValueT, TReduceValueT> mr;
|
||||
try {
|
||||
mr = (MemoryMapReduce<TInputT, TMapKeyT, TMapValueT, TReduceValueT>) Class
|
||||
.forName(mrClassName).getConstructors()[0].newInstance();
|
||||
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
|
||||
| InvocationTargetException | SecurityException | ClassNotFoundException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
ArrayList<SortedMap<TMapKeyT, List<TMapValueT>>> out = new ArrayList<>();
|
||||
for (int i = 0; i < reducerCount; i++) {
|
||||
out.add(new TreeMap<>());
|
||||
}
|
||||
|
||||
for (List<TInputT> inputSeg : inputs) {
|
||||
for (TInputT input : inputSeg) {
|
||||
try {
|
||||
List<Pair<TMapKeyT, TMapValueT>> result = mr.map(input);
|
||||
for (Pair<TMapKeyT, TMapValueT> pr : result) {
|
||||
int reducerIndex = Math.abs(pr.getKey().hashCode()) % reducerCount;
|
||||
out.get(reducerIndex).computeIfAbsent(pr.getKey(), k -> new ArrayList<>());
|
||||
out.get(reducerIndex).get(pr.getKey()).add(pr.getValue());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
//public static class Helper {
|
||||
//
|
||||
// given a set of input sets from all mappers, performance merge sort and apply reducer function
|
||||
//
|
||||
// @param inputs each file contains SortedMap<TMapKey, List<TMapValue>>
|
||||
// @return an output file contains SortedMap<TMapKey, TReduceValue>
|
||||
//
|
||||
@RayRemote
|
||||
public static <TInputT, TMapKeyT, TMapValueT, TReduceValueT> SortedMap<TMapKeyT, TReduceValueT>
|
||||
internalReduce(
|
||||
List<SortedMap<TMapKeyT, List<TMapValueT>>> inputs,
|
||||
String mrClassName) {
|
||||
MemoryMapReduce<TInputT, TMapKeyT, TMapValueT, TReduceValueT> mr;
|
||||
try {
|
||||
mr = (MemoryMapReduce<TInputT, TMapKeyT, TMapValueT, TReduceValueT>) Class
|
||||
.forName(mrClassName).getConstructors()[0].newInstance();
|
||||
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
|
||||
| InvocationTargetException | SecurityException | ClassNotFoundException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
// merge inputs from many mappers
|
||||
TreeMap<TMapKeyT, List<TMapValueT>> minputs = new TreeMap<>();
|
||||
for (SortedMap<TMapKeyT, List<TMapValueT>> input : inputs) {
|
||||
for (Map.Entry<TMapKeyT, List<TMapValueT>> entry : input.entrySet()) {
|
||||
if (!minputs.containsKey(entry.getKey())) {
|
||||
minputs.put(entry.getKey(), new ArrayList<>());
|
||||
}
|
||||
minputs.get(entry.getKey()).addAll(entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
// reduce
|
||||
TreeMap<TMapKeyT, TReduceValueT> out = new TreeMap<>();
|
||||
for (Map.Entry<TMapKeyT, List<TMapValueT>> entry : minputs.entrySet()) {
|
||||
try {
|
||||
out.put(entry.getKey(), mr.reduce(entry.getKey(), entry.getValue()));
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
// }
|
||||
}
|
||||
@@ -6,7 +6,6 @@ import org.junit.runner.RunWith;
|
||||
import org.ray.api.Ray;
|
||||
import org.ray.api.RayObject;
|
||||
import org.ray.api.RayRemote;
|
||||
import org.ray.util.logger.RayLog;
|
||||
|
||||
/**
|
||||
* Hello world.
|
||||
@@ -15,37 +14,25 @@ import org.ray.util.logger.RayLog;
|
||||
public class HelloWorldTest {
|
||||
|
||||
@RayRemote
|
||||
public static String sayHello() {
|
||||
String ret = "he";
|
||||
ret += "llo";
|
||||
RayLog.rapp.info("real say hello");
|
||||
//throw new RuntimeException("+++++++++++++++++++++hello exception");
|
||||
return ret;
|
||||
private static String hello() {
|
||||
return "hello";
|
||||
}
|
||||
|
||||
@RayRemote
|
||||
public static String sayWorld() {
|
||||
String ret = "world";
|
||||
ret += "!";
|
||||
return ret;
|
||||
private static String world() {
|
||||
return "world!";
|
||||
}
|
||||
|
||||
@RayRemote
|
||||
public static String merge(String hello, String world) {
|
||||
private static String merge(String hello, String world) {
|
||||
return hello + "," + world;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String helloWorld = sayHelloWorld();
|
||||
RayLog.rapp.info(helloWorld);
|
||||
public void testHelloWorld() {
|
||||
RayObject<String> hello = Ray.call(HelloWorldTest::hello);
|
||||
RayObject<String> world = Ray.call(HelloWorldTest::world);
|
||||
String helloWorld = Ray.call(HelloWorldTest::merge, hello, world).get();
|
||||
Assert.assertEquals("hello,world!", helloWorld);
|
||||
Assert.assertTrue(Ray.call(TypesTest::sayBool).get());
|
||||
}
|
||||
|
||||
public String sayHelloWorld() {
|
||||
RayObject<String> hello = Ray.call(HelloWorldTest::sayHello);
|
||||
RayObject<String> world = Ray.call(HelloWorldTest::sayWorld);
|
||||
return Ray.call(HelloWorldTest::merge, hello, world).get();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,14 +4,13 @@ import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.lang.invoke.SerializedLambda;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.ray.api.funcs.RayFunc_0_1;
|
||||
import org.ray.api.funcs.RayFunc_1_1;
|
||||
import org.ray.api.funcs.RayFunc_3_1;
|
||||
import org.ray.api.funcs.RayFunc0;
|
||||
import org.ray.api.funcs.RayFunc1;
|
||||
import org.ray.api.funcs.RayFunc3;
|
||||
import org.ray.util.MethodId;
|
||||
import org.ray.util.logger.RayLog;
|
||||
|
||||
@@ -36,7 +35,7 @@ public class LambdaUtilsTest {
|
||||
}
|
||||
}
|
||||
|
||||
public static <T0, T1, T2, R0> void testRemoteLambdaParse(RayFunc_3_1<T0, T1, T2, R0> f, int n,
|
||||
public static <T0, T1, T2, R0> void testRemoteLambdaParse(RayFunc3<T0, T1, T2, R0> f, int n,
|
||||
boolean forceNew, boolean debug)
|
||||
throws Exception {
|
||||
if (debug) {
|
||||
@@ -52,7 +51,7 @@ public class LambdaUtilsTest {
|
||||
(end - start) / n));
|
||||
}
|
||||
|
||||
public static <T0, T1, T2, R0> void testRemoteLambdaSerde(RayFunc_3_1<T0, T1, T2, R0> f, int n,
|
||||
public static <T0, T1, T2, R0> void testRemoteLambdaSerde(RayFunc3<T0, T1, T2, R0> f, int n,
|
||||
boolean de, boolean debug)
|
||||
throws Exception {
|
||||
if (debug) {
|
||||
@@ -66,7 +65,7 @@ public class LambdaUtilsTest {
|
||||
out.close();
|
||||
if (de) {
|
||||
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()));
|
||||
RayFunc_3_1 def = (RayFunc_3_1) in.readObject();
|
||||
RayFunc3 def = (RayFunc3) in.readObject();
|
||||
in.close();
|
||||
if (debug) {
|
||||
RayLog.core.info("de#" + def.getClass().getName());
|
||||
@@ -80,28 +79,28 @@ public class LambdaUtilsTest {
|
||||
(end - start) / n));
|
||||
}
|
||||
|
||||
public static void testCall0(RayFunc_0_1 f) {
|
||||
public static void testCall0(RayFunc0 f) {
|
||||
MethodId mid = MethodId.fromSerializedLambda(f);
|
||||
RayLog.core.info(mid.toString());
|
||||
Assert.assertEquals(mid.load(), CALL0);
|
||||
Assert.assertTrue(mid.isStatic);
|
||||
}
|
||||
|
||||
public static <T, R> void testCall1(RayFunc_1_1<T, R> f, T t) {
|
||||
public static <T, R> void testCall1(RayFunc1<T, R> f, T t) {
|
||||
MethodId mid = MethodId.fromSerializedLambda(f);
|
||||
RayLog.core.info(mid.toString());
|
||||
Assert.assertEquals(mid.load(), CALL1);
|
||||
Assert.assertTrue(mid.isStatic);
|
||||
}
|
||||
|
||||
public static <T, R> void testCall2(RayFunc_1_1<T, R> f) {
|
||||
public static <T, R> void testCall2(RayFunc1<T, R> f) {
|
||||
MethodId mid = MethodId.fromSerializedLambda(f);
|
||||
RayLog.core.info(mid.toString());
|
||||
Assert.assertEquals(mid.load(), CALL2);
|
||||
Assert.assertTrue(!mid.isStatic);
|
||||
}
|
||||
|
||||
public static <T0, T1, T2, R0> void testCall3(RayFunc_3_1<T0, T1, T2, R0> f) {
|
||||
public static <T0, T1, T2, R0> void testCall3(RayFunc3<T0, T1, T2, R0> f) {
|
||||
MethodId mid = MethodId.fromSerializedLambda(f);
|
||||
RayLog.core.info(mid.toString());
|
||||
Assert.assertEquals(mid.load(), CALL3);
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
package org.ray.api.test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.SortedMap;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.ray.api.experiment.mr.MemoryMapReduce;
|
||||
|
||||
/**
|
||||
* test the MapReduce interface.
|
||||
*/
|
||||
@RunWith(MyRunner.class)
|
||||
public class MemoryWordCountTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
List<List<String>> iinputs = new ArrayList<>();
|
||||
List<String> inputs = new ArrayList<>();
|
||||
inputs.add("1 3 5 7 9");
|
||||
inputs.add("0 2 4 6 8");
|
||||
iinputs.add(inputs);
|
||||
|
||||
inputs = new ArrayList<>();
|
||||
inputs.add("1 2 3 4 5 6 7 8 9 0");
|
||||
inputs.add("1 3 5 7 9");
|
||||
iinputs.add(inputs);
|
||||
|
||||
inputs = new ArrayList<>();
|
||||
inputs.add("1 2 3 4 5 6 7 8 9 0");
|
||||
inputs.add("0 2 4 6 8");
|
||||
iinputs.add(inputs);
|
||||
|
||||
inputs = new ArrayList<>();
|
||||
inputs.add("1 2 3 4 5 6 7 8 9 0");
|
||||
inputs.add("1 3 5 7 9");
|
||||
inputs.add("0 2 4 6 8");
|
||||
iinputs.add(inputs);
|
||||
|
||||
MemoryWordCount wc = new MemoryWordCount();
|
||||
SortedMap<String, Integer> result = wc.run(iinputs, 2, 2);
|
||||
|
||||
Assert.assertEquals(6, (int) result.get("0"));
|
||||
Assert.assertEquals(6, (int) result.get("1"));
|
||||
Assert.assertEquals(6, (int) result.get("2"));
|
||||
Assert.assertEquals(6, (int) result.get("3"));
|
||||
Assert.assertEquals(6, (int) result.get("4"));
|
||||
Assert.assertEquals(6, (int) result.get("5"));
|
||||
Assert.assertEquals(6, (int) result.get("6"));
|
||||
Assert.assertEquals(6, (int) result.get("7"));
|
||||
Assert.assertEquals(6, (int) result.get("8"));
|
||||
Assert.assertEquals(6, (int) result.get("9"));
|
||||
}
|
||||
|
||||
public static class MemoryWordCount extends MemoryMapReduce<String, String, Integer, Integer> {
|
||||
|
||||
public List<Pair<String, Integer>> map(String input) {
|
||||
ArrayList<Pair<String, Integer>> counts = new ArrayList<>();
|
||||
for (String s : input.split(" ")) {
|
||||
counts.add(Pair.of(s, 1));
|
||||
}
|
||||
return counts;
|
||||
}
|
||||
|
||||
public Integer reduce(String k, List<Integer> values) {
|
||||
return values.size();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,16 @@
|
||||
package org.ray.api.test;
|
||||
|
||||
|
||||
import java.lang.invoke.SerializedLambda;
|
||||
import java.lang.reflect.Method;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.ray.api.funcs.RayFunc_3_1;
|
||||
import org.ray.util.LambdaUtils;
|
||||
import org.ray.api.funcs.RayFunc3;
|
||||
import org.ray.util.MethodId;
|
||||
import org.ray.util.logger.RayLog;
|
||||
|
||||
public class MethodIdTest {
|
||||
|
||||
public static <T0, T1, T2, R0> MethodId fromLambda(RayFunc_3_1<T0, T1, T2, R0> f) {
|
||||
public static <T0, T1, T2, R0> MethodId fromLambda(RayFunc3<T0, T1, T2, R0> f) {
|
||||
MethodId mid = MethodId.fromSerializedLambda(f, true);
|
||||
return mid;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package org.ray.api.test;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.ray.api.Ray;
|
||||
import org.ray.api.RayList;
|
||||
import org.ray.api.RayObject;
|
||||
import org.ray.api.WaitResult;
|
||||
import org.ray.util.logger.RayLog;
|
||||
@@ -24,12 +26,9 @@ public class RayMethodsTest {
|
||||
RayObject<String> s2Id = Ray.put(String.valueOf("World!"));
|
||||
RayObject<Object> n1Id = Ray.put(null);
|
||||
|
||||
RayList<String> waitIds = new RayList<>();
|
||||
waitIds.add(s1Id);
|
||||
waitIds.add(s2Id);
|
||||
WaitResult<String> readys = Ray.wait(waitIds, 2);
|
||||
WaitResult<String> res = Ray.wait(ImmutableList.of(s1Id, s2Id), 2);
|
||||
|
||||
List<String> ss = readys.getReadyOnes().get();
|
||||
List<String> ss = res.getReadyOnes().stream().map(RayObject::get).collect(Collectors.toList());
|
||||
int i1 = i1Id.get();
|
||||
double f1 = f1Id.get();
|
||||
Object n1 = n1Id.get();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.ray.api.test;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Assume;
|
||||
import org.junit.Test;
|
||||
@@ -55,7 +56,7 @@ public class ResourcesManagementTest {
|
||||
|
||||
// This is a case that can't satisfy required resources.
|
||||
final RayObject<Integer> result2 = Ray.call(ResourcesManagementTest::echo2, 200);
|
||||
WaitResult<Integer> waitResult = Ray.wait(result2, 1000);
|
||||
WaitResult<Integer> waitResult = Ray.wait(ImmutableList.of(result2), 1, 1000);
|
||||
|
||||
Assert.assertEquals(0, waitResult.getReadyOnes().size());
|
||||
Assert.assertEquals(1, waitResult.getRemainOnes().size());
|
||||
@@ -72,7 +73,7 @@ public class ResourcesManagementTest {
|
||||
// This is a case that can't satisfy required resources.
|
||||
RayActor<ResourcesManagementTest.Echo2> echo2 = Ray.create(Echo2.class);
|
||||
final RayObject<Integer> result2 = Ray.call(Echo2::echo, echo2, 100);
|
||||
WaitResult<Integer> waitResult = Ray.wait(result2, 1000);
|
||||
WaitResult<Integer> waitResult = Ray.wait(ImmutableList.of(result2), 1, 1000);
|
||||
|
||||
Assert.assertEquals(0, waitResult.getReadyOnes().size());
|
||||
Assert.assertEquals(1, waitResult.getRemainOnes().size());
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
package org.ray.api.test;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.ray.api.Ray;
|
||||
|
||||
/**
|
||||
* local test in IDE, for class lazy load.
|
||||
*/
|
||||
@RunWith(MyRunner.class)
|
||||
public class TwoClassTest {
|
||||
|
||||
@Test
|
||||
public void testLocal() {
|
||||
Assert.assertTrue(
|
||||
Ray.call(TypesTest::sayBool).get());//call function in other class which may not be loaded
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,202 +1,82 @@
|
||||
package org.ray.api.test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.ray.api.Ray;
|
||||
import org.ray.api.RayList;
|
||||
import org.ray.api.RayMap;
|
||||
import org.ray.api.RayObject;
|
||||
import org.ray.api.RayRemote;
|
||||
import org.ray.api.returns.MultipleReturns2;
|
||||
import org.ray.api.returns.RayObjects2;
|
||||
|
||||
/**
|
||||
* types test.
|
||||
* Test returning different types.
|
||||
*/
|
||||
@RunWith(MyRunner.class)
|
||||
public class TypesTest {
|
||||
|
||||
@RayRemote
|
||||
public static int sayInt() {
|
||||
private static int testInt() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@RayRemote
|
||||
public static byte sayByte() {
|
||||
private static byte testByte() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@RayRemote
|
||||
public static short sayShort() {
|
||||
private static short testShort() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@RayRemote
|
||||
public static long sayLong() {
|
||||
private static long testLong() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@RayRemote
|
||||
public static double sayDouble() {
|
||||
private static double testDouble() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@RayRemote
|
||||
public static float sayFloat() {
|
||||
private static float testFloat() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@RayRemote
|
||||
public static boolean sayBool() {
|
||||
private static boolean testBool() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@RayRemote
|
||||
public static Object sayReference() {
|
||||
return "object";
|
||||
private static String testString() {
|
||||
return "foo";
|
||||
}
|
||||
|
||||
@RayRemote
|
||||
public static MultipleReturns2<Integer, String> sayReferences() {
|
||||
return new MultipleReturns2<>(123, "123");
|
||||
private static List<Integer> testList() {
|
||||
return ImmutableList.of(1, 2, 3);
|
||||
}
|
||||
|
||||
@RayRemote
|
||||
public static Map<Integer, String> sayReferencesN(Collection<Integer> userReturnIds,
|
||||
String prefix, String suffix) {
|
||||
Map<Integer, String> ret = new HashMap<>();
|
||||
for (Integer returnid : userReturnIds) {
|
||||
ret.put(returnid, prefix + returnid + suffix);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@RayRemote
|
||||
public static List<Integer> sayArray(Integer returnCount) {
|
||||
ArrayList<Integer> rets = new ArrayList<>();
|
||||
for (int i = 0; i < returnCount; i++) {
|
||||
rets.add(i);
|
||||
}
|
||||
return rets;
|
||||
}
|
||||
|
||||
@RayRemote
|
||||
public static Integer sayRayFuture() {
|
||||
return 123;
|
||||
}
|
||||
|
||||
@RayRemote
|
||||
public static MultipleReturns2<Integer, String> sayRayFutures() {
|
||||
return new MultipleReturns2<>(123, "123");
|
||||
}
|
||||
|
||||
@RayRemote
|
||||
public static Map<Integer, String> sayRayFuturesN(
|
||||
Collection<Integer/*user's custom return_id*/> userReturnIds,
|
||||
String prefix) {
|
||||
Map<Integer, String> ret = new HashMap<>();
|
||||
for (int id : userReturnIds) {
|
||||
ret.put(id, prefix + id);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@RayRemote
|
||||
public static int sayReadRayList(List<Integer> ints) {
|
||||
int sum = 0;
|
||||
for (Integer i : ints) {
|
||||
sum += i;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
@RayRemote
|
||||
public static int sayReadRayMap(Map<String, Integer> ints) {
|
||||
int sum = 0;
|
||||
for (Integer i : ints.values()) {
|
||||
sum += i;
|
||||
}
|
||||
return sum;
|
||||
private static Map<String, Integer> testMap() {
|
||||
return ImmutableMap.of("1", 1, "2", 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
sayTypes();
|
||||
}
|
||||
|
||||
public void sayTypes() {
|
||||
|
||||
Assert.assertEquals(1, (int) Ray.call(TypesTest::sayInt).get());
|
||||
Assert.assertEquals(1, (byte) Ray.call(TypesTest::sayByte).get());
|
||||
Assert.assertEquals(1, (short) Ray.call(TypesTest::sayShort).get());
|
||||
Assert.assertEquals(1, (long) Ray.call(TypesTest::sayLong).get());
|
||||
Assert.assertEquals(1.0, Ray.call(TypesTest::sayDouble).get(), 0.0);
|
||||
Assert.assertEquals(1.0f, Ray.call(TypesTest::sayFloat).get(), 0.0);
|
||||
Assert.assertEquals(true, Ray.call(TypesTest::sayBool).get());
|
||||
Assert.assertEquals("object", Ray.call(TypesTest::sayReference).get());
|
||||
|
||||
RayObjects2<Integer, String> refs = Ray.call_2(TypesTest::sayReferences);
|
||||
Assert.assertEquals(123, (int) refs.r0().get());
|
||||
Assert.assertEquals("123", refs.r1().get());
|
||||
|
||||
RayMap<Integer, String> futureRs = Ray.call_n(TypesTest::sayReferencesN,
|
||||
Arrays.asList(1, 2, 4, 3), "n_refs_", "_suffix");
|
||||
for (Entry<Integer, RayObject<String>> fne : futureRs.EntrySet()) {
|
||||
Assert.assertEquals(fne.getValue().get(), "n_refs_" + fne.getKey() + "_suffix");
|
||||
}
|
||||
|
||||
RayMap<Integer, String> futureRs2 = Ray.call_n(TypesTest::sayReferencesN,
|
||||
Arrays.asList(1), "n_refs_", "_suffix");
|
||||
for (Entry<Integer, RayObject<String>> fne : futureRs2.EntrySet()) {
|
||||
Assert.assertEquals(fne.getValue().get(), "n_refs_" + fne.getKey() + "_suffix");
|
||||
}
|
||||
|
||||
RayObject<Integer> future = Ray.call(TypesTest::sayRayFuture);
|
||||
Assert.assertEquals(123, (int) future.get());
|
||||
RayObjects2<Integer, String> futures = Ray.call_2(TypesTest::sayRayFutures);
|
||||
Assert.assertEquals(123, (int) futures.r0().get());
|
||||
Assert.assertEquals("123", futures.r1().get());
|
||||
RayMap<Integer, String> futureNs = Ray.call_n(TypesTest::sayRayFuturesN,
|
||||
Arrays.asList(1, 2, 4, 3), "n_futures_");
|
||||
for (Entry<Integer, RayObject<String>> fne : futureNs.EntrySet()) {
|
||||
Assert.assertEquals(fne.getValue().get(), "n_futures_" + fne.getKey());
|
||||
}
|
||||
|
||||
RayList<Integer> ns = Ray.call_n(TypesTest::sayArray, 10, 10);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Assert.assertEquals(i, (int) ns.Get(i).get());
|
||||
}
|
||||
|
||||
RayList<Integer> ns2 = Ray.call_n(TypesTest::sayArray, 1, 1);
|
||||
Assert.assertEquals(0, (int) ns2.Get(0).get());
|
||||
|
||||
RayObject<List<Integer>> ns3 = Ray.call(TypesTest::sayArray, 1);
|
||||
Assert.assertEquals(0, (int) ns3.get().get(0));
|
||||
|
||||
RayList<Integer> ints = new RayList<>();
|
||||
ints.add(Ray.call(TypesTest::sayInt));
|
||||
ints.add(Ray.call(TypesTest::sayInt));
|
||||
ints.add(Ray.call(TypesTest::sayInt));
|
||||
// TODO: when RayParameters.use_remote_lambda is on, we have to explicitly
|
||||
// cast RayList and RayMap to List and map explicitly, so that the parameter
|
||||
// types of the lambdas can be correctly deducted.
|
||||
RayObject<Integer> collection = Ray.call(TypesTest::sayReadRayList, (List<Integer>) ints);
|
||||
Assert.assertEquals(3, (int) collection.get());
|
||||
|
||||
RayMap<String, Integer> namedInts = new RayMap();
|
||||
namedInts.put("a", Ray.call(TypesTest::sayInt));
|
||||
namedInts.put("b", Ray.call(TypesTest::sayInt));
|
||||
namedInts.put("c", Ray.call(TypesTest::sayInt));
|
||||
RayObject<Integer> collection2 = Ray
|
||||
.call(TypesTest::sayReadRayMap, (Map<String, Integer>) namedInts);
|
||||
Assert.assertEquals(3, (int) collection2.get());
|
||||
Assert.assertEquals(1, (int) Ray.call(TypesTest::testInt).get());
|
||||
Assert.assertEquals(1, (byte) Ray.call(TypesTest::testByte).get());
|
||||
Assert.assertEquals(1, (short) Ray.call(TypesTest::testShort).get());
|
||||
Assert.assertEquals(1, (long) Ray.call(TypesTest::testLong).get());
|
||||
Assert.assertEquals(1.0, Ray.call(TypesTest::testDouble).get(), 0.0);
|
||||
Assert.assertEquals(1.0f, Ray.call(TypesTest::testFloat).get(), 0.0);
|
||||
Assert.assertEquals(true, Ray.call(TypesTest::testBool).get());
|
||||
Assert.assertEquals("foo", Ray.call(TypesTest::testString).get());
|
||||
Assert.assertEquals(ImmutableList.of(1, 2, 3), Ray.call(TypesTest::testList).get());
|
||||
Assert.assertEquals(ImmutableMap.of("1", 1, "2", 2), Ray.call(TypesTest::testMap).get());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
package org.ray.api.test;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.ray.api.RayObject;
|
||||
import org.ray.api.RayRemote;
|
||||
import org.ray.api.UniqueID;
|
||||
import org.ray.api.funcs.RayFunc_1_1;
|
||||
import org.ray.core.RayRuntime;
|
||||
import org.ray.core.UniqueIdHelper;
|
||||
|
||||
@RunWith(MyRunner.class)
|
||||
public class UIdTest {
|
||||
|
||||
|
||||
@RayRemote
|
||||
public static String hi(Integer i) {
|
||||
return "hi" + i;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
UniqueID tid = UniqueIdHelper.nextTaskId(0xdeadbeefL);
|
||||
UniqueIdHelper.setTest(tid, true);
|
||||
System.out.println("Tested task id = " + tid);
|
||||
RayFunc_1_1<Integer, String> f = UIdTest::hi;
|
||||
RayObject<String> result = new RayObject<>(
|
||||
RayRuntime.getInstance().call(
|
||||
tid,
|
||||
RayFunc_1_1.class,
|
||||
f,
|
||||
1, 1
|
||||
).getObjs()[0].getId()
|
||||
);
|
||||
System.out.println("Tested task return object id = " + result.getId());
|
||||
Assert.assertEquals("hi1", result.get());
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
package org.ray.api.test;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import java.util.List;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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;
|
||||
@@ -33,16 +34,14 @@ public class WaitTest {
|
||||
RayObject<String> obj1 = Ray.call(WaitTest::hi);
|
||||
RayObject<String> obj2 = Ray.call(WaitTest::delayHi);
|
||||
|
||||
RayList<String> waitfor = new RayList<>();
|
||||
waitfor.add(obj1);
|
||||
waitfor.add(obj2);
|
||||
List<RayObject<String>> waitfor = ImmutableList.of(obj1, obj2);
|
||||
WaitResult<String> waitResult = Ray.wait(waitfor, 2, 2 * 1000);
|
||||
RayList<String> readys = waitResult.getReadyOnes();
|
||||
List<RayObject<String>> readys = waitResult.getReadyOnes();
|
||||
|
||||
if (!readys.isEmpty()) {
|
||||
Assert.assertEquals(1, waitResult.getReadyOnes().size());
|
||||
Assert.assertEquals(1, waitResult.getRemainOnes().size());
|
||||
Assert.assertEquals("hi", readys.get(0));
|
||||
Assert.assertEquals("hi", readys.get(0).get());
|
||||
} else {
|
||||
Assert.assertEquals(0, waitResult.getReadyOnes().size());
|
||||
Assert.assertEquals(2, waitResult.getRemainOnes().size());
|
||||
|
||||
Reference in New Issue
Block a user