[cpp worker] support pass by reference on cluster mode (#11753)

This commit is contained in:
SongGuyang
2020-11-17 06:30:35 +08:00
committed by GitHub
parent 8d599bb3f5
commit df2c2a7ce5
22 changed files with 235 additions and 223 deletions
+10 -12
View File
@@ -197,13 +197,12 @@ template <typename ReturnType, typename FuncType, typename ExecFuncType,
typename... ArgTypes>
inline TaskCaller<ReturnType> Ray::TaskInternal(FuncType &func, ExecFuncType &exec_func,
ArgTypes &... args) {
std::shared_ptr<msgpack::sbuffer> buffer(new msgpack::sbuffer());
msgpack::packer<msgpack::sbuffer> packer(buffer.get());
Arguments::WrapArgs(packer, args...);
std::vector<std::unique_ptr<::ray::TaskArg>> task_args;
Arguments::WrapArgs(&task_args, args...);
RemoteFunctionPtrHolder ptr;
ptr.function_pointer = reinterpret_cast<uintptr_t>(func);
ptr.exec_function_pointer = reinterpret_cast<uintptr_t>(exec_func);
return TaskCaller<ReturnType>(runtime_.get(), ptr, buffer);
return TaskCaller<ReturnType>(runtime_.get(), ptr, std::move(task_args));
}
template <typename ActorType, typename FuncType, typename ExecFuncType,
@@ -211,13 +210,12 @@ template <typename ActorType, typename FuncType, typename ExecFuncType,
inline ActorCreator<ActorType> Ray::CreateActorInternal(FuncType &create_func,
ExecFuncType &exec_func,
ArgTypes &... args) {
std::shared_ptr<msgpack::sbuffer> buffer(new msgpack::sbuffer());
msgpack::packer<msgpack::sbuffer> packer(buffer.get());
Arguments::WrapArgs(packer, args...);
std::vector<std::unique_ptr<::ray::TaskArg>> task_args;
Arguments::WrapArgs(&task_args, args...);
RemoteFunctionPtrHolder ptr;
ptr.function_pointer = reinterpret_cast<uintptr_t>(create_func);
ptr.exec_function_pointer = reinterpret_cast<uintptr_t>(exec_func);
return ActorCreator<ActorType>(runtime_.get(), ptr, buffer);
return ActorCreator<ActorType>(runtime_.get(), ptr, std::move(task_args));
}
template <typename ReturnType, typename ActorType, typename FuncType,
@@ -226,14 +224,14 @@ inline ActorTaskCaller<ReturnType> Ray::CallActorInternal(FuncType &actor_func,
ExecFuncType &exec_func,
ActorHandle<ActorType> &actor,
ArgTypes &... args) {
std::shared_ptr<msgpack::sbuffer> buffer(new msgpack::sbuffer());
msgpack::packer<msgpack::sbuffer> packer(buffer.get());
Arguments::WrapArgs(packer, args...);
std::vector<std::unique_ptr<::ray::TaskArg>> task_args;
Arguments::WrapArgs(&task_args, args...);
RemoteFunctionPtrHolder ptr;
MemberFunctionPtrHolder holder = *(MemberFunctionPtrHolder *)(&actor_func);
ptr.function_pointer = reinterpret_cast<uintptr_t>(holder.value[0]);
ptr.exec_function_pointer = reinterpret_cast<uintptr_t>(exec_func);
return ActorTaskCaller<ReturnType>(runtime_.get(), actor.ID(), ptr, buffer);
return ActorTaskCaller<ReturnType>(runtime_.get(), actor.ID(), ptr,
std::move(task_args));
}
// TODO(barakmich): These includes are generated files that do not contain their
+4 -4
View File
@@ -12,14 +12,14 @@ class ActorCreator {
ActorCreator();
ActorCreator(RayRuntime *runtime, RemoteFunctionPtrHolder ptr,
std::shared_ptr<msgpack::sbuffer> args);
std::vector<std::unique_ptr<::ray::TaskArg>> &&args);
ActorHandle<ActorType> Remote();
private:
RayRuntime *runtime_;
RemoteFunctionPtrHolder ptr_;
std::shared_ptr<msgpack::sbuffer> args_;
std::vector<std::unique_ptr<::ray::TaskArg>> args_;
};
// ---------- implementation ----------
@@ -29,8 +29,8 @@ 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) {}
std::vector<std::unique_ptr<::ray::TaskArg>> &&args)
: runtime_(runtime), ptr_(ptr), args_(std::move(args)) {}
template <typename ActorType>
ActorHandle<ActorType> ActorCreator<ActorType>::Remote() {
+6 -6
View File
@@ -12,7 +12,7 @@ class ActorTaskCaller {
ActorTaskCaller();
ActorTaskCaller(RayRuntime *runtime, ActorID id, RemoteFunctionPtrHolder ptr,
std::shared_ptr<msgpack::sbuffer> args);
std::vector<std::unique_ptr<::ray::TaskArg>> &&args);
ObjectRef<ReturnType> Remote();
@@ -20,7 +20,7 @@ class ActorTaskCaller {
RayRuntime *runtime_;
ActorID id_;
RemoteFunctionPtrHolder ptr_;
std::shared_ptr<msgpack::sbuffer> args_;
std::vector<std::unique_ptr<::ray::TaskArg>> args_;
};
// ---------- implementation ----------
@@ -29,10 +29,10 @@ 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) {}
ActorTaskCaller<ReturnType>::ActorTaskCaller(
RayRuntime *runtime, ActorID id, RemoteFunctionPtrHolder ptr,
std::vector<std::unique_ptr<::ray::TaskArg>> &&args)
: runtime_(runtime), id_(id), ptr_(ptr), args_(std::move(args)) {}
template <typename ReturnType>
ObjectRef<ReturnType> ActorTaskCaller<ReturnType>::Remote() {
+71 -44
View File
@@ -1,7 +1,9 @@
#pragma once
#include <ray/api/object_ref.h>
#include <ray/api/serializer.h>
#include "ray/common/task/task_util.h"
#include <msgpack.hpp>
@@ -10,75 +12,100 @@ namespace api {
class Arguments {
public:
static void WrapArgs(msgpack::packer<msgpack::sbuffer> &packer);
static void WrapArgs(std::vector<std::unique_ptr<::ray::TaskArg>> *task_args);
template <typename Arg1Type>
static void WrapArgs(msgpack::packer<msgpack::sbuffer> &packer, Arg1Type &arg1);
template <typename Arg1Type, typename... OtherArgTypes>
static void WrapArgs(msgpack::packer<msgpack::sbuffer> &packer, Arg1Type &arg1,
OtherArgTypes &... args);
static void UnwrapArgs(msgpack::unpacker &unpacker);
static void WrapArgs(std::vector<std::unique_ptr<::ray::TaskArg>> *task_args,
Arg1Type &arg1);
template <typename Arg1Type>
static void UnwrapArgs(msgpack::unpacker &unpacker, std::shared_ptr<Arg1Type> *arg1);
static void WrapArgs(std::vector<std::unique_ptr<::ray::TaskArg>> *task_args,
ObjectRef<Arg1Type> &arg1);
template <typename Arg1Type, typename... OtherArgTypes>
static void UnwrapArgs(msgpack::unpacker &unpacker, std::shared_ptr<Arg1Type> *arg1,
static void WrapArgs(std::vector<std::unique_ptr<::ray::TaskArg>> *task_args,
Arg1Type &arg1, OtherArgTypes &... args);
static void UnwrapArgs(const std::vector<std::shared_ptr<RayObject>> &args_buffer,
int &arg_index);
template <typename Arg1Type>
static void UnwrapArgs(const std::vector<std::shared_ptr<RayObject>> &args_buffer,
int &arg_index, std::shared_ptr<Arg1Type> *arg1);
template <typename Arg1Type, typename... OtherArgTypes>
static void UnwrapArgs(const std::vector<std::shared_ptr<RayObject>> &args_buffer,
int &arg_index, std::shared_ptr<Arg1Type> *arg1,
std::shared_ptr<OtherArgTypes> *... args);
};
// --------- inline implementation ------------
#include <typeinfo>
inline void Arguments::WrapArgs(msgpack::packer<msgpack::sbuffer> &packer) {}
inline void Arguments::WrapArgs(std::vector<std::unique_ptr<::ray::TaskArg>> *task_args) {
}
template <typename Arg1Type>
inline void Arguments::WrapArgs(msgpack::packer<msgpack::sbuffer> &packer,
inline void Arguments::WrapArgs(std::vector<std::unique_ptr<::ray::TaskArg>> *task_args,
Arg1Type &arg1) {
/// TODO(Guyang Song): optimize the memory copy.
msgpack::sbuffer buffer;
msgpack::packer<msgpack::sbuffer> packer(buffer);
/// 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(ObjectRefClassPrefix, 0) == 0) {
/// Pass by reference.
Serializer::Serialize(packer, true);
} else {
/// Pass by value.
Serializer::Serialize(packer, false);
}
RAY_CHECK(type_name.rfind(ObjectRefClassPrefix, 0) != 0)
<< "ObjectRef can not be wrapped";
Serializer::Serialize(packer, arg1);
auto memory_buffer = std::make_shared<::ray::LocalMemoryBuffer>(
reinterpret_cast<uint8_t *>(buffer.data()), buffer.size(), true);
/// Pass by value.
auto task_arg = new TaskArgByValue(std::make_shared<::ray::RayObject>(
memory_buffer, nullptr, std::vector<ObjectID>()));
task_args->emplace_back(task_arg);
}
template <typename Arg1Type, typename... OtherArgTypes>
inline void Arguments::WrapArgs(msgpack::packer<msgpack::sbuffer> &packer, Arg1Type &arg1,
OtherArgTypes &... args) {
WrapArgs(packer, arg1);
WrapArgs(packer, args...);
}
inline void Arguments::UnwrapArgs(msgpack::unpacker &unpacker) {}
template <typename Arg1Type>
inline void Arguments::UnwrapArgs(msgpack::unpacker &unpacker,
std::shared_ptr<Arg1Type> *arg1) {
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);
}
inline void Arguments::WrapArgs(std::vector<std::unique_ptr<::ray::TaskArg>> *task_args,
ObjectRef<Arg1Type> &arg1) {
/// Pass by reference.
auto task_arg = new TaskArgByReference(arg1.ID(), rpc::Address());
task_args->emplace_back(task_arg);
}
template <typename Arg1Type, typename... OtherArgTypes>
inline void Arguments::UnwrapArgs(msgpack::unpacker &unpacker,
std::shared_ptr<Arg1Type> *arg1,
std::shared_ptr<OtherArgTypes> *... args) {
UnwrapArgs(unpacker, arg1);
UnwrapArgs(unpacker, args...);
inline void Arguments::WrapArgs(std::vector<std::unique_ptr<::ray::TaskArg>> *task_args,
Arg1Type &arg1, OtherArgTypes &... args) {
WrapArgs(task_args, arg1);
WrapArgs(task_args, args...);
}
inline void Arguments::UnwrapArgs(
const std::vector<std::shared_ptr<RayObject>> &args_buffer, int &arg_index) {}
template <typename Arg1Type>
inline void Arguments::UnwrapArgs(
const std::vector<std::shared_ptr<RayObject>> &args_buffer, int &arg_index,
std::shared_ptr<Arg1Type> *arg1) {
std::shared_ptr<msgpack::sbuffer> sbuffer;
auto arg_buffer = args_buffer[arg_index]->GetData();
sbuffer = std::make_shared<msgpack::sbuffer>(arg_buffer->Size());
/// TODO(Guyang Song): Avoid the memory copy.
sbuffer->write(reinterpret_cast<const char *>(arg_buffer->Data()), arg_buffer->Size());
msgpack::unpacker unpacker;
unpacker.reserve_buffer(sbuffer->size());
memcpy(unpacker.buffer(), sbuffer->data(), sbuffer->size());
unpacker.buffer_consumed(sbuffer->size());
Serializer::Deserialize(unpacker, arg1);
arg_index++;
}
template <typename Arg1Type, typename... OtherArgTypes>
inline void Arguments::UnwrapArgs(
const std::vector<std::shared_ptr<RayObject>> &args_buffer, int &arg_index,
std::shared_ptr<Arg1Type> *arg1, std::shared_ptr<OtherArgTypes> *... args) {
UnwrapArgs(args_buffer, arg_index, arg1);
UnwrapArgs(args_buffer, arg_index, args...);
}
} // namespace api
@@ -10,13 +10,10 @@
template <typename ReturnType, typename CastReturnType, typename... OtherArgTypes>
std::shared_ptr<msgpack::sbuffer> ExecuteNormalFunction(
uintptr_t base_addr, size_t func_offset,
std::shared_ptr<msgpack::sbuffer> &args_buffer, TaskType task_type,
const std::vector<std::shared_ptr<RayObject>> &args_buffer, TaskType task_type,
std::shared_ptr<OtherArgTypes> &... args) {
msgpack::unpacker unpacker;
unpacker.reserve_buffer(args_buffer->size());
memcpy(unpacker.buffer(), args_buffer->data(), args_buffer->size());
unpacker.buffer_consumed(args_buffer->size());
Arguments::UnwrapArgs(unpacker, &args...);
int arg_index = 0;
Arguments::UnwrapArgs(args_buffer, arg_index, &args...);
ReturnType return_value;
typedef ReturnType (*Func)(OtherArgTypes...);
@@ -33,7 +30,7 @@ std::shared_ptr<msgpack::sbuffer> ExecuteNormalFunction(
template <typename ReturnType, typename ActorType, typename... OtherArgTypes>
std::shared_ptr<msgpack::sbuffer> ExecuteActorFunction(
uintptr_t base_addr, size_t func_offset,
std::shared_ptr<msgpack::sbuffer> &args_buffer,
const std::vector<std::shared_ptr<RayObject>> &args_buffer,
std::shared_ptr<msgpack::sbuffer> &actor_buffer,
std::shared_ptr<OtherArgTypes> &... args) {
msgpack::unpacker actor_unpacker;
@@ -44,11 +41,8 @@ std::shared_ptr<msgpack::sbuffer> ExecuteActorFunction(
Serializer::Deserialize(actor_unpacker, &actor_ptr);
ActorType *actor_object = (ActorType *)actor_ptr;
msgpack::unpacker unpacker;
unpacker.reserve_buffer(args_buffer->size());
memcpy(unpacker.buffer(), args_buffer->data(), args_buffer->size());
unpacker.buffer_consumed(args_buffer->size());
Arguments::UnwrapArgs(unpacker, &args...);
int arg_index = 0;
Arguments::UnwrapArgs(args_buffer, arg_index, &args...);
ReturnType return_value;
typedef ReturnType (ActorType::*Func)(OtherArgTypes...);
@@ -68,7 +62,7 @@ std::shared_ptr<msgpack::sbuffer> ExecuteActorFunction(
template <typename ReturnType>
std::shared_ptr<msgpack::sbuffer> NormalExecFunction(
uintptr_t base_addr, size_t func_offset,
std::shared_ptr<msgpack::sbuffer> &args_buffer) {
const std::vector<std::shared_ptr<RayObject>> &args_buffer) {
return ExecuteNormalFunction<ReturnType, ReturnType>(
base_addr, func_offset, args_buffer, TaskType::NORMAL_TASK);
}
@@ -77,7 +71,7 @@ std::shared_ptr<msgpack::sbuffer> NormalExecFunction(
template <typename ReturnType, typename Arg1Type>
std::shared_ptr<msgpack::sbuffer> NormalExecFunction(
uintptr_t base_addr, size_t func_offset,
std::shared_ptr<msgpack::sbuffer> &args_buffer) {
const std::vector<std::shared_ptr<RayObject>> &args_buffer) {
std::shared_ptr<Arg1Type> arg1_ptr;
return ExecuteNormalFunction<ReturnType, ReturnType>(
base_addr, func_offset, args_buffer, TaskType::NORMAL_TASK, arg1_ptr);
@@ -87,7 +81,7 @@ std::shared_ptr<msgpack::sbuffer> NormalExecFunction(
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
std::shared_ptr<msgpack::sbuffer> NormalExecFunction(
uintptr_t base_addr, size_t func_offset,
std::shared_ptr<msgpack::sbuffer> &args_buffer) {
const std::vector<std::shared_ptr<RayObject>> &args_buffer) {
std::shared_ptr<Arg1Type> arg1_ptr;
std::shared_ptr<Arg2Type> arg2_ptr;
return ExecuteNormalFunction<ReturnType, ReturnType>(
@@ -98,7 +92,7 @@ std::shared_ptr<msgpack::sbuffer> NormalExecFunction(
template <typename ReturnType>
std::shared_ptr<msgpack::sbuffer> CreateActorExecFunction(
uintptr_t base_addr, size_t func_offset,
std::shared_ptr<msgpack::sbuffer> &args_buffer) {
const std::vector<std::shared_ptr<RayObject>> &args_buffer) {
return ExecuteNormalFunction<ReturnType, uintptr_t>(base_addr, func_offset, args_buffer,
TaskType::ACTOR_CREATION_TASK);
}
@@ -107,7 +101,7 @@ std::shared_ptr<msgpack::sbuffer> CreateActorExecFunction(
template <typename ReturnType, typename Arg1Type>
std::shared_ptr<msgpack::sbuffer> CreateActorExecFunction(
uintptr_t base_addr, size_t func_offset,
std::shared_ptr<msgpack::sbuffer> &args_buffer) {
const std::vector<std::shared_ptr<RayObject>> &args_buffer) {
std::shared_ptr<Arg1Type> arg1_ptr;
return ExecuteNormalFunction<ReturnType, uintptr_t>(
base_addr, func_offset, args_buffer, TaskType::ACTOR_CREATION_TASK, arg1_ptr);
@@ -117,7 +111,7 @@ std::shared_ptr<msgpack::sbuffer> CreateActorExecFunction(
template <typename ReturnType, typename Arg1Type, typename Arg2Type>
std::shared_ptr<msgpack::sbuffer> CreateActorExecFunction(
uintptr_t base_addr, size_t func_offset,
std::shared_ptr<msgpack::sbuffer> &args_buffer) {
const std::vector<std::shared_ptr<RayObject>> &args_buffer) {
std::shared_ptr<Arg1Type> arg1_ptr;
std::shared_ptr<Arg2Type> arg2_ptr;
return ExecuteNormalFunction<ReturnType, uintptr_t>(base_addr, func_offset, args_buffer,
@@ -129,7 +123,7 @@ std::shared_ptr<msgpack::sbuffer> CreateActorExecFunction(
template <typename ReturnType, typename ActorType>
std::shared_ptr<msgpack::sbuffer> ActorExecFunction(
uintptr_t base_addr, size_t func_offset,
std::shared_ptr<msgpack::sbuffer> &args_buffer,
const std::vector<std::shared_ptr<RayObject>> &args_buffer,
std::shared_ptr<msgpack::sbuffer> &actor_buffer) {
return ExecuteActorFunction<ReturnType, ActorType>(base_addr, func_offset, args_buffer,
actor_buffer);
@@ -139,7 +133,7 @@ std::shared_ptr<msgpack::sbuffer> ActorExecFunction(
template <typename ReturnType, typename ActorType, typename Arg1Type>
std::shared_ptr<msgpack::sbuffer> ActorExecFunction(
uintptr_t base_addr, size_t func_offset,
std::shared_ptr<msgpack::sbuffer> &args_buffer,
const std::vector<std::shared_ptr<RayObject>> &args_buffer,
std::shared_ptr<msgpack::sbuffer> &actor_buffer) {
std::shared_ptr<Arg1Type> arg1_ptr;
return ExecuteActorFunction<ReturnType, ActorType>(base_addr, func_offset, args_buffer,
@@ -150,7 +144,7 @@ std::shared_ptr<msgpack::sbuffer> ActorExecFunction(
template <typename ReturnType, typename ActorType, typename Arg1Type, typename Arg2Type>
std::shared_ptr<msgpack::sbuffer> ActorExecFunction(
uintptr_t base_addr, size_t func_offset,
std::shared_ptr<msgpack::sbuffer> &args_buffer,
const std::vector<std::shared_ptr<RayObject>> &args_buffer,
std::shared_ptr<msgpack::sbuffer> &actor_buffer) {
std::shared_ptr<Arg1Type> arg1_ptr;
std::shared_ptr<Arg2Type> arg2_ptr;
+3 -3
View File
@@ -37,11 +37,11 @@ class RayRuntime {
int timeout_ms) = 0;
virtual ObjectID Call(const RemoteFunctionPtrHolder &fptr,
std::shared_ptr<msgpack::sbuffer> args) = 0;
std::vector<std::unique_ptr<::ray::TaskArg>> &args) = 0;
virtual ActorID CreateActor(const RemoteFunctionPtrHolder &fptr,
std::shared_ptr<msgpack::sbuffer> args) = 0;
std::vector<std::unique_ptr<::ray::TaskArg>> &args) = 0;
virtual ObjectID CallActor(const RemoteFunctionPtrHolder &fptr, const ActorID &actor,
std::shared_ptr<msgpack::sbuffer> args) = 0;
std::vector<std::unique_ptr<::ray::TaskArg>> &args) = 0;
};
} // namespace api
} // namespace ray
+4 -4
View File
@@ -12,14 +12,14 @@ class TaskCaller {
TaskCaller();
TaskCaller(RayRuntime *runtime, RemoteFunctionPtrHolder ptr,
std::shared_ptr<msgpack::sbuffer> args);
std::vector<std::unique_ptr<::ray::TaskArg>> &&args);
ObjectRef<ReturnType> Remote();
private:
RayRuntime *runtime_;
RemoteFunctionPtrHolder ptr_;
std::shared_ptr<msgpack::sbuffer> args_;
std::vector<std::unique_ptr<::ray::TaskArg>> args_;
};
// ---------- implementation ----------
@@ -29,8 +29,8 @@ 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) {}
std::vector<std::unique_ptr<::ray::TaskArg>> &&args)
: runtime_(runtime), ptr_(ptr), args_(std::move(args)) {}
template <typename ReturnType>
ObjectRef<ReturnType> TaskCaller<ReturnType>::Remote() {