Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions paddle/phi/api/lib/tensor_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ PADDLE_API phi::Place GetPlaceFromPtr(void* data) {
#ifdef PADDLE_WITH_CUDA
cudaPointerAttributes attr = {};
cudaError_t status = cudaPointerGetAttributes(&attr, data);
if (status == cudaSuccess && attr.type == cudaMemoryTypeDevice) {
return phi::GPUPlace(attr.device);
if (status == cudaSuccess) {
if (attr.type == cudaMemoryTypeDevice) {
return phi::GPUPlace(attr.device);
} else if (attr.type == cudaMemoryTypeHost) {
return phi::GPUPinnedPlace();
}
}
#else
hipPointerAttribute_t attr = {};
Expand Down
9 changes: 9 additions & 0 deletions test/cpp/phi/api/test_from_blob.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ TEST(GetPlaceFromPtr, GPU) {
ASSERT_EQ(gpu1_data_place, phi::GPUPlace(1));
std::cout << "gpu1_data_place: " << gpu1_data_place << std::endl;
}

// Test GPUPinnedPlace (cudaMemoryTypeHost)
auto pinned_alloc_ptr =
paddle::GetAllocator(phi::GPUPinnedPlace())->Allocate(sizeof(cpu_data));
float* pinned_data = static_cast<float*>(pinned_alloc_ptr->ptr());
auto pinned_data_place = GetPlaceFromPtr(pinned_data);
ASSERT_EQ(pinned_data_place, phi::GPUPinnedPlace());
std::cout << "pinned_data_place: " << pinned_data_place << std::endl;
pinned_alloc_ptr.release();
}

TEST(from_blob, GPU) {
Expand Down