-
Notifications
You must be signed in to change notification settings - Fork 741
Expand file tree
/
Copy pathget_data_ptr_ipc.cu
More file actions
70 lines (63 loc) · 2.33 KB
/
get_data_ptr_ipc.cu
File metadata and controls
70 lines (63 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cstring>
#include "cuda_multiprocess.h"
#include "helper.h"
namespace {
#if !defined(_WIN32)
int sharedMemoryOpen2(const char *name, size_t sz, sharedMemoryInfo *info) {
info->size = sz;
info->shmFd = shm_open(name, O_RDWR, 0777);
if (info->shmFd < 0) {
return errno;
}
info->addr = mmap(0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, info->shmFd, 0);
if (info->addr == NULL) {
return errno;
}
return 0;
}
#endif
} // namespace
std::vector<paddle::Tensor> GetDataPtrIpc(const paddle::Tensor &tmp_input,
const std::string &shm_name) {
#if defined(_WIN32)
PD_THROW(
"GetDataPtrIpc is not supported on Windows "
"(POSIX shared memory required).");
#else
auto out_data_ptr_tensor =
paddle::full({1}, 0, paddle::DataType::INT64, paddle::CPUPlace());
auto out_data_ptr_tensor_ptr = out_data_ptr_tensor.data<int64_t>();
volatile shmStruct *shm = NULL;
sharedMemoryInfo info;
if (sharedMemoryOpen2(shm_name.c_str(), sizeof(shmStruct), &info) != 0) {
throw std::runtime_error(
"Failed to open shared memory slab in GetDataPtrIpc, shm_name: " +
shm_name + ", errno: " + std::string(strerror(errno)));
}
shm = (volatile shmStruct *)info.addr;
void *ptr = nullptr;
checkCudaErrors(cudaIpcOpenMemHandle(&ptr,
*(cudaIpcMemHandle_t *)&shm->memHandle,
cudaIpcMemLazyEnablePeerAccess));
out_data_ptr_tensor_ptr[0] = reinterpret_cast<int64_t>(ptr);
return {out_data_ptr_tensor};
#endif
}
PD_BUILD_STATIC_OP(get_data_ptr_ipc)
.Inputs({"tmp_input"})
.Attrs({"shm_name: std::string"})
.Outputs({"data_ptr"})
.SetKernelFn(PD_KERNEL(GetDataPtrIpc));