Skip to content

Commit

Permalink
hal remote library
Browse files Browse the repository at this point in the history
This adds support for generic client and server hal support to allow
sending commands as binary requests over a `hal_transmitter`.

Added encoders and decoders for converting hal commands to binary format

Add hal_server base class which uses the encoders and decoders and a
hal_transmitter to receive hal commands remotely and act on the given
hal device.

Add a hal_client and hal_device_client base class which can be used with
a transmitter to send to a remote server binary commands.

Added a hal socket cpu client.
Added hal cpu socket based server program.
  • Loading branch information
coldav committed Apr 18, 2024
1 parent bc9312e commit e9031c4
Show file tree
Hide file tree
Showing 20 changed files with 2,222 additions and 24 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ add_subdirectory(source)
if(CA_ENABLE_DOCUMENTATION)
add_subdirectory(doc)
endif()
add_subdirectory(examples)

# Add the deferred add_ca_extension_subdirectory() entries until all
# dependencies are available for use in the extension's CMake.
Expand Down
20 changes: 20 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright (C) Codeplay Software Limited
#
# Licensed under the Apache License, Version 2.0 (the "License") with LLVM
# Exceptions; you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://github.com/codeplaysoftware/oneapi-construction-kit/blob/main/LICENSE.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
# Only builds on linux at the moment
add_subdirectory(hal_cpu_remote_server)
endif()
38 changes: 38 additions & 0 deletions examples/hal_cpu_remote_server/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright (C) Codeplay Software Limited
#
# Licensed under the Apache License, Version 2.0 (the "License") with LLVM
# Exceptions; you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://github.com/codeplaysoftware/oneapi-construction-kit/blob/main/LICENSE.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

set(HAL_CPU_REMOTE_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../clik/external/hal_cpu)

# Add the files in directly, rather than drag in as a shared library or take
# all the extra clik code
add_executable(hal_cpu_server_bin
hal_cpu_executable.cpp
${HAL_CPU_REMOTE_SOURCE_DIR}/source/cpu_hal.cpp
${HAL_CPU_REMOTE_SOURCE_DIR}/source/hal_main.cpp
)

target_compile_definitions(hal_cpu_server_bin PUBLIC -DHAL_CPU_MODE=HAL_CPU_WG_MODE)
target_include_directories(hal_cpu_server_bin PRIVATE
${CMAKE_CURRENT_BINARY_DIR}
${HAL_CPU_REMOTE_SOURCE_DIR}/include
)

target_link_libraries(hal_cpu_server_bin hal_remote hal_common dl pthread)


install(TARGETS hal_cpu_server_bin
LIBRARY DESTINATION bin
)
60 changes: 60 additions & 0 deletions examples/hal_cpu_remote_server/hal_cpu_executable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (C) Codeplay Software Limited
//
// Licensed under the Apache License, Version 2.0 (the "License") with LLVM
// Exceptions; you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://github.com/codeplaysoftware/oneapi-construction-kit/blob/main/LICENSE.txt
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
//
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <unistd.h>

#include "hal_remote/hal_server.h"
#include "hal_remote/hal_socket_client.h"
#include "hal_remote/hal_socket_transmitter.h"

int main(int argc, char **argv) {
uint32_t api_version;
auto *hal = get_hal(api_version);
uint16_t port = 0;
std::string node = "127.0.0.1";
if (const char *env = getenv("HAL_REMOTE_PORT")) {
port = std::stoi(env);
}

if (const char *env = getenv("HAL_REMOTE_NODE")) {
node = env;
}

hal::hal_socket_transmitter transmitter(node.c_str(), port);
hal::hal_server::error_code last_error = hal::hal_server::status_success;
auto res = transmitter.start_server(true);
if (res != hal::hal_socket_transmitter::success) {
(void)fprintf(stderr, "Unable to start server on requested port %d\n",
port);
return 1;
}

hal::hal_server server(&transmitter, hal);
last_error = server.process_commands();
// Check if the failure was due the transmitter, if so check if connection
// closed, otherwise we have a real error.
if (last_error == hal::hal_server::status_transmitter_failed) {
if (transmitter.get_last_error() !=
hal::hal_socket_transmitter::connection_closed) {
(void)fprintf(stderr, "Error with tcp/ip connection\n");
return 1;
} else {
return 0;
}
}

return 1;
}
21 changes: 21 additions & 0 deletions examples/hals/hal_cpu_client/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (C) Codeplay Software Limited
#
# Licensed under the Apache License, Version 2.0 (the "License") with LLVM
# Exceptions; you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://github.com/codeplaysoftware/oneapi-construction-kit/blob/main/LICENSE.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

set_property(GLOBAL APPEND PROPERTY KNOWN_HAL_DEVICES "cpu_client")
set_property(GLOBAL PROPERTY HAL_CPU_CLIENT_NAME "cpu_remote")
set_property(GLOBAL PROPERTY HAL_CPU_CLIENT_DEVICE_NAME "ock cpu")

add_subdirectory(source)
41 changes: 41 additions & 0 deletions examples/hals/hal_cpu_client/source/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright (C) Codeplay Software Limited
#
# Licensed under the Apache License, Version 2.0 (the "License") with LLVM
# Exceptions; you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://github.com/codeplaysoftware/oneapi-construction-kit/blob/main/LICENSE.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

