Skip to content

Commit 6da36e1

Browse files
committed
enable IPC tests and examples on Windows
1 parent 85b1d6f commit 6da36e1

File tree

9 files changed

+245
-61
lines changed

9 files changed

+245
-61
lines changed

Testing/Temporary/CTestCostData.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
---

examples/CMakeLists.txt

+11-2
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ function(build_umf_ipc_example name)
135135
${UMF_CMAKE_SOURCE_DIR}/include)
136136

137137
target_link_directories(${EX_NAME} PRIVATE ${LIBHWLOC_LIBRARY_DIRS})
138+
if(WINDOWS)
139+
# link with Winsock2 lib
140+
target_link_libraries(${EX_NAME} PRIVATE ws2_32)
141+
# append PATH to DLLs set_property(TEST ${EX_NAME} PROPERTY
142+
# ENVIRONMENT_MODIFICATION
143+
# "${DLL_PATH_LIST}")
144+
endif()
138145
endforeach(loop_var)
139146
endfunction()
140147

@@ -155,14 +162,16 @@ function(add_umf_ipc_example script)
155162
endif()
156163
endfunction()
157164

165+
# IPC API examples
166+
build_umf_ipc_example(ipc_ipcapi)
167+
158168
if(LINUX)
159-
build_umf_ipc_example(ipc_ipcapi)
160169
add_umf_ipc_example(ipc_ipcapi_anon_fd)
161170
add_umf_ipc_example(ipc_ipcapi_shm)
162171
else()
163172
message(
164173
STATUS
165-
"IPC examples with UMF pool API are supported on Linux only - skipping"
174+
"IPC anon_fd and shm examples with UMF pool API are supported on Linux only - skipping"
166175
)
167176
endif()
168177

examples/ipc_ipcapi/CMakeLists.txt

+11-2
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,28 @@ 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(LINUX)
52+
set(UMF_SCRIPT_EXT sh)
53+
else()
54+
set(UMF_SCRIPT_EXT bat)
55+
56+
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${script}.${UMF_SCRIPT_EXT}
4857
DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
4958

5059
add_test(
5160
NAME ${EXAMPLE_NAME}
52-
COMMAND ${script}.sh
61+
COMMAND ${script}.${UMF_SCRIPT_EXT}
5362
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
5463

5564
if(LINUX)
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
# port should be a number from the range <1024, 65535>
3+
PORT=$(( 1024 + ( $$ % ( 65535 - 1024 ))))
4+
5+
UMF_LOG_VAL="level:debug;flush:debug;output:stderr;pid:yes"
6+
7+
echo "Starting ipc_ipcapi_anon_fd CONSUMER on port $PORT ..."
8+
UMF_LOG=$UMF_LOG_VAL ./umf_example_ipc_ipcapi_consumer $PORT &
9+
10+
echo "Waiting 1 sec ..."
11+
sleep 1
12+
13+
echo "Starting ipc_ipcapi_anon_fd PRODUCER on port $PORT ..."
14+
UMF_LOG=$UMF_LOG_VAL ./umf_example_ipc_ipcapi_producer $PORT

examples/ipc_ipcapi/ipc_ipcapi_consumer.c

+41-9
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
}
@@ -152,8 +184,8 @@ int main(int argc, char *argv[]) {
152184
len, size_IPC_handle);
153185

154186
// send received size to the producer as a confirmation
155-
recv_len =
156-
send(producer_socket, &size_IPC_handle, sizeof(size_IPC_handle), 0);
187+
recv_len = send(producer_socket, (const char *)&size_IPC_handle,
188+
sizeof(size_IPC_handle), 0);
157189
if (recv_len < 0) {
158190
fprintf(stderr, "[consumer] ERROR: sending confirmation failed\n");
159191
goto err_close_producer_socket;

examples/ipc_ipcapi/ipc_ipcapi_producer.c

+46-10
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>
@@ -22,21 +30,35 @@
2230
#define SIZE_SHM 1024
2331

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

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

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

3759
// set IP address and port the same as for the consumer
3860
consumer_addr.sin_family = AF_INET;
39-
consumer_addr.sin_port = htons(port);
61+
consumer_addr.sin_port = htons((u_short)port);
4062
consumer_addr.sin_addr.s_addr = inet_addr(INET_ADDR);
4163

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

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

52-
return producer_socket; // success
74+
return (int)producer_socket; // success
5375

5476
err_close_producer_socket_connect:
77+
#ifdef _WIN32
78+
closesocket(producer_socket);
79+
#else
5580
close(producer_socket);
81+
#endif
82+
83+
err_WSA_cleanup:
84+
#ifdef _WIN32
85+
WSACleanup();
86+
#endif
5687

5788
return -1;
5889
}
@@ -131,8 +162,8 @@ int main(int argc, char *argv[]) {
131162
}
132163

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

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

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

