Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example/c pingpong #650

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,5 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug")
else()
message("not debug mode and disable cpp unit test")
endif()

add_subdirectory(examples/c_pingpong)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
include_directories(../../csrc)

add_executable(cprocess
"cprocess.cc"
)

target_link_libraries(cprocess PRIVATE
message_infrastructure
)


set_target_properties(cprocess
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (C) 2023 Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
// See: https://spdx.org/licenses/

#include <core/abstract_channel.h>
#include <core/channel_factory.h>
#include <core/abstract_port.h>
#include <core/utils.h>
#include <channel/socket/socket_port.h>

using namespace message_infrastructure; // NOLINT

int main(int argc, char *argv[]) {
ChannelFactory &channel_factory = GetChannelFactory();

AbstractChannelPtr ch = channel_factory.GetTempChannel("./py2c");
AbstractRecvPortPtr rc = ch->GetRecvPort();

// order matters
rc->Start();

for (uint _ = 0; _ < 10; ++_) {
std::cout << "receiving\n";
MetaDataPtr recvd = rc->Recv();
std::cout << "received from py, total size: "
<< recvd->total_size
<< "\n";

AbstractChannelPtr ch2 = channel_factory.GetTempChannel("./c2py");
AbstractSendPortPtr sd = ch2->GetSendPort();
sd->Start();
sd->Send(recvd);
sd->Join();
}

rc->Join();

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import os
import random
import numpy as np

from lava.magma.runtime.message_infrastructure. \
MessageInfrastructurePywrapper import (
TempChannel
)

C2PY = "./c2py"
PY2C = "./py2c"

# float equal


def f_eq(a, b):
return abs(a - b) < 0.001


def main():

if os.path.exists(C2PY):
os.remove(C2PY)
if os.path.exists(PY2C):
os.remove(PY2C)

# order matters
ch2 = TempChannel(C2PY)
rc = ch2.dst_port
rc.start()

input("Start the c process, hit enter when you see *receiving*")

for i in range(10):
# send port is one-off
ch = TempChannel(PY2C)
sd = ch.src_port
sd.start()

print("round ", i)
rands = np.array([np.random.random() * 100 for __ in range(10)]) # noqa
print("Sending array to C: ", rands)
sd.send(rands)

rands2 = rc.recv()
print("Got array from C: ", rands2)

print("Correctness: ", all([f_eq(x, y) for x, y in zip(rands, rands2)])) # noqa
print("========================================")
sd.join()
rc.join()


if __name__ == "__main__":
main()