set(HAL_CPU_REMOTE_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../clik/external/hal_cpu)
add_library(hal_cpu_client SHARED
# Even though it is remote we need hal_cpu.cpp to get the info
${HAL_CPU_REMOTE_SOURCE_DIR}/source/cpu_hal.cpp
hal_main.cpp
)

set(HAL_CPU_WG_MODE ON)

target_compile_definitions(hal_cpu_client PUBLIC -DBUILD_HAL_DLL)
target_compile_definitions(hal_cpu_client PUBLIC -DHAL_CPU_MODE=HAL_CPU_WG_MODE)

target_include_directories(hal_cpu_client PRIVATE
${CMAKE_CURRENT_BINARY_DIR}
${HAL_CPU_REMOTE_SOURCE_DIR}/include
)

target_link_libraries(hal_cpu_client hal_remote dl pthread)

set_target_properties(hal_cpu_client PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)

install(TARGETS hal_cpu_client
LIBRARY DESTINATION lib
)
52 changes: 52 additions & 0 deletions examples/hals/hal_cpu_client/source/hal_cpu_executable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (C) Codeplay Software Limited
//
// Licensed under the Apache License, Version 2.0 (the "License") with LLVM
// Exceptions; you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://github.com/codeplaysoftware/oneapi-construction-kit/blob/main/LICENSE.txt
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
//
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <hal_remote/hal_server.h>
#include <hal_remote/hal_socket_client.h>
#include <hal_remote/hal_socket_transmitter.h>
#include <unistd.h>

int main(int argc, char **argv) {
uint32_t api_version;
auto *hal = get_hal(api_version);
uint16_t port = 0;
if (const char *env = getenv("HAL_REMOTE_PORT")) {
port = atoi(env);
}

hal::hal_socket_transmitter transmitter(port);
hal::hal_server::error_code last_error = hal::hal_server::status_success;
do {
auto res = transmitter.start_server(true);
if (res != hal::hal_socket_transmitter::success) {
printf("Unable to start server on requested port %d\n", port);
fflush(stdout);
exit(1);
}

hal::hal_server server(&transmitter, hal);
last_error = server.process_commands();
// Check if the failure was due the transmitter, if so check if connection
// closed, otherwise we have a real error.
if (last_error == hal::hal_server::status_transmitter_failed &&
transmitter.get_last_error() !=
hal::hal_socket_transmitter::connection_closed) {
fprintf(stderr, "Error with tcp/ip connection\n");
exit(1);
}

} while (last_error == hal::hal_server::status_transmitter_failed);
}
61 changes: 61 additions & 0 deletions examples/hals/hal_cpu_client/source/hal_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (C) Codeplay Software Limited
//
// Licensed under the Apache License, Version 2.0 (the "License") with LLVM
// Exceptions; you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://github.com/codeplaysoftware/oneapi-construction-kit/blob/main/LICENSE.txt
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
//
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <cpu_hal.h>
#include <hal_remote/hal_socket_client.h>

namespace hal {
class hal_cpu_socket_client : public hal::hal_socket_client {
public:
hal_cpu_socket_client() : hal_socket_client(0) {
constexpr static uint32_t implemented_api_version = 6;
static_assert(
implemented_api_version == hal_t::api_version,
"Implemented API version for hal_socket_client does not match hal.h");
hal_device_info = cpu_hal::setup_cpu_hal_device_info();
hal_info.platform_name = hal_device_info.target_name;
hal_info.num_devices = 1;
hal_info.api_version = implemented_api_version;
uint32_t port = 0;
if (const char *env = getenv("HAL_REMOTE_PORT")) {
port = atoi(env);
}
set_port(port);
}

// return generic platform information
const hal::hal_info_t &get_info() override {
std::lock_guard<std::mutex> locker(lock);
return hal_info;
}

// return generic target information
const hal::hal_device_info_t *device_get_info(uint32_t index) override {
std::lock_guard<std::mutex> locker(lock);
return &hal_device_info;
}

private:
hal::hal_info_t hal_info;
hal::hal_device_info_t hal_device_info;
};
} // namespace hal
static hal::hal_cpu_socket_client hal_object;

hal::hal_t *get_hal(uint32_t &api_version) {
api_version = hal_object.get_info().api_version;
return &hal_object;
}
8 changes: 8 additions & 0 deletions hal/hal_remote/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

set(HAL_SOURCE
${CMAKE_CURRENT_SOURCE_DIR}/include/hal_remote/hal_binary_encoder.h
${CMAKE_CURRENT_SOURCE_DIR}/include/hal_remote/hal_binary_decoder.h
${CMAKE_CURRENT_SOURCE_DIR}/include/hal_remote/hal_client.h
${CMAKE_CURRENT_SOURCE_DIR}/include/hal_remote/hal_device_client.h
${CMAKE_CURRENT_SOURCE_DIR}/include/hal_remote/hal_server.h
${CMAKE_CURRENT_SOURCE_DIR}/include/hal_remote/hal_transmitter.h
${CMAKE_CURRENT_SOURCE_DIR}/source/hal_device_client.cpp
${CMAKE_CURRENT_SOURCE_DIR}/source/hal_server.cpp
)

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
list (APPEND HAL_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/include/hal_remote/hal_socket_client.h)
list (APPEND HAL_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/include/hal_remote/hal_socket_transmitter.h)
list (APPEND HAL_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/source/hal_socket_transmitter.cpp)
endif()
Expand Down

0 comments on commit e9031c4

Please sign in to comment.