Skip to content

Commit 63b0f81

Browse files
committed
enable GPU IPC tests and examples on Windows
1 parent cfc99b7 commit 63b0f81

File tree

9 files changed

+320
-79
lines changed

9 files changed

+320
-79
lines changed

examples/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ function(build_umf_ipc_example name)
171171

172172
target_link_directories(${EX_NAME} PRIVATE ${LIBHWLOC_LIBRARY_DIRS}
173173
${TBB_LIBRARY_DIRS})
174+
if(WINDOWS)
175+
# link with Winsock2 lib
176+
target_link_libraries(${EX_NAME} PRIVATE ws2_32)
177+
# append PATH to DLLs
178+
set_property(TEST ${EX_NAME} PROPERTY ENVIRONMENT_MODIFICATION
179+
"${DLL_PATH_LIST}")
180+
endif()
174181
endforeach(loop_var)
175182
endfunction()
176183

examples/ipc_ipcapi/CMakeLists.txt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,29 @@ function(build_umf_ipc_example name)
3737
target_include_directories(${EX_NAME} PRIVATE ${LIBUMF_INCLUDE_DIRS})
3838
target_link_directories(${EX_NAME} PRIVATE ${LIBHWLOC_LIBRARY_DIRS})
3939
target_link_libraries(${EX_NAME} PRIVATE ${LIBUMF_LIBRARIES} hwloc)
40+
if(WINDOWS)
41+
# link with Winsock lib
42+
target_link_libraries(${EX_NAME} PRIVATE ${LIBUMF_LIBRARIES} ws2_32)
43+
endif()
4044
endforeach(loop_var)
4145
endfunction()
4246

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

47-
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${script}.sh
51+
if(WINDOWS)
52+
set(script_ext bat)
53+
else()
54+
set(script_ext sh)
55+
endif()
56+
57+
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${script}.${script_ext}
4858
DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
4959

5060
add_test(
5161
NAME ${EXAMPLE_NAME}
52-
COMMAND ${script}.sh
62+
COMMAND ${script}.${script_ext}
5363
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
5464

5565
if(LINUX)
@@ -72,4 +82,7 @@ build_umf_ipc_example(ipc_ipcapi)
7282

7383
# an optional part - adds a test of this example
7484
add_test_for_umf_ipc_example(ipc_ipcapi_anon_fd)
75-
add_test_for_umf_ipc_example(ipc_ipcapi_shm)
85+
86+
if(LINUX)
87+
add_test_for_umf_ipc_example(ipc_ipcapi_shm)
88+
endif()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Copyright (C) 2025 Intel Corporation
3+
#
4+
# Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
5+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
#
7+
8+
# port should be a number from the range <1024, 65535>
9+
PORT=$(( 1024 + ( $$ % ( 65535 - 1024 ))))
10+
11+
UMF_LOG_VAL="level:debug;flush:debug;output:stderr;pid:yes"
12+
13+
echo "Starting ipc_ipcapi_anon_fd CONSUMER on port $PORT ..."
14+
UMF_LOG=$UMF_LOG_VAL ./umf_example_ipc_ipcapi_consumer $PORT &
15+
16+
echo "Waiting 1 sec ..."
17+
sleep 1
18+
19+
echo "Starting ipc_ipcapi_anon_fd PRODUCER on port $PORT ..."
20+
UMF_LOG=$UMF_LOG_VAL ./umf_example_ipc_ipcapi_producer $PORT

examples/ipc_ipcapi/ipc_ipcapi_consumer.c

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@
55
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
*/
77

8+
#ifdef _WIN32
9+
#define _WINSOCK_DEPRECATED_NO_WARNINGS
10+
#include <winsock2.h>
11+
typedef int socklen_t;
12+
typedef SSIZE_T ssize_t;
13+
#else
814
#include <arpa/inet.h>
15+
#include <sys/socket.h>
16+
#include <unistd.h>
17+
#endif
18+
919
#include <stdio.h>
1020
#include <stdlib.h>
1121
#include <string.h>
12-
#include <sys/socket.h>
13-
#include <unistd.h>
1422

1523
#include <umf/ipc.h>
1624
#include <umf/memory_pool.h>
@@ -27,25 +35,40 @@
2735
"shared memory!"
2836

2937
int consumer_connect_to_producer(int port) {
38+
#ifdef _WIN32
39+
WSADATA wsaData;
40+
SOCKET producer_socket, consumer_socket;
41+
#else
42+
int producer_socket = -1;
43+
int consumer_socket = -1;
44+
#endif
45+
3046
struct sockaddr_in consumer_addr;
3147
struct sockaddr_in producer_addr;
48+
3249
int producer_addr_len;
33-
int producer_socket = -1;
34-
int consumer_socket = -1;
3550
int ret = -1;
3651

52+
#ifdef _WIN32
53+
// initialize Winsock
54+
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
55+
fprintf(stderr, "Failed. Error Code : %d", WSAGetLastError());
56+
return -1;
57+
}
58+
#endif
59+
3760
// create a socket
3861
consumer_socket = socket(AF_INET, SOCK_STREAM, 0);
3962
if (consumer_socket < 0) {
4063
fprintf(stderr, "[consumer] ERROR: creating socket failed\n");
41-
return -1;
64+
goto err_WSA_cleanup;
4265
}
4366

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

4669
// set the IP address and the port
4770
consumer_addr.sin_family = AF_INET;
48-
consumer_addr.sin_port = htons(port);
71+
consumer_addr.sin_port = htons((u_short)port);
4972
consumer_addr.sin_addr.s_addr = inet_addr(INET_ADDR);
5073

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

80-
ret = producer_socket; // success
103+
ret = (int)producer_socket; // success
81104

82105
err_close_consumer_socket:
106+
#ifdef _WIN32
107+
closesocket(consumer_socket);
108+
#else
83109
close(consumer_socket);
110+
#endif
111+
112+
err_WSA_cleanup:
113+
#ifdef _WIN32
114+
WSACleanup();
115+
#endif
84116

85117
return ret;
86118
}
@@ -175,8 +207,8 @@ int main(int argc, char *argv[]) {
175207
len, size_IPC_handle);
176208

