Skip to content

Commit

Permalink
enable IPC tests and examples on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
bratpiorka committed Sep 19, 2024
1 parent 85b1d6f commit 6da36e1
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 61 deletions.
1 change: 1 addition & 0 deletions Testing/Temporary/CTestCostData.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
13 changes: 11 additions & 2 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ function(build_umf_ipc_example name)
${UMF_CMAKE_SOURCE_DIR}/include)

target_link_directories(${EX_NAME} PRIVATE ${LIBHWLOC_LIBRARY_DIRS})
if(WINDOWS)
# link with Winsock2 lib
target_link_libraries(${EX_NAME} PRIVATE ws2_32)
# append PATH to DLLs set_property(TEST ${EX_NAME} PROPERTY
# ENVIRONMENT_MODIFICATION
# "${DLL_PATH_LIST}")
endif()
endforeach(loop_var)
endfunction()

Expand All @@ -155,14 +162,16 @@ function(add_umf_ipc_example script)
endif()
endfunction()

# IPC API examples
build_umf_ipc_example(ipc_ipcapi)

if(LINUX)
build_umf_ipc_example(ipc_ipcapi)
add_umf_ipc_example(ipc_ipcapi_anon_fd)
add_umf_ipc_example(ipc_ipcapi_shm)
else()
message(
STATUS
"IPC examples with UMF pool API are supported on Linux only - skipping"
"IPC anon_fd and shm examples with UMF pool API are supported on Linux only - skipping"
)
endif()

Expand Down
13 changes: 11 additions & 2 deletions examples/ipc_ipcapi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,28 @@ function(build_umf_ipc_example name)
target_include_directories(${EX_NAME} PRIVATE ${LIBUMF_INCLUDE_DIRS})
target_link_directories(${EX_NAME} PRIVATE ${LIBHWLOC_LIBRARY_DIRS})
target_link_libraries(${EX_NAME} PRIVATE ${LIBUMF_LIBRARIES} hwloc)
if(WINDOWS)
# link with Winsock lib
target_link_libraries(${EX_NAME} PRIVATE ${LIBUMF_LIBRARIES} ws2_32)
endif()
endforeach(loop_var)
endfunction()

# an optional part - adds a test of this example
function(add_test_for_umf_ipc_example script)
set(EXAMPLE_NAME umf_example_${script})

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${script}.sh
if(LINUX)
set(UMF_SCRIPT_EXT sh)
else()
set(UMF_SCRIPT_EXT bat)

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${script}.${UMF_SCRIPT_EXT}
DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

