From ad9908e32b4ec023c9401e8174f8c2bc624b0aea Mon Sep 17 00:00:00 2001 From: shawnz Date: Wed, 2 Apr 2025 11:20:09 +0800 Subject: [PATCH] Bug4914019 & 4191696: Append pid in shmName for MIG multiple thread scenario --- Samples/0_Introduction/simpleIPC/simpleIPC.cu | 24 +++++++++++++++++-- .../streamOrderedAllocationIPC.cu | 24 +++++++++++++++++-- .../memMapIPCDrv/memMapIpc.cpp | 23 ++++++++++++++++-- 3 files changed, 65 insertions(+), 6 deletions(-) diff --git a/Samples/0_Introduction/simpleIPC/simpleIPC.cu b/Samples/0_Introduction/simpleIPC/simpleIPC.cu index 1e2a9a93..544be8bd 100644 --- a/Samples/0_Introduction/simpleIPC/simpleIPC.cu +++ b/Samples/0_Introduction/simpleIPC/simpleIPC.cu @@ -99,8 +99,18 @@ static void childProcess(int id) std::vector ptrs; std::vector events; std::vector verification_buffer(DATA_SIZE); + pid_t pid; + char pidString[20] = {0}; + char lshmName[40] = {0}; - if (sharedMemoryOpen(shmName, sizeof(shmStruct), &info) != 0) { + pid = getppid(); + snprintf(pidString, sizeof(pidString), "%d", pid); + strcat(lshmName, shmName); + strcat(lshmName, pidString); + + printf("CP: lshmName = %s\n", lshmName); + + if (sharedMemoryOpen(lshmName, sizeof(shmStruct), &info) != 0) { printf("Failed to create shared memory slab\n"); exit(EXIT_FAILURE); } @@ -195,10 +205,20 @@ static void parentProcess(char *app) std::vector ptrs; std::vector events; std::vector processes; + pid_t pid; + char pidString[20] = {0}; + char lshmName[40] = {0}; + + pid = getpid(); + snprintf(pidString, sizeof(pidString), "%d", pid); + strcat(lshmName, shmName); + strcat(lshmName, pidString); + + printf("PP: lshmName = %s\n", lshmName); checkCudaErrors(cudaGetDeviceCount(&devCount)); - if (sharedMemoryCreate(shmName, sizeof(*shm), &info) != 0) { + if (sharedMemoryCreate(lshmName, sizeof(*shm), &info) != 0) { printf("Failed to create shared memory slab\n"); exit(EXIT_FAILURE); } diff --git a/Samples/2_Concepts_and_Techniques/streamOrderedAllocationIPC/streamOrderedAllocationIPC.cu b/Samples/2_Concepts_and_Techniques/streamOrderedAllocationIPC/streamOrderedAllocationIPC.cu index 199a862c..b7dbdf66 100644 --- a/Samples/2_Concepts_and_Techniques/streamOrderedAllocationIPC/streamOrderedAllocationIPC.cu +++ b/Samples/2_Concepts_and_Techniques/streamOrderedAllocationIPC/streamOrderedAllocationIPC.cu @@ -102,13 +102,23 @@ static void childProcess(int id) int threads = 128; cudaDeviceProp prop; std::vector ptrs; + pid_t pid; + char pidString[20] = {0}; + char lshmName[40] = {0}; std::vector verification_buffer(DATA_SIZE); + pid = getppid(); + snprintf(pidString, sizeof(pidString), "%d", pid); + strcat(lshmName, shmName); + strcat(lshmName, pidString); + + printf("CP: lshmName = %s\n", lshmName); + ipcHandle *ipcChildHandle = NULL; checkIpcErrors(ipcOpenSocket(ipcChildHandle)); - if (sharedMemoryOpen(shmName, sizeof(shmStruct), &info) != 0) { + if (sharedMemoryOpen(lshmName, sizeof(shmStruct), &info) != 0) { printf("Failed to create shared memory slab\n"); exit(EXIT_FAILURE); } @@ -245,6 +255,16 @@ static void parentProcess(char *app) std::vector ptrs; std::vector processes; cudaMemAllocationHandleType handleType = cudaMemHandleTypeNone; + pid_t pid; + char pidString[20] = {0}; + char lshmName[40] = {0}; + + pid = getpid(); + snprintf(pidString, sizeof(pidString), "%d", pid); + strcat(lshmName, shmName); + strcat(lshmName, pidString); + + printf("PP: lshmName = %s\n", lshmName); checkCudaErrors(cudaGetDeviceCount(&devCount)); std::vector devices(devCount); @@ -252,7 +272,7 @@ static void parentProcess(char *app) cuDeviceGet(&devices[i], i); } - if (sharedMemoryCreate(shmName, sizeof(*shm), &info) != 0) { + if (sharedMemoryCreate(lshmName, sizeof(*shm), &info) != 0) { printf("Failed to create shared memory slab\n"); exit(EXIT_FAILURE); } diff --git a/Samples/3_CUDA_Features/memMapIPCDrv/memMapIpc.cpp b/Samples/3_CUDA_Features/memMapIPCDrv/memMapIpc.cpp index 7811a9c0..d95ffb64 100644 --- a/Samples/3_CUDA_Features/memMapIPCDrv/memMapIpc.cpp +++ b/Samples/3_CUDA_Features/memMapIPCDrv/memMapIpc.cpp @@ -310,10 +310,20 @@ static void childProcess(int devId, int id, char **argv) ipcHandle *ipcChildHandle = NULL; int blocks = 0; int threads = 128; + pid_t pid; + char pidString[20] = {0}; + char lshmName[40] = {0}; + + pid = getppid(); + snprintf(pidString, sizeof(pidString), "%d", pid); + strcat(lshmName, shmName); + strcat(lshmName, pidString); + + printf("CP: lshmName = %s\n", lshmName); checkIpcErrors(ipcOpenSocket(ipcChildHandle)); - if (sharedMemoryOpen(shmName, sizeof(shmStruct), &info) != 0) { + if (sharedMemoryOpen(lshmName, sizeof(shmStruct), &info) != 0) { printf("Failed to create shared memory slab\n"); exit(EXIT_FAILURE); } @@ -421,11 +431,20 @@ static void parentProcess(char *app) volatile shmStruct *shm = NULL; sharedMemoryInfo info; std::vector processes; + pid_t pid; + char pidString[20] = {0}; + char lshmName[40] = {0}; + pid = getpid(); + snprintf(pidString, sizeof(pidString), "%d", pid); + strcat(lshmName, shmName); + strcat(lshmName, pidString); + + printf("PP: lshmName = %s\n", lshmName); checkCudaErrors(cuDeviceGetCount(&devCount)); std::vector devices(devCount); - if (sharedMemoryCreate(shmName, sizeof(*shm), &info) != 0) { + if (sharedMemoryCreate(lshmName, sizeof(*shm), &info) != 0) { printf("Failed to create shared memory slab\n"); exit(EXIT_FAILURE); }