Skip to content

Commit

Permalink
Cross-language invocation Part 1: Java calling Python functions and a…
Browse files Browse the repository at this point in the history
…ctors (ray-project#4166)
  • Loading branch information
raulchen authored Mar 21, 2019
1 parent 828dc08 commit d03999d
Show file tree
Hide file tree
Showing 28 changed files with 872 additions and 228 deletions.
141 changes: 139 additions & 2 deletions java/api/src/main/java/org/ray/api/RayCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

package org.ray.api;

import org.ray.api.function.RayFunc;
import org.ray.api.function.RayFunc0;
import org.ray.api.function.RayFunc1;
import org.ray.api.function.RayFunc2;
Expand All @@ -11,7 +10,6 @@
import org.ray.api.function.RayFunc5;
import org.ray.api.function.RayFunc6;
import org.ray.api.options.ActorCreationOptions;
import org.ray.api.options.BaseTaskOptions;
import org.ray.api.options.CallOptions;

/**
Expand Down Expand Up @@ -2312,4 +2310,143 @@ public static <T0, T1, T2, T3, T4, T5, A> RayActor<A> createActor(RayFunc6<T0, T
Object[] args = new Object[]{t0, t1, t2, t3, t4, t5};
return Ray.internal().createActor(f, args, options);
}
// ===========================
// Cross-language methods.
// ===========================
public static RayObject callPy(String moduleName, String functionName) {
Object[] args = new Object[]{};
return Ray.internal().callPy(moduleName, functionName, args, null);
}
public static RayObject callPy(String moduleName, String functionName, CallOptions options) {
Object[] args = new Object[]{};
return Ray.internal().callPy(moduleName, functionName, args, options);
}
public static RayObject callPy(String moduleName, String functionName, Object obj0) {
Object[] args = new Object[]{obj0};
return Ray.internal().callPy(moduleName, functionName, args, null);
}
public static RayObject callPy(String moduleName, String functionName, Object obj0, CallOptions options) {
Object[] args = new Object[]{obj0};
return Ray.internal().callPy(moduleName, functionName, args, options);
}
public static RayObject callPy(String moduleName, String functionName, Object obj0, Object obj1) {
Object[] args = new Object[]{obj0, obj1};
return Ray.internal().callPy(moduleName, functionName, args, null);
}
public static RayObject callPy(String moduleName, String functionName, Object obj0, Object obj1, CallOptions options) {
Object[] args = new Object[]{obj0, obj1};
return Ray.internal().callPy(moduleName, functionName, args, options);
}
public static RayObject callPy(String moduleName, String functionName, Object obj0, Object obj1, Object obj2) {
Object[] args = new Object[]{obj0, obj1, obj2};
return Ray.internal().callPy(moduleName, functionName, args, null);
}
public static RayObject callPy(String moduleName, String functionName, Object obj0, Object obj1, Object obj2, CallOptions options) {
Object[] args = new Object[]{obj0, obj1, obj2};
return Ray.internal().callPy(moduleName, functionName, args, options);
}
public static RayObject callPy(String moduleName, String functionName, Object obj0, Object obj1, Object obj2, Object obj3) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3};
return Ray.internal().callPy(moduleName, functionName, args, null);
}
public static RayObject callPy(String moduleName, String functionName, Object obj0, Object obj1, Object obj2, Object obj3, CallOptions options) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3};
return Ray.internal().callPy(moduleName, functionName, args, options);
}
public static RayObject callPy(String moduleName, String functionName, Object obj0, Object obj1, Object obj2, Object obj3, Object obj4) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3, obj4};
return Ray.internal().callPy(moduleName, functionName, args, null);
}
public static RayObject callPy(String moduleName, String functionName, Object obj0, Object obj1, Object obj2, Object obj3, Object obj4, CallOptions options) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3, obj4};
return Ray.internal().callPy(moduleName, functionName, args, options);
}
public static RayObject callPy(String moduleName, String functionName, Object obj0, Object obj1, Object obj2, Object obj3, Object obj4, Object obj5) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3, obj4, obj5};
return Ray.internal().callPy(moduleName, functionName, args, null);
}
public static RayObject callPy(String moduleName, String functionName, Object obj0, Object obj1, Object obj2, Object obj3, Object obj4, Object obj5, CallOptions options) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3, obj4, obj5};
return Ray.internal().callPy(moduleName, functionName, args, options);
}
public static RayObject callPy(RayPyActor pyActor, String functionName) {
Object[] args = new Object[]{};
return Ray.internal().callPy(pyActor, functionName, args);
}
public static RayObject callPy(RayPyActor pyActor, String functionName, Object obj0) {
Object[] args = new Object[]{obj0};
return Ray.internal().callPy(pyActor, functionName, args);
}
public static RayObject callPy(RayPyActor pyActor, String functionName, Object obj0, Object obj1) {
Object[] args = new Object[]{obj0, obj1};
return Ray.internal().callPy(pyActor, functionName, args);
}
public static RayObject callPy(RayPyActor pyActor, String functionName, Object obj0, Object obj1, Object obj2) {
Object[] args = new Object[]{obj0, obj1, obj2};
return Ray.internal().callPy(pyActor, functionName, args);
}
public static RayObject callPy(RayPyActor pyActor, String functionName, Object obj0, Object obj1, Object obj2, Object obj3) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3};
return Ray.internal().callPy(pyActor, functionName, args);
}
public static RayObject callPy(RayPyActor pyActor, String functionName, Object obj0, Object obj1, Object obj2, Object obj3, Object obj4) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3, obj4};
return Ray.internal().callPy(pyActor, functionName, args);
}
public static RayPyActor createPyActor(String moduleName, String className) {
Object[] args = new Object[]{};
return Ray.internal().createPyActor(moduleName, className, args, null);
}
public static RayPyActor createPyActor(String moduleName, String className, ActorCreationOptions options) {
Object[] args = new Object[]{};
return Ray.internal().createPyActor(moduleName, className, args, options);
}
public static RayPyActor createPyActor(String moduleName, String className, Object obj0) {
Object[] args = new Object[]{obj0};
return Ray.internal().createPyActor(moduleName, className, args, null);
}
public static RayPyActor createPyActor(String moduleName, String className, Object obj0, ActorCreationOptions options) {
Object[] args = new Object[]{obj0};
return Ray.internal().createPyActor(moduleName, className, args, options);
}
public static RayPyActor createPyActor(String moduleName, String className, Object obj0, Object obj1) {
Object[] args = new Object[]{obj0, obj1};
return Ray.internal().createPyActor(moduleName, className, args, null);
}
public static RayPyActor createPyActor(String moduleName, String className, Object obj0, Object obj1, ActorCreationOptions options) {
Object[] args = new Object[]{obj0, obj1};
return Ray.internal().createPyActor(moduleName, className, args, options);
}
public static RayPyActor createPyActor(String moduleName, String className, Object obj0, Object obj1, Object obj2) {
Object[] args = new Object[]{obj0, obj1, obj2};
return Ray.internal().createPyActor(moduleName, className, args, null);
}
public static RayPyActor createPyActor(String moduleName, String className, Object obj0, Object obj1, Object obj2, ActorCreationOptions options) {
Object[] args = new Object[]{obj0, obj1, obj2};
return Ray.internal().createPyActor(moduleName, className, args, options);
}
public static RayPyActor createPyActor(String moduleName, String className, Object obj0, Object obj1, Object obj2, Object obj3) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3};
return Ray.internal().createPyActor(moduleName, className, args, null);
}
public static RayPyActor createPyActor(String moduleName, String className, Object obj0, Object obj1, Object obj2, Object obj3, ActorCreationOptions options) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3};
return Ray.internal().createPyActor(moduleName, className, args, options);
}
public static RayPyActor createPyActor(String moduleName, String className, Object obj0, Object obj1, Object obj2, Object obj3, Object obj4) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3, obj4};
return Ray.internal().createPyActor(moduleName, className, args, null);
}
public static RayPyActor createPyActor(String moduleName, String className, Object obj0, Object obj1, Object obj2, Object obj3, Object obj4, ActorCreationOptions options) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3, obj4};
return Ray.internal().createPyActor(moduleName, className, args, options);
}
public static RayPyActor createPyActor(String moduleName, String className, Object obj0, Object obj1, Object obj2, Object obj3, Object obj4, Object obj5) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3, obj4, obj5};
return Ray.internal().createPyActor(moduleName, className, args, null);
}
public static RayPyActor createPyActor(String moduleName, String className, Object obj0, Object obj1, Object obj2, Object obj3, Object obj4, Object obj5, ActorCreationOptions options) {
Object[] args = new Object[]{obj0, obj1, obj2, obj3, obj4, obj5};
return Ray.internal().createPyActor(moduleName, className, args, options);
}
}
18 changes: 18 additions & 0 deletions java/api/src/main/java/org/ray/api/RayPyActor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.ray.api;

/**
* Handle of a Python actor.
*/
public interface RayPyActor extends RayActor {

/**
* @return Module name of the Python actor class.
*/
String getModuleName();

/**
* @return Name of the Python actor class.
*/
String getClassName();
}

38 changes: 36 additions & 2 deletions java/api/src/main/java/org/ray/api/runtime/RayRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import org.ray.api.RayActor;
import org.ray.api.RayObject;
import org.ray.api.RayPyActor;
import org.ray.api.RuntimeContext;
import org.ray.api.WaitResult;
import org.ray.api.function.RayFunc;
Expand Down Expand Up @@ -45,8 +46,8 @@ public interface RayRuntime {
<T> List<T> get(List<UniqueId> objectIds);

/**
* Wait for a list of RayObjects to be locally available,
* until specified number of objects are ready, or specified timeout has passed.
* Wait for a list of RayObjects to be locally available, until specified number of objects are
* ready, or specified timeout has passed.
*
* @param waitList A list of RayObject to wait for.
* @param numReturns The number of objects that should be returned.
Expand Down Expand Up @@ -96,4 +97,37 @@ <T> RayActor<T> createActor(RayFunc actorFactoryFunc, Object[] args,
ActorCreationOptions options);

RuntimeContext getRuntimeContext();

/**
* Invoke a remote Python function.
*
* @param moduleName Module name of the Python function.
* @param functionName Name of the Python function.
* @param args Arguments of the function.
* @param options The options for this call.
* @return The result object.
*/
RayObject callPy(String moduleName, String functionName, Object[] args, CallOptions options);

/**
* Invoke a remote Python function on an actor.
*
* @param pyActor A handle to the actor.
* @param functionName Name of the actor method.
* @param args Arguments of the function.
* @return The result object.
*/
RayObject callPy(RayPyActor pyActor, String functionName, Object[] args);

/**
* Create a Python actor on a remote node.
*
* @param moduleName Module name of the Python actor class.
* @param className Name of the Python actor class.
* @param args Arguments of the actor constructor.
* @param options The options for creating actor.
* @return A handle to the actor.
*/
RayPyActor createPyActor(String moduleName, String className, Object[] args,
ActorCreationOptions options);
}
1 change: 0 additions & 1 deletion java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@
<failsOnError>true</failsOnError>
<failOnViolation>true</failOnViolation>
<violationSeverity>warning</violationSeverity>
<format>xml</format>
<outputFile>${project.build.directory}/checkstyle-errors.xml</outputFile>
<linkXRef>false</linkXRef>
</configuration>
Expand Down
Loading

0 comments on commit d03999d

Please sign in to comment.