diff --git a/cpp/include/ray/api.h b/cpp/include/ray/api.h index 8d1952d71..c366951af 100644 --- a/cpp/include/ray/api.h +++ b/cpp/include/ray/api.h @@ -13,9 +13,17 @@ namespace ray { namespace api { template -class RayObject; +class ObjectRef; template -class RayActor; +class ActorHandle; +template +class TaskCaller; + +template +class ActorTaskCaller; + +template +class ActorCreator; class WaitResult; @@ -27,9 +35,9 @@ class Ray { /// Store an object in the object store. /// /// \param[in] obj The object which should be stored. - /// \return RayObject A reference to the object in the object store. + /// \return ObjectRef A reference to the object in the object store. template - static RayObject Put(const T &obj); + static ObjectRef Put(const T &obj); /// Get a list of objects from the object store. /// This method will be blocked until all the objects are ready. @@ -45,9 +53,9 @@ class Ray { /// \param[in] objects The object array which should be got. /// \return shared pointer array of the result. template - static std::vector> Get(const std::vector> &ids); + static std::vector> Get(const std::vector> &ids); - /// Wait for a list of RayObjects to be locally available, + /// Wait for a list of objects to be locally available, /// until specified number of objects are ready, or specified timeout has passed. /// /// \param[in] ids The object id array which should be waited. @@ -61,7 +69,7 @@ class Ray { /// Include the `Call` methods for calling remote functions. #include "api/generated/call_funcs.generated.h" -/// Include the `CreateActor` methods for creating actors. +/// Include the `Actor` methods for creating actors. #include "api/generated/create_actors.generated.h" private: @@ -69,72 +77,76 @@ class Ray { static std::once_flag is_inited_; - /// Used by RayObject to implement .Get() + /// Used by ObjectRef to implement .Get() template - static std::shared_ptr Get(const RayObject &object); + static std::shared_ptr Get(const ObjectRef &object); template - static RayObject CallInternal(FuncType &func, ExecFuncType &exec_func, - ArgTypes &... args); + static TaskCaller TaskInternal(FuncType &func, ExecFuncType &exec_func, + ArgTypes &... args); - template - static RayActor CreateActorInternal(FuncType &func, ExecFuncType &exec_func, - ArgTypes &... args); + static ActorCreator CreateActorInternal(FuncType &func, + ExecFuncType &exec_func, + ArgTypes &... args); template - static RayObject CallActorInternal(FuncType &actor_func, - ExecFuncType &exec_func, - RayActor &actor, - ArgTypes &... args); + static ActorTaskCaller CallActorInternal(FuncType &actor_func, + ExecFuncType &exec_func, + ActorHandle &actor, + ArgTypes &... args); /// Include the `Call` methods for calling actor methods. -/// Used by RayActor to implement .Call() +/// Used by ActorHandle to implement .Call() #include "api/generated/call_actors.generated.h" template - friend class RayObject; + friend class ObjectRef; template - friend class RayActor; + friend class ActorHandle; }; } // namespace api } // namespace ray // --------- inline implementation ------------ +#include +#include +#include #include -#include -#include +#include #include +#include #include namespace ray { namespace api { template -inline static std::vector RayObjectsToObjectIDs( - const std::vector> &ray_objects) { +inline static std::vector ObjectRefsToObjectIDs( + const std::vector> &object_refs) { std::vector object_ids; - for (auto it = ray_objects.begin(); it != ray_objects.end(); it++) { + for (auto it = object_refs.begin(); it != object_refs.end(); it++) { object_ids.push_back(it->ID()); } return object_ids; } template -inline RayObject Ray::Put(const T &obj) { +inline ObjectRef Ray::Put(const T &obj) { std::shared_ptr buffer(new msgpack::sbuffer()); msgpack::packer packer(buffer.get()); Serializer::Serialize(packer, obj); auto id = runtime_->Put(buffer); - return RayObject(id); + return ObjectRef(id); } template -inline std::shared_ptr Ray::Get(const RayObject &object) { +inline std::shared_ptr Ray::Get(const ObjectRef &object) { auto packed_object = runtime_->Get(object.ID()); msgpack::unpacker unpacker; unpacker.reserve_buffer(packed_object->size()); @@ -163,8 +175,8 @@ inline std::vector> Ray::Get(const std::vector &ids } template -inline std::vector> Ray::Get(const std::vector> &ids) { - auto object_ids = RayObjectsToObjectIDs(ids); +inline std::vector> Ray::Get(const std::vector> &ids) { + auto object_ids = ObjectRefsToObjectIDs(ids); return Get(object_ids); } @@ -175,39 +187,37 @@ inline WaitResult Ray::Wait(const std::vector &ids, int num_objects, template -inline RayObject Ray::CallInternal(FuncType &func, ExecFuncType &exec_func, - ArgTypes &... args) { +inline TaskCaller Ray::TaskInternal(FuncType &func, ExecFuncType &exec_func, + ArgTypes &... args) { std::shared_ptr buffer(new msgpack::sbuffer()); msgpack::packer packer(buffer.get()); Arguments::WrapArgs(packer, args...); RemoteFunctionPtrHolder ptr; ptr.function_pointer = reinterpret_cast(func); ptr.exec_function_pointer = reinterpret_cast(exec_func); - auto returned_object_id = runtime_->Call(ptr, buffer); - return RayObject(returned_object_id); + return TaskCaller(runtime_, ptr, buffer); } -template -inline RayActor Ray::CreateActorInternal(FuncType &create_func, - ExecFuncType &exec_func, - ArgTypes &... args) { +inline ActorCreator Ray::CreateActorInternal(FuncType &create_func, + ExecFuncType &exec_func, + ArgTypes &... args) { std::shared_ptr buffer(new msgpack::sbuffer()); msgpack::packer packer(buffer.get()); Arguments::WrapArgs(packer, args...); RemoteFunctionPtrHolder ptr; ptr.function_pointer = reinterpret_cast(create_func); ptr.exec_function_pointer = reinterpret_cast(exec_func); - auto returned_actor_id = runtime_->CreateActor(ptr, buffer); - return RayActor(returned_actor_id); + return ActorCreator(runtime_, ptr, buffer); } template -inline RayObject Ray::CallActorInternal(FuncType &actor_func, - ExecFuncType &exec_func, - RayActor &actor, - ArgTypes &... args) { +inline ActorTaskCaller Ray::CallActorInternal(FuncType &actor_func, + ExecFuncType &exec_func, + ActorHandle &actor, + ArgTypes &... args) { std::shared_ptr buffer(new msgpack::sbuffer()); msgpack::packer packer(buffer.get()); Arguments::WrapArgs(packer, args...); @@ -215,8 +225,7 @@ inline RayObject Ray::CallActorInternal(FuncType &actor_func, MemberFunctionPtrHolder holder = *(MemberFunctionPtrHolder *)(&actor_func); ptr.function_pointer = reinterpret_cast(holder.value[0]); ptr.exec_function_pointer = reinterpret_cast(exec_func); - auto returned_object_id = runtime_->CallActor(ptr, actor.ID(), buffer); - return RayObject(returned_object_id); + return ActorTaskCaller(runtime_, actor.ID(), ptr, buffer); } #include diff --git a/cpp/include/ray/api/actor_creator.h b/cpp/include/ray/api/actor_creator.h new file mode 100644 index 000000000..a70fb166a --- /dev/null +++ b/cpp/include/ray/api/actor_creator.h @@ -0,0 +1,41 @@ + +#pragma once + +#include "ray/core.h" + +namespace ray { +namespace api { + +template +class ActorCreator { + public: + ActorCreator(); + + ActorCreator(RayRuntime *runtime, RemoteFunctionPtrHolder ptr, + std::shared_ptr args); + + ActorHandle Remote(); + + private: + RayRuntime *runtime_; + RemoteFunctionPtrHolder ptr_; + std::shared_ptr args_; +}; + +// ---------- implementation ---------- + +template +ActorCreator::ActorCreator() {} + +template +ActorCreator::ActorCreator(RayRuntime *runtime, RemoteFunctionPtrHolder ptr, + std::shared_ptr args) + : runtime_(runtime), ptr_(ptr), args_(args) {} + +template +ActorHandle ActorCreator::Remote() { + auto returned_actor_id = runtime_->CreateActor(ptr_, args_); + return ActorHandle(returned_actor_id); +} +} // namespace api +} // namespace ray diff --git a/cpp/include/ray/api/ray_actor.h b/cpp/include/ray/api/actor_handle.h similarity index 77% rename from cpp/include/ray/api/ray_actor.h rename to cpp/include/ray/api/actor_handle.h index c609e7fe9..475d5f4f2 100644 --- a/cpp/include/ray/api/ray_actor.h +++ b/cpp/include/ray/api/actor_handle.h @@ -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 -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 - /// Make RayActor serializable + /// Make ActorHandle serializable MSGPACK_DEFINE(id_); private: @@ -35,15 +35,15 @@ class RayActor { // ---------- implementation ---------- template -RayActor::RayActor() {} +ActorHandle::ActorHandle() {} template -RayActor::RayActor(const ActorID &id) { +ActorHandle::ActorHandle(const ActorID &id) { id_ = id; } template -const ActorID &RayActor::ID() const { +const ActorID &ActorHandle::ID() const { return id_; } diff --git a/cpp/include/ray/api/actor_task_caller.h b/cpp/include/ray/api/actor_task_caller.h new file mode 100644 index 000000000..f720db777 --- /dev/null +++ b/cpp/include/ray/api/actor_task_caller.h @@ -0,0 +1,43 @@ + +#pragma once + +#include "ray/core.h" + +namespace ray { +namespace api { + +template +class ActorTaskCaller { + public: + ActorTaskCaller(); + + ActorTaskCaller(RayRuntime *runtime, ActorID id, RemoteFunctionPtrHolder ptr, + std::shared_ptr args); + + ObjectRef Remote(); + + private: + RayRuntime *runtime_; + ActorID id_; + RemoteFunctionPtrHolder ptr_; + std::shared_ptr args_; +}; + +// ---------- implementation ---------- + +template +ActorTaskCaller::ActorTaskCaller() {} + +template +ActorTaskCaller::ActorTaskCaller(RayRuntime *runtime, ActorID id, + RemoteFunctionPtrHolder ptr, + std::shared_ptr args) + : runtime_(runtime), id_(id), ptr_(ptr), args_(args) {} + +template +ObjectRef ActorTaskCaller::Remote() { + auto returned_object_id = runtime_->CallActor(ptr_, id_, args_); + return ObjectRef(returned_object_id); +} +} // namespace api +} // namespace ray diff --git a/cpp/include/ray/api/arguments.h b/cpp/include/ray/api/arguments.h index 1642404a5..597184061 100644 --- a/cpp/include/ray/api/arguments.h +++ b/cpp/include/ray/api/arguments.h @@ -36,10 +36,10 @@ inline void Arguments::WrapArgs(msgpack::packer &packer) {} template inline void Arguments::WrapArgs(msgpack::packer &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 inline void Arguments::UnwrapArgs(msgpack::unpacker &unpacker, std::shared_ptr *arg1) { - bool is_ray_object; - Serializer::Deserialize(unpacker, &is_ray_object); - if (is_ray_object) { - RayObject 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 object_ref; + Serializer::Deserialize(unpacker, &object_ref); + *arg1 = object_ref.Get(); } else { Serializer::Deserialize(unpacker, arg1); } diff --git a/cpp/include/ray/api/generated/actor_call.generated.h b/cpp/include/ray/api/generated/actor_call.generated.h index 4ba63903f..1284da9f6 100644 --- a/cpp/include/ray/api/generated/actor_call.generated.h +++ b/cpp/include/ray/api/generated/actor_call.generated.h @@ -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 -RayObject Call(ActorFunc0 actor_func); +ActorTaskCaller Task(ActorFunc0 actor_func); // 1 arg template -RayObject Call(ActorFunc1 actor_func, - Arg1Type arg1); +ActorTaskCaller Task(ActorFunc1 actor_func, + Arg1Type arg1); template -RayObject Call(ActorFunc1 actor_func, - RayObject &arg1); +ActorTaskCaller Task(ActorFunc1 actor_func, + ObjectRef &arg1); // 2 args template -RayObject Call( +ActorTaskCaller Task( ActorFunc2 actor_func, Arg1Type arg1, Arg2Type arg2); template -RayObject Call( +ActorTaskCaller Task( ActorFunc2 actor_func, - RayObject &arg1, Arg2Type arg2); + ObjectRef &arg1, Arg2Type arg2); template -RayObject Call( +ActorTaskCaller Task( ActorFunc2 actor_func, Arg1Type arg1, - RayObject &arg2); + ObjectRef &arg2); template -RayObject Call( +ActorTaskCaller Task( ActorFunc2 actor_func, - RayObject &arg1, RayObject &arg2); + ObjectRef &arg1, ObjectRef &arg2); diff --git a/cpp/include/ray/api/generated/actor_call_impl.generated.h b/cpp/include/ray/api/generated/actor_call_impl.generated.h index f7861baed..fa86aaaf8 100644 --- a/cpp/include/ray/api/generated/actor_call_impl.generated.h +++ b/cpp/include/ray/api/generated/actor_call_impl.generated.h @@ -3,55 +3,55 @@ // 0 args template template -RayObject RayActor::Call( +ActorTaskCaller ActorHandle::Task( ActorFunc0 actor_func) { - return Ray::Call(actor_func, *this); + return Ray::Task(actor_func, *this); } // 1 arg template template -RayObject RayActor::Call( +ActorTaskCaller ActorHandle::Task( ActorFunc1 actor_func, Arg1Type arg1) { - return Ray::Call(actor_func, *this, arg1); + return Ray::Task(actor_func, *this, arg1); } template template -RayObject RayActor::Call( - ActorFunc1 actor_func, RayObject &arg1) { - return Ray::Call(actor_func, *this, arg1); +ActorTaskCaller ActorHandle::Task( + ActorFunc1 actor_func, ObjectRef &arg1) { + return Ray::Task(actor_func, *this, arg1); } // 2 args template template -RayObject RayActor::Call( +ActorTaskCaller ActorHandle::Task( ActorFunc2 actor_func, Arg1Type arg1, Arg2Type arg2) { - return Ray::Call(actor_func, *this, arg1, arg2); + return Ray::Task(actor_func, *this, arg1, arg2); } template template -RayObject RayActor::Call( +ActorTaskCaller ActorHandle::Task( ActorFunc2 actor_func, - RayObject &arg1, Arg2Type arg2) { - return Ray::Call(actor_func, *this, arg1, arg2); + ObjectRef &arg1, Arg2Type arg2) { + return Ray::Task(actor_func, *this, arg1, arg2); } template template -RayObject RayActor::Call( +ActorTaskCaller ActorHandle::Task( ActorFunc2 actor_func, Arg1Type arg1, - RayObject &arg2) { - return Ray::Call(actor_func, *this, arg1, arg2); + ObjectRef &arg2) { + return Ray::Task(actor_func, *this, arg1, arg2); } template template -RayObject RayActor::Call( +ActorTaskCaller ActorHandle::Task( ActorFunc2 actor_func, - RayObject &arg1, RayObject &arg2) { - return Ray::Call(actor_func, *this, arg1, arg2); + ObjectRef &arg1, ObjectRef &arg2) { + return Ray::Task(actor_func, *this, arg1, arg2); } diff --git a/cpp/include/ray/api/generated/call_actors.generated.h b/cpp/include/ray/api/generated/call_actors.generated.h index cd7022c5d..fc8fa2912 100644 --- a/cpp/include/ray/api/generated/call_actors.generated.h +++ b/cpp/include/ray/api/generated/call_actors.generated.h @@ -4,35 +4,37 @@ // 0 args template -static RayObject Call(ActorFunc0 actor_func, - RayActor &actor); +static ActorTaskCaller Task(ActorFunc0 actor_func, + ActorHandle &actor); // 1 arg template -static RayObject Call(ActorFunc1 actor_func, - RayActor &actor, Arg1Type arg1); +static ActorTaskCaller Task( + ActorFunc1 actor_func, ActorHandle &actor, + Arg1Type arg1); template -static RayObject Call(ActorFunc1 actor_func, - RayActor &actor, RayObject &arg1); +static ActorTaskCaller Task( + ActorFunc1 actor_func, ActorHandle &actor, + ObjectRef &arg1); // 2 args template -static RayObject Call( +static ActorTaskCaller Task( ActorFunc2 actor_func, - RayActor &actor, Arg1Type arg1, Arg2Type arg2); + ActorHandle &actor, Arg1Type arg1, Arg2Type arg2); template -static RayObject Call( +static ActorTaskCaller Task( ActorFunc2 actor_func, - RayActor &actor, RayObject &arg1, Arg2Type arg2); + ActorHandle &actor, ObjectRef &arg1, Arg2Type arg2); template -static RayObject Call( +static ActorTaskCaller Task( ActorFunc2 actor_func, - RayActor &actor, Arg1Type arg1, RayObject &arg2); + ActorHandle &actor, Arg1Type arg1, ObjectRef &arg2); template -static RayObject Call( +static ActorTaskCaller Task( ActorFunc2 actor_func, - RayActor &actor, RayObject &arg1, RayObject &arg2); + ActorHandle &actor, ObjectRef &arg1, ObjectRef &arg2); diff --git a/cpp/include/ray/api/generated/call_actors_impl.generated.h b/cpp/include/ray/api/generated/call_actors_impl.generated.h index 849981231..bc59db465 100644 --- a/cpp/include/ray/api/generated/call_actors_impl.generated.h +++ b/cpp/include/ray/api/generated/call_actors_impl.generated.h @@ -1,59 +1,61 @@ // TODO(Guyang Song): code generation // 0 args template -RayObject Ray::Call(ActorFunc0 actor_func, - RayActor &actor) { +ActorTaskCaller Ray::Task(ActorFunc0 actor_func, + ActorHandle &actor) { return CallActorInternal( actor_func, ActorExecFunction, actor); } // 1 arg template -RayObject Ray::Call(ActorFunc1 actor_func, - RayActor &actor, Arg1Type arg1) { +ActorTaskCaller Ray::Task( + ActorFunc1 actor_func, ActorHandle &actor, + Arg1Type arg1) { return CallActorInternal( actor_func, ActorExecFunction, actor, arg1); } template -RayObject Ray::Call(ActorFunc1 actor_func, - RayActor &actor, RayObject &arg1) { +ActorTaskCaller Ray::Task( + ActorFunc1 actor_func, ActorHandle &actor, + ObjectRef &arg1) { return CallActorInternal( actor_func, ActorExecFunction, actor, arg1); } // 2 args template -RayObject Ray::Call( +ActorTaskCaller Ray::Task( ActorFunc2 actor_func, - RayActor &actor, Arg1Type arg1, Arg2Type arg2) { + ActorHandle &actor, Arg1Type arg1, Arg2Type arg2) { return CallActorInternal( actor_func, ActorExecFunction, actor, arg1, arg2); } template -RayObject Ray::Call( +ActorTaskCaller Ray::Task( ActorFunc2 actor_func, - RayActor &actor, RayObject &arg1, Arg2Type arg2) { + ActorHandle &actor, ObjectRef &arg1, Arg2Type arg2) { return CallActorInternal( actor_func, ActorExecFunction, actor, arg1, arg2); } template -RayObject Ray::Call( +ActorTaskCaller Ray::Task( ActorFunc2 actor_func, - RayActor &actor, Arg1Type arg1, RayObject &arg2) { + ActorHandle &actor, Arg1Type arg1, ObjectRef &arg2) { return CallActorInternal( actor_func, ActorExecFunction, actor, arg1, arg2); } template -RayObject Ray::Call( +ActorTaskCaller Ray::Task( ActorFunc2 actor_func, - RayActor &actor, RayObject &arg1, RayObject &arg2) { + ActorHandle &actor, ObjectRef &arg1, ObjectRef &arg2) { return CallActorInternal( actor_func, ActorExecFunction, actor, arg1, arg2); diff --git a/cpp/include/ray/api/generated/call_funcs.generated.h b/cpp/include/ray/api/generated/call_funcs.generated.h index 6850bc289..a29c361ba 100644 --- a/cpp/include/ray/api/generated/call_funcs.generated.h +++ b/cpp/include/ray/api/generated/call_funcs.generated.h @@ -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 -static RayObject Call(Func0 func); +static TaskCaller Task(Func0 func); // 1 arg template -static RayObject Call(Func1 func, Arg1Type arg1); +static TaskCaller Task(Func1 func, Arg1Type arg1); template -static RayObject Call(Func1 func, - RayObject &arg1); +static TaskCaller Task(Func1 func, + ObjectRef &arg1); // 2 args template -static RayObject Call(Func2 func, - Arg1Type arg1, Arg2Type arg2); +static TaskCaller Task(Func2 func, + Arg1Type arg1, Arg2Type arg2); template -static RayObject Call(Func2 func, - RayObject &arg1, Arg2Type arg2); +static TaskCaller Task(Func2 func, + ObjectRef &arg1, Arg2Type arg2); template -static RayObject Call(Func2 func, - Arg1Type arg1, RayObject &arg2); +static TaskCaller Task(Func2 func, + Arg1Type arg1, ObjectRef &arg2); template -static RayObject Call(Func2 func, - RayObject &arg1, RayObject &arg2); \ No newline at end of file +static TaskCaller Task(Func2 func, + ObjectRef &arg1, ObjectRef &arg2); \ No newline at end of file diff --git a/cpp/include/ray/api/generated/call_funcs_impl.generated.h b/cpp/include/ray/api/generated/call_funcs_impl.generated.h index b9c8d6f71..1dd972a3b 100644 --- a/cpp/include/ray/api/generated/call_funcs_impl.generated.h +++ b/cpp/include/ray/api/generated/call_funcs_impl.generated.h @@ -2,47 +2,47 @@ // 0 args template -RayObject Ray::Call(Func0 func) { - return CallInternal(func, NormalExecFunction); +TaskCaller Ray::Task(Func0 func) { + return TaskInternal(func, NormalExecFunction); } // 1 arg template -RayObject Ray::Call(Func1 func, Arg1Type arg1) { - return CallInternal(func, NormalExecFunction, arg1); +TaskCaller Ray::Task(Func1 func, Arg1Type arg1) { + return TaskInternal(func, NormalExecFunction, arg1); } template -RayObject Ray::Call(Func1 func, - RayObject &arg1) { - return CallInternal(func, NormalExecFunction, arg1); +TaskCaller Ray::Task(Func1 func, + ObjectRef &arg1) { + return TaskInternal(func, NormalExecFunction, arg1); } // 2 args template -RayObject Ray::Call(Func2 func, Arg1Type arg1, - Arg2Type arg2) { - return CallInternal( +TaskCaller Ray::Task(Func2 func, + Arg1Type arg1, Arg2Type arg2) { + return TaskInternal( func, NormalExecFunction, arg1, arg2); } template -RayObject Ray::Call(Func2 func, - RayObject &arg1, Arg2Type arg2) { - return CallInternal( +TaskCaller Ray::Task(Func2 func, + ObjectRef &arg1, Arg2Type arg2) { + return TaskInternal( func, NormalExecFunction, arg1, arg2); } template -RayObject Ray::Call(Func2 func, Arg1Type arg1, - RayObject &arg2) { - return CallInternal( +TaskCaller Ray::Task(Func2 func, + Arg1Type arg1, ObjectRef &arg2) { + return TaskInternal( func, NormalExecFunction, arg1, arg2); } template -RayObject Ray::Call(Func2 func, - RayObject &arg1, RayObject &arg2) { - return CallInternal( +TaskCaller Ray::Task(Func2 func, + ObjectRef &arg1, ObjectRef &arg2) { + return TaskInternal( func, NormalExecFunction, arg1, arg2); } \ No newline at end of file diff --git a/cpp/include/ray/api/generated/create_actors.generated.h b/cpp/include/ray/api/generated/create_actors.generated.h index 1bf43d592..0e73b1b81 100644 --- a/cpp/include/ray/api/generated/create_actors.generated.h +++ b/cpp/include/ray/api/generated/create_actors.generated.h @@ -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 -static RayActor CreateActor(CreateActorFunc0 create_func); +template +static ActorCreator Actor(CreateActorFunc0 create_func); // 1 arg -template -static RayActor CreateActor( - CreateActorFunc1 create_func, Arg1Type arg1); +template +static ActorCreator Actor(CreateActorFunc1 create_func, + Arg1Type arg1); -template -static RayActor CreateActor( - CreateActorFunc1 create_func, RayObject &arg1); +template +static ActorCreator Actor(CreateActorFunc1 create_func, + ObjectRef &arg1); // 2 args -template -static RayActor CreateActor( - CreateActorFunc2 create_func, Arg1Type arg1, +template +static ActorCreator Actor( + CreateActorFunc2 create_func, Arg1Type arg1, Arg2Type arg2); -template -static RayActor CreateActor( - CreateActorFunc2 create_func, - RayObject &arg1, Arg2Type arg2); +template +static ActorCreator Actor( + CreateActorFunc2 create_func, + ObjectRef &arg1, Arg2Type arg2); -template -static RayActor CreateActor( - CreateActorFunc2 create_func, Arg1Type arg1, - RayObject &arg2); +template +static ActorCreator Actor( + CreateActorFunc2 create_func, Arg1Type arg1, + ObjectRef &arg2); -template -static RayActor CreateActor( - CreateActorFunc2 create_func, - RayObject &arg1, RayObject &arg2); \ No newline at end of file +template +static ActorCreator Actor( + CreateActorFunc2 create_func, + ObjectRef &arg1, ObjectRef &arg2); \ No newline at end of file diff --git a/cpp/include/ray/api/generated/create_actors_impl.generated.h b/cpp/include/ray/api/generated/create_actors_impl.generated.h index 6d096a0a7..24d957ea4 100644 --- a/cpp/include/ray/api/generated/create_actors_impl.generated.h +++ b/cpp/include/ray/api/generated/create_actors_impl.generated.h @@ -1,56 +1,56 @@ // TODO(Guyang Song): code generation // 0 args -template -RayActor Ray::CreateActor(CreateActorFunc0 create_func) { - return CreateActorInternal(create_func, - CreateActorExecFunction); +template +ActorCreator Ray::Actor(CreateActorFunc0 create_func) { + return CreateActorInternal(create_func, + CreateActorExecFunction); } // 1 arg -template -RayActor Ray::CreateActor(CreateActorFunc1 create_func, - Arg1Type arg1) { - return CreateActorInternal( - create_func, CreateActorExecFunction, arg1); +template +ActorCreator Ray::Actor(CreateActorFunc1 create_func, + Arg1Type arg1) { + return CreateActorInternal( + create_func, CreateActorExecFunction, arg1); } -template -RayActor Ray::CreateActor(CreateActorFunc1 create_func, - RayObject &arg1) { - return CreateActorInternal( - create_func, CreateActorExecFunction, arg1); +template +ActorCreator Ray::Actor(CreateActorFunc1 create_func, + ObjectRef &arg1) { + return CreateActorInternal( + create_func, CreateActorExecFunction, arg1); } // 2 args -template -RayActor Ray::CreateActor( - CreateActorFunc2 create_func, Arg1Type arg1, +template +ActorCreator Ray::Actor( + CreateActorFunc2 create_func, Arg1Type arg1, Arg2Type arg2) { - return CreateActorInternal( - create_func, CreateActorExecFunction, arg1, arg2); + return CreateActorInternal( + create_func, CreateActorExecFunction, arg1, arg2); } -template -RayActor Ray::CreateActor( - CreateActorFunc2 create_func, - RayObject &arg1, Arg2Type arg2) { - return CreateActorInternal( - create_func, CreateActorExecFunction, arg1, arg2); +template +ActorCreator Ray::Actor( + CreateActorFunc2 create_func, + ObjectRef &arg1, Arg2Type arg2) { + return CreateActorInternal( + create_func, CreateActorExecFunction, arg1, arg2); } -template -RayActor Ray::CreateActor( - CreateActorFunc2 create_func, Arg1Type arg1, - RayObject &arg2) { - return CreateActorInternal( - create_func, CreateActorExecFunction, arg1, arg2); +template +ActorCreator Ray::Actor( + CreateActorFunc2 create_func, Arg1Type arg1, + ObjectRef &arg2) { + return CreateActorInternal( + create_func, CreateActorExecFunction, arg1, arg2); } -template -RayActor Ray::CreateActor( - CreateActorFunc2 create_func, - RayObject &arg1, RayObject &arg2) { - return CreateActorInternal( - create_func, CreateActorExecFunction, arg1, arg2); +template +ActorCreator Ray::Actor( + CreateActorFunc2 create_func, + ObjectRef &arg1, ObjectRef &arg2) { + return CreateActorInternal( + create_func, CreateActorExecFunction, arg1, arg2); } \ No newline at end of file diff --git a/cpp/include/ray/api/ray_object.h b/cpp/include/ray/api/object_ref.h similarity index 69% rename from cpp/include/ray/api/ray_object.h rename to cpp/include/ray/api/object_ref.h index aeb9d24a6..54f7823ae 100644 --- a/cpp/include/ray/api/ray_object.h +++ b/cpp/include/ray/api/object_ref.h @@ -14,13 +14,13 @@ namespace api { /// Represents an object in the object store.. /// \param T The type of object. template -class RayObject { +class ObjectRef { public: - RayObject(); + ObjectRef(); - RayObject(const ObjectID &id); + ObjectRef(const ObjectID &id); - bool operator==(const RayObject &object) const; + bool operator==(const ObjectRef &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 Get() const; - /// Make RayObject serializable + /// Make ObjectRef serializable MSGPACK_DEFINE(id_); private: @@ -42,25 +42,25 @@ class RayObject { #include template -RayObject::RayObject() {} +ObjectRef::ObjectRef() {} template -RayObject::RayObject(const ObjectID &id) { +ObjectRef::ObjectRef(const ObjectID &id) { id_ = id; } template -inline bool RayObject::operator==(const RayObject &object) const { +inline bool ObjectRef::operator==(const ObjectRef &object) const { return id_ == object.id_; } template -const ObjectID &RayObject::ID() const { +const ObjectID &ObjectRef::ID() const { return id_; } template -inline std::shared_ptr RayObject::Get() const { +inline std::shared_ptr ObjectRef::Get() const { return Ray::Get(*this); } } // namespace api diff --git a/cpp/include/ray/api/task_caller.h b/cpp/include/ray/api/task_caller.h new file mode 100644 index 000000000..763e6089a --- /dev/null +++ b/cpp/include/ray/api/task_caller.h @@ -0,0 +1,41 @@ + +#pragma once + +#include "ray/core.h" + +namespace ray { +namespace api { + +template +class TaskCaller { + public: + TaskCaller(); + + TaskCaller(RayRuntime *runtime, RemoteFunctionPtrHolder ptr, + std::shared_ptr args); + + ObjectRef Remote(); + + private: + RayRuntime *runtime_; + RemoteFunctionPtrHolder ptr_; + std::shared_ptr args_; +}; + +// ---------- implementation ---------- + +template +TaskCaller::TaskCaller() {} + +template +TaskCaller::TaskCaller(RayRuntime *runtime, RemoteFunctionPtrHolder ptr, + std::shared_ptr args) + : runtime_(runtime), ptr_(ptr), args_(args) {} + +template +ObjectRef TaskCaller::Remote() { + auto returned_object_id = runtime_->Call(ptr_, args_); + return ObjectRef(returned_object_id); +} +} // namespace api +} // namespace ray diff --git a/cpp/src/example/example.cc b/cpp/src/example/example.cc index 04ed01dc5..28a067893 100644 --- a/cpp/src/example/example.cc +++ b/cpp/src/example/example.cc @@ -36,9 +36,9 @@ int main() { auto getRsult = obj.Get(); /// general function remote call(args passed by value) - auto r0 = Ray::Call(Return1); - auto r1 = Ray::Call(Plus1, 1); - auto r2 = Ray::Call(Plus, 1, 2); + auto r0 = Ray::Task(Return1).Remote(); + auto r1 = Ray::Task(Plus1, 1).Remote(); + auto r2 = Ray::Task(Plus, 1, 2).Remote(); int result0 = *(r0.Get()); int result1 = *(r1.Get()); @@ -48,9 +48,9 @@ int main() { << result2 << std::endl; /// general function remote call(args passed by reference) - auto r3 = Ray::Call(Return1); - auto r4 = Ray::Call(Plus1, r3); - auto r5 = Ray::Call(Plus, r4, 1); + auto r3 = Ray::Task(Return1).Remote(); + auto r4 = Ray::Task(Plus1, r3).Remote(); + auto r5 = Ray::Task(Plus, r4, 1).Remote(); int result3 = *(r3.Get()); int result4 = *(r4.Get()); @@ -60,11 +60,11 @@ int main() { << result5 << std::endl; /// create actor and actor function remote call - RayActor actor = Ray::CreateActor(Counter::FactoryCreate); - auto r6 = actor.Call(&Counter::Add, 5); - auto r7 = actor.Call(&Counter::Add, 1); - auto r8 = actor.Call(&Counter::Add, 1); - auto r9 = actor.Call(&Counter::Add, r8); + ActorHandle actor = Ray::Actor(Counter::FactoryCreate).Remote(); + auto r6 = actor.Task(&Counter::Add, 5).Remote(); + auto r7 = actor.Task(&Counter::Add, 1).Remote(); + auto r8 = actor.Task(&Counter::Add, 1).Remote(); + auto r9 = actor.Task(&Counter::Add, r8).Remote(); int result6 = *(r6.Get()); int result7 = *(r7.Get()); diff --git a/cpp/src/ray/runtime/object/object_store.h b/cpp/src/ray/runtime/object/object_store.h index a506a8987..cc7839df4 100644 --- a/cpp/src/ray/runtime/object/object_store.h +++ b/cpp/src/ray/runtime/object/object_store.h @@ -40,7 +40,7 @@ class ObjectStore { std::vector> Get( const std::vector &ids, int timeout_ms = default_get_timeout_ms); - /// Wait for a list of RayObjects to be locally available, + /// Wait for a list of ObjectRefs to be locally available, /// until specified number of objects are ready, or specified timeout has passed. /// /// \param[in] ids The object id array which should be waited. diff --git a/cpp/src/ray/test/api_test.cc b/cpp/src/ray/test/api_test.cc index c24a7c2d9..59de36ea7 100644 --- a/cpp/src/ray/test/api_test.cc +++ b/cpp/src/ray/test/api_test.cc @@ -44,9 +44,9 @@ TEST(RayApiTest, PutTest) { TEST(RayApiTest, WaitTest) { Ray::Init(); - auto r0 = Ray::Call(Return1); - auto r1 = Ray::Call(Plus1, 3); - auto r2 = Ray::Call(Plus, 2, 3); + auto r0 = Ray::Task(Return1).Remote(); + auto r1 = Ray::Task(Plus1, 3).Remote(); + auto r2 = Ray::Task(Plus, 2, 3).Remote(); std::vector objects = {r0.ID(), r1.ID(), r2.ID()}; WaitResult result = Ray::Wait(objects, 3, 1000); EXPECT_EQ(result.ready.size(), 3); @@ -59,9 +59,9 @@ TEST(RayApiTest, WaitTest) { } TEST(RayApiTest, CallWithValueTest) { - auto r0 = Ray::Call(Return1); - auto r1 = Ray::Call(Plus1, 3); - auto r2 = Ray::Call(Plus, 2, 3); + auto r0 = Ray::Task(Return1).Remote(); + auto r1 = Ray::Task(Plus1, 3).Remote(); + auto r2 = Ray::Task(Plus, 2, 3).Remote(); int result0 = *(r0.Get()); int result1 = *(r1.Get()); @@ -73,11 +73,11 @@ TEST(RayApiTest, CallWithValueTest) { } TEST(RayApiTest, CallWithObjectTest) { - auto rt0 = Ray::Call(Return1); - auto rt1 = Ray::Call(Plus1, rt0); - auto rt2 = Ray::Call(Plus, rt1, 3); - auto rt3 = Ray::Call(Plus1, 3); - auto rt4 = Ray::Call(Plus, rt2, rt3); + auto rt0 = Ray::Task(Return1).Remote(); + auto rt1 = Ray::Task(Plus1, rt0).Remote(); + auto rt2 = Ray::Task(Plus, rt1, 3).Remote(); + auto rt3 = Ray::Task(Plus1, 3).Remote(); + auto rt4 = Ray::Task(Plus, rt2, rt3).Remote(); int return0 = *(rt0.Get()); int return1 = *(rt1.Get()); @@ -94,11 +94,11 @@ TEST(RayApiTest, CallWithObjectTest) { TEST(RayApiTest, ActorTest) { Ray::Init(); - RayActor actor = Ray::CreateActor(Counter::FactoryCreate); - auto rt1 = actor.Call(&Counter::Add, 1); - auto rt2 = actor.Call(&Counter::Add, 2); - auto rt3 = actor.Call(&Counter::Add, 3); - auto rt4 = actor.Call(&Counter::Add, rt3); + ActorHandle actor = Ray::Actor(Counter::FactoryCreate).Remote(); + auto rt1 = actor.Task(&Counter::Add, 1).Remote(); + auto rt2 = actor.Task(&Counter::Add, 2).Remote(); + auto rt3 = actor.Task(&Counter::Add, 3).Remote(); + auto rt4 = actor.Task(&Counter::Add, rt3).Remote(); int return1 = *(rt1.Get()); int return2 = *(rt2.Get()); @@ -124,7 +124,7 @@ TEST(RayApiTest, CompareWithFuture) { // Ray API Ray::Init(); - auto f3 = Ray::Call(Plus1, 1); + auto f3 = Ray::Task(Plus1, 1).Remote(); int rt3 = *f3.Get(); EXPECT_EQ(rt1, 2); diff --git a/cpp/src/ray/test/slow_function_test.cc b/cpp/src/ray/test/slow_function_test.cc index 4e4b8ab62..7fceace71 100644 --- a/cpp/src/ray/test/slow_function_test.cc +++ b/cpp/src/ray/test/slow_function_test.cc @@ -15,10 +15,10 @@ TEST(RaySlowFunctionTest, BaseTest) { Ray::Init(); auto time1 = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()); - auto r0 = Ray::Call(slow_function, 1); - auto r1 = Ray::Call(slow_function, 2); - auto r2 = Ray::Call(slow_function, 3); - auto r3 = Ray::Call(slow_function, 4); + auto r0 = Ray::Task(slow_function, 1).Remote(); + auto r1 = Ray::Task(slow_function, 2).Remote(); + auto r2 = Ray::Task(slow_function, 3).Remote(); + auto r3 = Ray::Task(slow_function, 4).Remote(); int result0 = *(r0.Get()); int result1 = *(r1.Get());