223255
err_close_producer_socket:
256+
#ifdef _WIN32
257+
closesocket(producer_socket);
258+
#else
224259
close(producer_socket);
260+
#endif
225261

226262
err_PutIPCHandle:
227263
umf_result = umfPutIPCHandle(IPC_handle);

src/provider/provider_level_zero.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@ static umf_result_t ze_memory_provider_initialize(void *params,
138138
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
139139
}
140140

141-
if ((ze_params->memory_type == UMF_MEMORY_TYPE_HOST) ==
142-
(bool)ze_params->level_zero_device_handle) {
141+
if ((ze_params->memory_type == UMF_MEMORY_TYPE_HOST) &&
142+
(ze_params->level_zero_device_handle != NULL)) {
143143
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
144144
}
145145

146-
if ((bool)ze_params->resident_device_count !=
147-
(bool)ze_params->resident_device_handles) {
146+
if ((ze_params->resident_device_count > 0) !=
147+
(ze_params->resident_device_handles != NULL)) {
148148
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
149149
}
150150

test/CMakeLists.txt

+29-17
Original file line numberDiff line numberDiff line change
@@ -340,23 +340,35 @@ function(add_umf_ipc_test)
340340
endif()
341341
endfunction()
342342

343+
if(NOT UMF_DISABLE_HWLOC)
344+
345+
if(WINDOWS)
346+
set(UMF_IPC_LIBS ws2_32)
347+
endif()
348+
349+
build_umf_test(
350+
NAME
351+
ipc_os_prov_consumer
352+
SRCS
353+
ipc_os_prov_consumer.c
354+
common/ipc_common.c
355+
common/ipc_os_prov_common.c
356+
LIBS
357+
${UMF_IPC_LIBS})
358+
build_umf_test(
359+
NAME
360+
ipc_os_prov_producer
361+
SRCS
362+
ipc_os_prov_producer.c
363+
common/ipc_common.c
364+
common/ipc_os_prov_common.c
365+
LIBS
366+
${UMF_IPC_LIBS})
367+
add_umf_ipc_test(TEST ipc_os_prov_anon_fd)
368+
endif()
369+
343370
if(LINUX)
344371
if(NOT UMF_DISABLE_HWLOC)
345-
build_umf_test(
346-
NAME
347-
ipc_os_prov_consumer
348-
SRCS
349-
ipc_os_prov_consumer.c
350-
common/ipc_common.c
351-
common/ipc_os_prov_common.c)
352-
build_umf_test(
353-
NAME
354-
ipc_os_prov_producer
355-
SRCS
356-
ipc_os_prov_producer.c
357-
common/ipc_common.c
358-
common/ipc_os_prov_common.c)
359-
add_umf_ipc_test(TEST ipc_os_prov_anon_fd)
360372
add_umf_ipc_test(TEST ipc_os_prov_shm)
361373

362374
build_umf_test(
@@ -482,12 +494,12 @@ if(LINUX
482494
)
483495
endif()
484496

485-
if(LINUX AND UMF_POOL_SCALABLE_ENABLED)
497+
if(UMF_POOL_SCALABLE_ENABLED)
486498
set(EXAMPLES ${EXAMPLES} ipc_ipcapi)
487499
else()
488500
message(
489501
STATUS
490-
"IPC examples with UMF pool API are supported on Linux with TBB installed only - skipping"
502+
"IPC examples with UMF pool API are supported with TBB installed only - skipping"
491503
)
492504
endif()
493505

0 commit comments

Comments
 (0)