Skip to content

add matrix multiply example for CPP #402

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

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions erpc_c/transports/erpc_tcp_transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ extern "C" {
#else
#include <netdb.h>
#include <netinet/tcp.h>
#ifdef ANDROID
#include <netinet/in.h>
#endif
#include <sys/socket.h>
#endif
#include <signal.h>
Expand Down
45 changes: 45 additions & 0 deletions examples/matrix_multiply_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
cmake_minimum_required(VERSION 3.10)
project(HelloWorldERPC)


# Generate eRPC code from IDL file
function(add_erpcgen_target target_name)
# Construct file names based on the provided target name
set(OUTPUT_FILES
"${CMAKE_CURRENT_BINARY_DIR}/${target_name}_interface.cpp"
"${CMAKE_CURRENT_BINARY_DIR}/${target_name}_client.cpp"
"${CMAKE_CURRENT_BINARY_DIR}/${target_name}_server.cpp"
)

# The custom command
add_custom_command(
OUTPUT ${OUTPUT_FILES}
COMMAND erpcgen -g c ${CMAKE_CURRENT_SOURCE_DIR}/${target_name}.erpc
DEPENDS src/${target_name}.erpc
)

add_library(${target_name}_client_lib
${CMAKE_CURRENT_BINARY_DIR}/${target_name}_client.cpp
${CMAKE_CURRENT_BINARY_DIR}/${target_name}_interface.cpp)

add_library(${target_name}_server_lib
${CMAKE_CURRENT_BINARY_DIR}/${target_name}_server.cpp
${CMAKE_CURRENT_BINARY_DIR}/${target_name}_interface.cpp)
endfunction()


add_erpcgen_target("matrix_multiply")


add_compile_definitions(TCP_HOST="localhost" TCP_PORT=8085)

include_directories("/usr/local/include/erpc"
"${CMAKE_CURRENT_BINARY_DIR}")

# Server executable
add_executable(server src/server.cpp)
target_link_libraries(server erpc matrix_multiply_server_lib)

# Client executable
add_executable(client src/client.cpp)
target_link_libraries(client erpc matrix_multiply_client_lib)
53 changes: 53 additions & 0 deletions examples/matrix_multiply_cpp/src/client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <iostream>

#include "erpc_client_setup.h"
#include "erpc_transport_setup.h"
#include "matrix_multiply_client.hpp"

Matrix A = {{3, 3, 3, 7, 6},
{8, 1, 3, 8, 8},
{4, 6, 3, 4, 7},
{4, 6, 7, 2, 1},
{4, 2, 9, 9, 6}};

Matrix B = {{4, 1, 7, 1, 4},
{1, 5, 7, 2, 5},
{6, 4, 2, 1, 6},
{5, 9, 5, 8, 7},
{3, 7, 1, 9, 4}};

Matrix C = {};

void printMatrix(const Matrix M) {
for (int i = 0; i < matrix_size; i++) {
for (int j = 0; j < matrix_size; j++) {
std::cout << M[i][j] << "\t";
}
std::cout << std::endl;
}
}

int main() {
// Initialize client
erpc_transport_t transport =
erpc_transport_tcp_init(TCP_HOST, TCP_PORT, false);
auto client = erpc_client_init(transport, erpc_mbf_dynamic_init());

// Create client stub
erpcShim::MatrixMultiplyService_client hello_client{
reinterpret_cast<erpc::ClientManager *>(client)};

for (auto i = 0; i < 10; ++i) {
// Call server function
hello_client.erpcMatrixMultiply(A, B, C);
printMatrix(C);

auto greet = hello_client.hello("Alice");
std::cout << "hello result: " << greet << std::endl;
}

// De-initialize client
erpc_client_deinit(client);

return 0;
}
24 changes: 24 additions & 0 deletions examples/matrix_multiply_cpp/src/matrix_multiply.erpc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*!
* Copyright 2017 NXP
* All rights reserved.
*
*
* SPDX-License-Identifier: BSD-3-Clause
*/

program matrix_multiply

/*! This const defines the matrix size. The value has to be the same as the
Matrix array dimension. Do not forget to re-generate the erpc code once the
matrix size is changed in the erpc file */
const int32 matrix_size = 5;

/*! This is the matrix array type. The dimension has to be the same as the
matrix size const. Do not forget to re-generate the erpc code once the
matrix size is changed in the erpc file */
type Matrix = int32[matrix_size][matrix_size];

interface MatrixMultiplyService {
erpcMatrixMultiply(in Matrix matrix1, in Matrix matrix2, out Matrix result_matrix) -> void
hello(in string name) -> string
}
54 changes: 54 additions & 0 deletions examples/matrix_multiply_cpp/src/server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <iostream>

#include "erpc_port.h"
#include "erpc_server_setup.h"
#include "matrix_multiply_server.hpp"

// Implementation of the "MatrixMultiply" service
class MatrixMultiplyServiceInterface
: public erpcShim::MatrixMultiplyService_interface {
public:
void erpcMatrixMultiply(Matrix A, Matrix B, Matrix C) override {
std::cout << "handle erpcMatrixMultiply" << std::endl;
for (int i = 0; i < matrix_size; i++) {
for (int j = 0; j < matrix_size; j++) {
C[i][j] = 0;
for (int k = 0; k < matrix_size; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
}

char* hello(const char* name) override {
std::cout << "handle hello" << std::endl;
std::string input_name{name};
std::string greet = "hello " + input_name + ", from server";
char* ret = reinterpret_cast<char*>(erpc_malloc(greet.length() + 1));
strcpy(ret, greet.c_str());

return ret;
}
};

int main() {
// Initialize server
erpc_transport_t transport =
erpc_transport_tcp_init(TCP_HOST, TCP_PORT, true);
auto server = erpc_server_init(transport, erpc_mbf_dynamic_init());

// Register service
MatrixMultiplyServiceInterface interface {};
erpcShim::MatrixMultiplyService_service service{&interface};

erpc_add_service_to_server(server, &service);

// Run server
while (1) {
erpc_server_run(server);
}

erpc_server_stop(server);

return 0;
}