diff --git a/.gitmodules b/.gitmodules index e11057ce3..16059daea 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "docs"] path = docs url = https://github.com/lava-nc/lava-docs.git +[submodule "src/lava/magma/runtime/cpp_message_infrastructure/pybind11"] + path = src/lava/magma/runtime/cpp_message_infrastructure/pybind11 + url = https://github.com/pybind/pybind11.git diff --git a/src/lava/magma/runtime/cpp_message_infrastructure/CMakeLists.txt b/src/lava/magma/runtime/cpp_message_infrastructure/CMakeLists.txt new file mode 100644 index 000000000..6002ec4e6 --- /dev/null +++ b/src/lava/magma/runtime/cpp_message_infrastructure/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 2.8.3) +project(message_passing) + +set (CMAKE_CXX_STANDARD 17) +include_directories(include) + +file (GLOB MULTI_PROC_SRCS "src/multiprocessing.cc") + +file (GLOB PY_WRAPPER "src/message_infrastructure_py_wrapper.cc") + +add_subdirectory(pybind11) + +add_library(multi_processing SHARED ${MULTI_PROC_SRCS}) + +pybind11_add_module(MessageInfrastructurePywrapper ${PY_WRAPPER}) +target_link_libraries(MessageInfrastructurePywrapper PRIVATE multi_processing) diff --git a/src/lava/magma/runtime/cpp_message_infrastructure/docs/.gitkeep b/src/lava/magma/runtime/cpp_message_infrastructure/docs/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/src/lava/magma/runtime/cpp_message_infrastructure/docs/run_example.md b/src/lava/magma/runtime/cpp_message_infrastructure/docs/run_example.md new file mode 100644 index 000000000..448b4a188 --- /dev/null +++ b/src/lava/magma/runtime/cpp_message_infrastructure/docs/run_example.md @@ -0,0 +1,13 @@ +# An Example for MessageInfrastructurePywrapper + +*Welcome to the messaging refactory project, and this will show how an example run* + +``` +mkdir build +cd build +cmake .. +make +cd .. +source setenv.sh +python test/test_example.py +``` diff --git a/src/lava/magma/runtime/cpp_message_infrastructure/include/abstract_actor.h b/src/lava/magma/runtime/cpp_message_infrastructure/include/abstract_actor.h new file mode 100644 index 000000000..c4aeab341 --- /dev/null +++ b/src/lava/magma/runtime/cpp_message_infrastructure/include/abstract_actor.h @@ -0,0 +1,40 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: BSD-3-Clause +// See: https://spdx.org/licenses/ + +#ifndef ABSTACT_ACTOR_H_ +#define ABSTACT_ACTOR_H_ + +namespace message_infrastrature { + +class AbstractActor { + public: + virtual int GetPid() = 0; + virtual int Stop() = 0; + virtual int Pause() = 0; + + int pid_; +}; + +class PosixActor : public AbstractActor { + public: + explicit PosixActor(int pid){ + this->pid_ = pid; + } + int GetPid(){ + return this->pid_; + }; + int Stop(){ + return 0; + }; + int Pause(){ + return 0; + }; +}; + +using ActorPtr = AbstractActor *; +using PosixActorPtr = PosixActor *; + +} // namespace message_infrastrature + +#endif \ No newline at end of file diff --git a/src/lava/magma/runtime/cpp_message_infrastructure/include/abstract_channel.h b/src/lava/magma/runtime/cpp_message_infrastructure/include/abstract_channel.h new file mode 100644 index 000000000..80c658066 --- /dev/null +++ b/src/lava/magma/runtime/cpp_message_infrastructure/include/abstract_channel.h @@ -0,0 +1,22 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: BSD-3-Clause +// See: https://spdx.org/licenses/ + +#ifndef ABSTACT_CHANNEL_H_ +#define ABSTACT_CHANNEL_H_ + +#include "abstract_port.h" +#include "utils.h" +#include + +namespace message_infrastrature { + +class AbstractChannel { + public: + std::shared_ptr src_port_; + std::shared_ptr dst_port_; +}; + +} // namespace message_infrastrature + +#endif \ No newline at end of file diff --git a/src/lava/magma/runtime/cpp_message_infrastructure/include/abstract_port.h b/src/lava/magma/runtime/cpp_message_infrastructure/include/abstract_port.h new file mode 100644 index 000000000..4739933e6 --- /dev/null +++ b/src/lava/magma/runtime/cpp_message_infrastructure/include/abstract_port.h @@ -0,0 +1,43 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: BSD-3-Clause +// See: https://spdx.org/licenses/ + +#ifndef ABSTRACT_PORT_H_ +#define ABSTRACT_PORT_H_ + +#include +#include + +#include "shm.h" +#include "utils.h" + +namespace message_infrastrature { + +class AbstractPort { + public: + virtual int Start() = 0; + virtual int Join() = 0; + + std::string name_; + DataType dtype_; + std::vector shape_; + size_t size_; +}; + +class AbstractSendPort : public AbstractPort { + public: + virtual int Send() = 0; +}; + +class AbstractRecvPort : public AbstractPort { + public: + virtual int Recv() = 0; +}; + +using PortPtr = AbstractPort *; +using SendPortPtr = AbstractSendPort *; +using RecvPortPtr = AbstractRecvPort *; + +} // namespace message_infrastrature + +#endif \ No newline at end of file diff --git a/src/lava/magma/runtime/cpp_message_infrastructure/include/abstract_port_implementation.h b/src/lava/magma/runtime/cpp_message_infrastructure/include/abstract_port_implementation.h new file mode 100644 index 000000000..5df045dcf --- /dev/null +++ b/src/lava/magma/runtime/cpp_message_infrastructure/include/abstract_port_implementation.h @@ -0,0 +1,31 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: BSD-3-Clause +// See: https://spdx.org/licenses/ + +#ifndef ABSTRACT_PORT_IMPLEMENTATION_H_ +#define ABSTRACT_PORT_IMPLEMENTATION_H_ + +#include + +#include "abstract_port.h" +#include "process_model.h" + +namespace message_infrastrature { + +class AbstractPortImplementation { + public: + int Start(); + int Join(); + std::vector GetShape(); + std::vector GetPorts(); + + DataType dtype_; + std::vector shape_; + size_t size_; + ProcessModel process_model_; + std::vector ports_; +}; + +} // namespace message_infrastrature + +#endif \ No newline at end of file diff --git a/src/lava/magma/runtime/cpp_message_infrastructure/include/multiprocessing.h b/src/lava/magma/runtime/cpp_message_infrastructure/include/multiprocessing.h new file mode 100644 index 000000000..9d6502879 --- /dev/null +++ b/src/lava/magma/runtime/cpp_message_infrastructure/include/multiprocessing.h @@ -0,0 +1,29 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: BSD-3-Clause +// See: https://spdx.org/licenses/ + +#ifndef MULTIPROCESSING_H_ +#define MULTIPROCESSING_H_ + +#include +#include + +#include "abstract_actor.h" +#include "shm.h" + +namespace message_infrastrature { + +class MultiProcessing { + public: + // stop each actor in vector actors; + void Stop(); + void BuildActor(std::function); + void CheckActor(); + private: + std::vector actors_; + //SharedMemManager shmm_; +}; + +} // namespace message_infrastrature + +#endif diff --git a/src/lava/magma/runtime/cpp_message_infrastructure/include/process_model.h b/src/lava/magma/runtime/cpp_message_infrastructure/include/process_model.h new file mode 100644 index 000000000..5371d6264 --- /dev/null +++ b/src/lava/magma/runtime/cpp_message_infrastructure/include/process_model.h @@ -0,0 +1,16 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: BSD-3-Clause +// See: https://spdx.org/licenses/ + +#ifndef PROCESS_MODEL_H_ +#define PROCESS_MODEL_H_ + +namespace message_infrastrature { + +class ProcessModel { + +}; + +} // namespace message_infrastrature + +#endif \ No newline at end of file diff --git a/src/lava/magma/runtime/cpp_message_infrastructure/include/runtime.h b/src/lava/magma/runtime/cpp_message_infrastructure/include/runtime.h new file mode 100644 index 000000000..fc5be9c18 --- /dev/null +++ b/src/lava/magma/runtime/cpp_message_infrastructure/include/runtime.h @@ -0,0 +1,15 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: BSD-3-Clause +// See: https://spdx.org/licenses/ + +#ifndef RUNTIME_H_ +#define RUNTIME_H_ + +namespace message_infrastrature { + +class Runtime { +}; + +} // namespace message_infrastrature + +#endif \ No newline at end of file diff --git a/src/lava/magma/runtime/cpp_message_infrastructure/include/shm.h b/src/lava/magma/runtime/cpp_message_infrastructure/include/shm.h new file mode 100644 index 000000000..a4088de67 --- /dev/null +++ b/src/lava/magma/runtime/cpp_message_infrastructure/include/shm.h @@ -0,0 +1,24 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: BSD-3-Clause +// See: https://spdx.org/licenses/ + +#ifndef SHM_H_ +#define SHM_H_ + +#include +#include +#include +#include +#include +#include +#include + +namespace message_infrastrature { + +class SharedMemory { + +}; + +} // namespace message_infrastrature + +#endif \ No newline at end of file diff --git a/src/lava/magma/runtime/cpp_message_infrastructure/include/shmem_channel.h b/src/lava/magma/runtime/cpp_message_infrastructure/include/shmem_channel.h new file mode 100644 index 000000000..b68ec6a1e --- /dev/null +++ b/src/lava/magma/runtime/cpp_message_infrastructure/include/shmem_channel.h @@ -0,0 +1,18 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: BSD-3-Clause +// See: https://spdx.org/licenses/ + +#ifndef SHMEM_CHANNEL_H_ +#define SHMEM_CHANNEL_H_ + +#include "abstract_channel.h" + +namespace message_infrastrature { + +class ShmemChannel : public AbstractChannel { + +}; + +} // namespace message_infrastrature + +#endif diff --git a/src/lava/magma/runtime/cpp_message_infrastructure/include/shmem_port.h b/src/lava/magma/runtime/cpp_message_infrastructure/include/shmem_port.h new file mode 100644 index 000000000..0073bf7d4 --- /dev/null +++ b/src/lava/magma/runtime/cpp_message_infrastructure/include/shmem_port.h @@ -0,0 +1,22 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: BSD-3-Clause +// See: https://spdx.org/licenses/ + +#ifndef SHMEM_PORT_H_ +#define SHMEM_PORT_H_ + +#include "abstract_port.h" + +namespace message_infrastrature { + +class ShmemSendPort : public AbstractSendPort { + +}; + +class ShmemRecvPort : public AbstractRecvPort { + +}; + +} // namespace message_infrastrature + +#endif diff --git a/src/lava/magma/runtime/cpp_message_infrastructure/include/utils.h b/src/lava/magma/runtime/cpp_message_infrastructure/include/utils.h new file mode 100644 index 000000000..3f10ee193 --- /dev/null +++ b/src/lava/magma/runtime/cpp_message_infrastructure/include/utils.h @@ -0,0 +1,22 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: BSD-3-Clause +// See: https://spdx.org/licenses/ + +#ifndef UTILS_H_ +#define UTILS_H_ + +namespace message_infrastrature { + +enum ChannelType { + ShmemChannel = 0, + RpcChannel = 1, + DdsChannel = 2 +}; + +enum DataType { + // dtype +}; + +} // namespace message_infrastrature + +#endif \ No newline at end of file diff --git a/src/lava/magma/runtime/cpp_message_infrastructure/pybind11 b/src/lava/magma/runtime/cpp_message_infrastructure/pybind11 new file mode 160000 index 000000000..ba5ccd845 --- /dev/null +++ b/src/lava/magma/runtime/cpp_message_infrastructure/pybind11 @@ -0,0 +1 @@ +Subproject commit ba5ccd845a2261e538df651e3d528dc1bece094d diff --git a/src/lava/magma/runtime/cpp_message_infrastructure/setenv.sh b/src/lava/magma/runtime/cpp_message_infrastructure/setenv.sh new file mode 100644 index 000000000..f8d8b5786 --- /dev/null +++ b/src/lava/magma/runtime/cpp_message_infrastructure/setenv.sh @@ -0,0 +1,2 @@ +export PYTHONPATH=${PWD}/build +export LD_LIBRARY_PATH=${PWD}/build diff --git a/src/lava/magma/runtime/cpp_message_infrastructure/src/abstract_port_implementation.cc b/src/lava/magma/runtime/cpp_message_infrastructure/src/abstract_port_implementation.cc new file mode 100644 index 000000000..a6f849156 --- /dev/null +++ b/src/lava/magma/runtime/cpp_message_infrastructure/src/abstract_port_implementation.cc @@ -0,0 +1,29 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: BSD-3-Clause +// See: https://spdx.org/licenses/ + +#include "abstract_port_implementation.h" + +namespace message_infrastrature { + +int AbstractPortImplementation::Start() { + for (auto port : this->ports_){ + port->Start(); + } +} + +int AbstractPortImplementation::Join() { + for (auto port : this->ports_){ + port->Join(); + } +} + +std::vector AbstractPortImplementation::GetShape() { + return this->shape_; +} + +std::vector AbstractPortImplementation::GetPorts() { + return this->ports_; +} + +} // namespace message_infrastrature \ No newline at end of file diff --git a/src/lava/magma/runtime/cpp_message_infrastructure/src/message_infrastructure_py_wrapper.cc b/src/lava/magma/runtime/cpp_message_infrastructure/src/message_infrastructure_py_wrapper.cc new file mode 100644 index 000000000..329663324 --- /dev/null +++ b/src/lava/magma/runtime/cpp_message_infrastructure/src/message_infrastructure_py_wrapper.cc @@ -0,0 +1,23 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: BSD-3-Clause +// See: https://spdx.org/licenses/ + +#include +#include + +#include "multiprocessing.h" +//#include "abstract_port_implementation.h" + +namespace message_infrastrature { + +namespace py = pybind11; + +PYBIND11_MODULE(MessageInfrastructurePywrapper, m) { + py::class_ (m, "MultiProcessing") + .def(py::init<>()) + .def("build_actor", &MultiProcessing::BuildActor) + .def("check_actor", &MultiProcessing::CheckActor) + .def("stop", &MultiProcessing::Stop); +} + +} // namespace message_infrastrature diff --git a/src/lava/magma/runtime/cpp_message_infrastructure/src/multiprocessing.cc b/src/lava/magma/runtime/cpp_message_infrastructure/src/multiprocessing.cc new file mode 100644 index 000000000..59c6143bd --- /dev/null +++ b/src/lava/magma/runtime/cpp_message_infrastructure/src/multiprocessing.cc @@ -0,0 +1,45 @@ +// Copyright (C) 2021 Intel Corporation +// SPDX-License-Identifier: BSD-3-Clause +// See: https://spdx.org/licenses/ + +#include "multiprocessing.h" + +#include +#include +#include + +namespace message_infrastrature { + +#define CPP_INFO "[CPP_INFO] " + +void MultiProcessing::BuildActor(std::function target_fn) { + pid_t pid = fork(); + + if (pid < 0) { + std::cout << CPP_INFO << "cannot allocate pid\n"; + } + + if (pid > 0) { + std::cout << CPP_INFO << "parent, create actor\n"; + ActorPtr actor = new PosixActor(pid); + actors_.push_back(actor); + } + + if (pid == 0) { + std::cout << CPP_INFO << "child, new process\n"; + target_fn(); + exit(0); + } + +} + +void MultiProcessing::Stop() { +} + +void MultiProcessing::CheckActor() { + for(auto actor : actors_){ + std::cout << CPP_INFO << actor->pid_ << std::endl; + } +} + +} // namespace message_infrastrature diff --git a/src/lava/magma/runtime/cpp_message_infrastructure/test/.gitkeep b/src/lava/magma/runtime/cpp_message_infrastructure/test/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/src/lava/magma/runtime/cpp_message_infrastructure/test/test_example.py b/src/lava/magma/runtime/cpp_message_infrastructure/test/test_example.py new file mode 100644 index 000000000..40ba6aacf --- /dev/null +++ b/src/lava/magma/runtime/cpp_message_infrastructure/test/test_example.py @@ -0,0 +1,23 @@ +# Copyright (C) 2021 Intel Corporation +# SPDX-License-Identifier: BSD-3-Clause +# See: https://spdx.org/licenses/ + +from MessageInfrastructurePywrapper import MultiProcessing +import time + + +def print_hello(): + print("hello") + + +def main(): + mp = MultiProcessing() + for i in range(5): + mp.build_actor(print_hello) + + mp.check_actor() + + +main() +print("sleep 5") +time.sleep(5)