177209
// send received size to the producer as a confirmation
178-
recv_len =
179-
send(producer_socket, &size_IPC_handle, sizeof(size_IPC_handle), 0);
210+
recv_len = send(producer_socket, (const char *)&size_IPC_handle,
211+
sizeof(size_IPC_handle), 0);
180212
if (recv_len < 0) {
181213
fprintf(stderr, "[consumer] ERROR: sending confirmation failed\n");
182214
goto err_close_producer_socket;

examples/ipc_ipcapi/ipc_ipcapi_producer.c

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@
55
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
*/
77

8+
#ifdef _WIN32
9+
#define _WINSOCK_DEPRECATED_NO_WARNINGS
10+
#include <winsock2.h>
11+
typedef int socklen_t;
12+
typedef SSIZE_T ssize_t;
13+
#else
814
#include <arpa/inet.h>
9-
#include <stdio.h>
10-
#include <stdlib.h>
11-
#include <string.h>
1215
#include <sys/prctl.h>
1316
#include <sys/socket.h>
1417
#include <unistd.h>
18+
#endif
19+
20+
#include <stdio.h>
21+
#include <stdlib.h>
22+
#include <string.h>
1523

1624
#include <umf/ipc.h>
1725
#include <umf/memory_pool.h>
@@ -23,21 +31,35 @@
2331
#define SIZE_SHM 1024
2432

2533
int producer_connect_to_consumer(int port) {
26-
struct sockaddr_in consumer_addr;
34+
#ifdef _WIN32
35+
WSADATA wsaData;
36+
SOCKET producer_socket;
37+
#else
2738
int producer_socket = -1;
39+
#endif
40+
41+
struct sockaddr_in consumer_addr;
42+
43+
#ifdef _WIN32
44+
// initialize Winsock
45+
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
46+
fprintf(stderr, "Failed. Error Code : %d", WSAGetLastError());
47+
return -1;
48+
}
49+
#endif
2850

2951
// create a producer socket
3052
producer_socket = socket(AF_INET, SOCK_STREAM, 0);
3153
if (producer_socket < 0) {
3254
fprintf(stderr, "[producer] ERROR: Unable to create socket\n");
33-
return -1;
55+
goto err_WSA_cleanup;
3456
}
3557

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

3860
// set IP address and port the same as for the consumer
3961
consumer_addr.sin_family = AF_INET;
40-
consumer_addr.sin_port = htons(port);
62+
consumer_addr.sin_port = htons((u_short)port);
4163
consumer_addr.sin_addr.s_addr = inet_addr(INET_ADDR);
4264

4365
// send connection request to the consumer
@@ -50,10 +72,19 @@ int producer_connect_to_consumer(int port) {
5072

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

53-
return producer_socket; // success
75+
return (int)producer_socket; // success
5476

5577
err_close_producer_socket_connect:
78+
#ifdef _WIN32
79+
closesocket(producer_socket);
80+
#else
5681
close(producer_socket);
82+
#endif
83+
84+
err_WSA_cleanup:
85+
#ifdef _WIN32
86+
WSACleanup();
87+
#endif
5788

5889
return -1;
5990
}
@@ -163,8 +194,8 @@ int main(int argc, char *argv[]) {
163194
}
164195

165196
// send a size of the IPC_handle to the consumer
166-
ssize_t len =
167-
send(producer_socket, &IPC_handle_size, sizeof(IPC_handle_size), 0);
197+
ssize_t len = (ssize_t)send(producer_socket, (const char *)&IPC_handle_size,
198+
sizeof(IPC_handle_size), 0);
168199
if (len < 0) {
169200
fprintf(stderr, "[producer] ERROR: unable to send the message\n");
170201
goto err_close_producer_socket;
@@ -179,7 +210,7 @@ int main(int argc, char *argv[]) {
179210
memset(recv_buffer, 0, sizeof(recv_buffer));
180211

181212
// receive the consumer's confirmation
182-
len = recv(producer_socket, recv_buffer, sizeof(recv_buffer), 0);
213+
len = (ssize_t)recv(producer_socket, recv_buffer, sizeof(recv_buffer), 0);
183214
if (len < 0) {
184215
fprintf(stderr, "[producer] ERROR: error while receiving the "
185216
"confirmation from the consumer\n");
@@ -201,7 +232,8 @@ int main(int argc, char *argv[]) {
201232
}
202233

203234
// send the IPC_handle of IPC_handle_size to the consumer
204-
len = send(producer_socket, IPC_handle, IPC_handle_size, 0);
235+
len = (ssize_t)send(producer_socket, (const char *)IPC_handle,
236+
(int)IPC_handle_size, 0);
205237
if (len < 0) {
206238
fprintf(stderr, "[producer] ERROR: unable to send the message\n");
207239
goto err_close_producer_socket;
@@ -253,7 +285,11 @@ int main(int argc, char *argv[]) {
253285
}
254286

255287
err_close_producer_socket:
288+
#ifdef _WIN32
289+
closesocket(producer_socket);
290+
#else
256291
close(producer_socket);
292+
#endif
257293

258294
err_PutIPCHandle:
259295
umf_result = umfPutIPCHandle(IPC_handle);

0 commit comments

Comments
 (0)