add_test(
NAME ${EXAMPLE_NAME}
COMMAND ${script}.sh
COMMAND ${script}.${UMF_SCRIPT_EXT}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

if(LINUX)
Expand Down
14 changes: 14 additions & 0 deletions examples/ipc_ipcapi/ipc_ipcapi_anon_fd.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

# port should be a number from the range <1024, 65535>
PORT=$(( 1024 + ( $$ % ( 65535 - 1024 ))))

UMF_LOG_VAL="level:debug;flush:debug;output:stderr;pid:yes"

echo "Starting ipc_ipcapi_anon_fd CONSUMER on port $PORT ..."
UMF_LOG=$UMF_LOG_VAL ./umf_example_ipc_ipcapi_consumer $PORT &

echo "Waiting 1 sec ..."
sleep 1

echo "Starting ipc_ipcapi_anon_fd PRODUCER on port $PORT ..."
UMF_LOG=$UMF_LOG_VAL ./umf_example_ipc_ipcapi_producer $PORT
50 changes: 41 additions & 9 deletions examples/ipc_ipcapi/ipc_ipcapi_consumer.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/

#ifdef _WIN32
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <winsock2.h>
typedef int socklen_t;
typedef SSIZE_T ssize_t;
#else
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

#include <umf/ipc.h>
#include <umf/memory_pool.h>
Expand All @@ -27,25 +35,40 @@
"shared memory!"

int consumer_connect_to_producer(int port) {
#ifdef _WIN32
WSADATA wsaData;
SOCKET producer_socket, consumer_socket;
#else
int producer_socket = -1;
int consumer_socket = -1;
#endif

struct sockaddr_in consumer_addr;
struct sockaddr_in producer_addr;

int producer_addr_len;
int producer_socket = -1;
int consumer_socket = -1;
int ret = -1;

#ifdef _WIN32
// initialize Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
fprintf(stderr, "Failed. Error Code : %d", WSAGetLastError());
return -1;
}
#endif

// create a socket
consumer_socket = socket(AF_INET, SOCK_STREAM, 0);
if (consumer_socket < 0) {
fprintf(stderr, "[consumer] ERROR: creating socket failed\n");
return -1;
goto err_WSA_cleanup;
}

fprintf(stderr, "[consumer] Socket created\n");

// set the IP address and the port
consumer_addr.sin_family = AF_INET;
consumer_addr.sin_port = htons(port);
consumer_addr.sin_port = htons((u_short)port);
consumer_addr.sin_addr.s_addr = inet_addr(INET_ADDR);

// bind to the IP address and the port
Expand Down Expand Up @@ -77,10 +100,19 @@ int consumer_connect_to_producer(int port) {
fprintf(stderr, "[consumer] Producer connected at IP %s and port %i\n",
inet_ntoa(producer_addr.sin_addr), ntohs(producer_addr.sin_port));

ret = producer_socket; // success
ret = (int)producer_socket; // success

err_close_consumer_socket:
#ifdef _WIN32
closesocket(consumer_socket);
#else
close(consumer_socket);
#endif

err_WSA_cleanup:
#ifdef _WIN32
WSACleanup();
#endif

return ret;
}
Expand Down Expand Up @@ -152,8 +184,8 @@ int main(int argc, char *argv[]) {
len, size_IPC_handle);

// send received size to the producer as a confirmation
recv_len =
send(producer_socket, &size_IPC_handle, sizeof(size_IPC_handle), 0);
recv_len = send(producer_socket, (const char *)&size_IPC_handle,
sizeof(size_IPC_handle), 0);
if (recv_len < 0) {
fprintf(stderr, "[consumer] ERROR: sending confirmation failed\n");
goto err_close_producer_socket;
Expand Down
56 changes: 46 additions & 10 deletions examples/ipc_ipcapi/ipc_ipcapi_producer.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/

#ifdef _WIN32
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <winsock2.h>
typedef int socklen_t;
typedef SSIZE_T ssize_t;
#else
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

#include <umf/ipc.h>
#include <umf/memory_pool.h>
Expand All @@ -22,21 +30,35 @@
#define SIZE_SHM 1024

int producer_connect_to_consumer(int port) {
struct sockaddr_in consumer_addr;
#ifdef _WIN32
WSADATA wsaData;
SOCKET producer_socket;
#else
int producer_socket = -1;
#endif

struct sockaddr_in consumer_addr;

#ifdef _WIN32
// initialize Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
fprintf(stderr, "Failed. Error Code : %d", WSAGetLastError());
return -1;
}
#endif

// create a producer socket
producer_socket = socket(AF_INET, SOCK_STREAM, 0);
if (producer_socket < 0) {
fprintf(stderr, "[producer] ERROR: Unable to create socket\n");
return -1;
goto err_WSA_cleanup;
}

fprintf(stderr, "[producer] Socket created\n");

// set IP address and port the same as for the consumer
consumer_addr.sin_family = AF_INET;
consumer_addr.sin_port = htons(port);
consumer_addr.sin_port = htons((u_short)port);
consumer_addr.sin_addr.s_addr = inet_addr(INET_ADDR);

// send connection request to the consumer
Expand All @@ -49,10 +71,19 @@ int producer_connect_to_consumer(int port) {

fprintf(stderr, "[producer] Connected to the consumer\n");

return producer_socket; // success
return (int)producer_socket; // success

err_close_producer_socket_connect:
#ifdef _WIN32
closesocket(producer_socket);
#else
close(producer_socket);
#endif

err_WSA_cleanup:
#ifdef _WIN32
WSACleanup();
#endif

return -1;
}
Expand Down Expand Up @@ -131,8 +162,8 @@ int main(int argc, char *argv[]) {
}

// send a size of the IPC_handle to the consumer
ssize_t len =
send(producer_socket, &IPC_handle_size, sizeof(IPC_handle_size), 0);
ssize_t len = (ssize_t)send(producer_socket, (const char *)&IPC_handle_size,
sizeof(IPC_handle_size), 0);
if (len < 0) {
fprintf(stderr, "[producer] ERROR: unable to send the message\n");
goto err_close_producer_socket;
Expand All @@ -147,7 +178,7 @@ int main(int argc, char *argv[]) {
memset(recv_buffer, 0, sizeof(recv_buffer));

// receive the consumer's confirmation
len = recv(producer_socket, recv_buffer, sizeof(recv_buffer), 0);
len = (ssize_t)recv(producer_socket, recv_buffer, sizeof(recv_buffer), 0);
if (len < 0) {
fprintf(stderr, "[producer] ERROR: error while receiving the "
"confirmation from the consumer\n");
Expand All @@ -169,7 +200,8 @@ int main(int argc, char *argv[]) {
}

// send the IPC_handle of IPC_handle_size to the consumer
len = send(producer_socket, IPC_handle, IPC_handle_size, 0);
len = (ssize_t)send(producer_socket, (const char *)IPC_handle,
(int)IPC_handle_size, 0);
if (len < 0) {
fprintf(stderr, "[producer] ERROR: unable to send the message\n");
goto err_close_producer_socket;
Expand Down Expand Up @@ -221,7 +253,11 @@ int main(int argc, char *argv[]) {
}

err_close_producer_socket:
#ifdef _WIN32
closesocket(producer_socket);
#else
close(producer_socket);
#endif

err_PutIPCHandle:
umf_result = umfPutIPCHandle(IPC_handle);
Expand Down
8 changes: 4 additions & 4 deletions src/provider/provider_level_zero.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ static umf_result_t ze_memory_provider_initialize(void *params,
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

if ((ze_params->memory_type == UMF_MEMORY_TYPE_HOST) ==
(bool)ze_params->level_zero_device_handle) {
if ((ze_params->memory_type == UMF_MEMORY_TYPE_HOST) &&
(ze_params->level_zero_device_handle != NULL)) {
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

if ((bool)ze_params->resident_device_count !=
(bool)ze_params->resident_device_handles) {
if ((ze_params->resident_device_count > 0) !=
(ze_params->resident_device_handles != NULL)) {
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
}

Expand Down
46 changes: 29 additions & 17 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -340,23 +340,35 @@ function(add_umf_ipc_test)
endif()
endfunction()

if(NOT UMF_DISABLE_HWLOC)

if(WINDOWS)
set(UMF_IPC_LIBS ws2_32)
endif()

build_umf_test(
NAME
ipc_os_prov_consumer
SRCS
ipc_os_prov_consumer.c
common/ipc_common.c
common/ipc_os_prov_common.c
LIBS
${UMF_IPC_LIBS})
build_umf_test(
NAME
ipc_os_prov_producer
SRCS
ipc_os_prov_producer.c
common/ipc_common.c
common/ipc_os_prov_common.c
LIBS
${UMF_IPC_LIBS})
add_umf_ipc_test(TEST ipc_os_prov_anon_fd)
endif()

if(LINUX)
if(NOT UMF_DISABLE_HWLOC)
build_umf_test(
NAME
ipc_os_prov_consumer
SRCS
ipc_os_prov_consumer.c
common/ipc_common.c
common/ipc_os_prov_common.c)
build_umf_test(
NAME
ipc_os_prov_producer
SRCS
ipc_os_prov_producer.c
common/ipc_common.c
common/ipc_os_prov_common.c)
add_umf_ipc_test(TEST ipc_os_prov_anon_fd)
add_umf_ipc_test(TEST ipc_os_prov_shm)

build_umf_test(
Expand Down Expand Up @@ -482,12 +494,12 @@ if(LINUX
)
endif()

if(LINUX AND UMF_POOL_SCALABLE_ENABLED)
if(UMF_POOL_SCALABLE_ENABLED)
set(EXAMPLES ${EXAMPLES} ipc_ipcapi)
else()
message(
STATUS
"IPC examples with UMF pool API are supported on Linux with TBB installed only - skipping"
"IPC examples with UMF pool API are supported with TBB installed only - skipping"
)
endif()

Expand Down
Loading

0 comments on commit 6da36e1

Please sign in to comment.