C++ worker API refactoring (#9053)

This commit is contained in:
SongGuyang
2020-06-24 19:33:46 +08:00
committed by GitHub
parent aa231799ed
commit cbf38573bd
19 changed files with 399 additions and 261 deletions
+41
View File
@@ -0,0 +1,41 @@
#pragma once
#include "ray/core.h"
namespace ray {
namespace api {
template <typename ActorType>
class ActorCreator {
public:
ActorCreator();
ActorCreator(RayRuntime *runtime, RemoteFunctionPtrHolder ptr,
std::shared_ptr<msgpack::sbuffer> args);
ActorHandle<ActorType> Remote();
private:
RayRuntime *runtime_;
RemoteFunctionPtrHolder ptr_;
std::shared_ptr<msgpack::sbuffer> args_;
};
// ---------- implementation ----------
template <typename ActorType>
ActorCreator<ActorType>::ActorCreator() {}
template <typename ActorType>
ActorCreator<ActorType>::ActorCreator(RayRuntime *runtime, RemoteFunctionPtrHolder ptr,
std::shared_ptr<msgpack::sbuffer> args)
: runtime_(runtime), ptr_(ptr), args_(args) {}
template <typename ActorType>
ActorHandle<ActorType> ActorCreator<ActorType>::Remote() {
auto returned_actor_id = runtime_->CreateActor(ptr_, args_);
return ActorHandle<ActorType>(returned_actor_id);
}
} // namespace api
} // namespace ray
@@ -13,11 +13,11 @@ namespace api {
/// \param ActorType The type of the concrete actor class.
/// Note, the `Call` method is defined in actor_call.generated.h.
template <typename ActorType>
class RayActor {
class ActorHandle {
public:
RayActor();
ActorHandle();
RayActor(const ActorID &id);
ActorHandle(const ActorID &id);
/// Get a untyped ID of the actor
const ActorID &ID() const;
@@ -25,7 +25,7 @@ class RayActor {
/// Include the `Call` methods for calling remote functions.
#include <ray/api/generated/actor_call.generated.h>
/// Make RayActor serializable
/// Make ActorHandle serializable
MSGPACK_DEFINE(id_);
private:
@@ -35,15 +35,15 @@ class RayActor {
// ---------- implementation ----------
template <typename ActorType>
RayActor<ActorType>::RayActor() {}
ActorHandle<ActorType>::ActorHandle() {}
template <typename ActorType>
RayActor<ActorType>::RayActor(const ActorID &id) {
ActorHandle<ActorType>::ActorHandle(const ActorID &id) {
id_ = id;
}
template <typename ActorType>
const ActorID &RayActor<ActorType>::ID() const {
const ActorID &ActorHandle<ActorType>::ID() const {
return id_;
}
+43
View File
@@ -0,0 +1,43 @@
#pragma once
#include "ray/core.h"
namespace ray {
namespace api {
template <typename ReturnType>
class ActorTaskCaller {
public:
ActorTaskCaller();
ActorTaskCaller(RayRuntime *runtime, ActorID id, RemoteFunctionPtrHolder ptr,
std::shared_ptr<msgpack::sbuffer> args);
ObjectRef<ReturnType> Remote();
private:
RayRuntime *runtime_;
ActorID id_;
RemoteFunctionPtrHolder ptr_;
std::shared_ptr<msgpack::sbuffer> args_;
};
// ---------- implementation ----------
template <typename ReturnType>
ActorTaskCaller<ReturnType>::ActorTaskCaller() {}
template <typename ReturnType>
ActorTaskCaller<ReturnType>::ActorTaskCaller(RayRuntime *runtime, ActorID id,
RemoteFunctionPtrHolder ptr,
std::shared_ptr<msgpack::sbuffer> args)
: runtime_(runtime), id_(id), ptr_(ptr), args_(args) {}
template <typename ReturnType>
ObjectRef<ReturnType> ActorTaskCaller<ReturnType>::Remote() {
auto returned_object_id = runtime_->CallActor(ptr_, id_, args_);
return ObjectRef<ReturnType>(returned_object_id);
}
} // namespace api
} // namespace ray
+9 -9
View File
@@ -36,10 +36,10 @@ inline void Arguments::WrapArgs(msgpack::packer<msgpack::sbuffer> &packer) {}
template <typename Arg1Type>
inline void Arguments::WrapArgs(msgpack::packer<msgpack::sbuffer> &packer,
Arg1Type &arg1) {
/// Notice RayObjectClassPrefix should be modified by RayObject class name or namespace.
static const std::string RayObjectClassPrefix = "N3ray3api9RayObject";
/// Notice ObjectRefClassPrefix should be modified by ObjectRef class name or namespace.
static const std::string ObjectRefClassPrefix = "N3ray3api9ObjectRef";
std::string type_name = typeid(arg1).name();
if (type_name.rfind(RayObjectClassPrefix, 0) == 0) {
if (type_name.rfind(ObjectRefClassPrefix, 0) == 0) {
/// Pass by reference.
Serializer::Serialize(packer, true);
} else {
@@ -61,12 +61,12 @@ inline void Arguments::UnwrapArgs(msgpack::unpacker &unpacker) {}
template <typename Arg1Type>
inline void Arguments::UnwrapArgs(msgpack::unpacker &unpacker,
std::shared_ptr<Arg1Type> *arg1) {
bool is_ray_object;
Serializer::Deserialize(unpacker, &is_ray_object);
if (is_ray_object) {
RayObject<Arg1Type> ray_object;
Serializer::Deserialize(unpacker, &ray_object);
*arg1 = ray_object.Get();
bool is_object_ref;
Serializer::Deserialize(unpacker, &is_object_ref);
if (is_object_ref) {
ObjectRef<Arg1Type> object_ref;
Serializer::Deserialize(unpacker, &object_ref);
*arg1 = object_ref.Get();
} else {
Serializer::Deserialize(unpacker, arg1);
}
@@ -2,40 +2,40 @@
/// The following `Call` methods are used to call remote functions of actors.
/// Their arguments and return types are as following:
/// \param[in] actor_func The function pointer to be remote execution.
/// \param[in] arg1...argn The function arguments passed by a value or RayObject.
/// \return RayObject.
/// \param[in] arg1...argn The function arguments passed by a value or ObjectRef.
/// \return ActorTaskCaller.
// TODO(Guyang Song): code generation
// 0 args
template <typename ReturnType>
RayObject<ReturnType> Call(ActorFunc0<ActorType, ReturnType> actor_func);
ActorTaskCaller<ReturnType> Task(ActorFunc0<ActorType, ReturnType> actor_func);
// 1 arg
template <typename ReturnType, typename Arg1Type>
RayObject<ReturnType> Call(ActorFunc1<ActorType, ReturnType, Arg1Type> actor_func,
Arg1Type arg1);
ActorTaskCaller<ReturnType> Task(ActorFunc1<ActorType, ReturnType, Arg1Type> actor_func,
Arg1Type arg1);
template <typename ReturnType, typename Arg1Type>
RayObject<ReturnType> Call(ActorFunc1<ActorType, ReturnType, Arg1Type> actor_func,
RayObject<Arg1Type> &arg1);
ActorTaskCaller<ReturnType> Task(ActorFunc1<ActorType, ReturnType, Arg1Type> actor_func,
ObjectRef<Arg1Type> &arg1);
// 2 args
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
RayObject<ReturnType> Call(
ActorTaskCaller<ReturnType> Task(
ActorFunc2<ActorType, ReturnType, Arg1Type, Arg2Type> actor_func, Arg1Type arg1,
Arg2Type arg2);
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
RayObject<ReturnType> Call(
ActorTaskCaller<ReturnType> Task(
ActorFunc2<ActorType, ReturnType, Arg1Type, Arg2Type> actor_func,
RayObject<Arg1Type> &arg1, Arg2Type arg2);
ObjectRef<Arg1Type> &arg1, Arg2Type arg2);
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
RayObject<ReturnType> Call(
ActorTaskCaller<ReturnType> Task(
ActorFunc2<ActorType, ReturnType, Arg1Type, Arg2Type> actor_func, Arg1Type arg1,
RayObject<Arg2Type> &arg2);
ObjectRef<Arg2Type> &arg2);
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
RayObject<ReturnType> Call(
ActorTaskCaller<ReturnType> Task(
ActorFunc2<ActorType, ReturnType, Arg1Type, Arg2Type> actor_func,
RayObject<Arg1Type> &arg1, RayObject<Arg2Type> &arg2);
ObjectRef<Arg1Type> &arg1, ObjectRef<Arg2Type> &arg2);
@@ -3,55 +3,55 @@
// 0 args
template <typename ActorType>
template <typename ReturnType>
RayObject<ReturnType> RayActor<ActorType>::Call(
ActorTaskCaller<ReturnType> ActorHandle<ActorType>::Task(
ActorFunc0<ActorType, ReturnType> actor_func) {
return Ray::Call(actor_func, *this);
return Ray::Task(actor_func, *this);
}
// 1 arg
template <typename ActorType>
template <typename ReturnType, typename Arg1Type>
RayObject<ReturnType> RayActor<ActorType>::Call(
ActorTaskCaller<ReturnType> ActorHandle<ActorType>::Task(
ActorFunc1<ActorType, ReturnType, Arg1Type> actor_func, Arg1Type arg1) {
return Ray::Call(actor_func, *this, arg1);
return Ray::Task(actor_func, *this, arg1);
}
template <typename ActorType>
template <typename ReturnType, typename Arg1Type>
RayObject<ReturnType> RayActor<ActorType>::Call(
ActorFunc1<ActorType, ReturnType, Arg1Type> actor_func, RayObject<Arg1Type> &arg1) {
return Ray::Call(actor_func, *this, arg1);
ActorTaskCaller<ReturnType> ActorHandle<ActorType>::Task(
ActorFunc1<ActorType, ReturnType, Arg1Type> actor_func, ObjectRef<Arg1Type> &arg1) {
return Ray::Task(actor_func, *this, arg1);
}
// 2 args
template <typename ActorType>
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
RayObject<ReturnType> RayActor<ActorType>::Call(
ActorTaskCaller<ReturnType> ActorHandle<ActorType>::Task(
ActorFunc2<ActorType, ReturnType, Arg1Type, Arg2Type> actor_func, Arg1Type arg1,
Arg2Type arg2) {
return Ray::Call(actor_func, *this, arg1, arg2);
return Ray::Task(actor_func, *this, arg1, arg2);
}
template <typename ActorType>
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
RayObject<ReturnType> RayActor<ActorType>::Call(
ActorTaskCaller<ReturnType> ActorHandle<ActorType>::Task(
ActorFunc2<ActorType, ReturnType, Arg1Type, Arg2Type> actor_func,
RayObject<Arg1Type> &arg1, Arg2Type arg2) {
return Ray::Call(actor_func, *this, arg1, arg2);
ObjectRef<Arg1Type> &arg1, Arg2Type arg2) {
return Ray::Task(actor_func, *this, arg1, arg2);
}
template <typename ActorType>
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
RayObject<ReturnType> RayActor<ActorType>::Call(
ActorTaskCaller<ReturnType> ActorHandle<ActorType>::Task(
ActorFunc2<ActorType, ReturnType, Arg1Type, Arg2Type> actor_func, Arg1Type arg1,
RayObject<Arg2Type> &arg2) {
return Ray::Call(actor_func, *this, arg1, arg2);
ObjectRef<Arg2Type> &arg2) {
return Ray::Task(actor_func, *this, arg1, arg2);
}
template <typename ActorType>
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
RayObject<ReturnType> RayActor<ActorType>::Call(
ActorTaskCaller<ReturnType> ActorHandle<ActorType>::Task(
ActorFunc2<ActorType, ReturnType, Arg1Type, Arg2Type> actor_func,
RayObject<Arg1Type> &arg1, RayObject<Arg2Type> &arg2) {
return Ray::Call(actor_func, *this, arg1, arg2);
ObjectRef<Arg1Type> &arg1, ObjectRef<Arg2Type> &arg2) {
return Ray::Task(actor_func, *this, arg1, arg2);
}
@@ -4,35 +4,37 @@
// 0 args
template <typename ReturnType, typename ActorType>
static RayObject<ReturnType> Call(ActorFunc0<ActorType, ReturnType> actor_func,
RayActor<ActorType> &actor);
static ActorTaskCaller<ReturnType> Task(ActorFunc0<ActorType, ReturnType> actor_func,
ActorHandle<ActorType> &actor);
// 1 arg
template <typename ReturnType, typename ActorType, typename Arg1Type>
static RayObject<ReturnType> Call(ActorFunc1<ActorType, ReturnType, Arg1Type> actor_func,
RayActor<ActorType> &actor, Arg1Type arg1);
static ActorTaskCaller<ReturnType> Task(
ActorFunc1<ActorType, ReturnType, Arg1Type> actor_func, ActorHandle<ActorType> &actor,
Arg1Type arg1);
template <typename ReturnType, typename ActorType, typename Arg1Type>
static RayObject<ReturnType> Call(ActorFunc1<ActorType, ReturnType, Arg1Type> actor_func,
RayActor<ActorType> &actor, RayObject<Arg1Type> &arg1);
static ActorTaskCaller<ReturnType> Task(
ActorFunc1<ActorType, ReturnType, Arg1Type> actor_func, ActorHandle<ActorType> &actor,
ObjectRef<Arg1Type> &arg1);
// 2 args
template <typename ReturnType, typename ActorType, typename Arg1Type, typename Arg2Type>
static RayObject<ReturnType> Call(
static ActorTaskCaller<ReturnType> Task(
ActorFunc2<ActorType, ReturnType, Arg1Type, Arg2Type> actor_func,
RayActor<ActorType> &actor, Arg1Type arg1, Arg2Type arg2);
ActorHandle<ActorType> &actor, Arg1Type arg1, Arg2Type arg2);
template <typename ReturnType, typename ActorType, typename Arg1Type, typename Arg2Type>
static RayObject<ReturnType> Call(
static ActorTaskCaller<ReturnType> Task(
ActorFunc2<ActorType, ReturnType, Arg1Type, Arg2Type> actor_func,
RayActor<ActorType> &actor, RayObject<Arg1Type> &arg1, Arg2Type arg2);
ActorHandle<ActorType> &actor, ObjectRef<Arg1Type> &arg1, Arg2Type arg2);
template <typename ReturnType, typename ActorType, typename Arg1Type, typename Arg2Type>
static RayObject<ReturnType> Call(
static ActorTaskCaller<ReturnType> Task(
ActorFunc2<ActorType, ReturnType, Arg1Type, Arg2Type> actor_func,
RayActor<ActorType> &actor, Arg1Type arg1, RayObject<Arg2Type> &arg2);
ActorHandle<ActorType> &actor, Arg1Type arg1, ObjectRef<Arg2Type> &arg2);
template <typename ReturnType, typename ActorType, typename Arg1Type, typename Arg2Type>
static RayObject<ReturnType> Call(
static ActorTaskCaller<ReturnType> Task(
ActorFunc2<ActorType, ReturnType, Arg1Type, Arg2Type> actor_func,
RayActor<ActorType> &actor, RayObject<Arg1Type> &arg1, RayObject<Arg2Type> &arg2);
ActorHandle<ActorType> &actor, ObjectRef<Arg1Type> &arg1, ObjectRef<Arg2Type> &arg2);
@@ -1,59 +1,61 @@
// TODO(Guyang Song): code generation
// 0 args
template <typename ReturnType, typename ActorType>
RayObject<ReturnType> Ray::Call(ActorFunc0<ActorType, ReturnType> actor_func,
RayActor<ActorType> &actor) {
ActorTaskCaller<ReturnType> Ray::Task(ActorFunc0<ActorType, ReturnType> actor_func,
ActorHandle<ActorType> &actor) {
return CallActorInternal<ReturnType, ActorType>(
actor_func, ActorExecFunction<ReturnType, ActorType>, actor);
}
// 1 arg
template <typename ReturnType, typename ActorType, typename Arg1Type>
RayObject<ReturnType> Ray::Call(ActorFunc1<ActorType, ReturnType, Arg1Type> actor_func,
RayActor<ActorType> &actor, Arg1Type arg1) {
ActorTaskCaller<ReturnType> Ray::Task(
ActorFunc1<ActorType, ReturnType, Arg1Type> actor_func, ActorHandle<ActorType> &actor,
Arg1Type arg1) {
return CallActorInternal<ReturnType, ActorType>(
actor_func, ActorExecFunction<ReturnType, ActorType, Arg1Type>, actor, arg1);
}
template <typename ReturnType, typename ActorType, typename Arg1Type>
RayObject<ReturnType> Ray::Call(ActorFunc1<ActorType, ReturnType, Arg1Type> actor_func,
RayActor<ActorType> &actor, RayObject<Arg1Type> &arg1) {
ActorTaskCaller<ReturnType> Ray::Task(
ActorFunc1<ActorType, ReturnType, Arg1Type> actor_func, ActorHandle<ActorType> &actor,
ObjectRef<Arg1Type> &arg1) {
return CallActorInternal<ReturnType, ActorType>(
actor_func, ActorExecFunction<ReturnType, ActorType, Arg1Type>, actor, arg1);
}
// 2 args
template <typename ReturnType, typename ActorType, typename Arg1Type, typename Arg2Type>
RayObject<ReturnType> Ray::Call(
ActorTaskCaller<ReturnType> Ray::Task(
ActorFunc2<ActorType, ReturnType, Arg1Type, Arg2Type> actor_func,
RayActor<ActorType> &actor, Arg1Type arg1, Arg2Type arg2) {
ActorHandle<ActorType> &actor, Arg1Type arg1, Arg2Type arg2) {
return CallActorInternal<ReturnType, ActorType>(
actor_func, ActorExecFunction<ReturnType, ActorType, Arg1Type, Arg2Type>, actor,
arg1, arg2);
}
template <typename ReturnType, typename ActorType, typename Arg1Type, typename Arg2Type>
RayObject<ReturnType> Ray::Call(
ActorTaskCaller<ReturnType> Ray::Task(
ActorFunc2<ActorType, ReturnType, Arg1Type, Arg2Type> actor_func,
RayActor<ActorType> &actor, RayObject<Arg1Type> &arg1, Arg2Type arg2) {
ActorHandle<ActorType> &actor, ObjectRef<Arg1Type> &arg1, Arg2Type arg2) {
return CallActorInternal<ReturnType, ActorType>(
actor_func, ActorExecFunction<ReturnType, ActorType, Arg1Type, Arg2Type>, actor,
arg1, arg2);
}
template <typename ReturnType, typename ActorType, typename Arg1Type, typename Arg2Type>
RayObject<ReturnType> Ray::Call(
ActorTaskCaller<ReturnType> Ray::Task(
ActorFunc2<ActorType, ReturnType, Arg1Type, Arg2Type> actor_func,
RayActor<ActorType> &actor, Arg1Type arg1, RayObject<Arg2Type> &arg2) {
ActorHandle<ActorType> &actor, Arg1Type arg1, ObjectRef<Arg2Type> &arg2) {
return CallActorInternal<ReturnType, ActorType>(
actor_func, ActorExecFunction<ReturnType, ActorType, Arg1Type, Arg2Type>, actor,
arg1, arg2);
}
template <typename ReturnType, typename ActorType, typename Arg1Type, typename Arg2Type>
RayObject<ReturnType> Ray::Call(
ActorTaskCaller<ReturnType> Ray::Task(
ActorFunc2<ActorType, ReturnType, Arg1Type, Arg2Type> actor_func,
RayActor<ActorType> &actor, RayObject<Arg1Type> &arg1, RayObject<Arg2Type> &arg2) {
ActorHandle<ActorType> &actor, ObjectRef<Arg1Type> &arg1, ObjectRef<Arg2Type> &arg2) {
return CallActorInternal<ReturnType, ActorType>(
actor_func, ActorExecFunction<ReturnType, ActorType, Arg1Type, Arg2Type>, actor,
arg1, arg2);
@@ -2,36 +2,36 @@
/// The following `Call` methods are used to call remote functions.
/// Their arguments and return types are as following:
/// \param[in] func The function pointer to be remote execution.
/// \param[in] arg1...argn The function arguments passed by a value or RayObject.
/// \return RayObject.
/// \param[in] arg1...argn The function arguments passed by a value or ObjectRef.
/// \return TaskCaller.
// TODO(Guyang Song): code generation
// 0 args
template <typename ReturnType>
static RayObject<ReturnType> Call(Func0<ReturnType> func);
static TaskCaller<ReturnType> Task(Func0<ReturnType> func);
// 1 arg
template <typename ReturnType, typename Arg1Type>
static RayObject<ReturnType> Call(Func1<ReturnType, Arg1Type> func, Arg1Type arg1);
static TaskCaller<ReturnType> Task(Func1<ReturnType, Arg1Type> func, Arg1Type arg1);
template <typename ReturnType, typename Arg1Type>
static RayObject<ReturnType> Call(Func1<ReturnType, Arg1Type> func,
RayObject<Arg1Type> &arg1);
static TaskCaller<ReturnType> Task(Func1<ReturnType, Arg1Type> func,
ObjectRef<Arg1Type> &arg1);
// 2 args
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
static RayObject<ReturnType> Call(Func2<ReturnType, Arg1Type, Arg2Type> func,
Arg1Type arg1, Arg2Type arg2);
static TaskCaller<ReturnType> Task(Func2<ReturnType, Arg1Type, Arg2Type> func,
Arg1Type arg1, Arg2Type arg2);
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
static RayObject<ReturnType> Call(Func2<ReturnType, Arg1Type, Arg2Type> func,
RayObject<Arg1Type> &arg1, Arg2Type arg2);
static TaskCaller<ReturnType> Task(Func2<ReturnType, Arg1Type, Arg2Type> func,
ObjectRef<Arg1Type> &arg1, Arg2Type arg2);
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
static RayObject<ReturnType> Call(Func2<ReturnType, Arg1Type, Arg2Type> func,
Arg1Type arg1, RayObject<Arg2Type> &arg2);
static TaskCaller<ReturnType> Task(Func2<ReturnType, Arg1Type, Arg2Type> func,
Arg1Type arg1, ObjectRef<Arg2Type> &arg2);
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
static RayObject<ReturnType> Call(Func2<ReturnType, Arg1Type, Arg2Type> func,
RayObject<Arg1Type> &arg1, RayObject<Arg2Type> &arg2);
static TaskCaller<ReturnType> Task(Func2<ReturnType, Arg1Type, Arg2Type> func,
ObjectRef<Arg1Type> &arg1, ObjectRef<Arg2Type> &arg2);
@@ -2,47 +2,47 @@
// 0 args
template <typename ReturnType>
RayObject<ReturnType> Ray::Call(Func0<ReturnType> func) {
return CallInternal<ReturnType>(func, NormalExecFunction<ReturnType>);
TaskCaller<ReturnType> Ray::Task(Func0<ReturnType> func) {
return TaskInternal<ReturnType>(func, NormalExecFunction<ReturnType>);
}
// 1 arg
template <typename ReturnType, typename Arg1Type>
RayObject<ReturnType> Ray::Call(Func1<ReturnType, Arg1Type> func, Arg1Type arg1) {
return CallInternal<ReturnType>(func, NormalExecFunction<ReturnType, Arg1Type>, arg1);
TaskCaller<ReturnType> Ray::Task(Func1<ReturnType, Arg1Type> func, Arg1Type arg1) {
return TaskInternal<ReturnType>(func, NormalExecFunction<ReturnType, Arg1Type>, arg1);
}
template <typename ReturnType, typename Arg1Type>
RayObject<ReturnType> Ray::Call(Func1<ReturnType, Arg1Type> func,
RayObject<Arg1Type> &arg1) {
return CallInternal<ReturnType>(func, NormalExecFunction<ReturnType, Arg1Type>, arg1);
TaskCaller<ReturnType> Ray::Task(Func1<ReturnType, Arg1Type> func,
ObjectRef<Arg1Type> &arg1) {
return TaskInternal<ReturnType>(func, NormalExecFunction<ReturnType, Arg1Type>, arg1);
}
// 2 args
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
RayObject<ReturnType> Ray::Call(Func2<ReturnType, Arg1Type, Arg2Type> func, Arg1Type arg1,
Arg2Type arg2) {
return CallInternal<ReturnType>(
TaskCaller<ReturnType> Ray::Task(Func2<ReturnType, Arg1Type, Arg2Type> func,
Arg1Type arg1, Arg2Type arg2) {
return TaskInternal<ReturnType>(
func, NormalExecFunction<ReturnType, Arg1Type, Arg2Type>, arg1, arg2);
}
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
RayObject<ReturnType> Ray::Call(Func2<ReturnType, Arg1Type, Arg2Type> func,
RayObject<Arg1Type> &arg1, Arg2Type arg2) {
return CallInternal<ReturnType>(
TaskCaller<ReturnType> Ray::Task(Func2<ReturnType, Arg1Type, Arg2Type> func,
ObjectRef<Arg1Type> &arg1, Arg2Type arg2) {
return TaskInternal<ReturnType>(
func, NormalExecFunction<ReturnType, Arg1Type, Arg2Type>, arg1, arg2);
}
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
RayObject<ReturnType> Ray::Call(Func2<ReturnType, Arg1Type, Arg2Type> func, Arg1Type arg1,
RayObject<Arg2Type> &arg2) {
return CallInternal<ReturnType>(
TaskCaller<ReturnType> Ray::Task(Func2<ReturnType, Arg1Type, Arg2Type> func,
Arg1Type arg1, ObjectRef<Arg2Type> &arg2) {
return TaskInternal<ReturnType>(
func, NormalExecFunction<ReturnType, Arg1Type, Arg2Type>, arg1, arg2);
}
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
RayObject<ReturnType> Ray::Call(Func2<ReturnType, Arg1Type, Arg2Type> func,
RayObject<Arg1Type> &arg1, RayObject<Arg2Type> &arg2) {
return CallInternal<ReturnType>(
TaskCaller<ReturnType> Ray::Task(Func2<ReturnType, Arg1Type, Arg2Type> func,
ObjectRef<Arg1Type> &arg1, ObjectRef<Arg2Type> &arg2) {
return TaskInternal<ReturnType>(
func, NormalExecFunction<ReturnType, Arg1Type, Arg2Type>, arg1, arg2);
}
@@ -2,41 +2,41 @@
/// The following `Call` methods are used to call remote functions and create an actor.
/// Their arguments and return types are as following:
/// \param[in] create_func The function pointer to be remote execution.
/// \param[in] arg1...argn The function arguments passed by a value or RayObject.
/// \return RayActor.
/// \param[in] arg1...argn The function arguments passed by a value or ObjectRef.
/// \return ActorCreator.
// TODO(Guyang Song): code generation
// 0 args
template <typename ReturnType>
static RayActor<ReturnType> CreateActor(CreateActorFunc0<ReturnType> create_func);
template <typename ActorType>
static ActorCreator<ActorType> Actor(CreateActorFunc0<ActorType> create_func);
// 1 arg
template <typename ReturnType, typename Arg1Type>
static RayActor<ReturnType> CreateActor(
CreateActorFunc1<ReturnType, Arg1Type> create_func, Arg1Type arg1);
template <typename ActorType, typename Arg1Type>
static ActorCreator<ActorType> Actor(CreateActorFunc1<ActorType, Arg1Type> create_func,
Arg1Type arg1);
template <typename ReturnType, typename Arg1Type>
static RayActor<ReturnType> CreateActor(
CreateActorFunc1<ReturnType, Arg1Type> create_func, RayObject<Arg1Type> &arg1);
template <typename ActorType, typename Arg1Type>
static ActorCreator<ActorType> Actor(CreateActorFunc1<ActorType, Arg1Type> create_func,
ObjectRef<Arg1Type> &arg1);
// 2 args
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
static RayActor<ReturnType> CreateActor(
CreateActorFunc2<ReturnType, Arg1Type, Arg2Type> create_func, Arg1Type arg1,
template <typename ActorType, typename Arg1Type, typename Arg2Type>
static ActorCreator<ActorType> Actor(
CreateActorFunc2<ActorType, Arg1Type, Arg2Type> create_func, Arg1Type arg1,
Arg2Type arg2);
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
static RayActor<ReturnType> CreateActor(
CreateActorFunc2<ReturnType, Arg1Type, Arg2Type> create_func,
RayObject<Arg1Type> &arg1, Arg2Type arg2);
template <typename ActorType, typename Arg1Type, typename Arg2Type>
static ActorCreator<ActorType> Actor(
CreateActorFunc2<ActorType, Arg1Type, Arg2Type> create_func,
ObjectRef<Arg1Type> &arg1, Arg2Type arg2);
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
static RayActor<ReturnType> CreateActor(
CreateActorFunc2<ReturnType, Arg1Type, Arg2Type> create_func, Arg1Type arg1,
RayObject<Arg2Type> &arg2);
template <typename ActorType, typename Arg1Type, typename Arg2Type>
static ActorCreator<ActorType> Actor(
CreateActorFunc2<ActorType, Arg1Type, Arg2Type> create_func, Arg1Type arg1,
ObjectRef<Arg2Type> &arg2);
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
static RayActor<ReturnType> CreateActor(
CreateActorFunc2<ReturnType, Arg1Type, Arg2Type> create_func,
RayObject<Arg1Type> &arg1, RayObject<Arg2Type> &arg2);
template <typename ActorType, typename Arg1Type, typename Arg2Type>
static ActorCreator<ActorType> Actor(
CreateActorFunc2<ActorType, Arg1Type, Arg2Type> create_func,
ObjectRef<Arg1Type> &arg1, ObjectRef<Arg2Type> &arg2);
@@ -1,56 +1,56 @@
// TODO(Guyang Song): code generation
// 0 args
template <typename ReturnType>
RayActor<ReturnType> Ray::CreateActor(CreateActorFunc0<ReturnType> create_func) {
return CreateActorInternal<ReturnType>(create_func,
CreateActorExecFunction<ReturnType *>);
template <typename ActorType>
ActorCreator<ActorType> Ray::Actor(CreateActorFunc0<ActorType> create_func) {
return CreateActorInternal<ActorType>(create_func,
CreateActorExecFunction<ActorType *>);
}
// 1 arg
template <typename ReturnType, typename Arg1Type>
RayActor<ReturnType> Ray::CreateActor(CreateActorFunc1<ReturnType, Arg1Type> create_func,
Arg1Type arg1) {
return CreateActorInternal<ReturnType>(
create_func, CreateActorExecFunction<ReturnType *, Arg1Type>, arg1);
template <typename ActorType, typename Arg1Type>
ActorCreator<ActorType> Ray::Actor(CreateActorFunc1<ActorType, Arg1Type> create_func,
Arg1Type arg1) {
return CreateActorInternal<ActorType>(
create_func, CreateActorExecFunction<ActorType *, Arg1Type>, arg1);
}
template <typename ReturnType, typename Arg1Type>
RayActor<ReturnType> Ray::CreateActor(CreateActorFunc1<ReturnType, Arg1Type> create_func,
RayObject<Arg1Type> &arg1) {
return CreateActorInternal<ReturnType>(
create_func, CreateActorExecFunction<ReturnType *, Arg1Type>, arg1);
template <typename ActorType, typename Arg1Type>
ActorCreator<ActorType> Ray::Actor(CreateActorFunc1<ActorType, Arg1Type> create_func,
ObjectRef<Arg1Type> &arg1) {
return CreateActorInternal<ActorType>(
create_func, CreateActorExecFunction<ActorType *, Arg1Type>, arg1);
}
// 2 args
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
RayActor<ReturnType> Ray::CreateActor(
CreateActorFunc2<ReturnType, Arg1Type, Arg2Type> create_func, Arg1Type arg1,
template <typename ActorType, typename Arg1Type, typename Arg2Type>
ActorCreator<ActorType> Ray::Actor(
CreateActorFunc2<ActorType, Arg1Type, Arg2Type> create_func, Arg1Type arg1,
Arg2Type arg2) {
return CreateActorInternal<ReturnType>(
create_func, CreateActorExecFunction<ReturnType *, Arg1Type, Arg2Type>, arg1, arg2);
return CreateActorInternal<ActorType>(
create_func, CreateActorExecFunction<ActorType *, Arg1Type, Arg2Type>, arg1, arg2);
}
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
RayActor<ReturnType> Ray::CreateActor(
CreateActorFunc2<ReturnType, Arg1Type, Arg2Type> create_func,
RayObject<Arg1Type> &arg1, Arg2Type arg2) {
return CreateActorInternal<ReturnType>(
create_func, CreateActorExecFunction<ReturnType *, Arg1Type, Arg2Type>, arg1, arg2);
template <typename ActorType, typename Arg1Type, typename Arg2Type>
ActorCreator<ActorType> Ray::Actor(
CreateActorFunc2<ActorType, Arg1Type, Arg2Type> create_func,
ObjectRef<Arg1Type> &arg1, Arg2Type arg2) {
return CreateActorInternal<ActorType>(
create_func, CreateActorExecFunction<ActorType *, Arg1Type, Arg2Type>, arg1, arg2);
}
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
RayActor<ReturnType> Ray::CreateActor(
CreateActorFunc2<ReturnType, Arg1Type, Arg2Type> create_func, Arg1Type arg1,
RayObject<Arg2Type> &arg2) {
return CreateActorInternal<ReturnType>(
create_func, CreateActorExecFunction<ReturnType *, Arg1Type, Arg2Type>, arg1, arg2);
template <typename ActorType, typename Arg1Type, typename Arg2Type>
ActorCreator<ActorType> Ray::Actor(
CreateActorFunc2<ActorType, Arg1Type, Arg2Type> create_func, Arg1Type arg1,
ObjectRef<Arg2Type> &arg2) {
return CreateActorInternal<ActorType>(
create_func, CreateActorExecFunction<ActorType *, Arg1Type, Arg2Type>, arg1, arg2);
}
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
RayActor<ReturnType> Ray::CreateActor(
CreateActorFunc2<ReturnType, Arg1Type, Arg2Type> create_func,
RayObject<Arg1Type> &arg1, RayObject<Arg2Type> &arg2) {
return CreateActorInternal<ReturnType>(
create_func, CreateActorExecFunction<ReturnType *, Arg1Type, Arg2Type>, arg1, arg2);
template <typename ActorType, typename Arg1Type, typename Arg2Type>
ActorCreator<ActorType> Ray::Actor(
CreateActorFunc2<ActorType, Arg1Type, Arg2Type> create_func,
ObjectRef<Arg1Type> &arg1, ObjectRef<Arg2Type> &arg2) {
return CreateActorInternal<ActorType>(
create_func, CreateActorExecFunction<ActorType *, Arg1Type, Arg2Type>, arg1, arg2);
}
@@ -14,13 +14,13 @@ namespace api {
/// Represents an object in the object store..
/// \param T The type of object.
template <typename T>
class RayObject {
class ObjectRef {
public:
RayObject();
ObjectRef();
RayObject(const ObjectID &id);
ObjectRef(const ObjectID &id);
bool operator==(const RayObject<T> &object) const;
bool operator==(const ObjectRef<T> &object) const;
/// Get a untyped ID of the object
const ObjectID &ID() const;
@@ -31,7 +31,7 @@ class RayObject {
/// \return shared pointer of the result.
std::shared_ptr<T> Get() const;
/// Make RayObject serializable
/// Make ObjectRef serializable
MSGPACK_DEFINE(id_);
private:
@@ -42,25 +42,25 @@ class RayObject {
#include <ray/api.h>
template <typename T>
RayObject<T>::RayObject() {}
ObjectRef<T>::ObjectRef() {}
template <typename T>
RayObject<T>::RayObject(const ObjectID &id) {
ObjectRef<T>::ObjectRef(const ObjectID &id) {
id_ = id;
}
template <typename T>
inline bool RayObject<T>::operator==(const RayObject<T> &object) const {
inline bool ObjectRef<T>::operator==(const ObjectRef<T> &object) const {
return id_ == object.id_;
}
template <typename T>
const ObjectID &RayObject<T>::ID() const {
const ObjectID &ObjectRef<T>::ID() const {
return id_;
}
template <typename T>
inline std::shared_ptr<T> RayObject<T>::Get() const {
inline std::shared_ptr<T> ObjectRef<T>::Get() const {
return Ray::Get(*this);
}
} // namespace api
+41
View File
@@ -0,0 +1,41 @@
#pragma once
#include "ray/core.h"
namespace ray {
namespace api {
template <typename ReturnType>
class TaskCaller {
public:
TaskCaller();
TaskCaller(RayRuntime *runtime, RemoteFunctionPtrHolder ptr,
std::shared_ptr<msgpack::sbuffer> args);
ObjectRef<ReturnType> Remote();
private:
RayRuntime *runtime_;
RemoteFunctionPtrHolder ptr_;
std::shared_ptr<msgpack::sbuffer> args_;
};
// ---------- implementation ----------
template <typename ReturnType>
TaskCaller<ReturnType>::TaskCaller() {}
template <typename ReturnType>
TaskCaller<ReturnType>::TaskCaller(RayRuntime *runtime, RemoteFunctionPtrHolder ptr,
std::shared_ptr<msgpack::sbuffer> args)
: runtime_(runtime), ptr_(ptr), args_(args) {}
template <typename ReturnType>
ObjectRef<ReturnType> TaskCaller<ReturnType>::Remote() {
auto returned_object_id = runtime_->Call(ptr_, args_);
return ObjectRef<ReturnType>(returned_object_id);
}
} // namespace api
} // namespace ray