Skip to content

Commit 6fc3e56

Browse files
authored
Merge pull request #451 from ldorau/Add_IPC_shared_memory_example_using_the_ipc.h_API
Add IPC shared memory example using the ipc.h API
2 parents 7be88e9 + 1989a7d commit 6fc3e56

File tree

9 files changed

+662
-19
lines changed

9 files changed

+662
-19
lines changed

CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,13 @@ install(FILES ${CMAKE_SOURCE_DIR}/LICENSE.TXT
447447
DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/doc/${PROJECT_NAME}/")
448448

449449
install(
450-
FILES examples/basic/gpu_shared_memory.c examples/basic/utils_level_zero.h
451-
examples/basic/basic.c examples/basic/ipc_level_zero.c
450+
FILES examples/basic/gpu_shared_memory.c
451+
examples/basic/utils_level_zero.h
452+
examples/basic/basic.c
453+
examples/basic/ipc_level_zero.c
454+
examples/basic/ipc_shm_ipcapi.sh
455+
examples/basic/ipc_shm_ipcapi_consumer.c
456+
examples/basic/ipc_shm_ipcapi_producer.c
452457
DESTINATION "${CMAKE_INSTALL_DOCDIR}/examples")
453458

454459
# Add the include directory and the headers target to the install.

examples/CMakeLists.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,37 @@ else()
108108
"UMF_BUILD_LEVEL_ZERO_PROVIDER, UMF_BUILD_LIBUMF_POOL_DISJOINT and "
109109
"UMF_ENABLE_POOL_TRACKING to be turned ON - skipping")
110110
endif()
111+
112+
if(LINUX AND UMF_BUILD_LIBUMF_POOL_SCALABLE)
113+
set(BASE_NAME ipc_shm_ipcapi)
114+
set(EXAMPLE_NAME umf_example_${BASE_NAME})
115+
116+
foreach(loop_var IN ITEMS "producer" "consumer")
117+
set(EX_NAME ${EXAMPLE_NAME}_${loop_var})
118+
add_umf_executable(
119+
NAME ${EX_NAME}
120+
SRCS basic/${BASE_NAME}_${loop_var}.c
121+
LIBS umf scalable_pool)
122+
123+
target_include_directories(
124+
${EX_NAME} PRIVATE ${UMF_CMAKE_SOURCE_DIR}/src/utils
125+
${UMF_CMAKE_SOURCE_DIR}/include)
126+
127+
target_link_directories(${EX_NAME} PRIVATE ${LIBHWLOC_LIBRARY_DIRS})
128+
endforeach(loop_var)
129+
130+
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/basic/${BASE_NAME}.sh
131+
DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
132+
133+
add_test(
134+
NAME ${EXAMPLE_NAME}
135+
COMMAND ${BASE_NAME}.sh
136+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
137+
138+
set_tests_properties(${EXAMPLE_NAME} PROPERTIES LABELS "example")
139+
else()
140+
message(
141+
STATUS
142+
"IPC shared memory example with UMF pool API is supported on Linux only - skipping"
143+
)
144+
endif()

examples/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,36 @@ and build this example Level Zero development package should be installed.
3838
* Level Zero headers and libraries
3939
* compatible GPU with installed driver
4040
* set UMF_BUILD_GPU_EXAMPLES, UMF_BUILD_LIBUMF_POOL_DISJOINT, UMF_BUILD_LEVEL_ZERO_PROVIDER and UMF_ENABLE_POOL_TRACKING CMake configuration flags to ON
41+
42+
## IPC example with shared memory
43+
This example also demonstrates how to use UMF IPC API. The example creates two
44+
processes: a producer and a consumer that communicate in the following way
45+
(the initial value N in the shared memory is quasi-random):
46+
- Consumer starts
47+
- Consumer creates a socket
48+
- Consumer listens for incoming connections
49+
- Producer starts
50+
- Producer's shared memory contains a number: N
51+
- Producer gets the IPC handle
52+
- Producer creates a socket
53+
- Producer connects to the consumer
54+
- Consumer connects at IP 127.0.0.1 and a port to the producer
55+
- Producer sends the size of the IPC handle to the consumer
56+
- Consumer receives the size of the IPC handle
57+
- Consumer sends the received size of the IPC handle as a confirmation back to the producer
58+
- Producer receives the confirmation from the consumer and verifies if it is correct
59+
- Producer sends the IPC handle to the consumer
60+
- Consumer receives the IPC handle from the producer
61+
- Consumer opens the IPC handle received from the producer
62+
- Consumer reads the number from the producer's shared memory: N
63+
- Consumer writes a new number directly to the producer's shared memory: N/2
64+
- Consumer sends a response message to the producer
65+
- Producer receives the response message from the consumer: "This is the consumer. I just wrote a new number directly into your shared memory!"
66+
- Producer verifies if the consumer wrote the correct value (the old one / 2) to the producer's shared memory: N/2
67+
- Consumer closes the IPC handle received from the producer
68+
- Producer puts the IPC handle
69+
- Consumer shuts down
70+
- Producer shuts down
71+
72+
### Requirements
73+
* set UMF_BUILD_LIBUMF_POOL_SCALABLE CMake configuration flag to ON

examples/basic/ipc_shm_ipcapi.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#
2+
# Copyright (C) 2024 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+
#!/bin/bash
9+
10+
# port should be a number from the range <1024, 65535>
11+
PORT=$(( 1024 + ( $$ % ( 65535 - 1024 ))))
12+
13+
# The ipc_shm_ipcapi example requires using pidfd_getfd(2)
14+
# to obtain a duplicate of another process's file descriptor.
15+
# Permission to duplicate another process's file descriptor
16+
# is governed by a ptrace access mode PTRACE_MODE_ATTACH_REALCREDS check (see ptrace(2))
17+
# that can be changed using the /proc/sys/kernel/yama/ptrace_scope interface.
18+
PTRACE_SCOPE_FILE="/proc/sys/kernel/yama/ptrace_scope"
19+
VAL=0
20+
if [ -f $PTRACE_SCOPE_FILE ]; then
21+
PTRACE_SCOPE_VAL=$(cat $PTRACE_SCOPE_FILE)
22+
if [ $PTRACE_SCOPE_VAL -ne $VAL ]; then
23+
# check if sudo requires password
24+
if ! timeout --kill-after=5s 3s sudo date; then
25+
echo "ERROR: sudo requires password - cannot set ptrace_scope to 0"
26+
exit 1
27+
fi
28+
echo "Setting ptrace_scope to 0 (classic ptrace permissions) ..."
29+
echo "$ sudo bash -c \"echo $VAL > $PTRACE_SCOPE_FILE\""
30+
sudo bash -c "echo $VAL > $PTRACE_SCOPE_FILE"
31+
fi
32+
PTRACE_SCOPE_VAL=$(cat $PTRACE_SCOPE_FILE)
33+
if [ $PTRACE_SCOPE_VAL -ne $VAL ]; then
34+
echo "SKIP: setting ptrace_scope to 0 (classic ptrace permissions) FAILED - skipping the test"
35+
exit 0
36+
fi
37+
fi
38+
39+
UMF_LOG_VAL="level:debug;flush:debug;output:stderr;pid:yes"
40+
41+
echo "Starting ipc_shm_ipcapi CONSUMER on port $PORT ..."
42+
UMF_LOG=$UMF_LOG_VAL ./umf_example_ipc_shm_ipcapi_consumer $PORT &
43+
44+
echo "Waiting 1 sec ..."
45+
sleep 1
46+
47+
echo "Starting ipc_shm_ipcapi PRODUCER on port $PORT ..."
48+
UMF_LOG=$UMF_LOG_VAL ./umf_example_ipc_shm_ipcapi_producer $PORT

0 commit comments

Comments
 (0)