Skip to content

Commit c75a880

Browse files
committed
fix windows bug
test=develop
1 parent 953214a commit c75a880

File tree

7 files changed

+20
-43
lines changed

7 files changed

+20
-43
lines changed

paddle/fluid/memory/allocation/auto_growth_best_fit_allocator_facade_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ TEST(allocator, allocator) {
4343
FLAGS_allocator_strategy = "auto_growth_best_fit";
4444

4545
auto &instance = AllocatorFacade::Instance();
46-
platform::Place place;
4746
size_t size = 1024;
47+
platform::Place place;
4848

4949
{
5050
place = platform::CPUPlace();

paddle/fluid/memory/allocation/cpu_allocator.cc

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,27 @@ namespace paddle {
2020
namespace memory {
2121
namespace allocation {
2222

23-
CPUAllocation::CPUAllocation(void *ptr, size_t size)
24-
: Allocation(ptr, size, platform::CPUPlace()) {}
25-
2623
bool CPUAllocator::IsAllocThreadSafe() const { return true; }
2724

2825
void CPUAllocator::FreeImpl(Allocation *allocation) {
29-
PADDLE_ENFORCE_NOT_NULL(dynamic_cast<CPUAllocation *>(allocation));
30-
free(allocation->ptr());
26+
void *p = allocation->ptr();
27+
#ifdef _WIN32
28+
_aligned_free(p);
29+
#else
30+
free(p);
31+
#endif
3132
delete allocation;
3233
}
3334

3435
Allocation *CPUAllocator::AllocateImpl(size_t size, Allocator::Attr attr) {
35-
void *ptr;
36-
auto status = posix_memalign(&ptr, kAlignment, size);
37-
if (UNLIKELY(status) != 0) {
38-
throw BadAlloc(string::Sprintf("Cannot allocate cpu memory %d. Errno is %d",
39-
size, status));
40-
}
41-
return new CPUAllocation(ptr, size);
36+
void *p;
37+
#ifdef _WIN32
38+
p = _aligned_malloc(size, 4096);
39+
#else
40+
PADDLE_ENFORCE_EQ(posix_memalign(&p, 4096, size), 0, "Alloc %ld error!",
41+
size);
42+
#endif
43+
return new Allocation(p, size, platform::CPUPlace());
4244
}
4345
} // namespace allocation
4446
} // namespace memory

paddle/fluid/memory/allocation/cpu_allocator.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ namespace allocation {
3131
//
3232
// NOTE(yy): It is no need to use `BestFitAllocator` in CPU. We can import
3333
// an open-sourced allocator into Paddle.
34-
class CPUAllocator;
35-
class CPUAllocation : public Allocation {
36-
public:
37-
CPUAllocation(void* ptr, size_t size);
38-
};
39-
4034
class CPUAllocator : public Allocator {
4135
public:
4236
constexpr static size_t kAlignment = 64u;

paddle/fluid/memory/allocation/cuda_allocator.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,12 @@ namespace allocation {
2525
bool CUDAAllocator::IsAllocThreadSafe() const { return true; }
2626
void CUDAAllocator::FreeImpl(Allocation* allocation) {
2727
platform::CUDADeviceGuard guard(place_.device);
28-
auto* cuda_allocation = dynamic_cast<CUDAAllocation*>(allocation);
29-
PADDLE_ENFORCE_NOT_NULL(cuda_allocation);
30-
PADDLE_ENFORCE_EQ(boost::get<platform::CUDAPlace>(cuda_allocation->place()),
28+
PADDLE_ENFORCE_EQ(boost::get<platform::CUDAPlace>(allocation->place()),
3129
place_);
3230
PADDLE_ENFORCE(cudaFree(allocation->ptr()));
33-
VLOG(2) << "cudaFree is called";
3431
delete allocation;
3532
}
33+
3634
Allocation* CUDAAllocator::AllocateImpl(size_t size, Allocator::Attr attr) {
3735
platform::CUDADeviceGuard guard(place_.device);
3836
void* ptr;
@@ -42,8 +40,9 @@ Allocation* CUDAAllocator::AllocateImpl(size_t size, Allocator::Attr attr) {
4240
"Cannot allocate %d on GPU %d, cuda status %d, %s", size, place_.device,
4341
status, cudaGetErrorString(status)));
4442
}
45-
return new CUDAAllocation(ptr, size, platform::Place(place_));
43+
return new Allocation(ptr, size, platform::Place(place_));
4644
}
45+
4746
} // namespace allocation
4847
} // namespace memory
4948
} // namespace paddle

paddle/fluid/memory/allocation/cuda_allocator.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ namespace paddle {
2020
namespace memory {
2121
namespace allocation {
2222

23-
// CUDA System allocator and allocation.
24-
// Just a flag type.
25-
class CUDAAllocation : public Allocation {
26-
public:
27-
using Allocation::Allocation;
28-
};
29-
3023
class CUDAAllocator : public Allocator {
3124
public:
3225
explicit CUDAAllocator(const platform::CUDAPlace& place) : place_(place) {}

paddle/fluid/memory/allocation/pinned_allocator.cc

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,14 @@ namespace memory {
2121
namespace allocation {
2222
bool CPUPinnedAllocator::IsAllocThreadSafe() const { return true; }
2323
void CPUPinnedAllocator::FreeImpl(Allocation *allocation) {
24-
PADDLE_ENFORCE_NOT_NULL(dynamic_cast<CPUPinnedAllocation *>(allocation));
2524
PADDLE_ENFORCE(cudaFreeHost(allocation->ptr()));
2625
delete allocation;
2726
}
2827
Allocation *CPUPinnedAllocator::AllocateImpl(size_t size,
2928
Allocator::Attr attr) {
30-
// PADDLE_ENFORCE_EQ(
31-
// attr, kCrossDevice,
32-
// "CPUPinnedAllocator should be used for Cross-Device Communication");
33-
3429
void *ptr;
3530
PADDLE_ENFORCE(cudaHostAlloc(&ptr, size, cudaHostAllocPortable));
36-
return new CPUPinnedAllocation(ptr, size);
31+
return new Allocation(ptr, size, platform::CUDAPinnedPlace());
3732
}
3833
} // namespace allocation
3934
} // namespace memory

paddle/fluid/memory/allocation/pinned_allocator.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ namespace memory {
2020
namespace allocation {
2121

2222
// Allocator uses `cudaHostAlloc`
23-
class CPUPinnedAllocation : public Allocation {
24-
public:
25-
CPUPinnedAllocation(void *ptr, size_t size)
26-
: Allocation(ptr, size, platform::CUDAPinnedPlace()) {}
27-
};
28-
2923
class CPUPinnedAllocator : public Allocator {
3024
public:
3125
bool IsAllocThreadSafe() const override;

0 commit comments

Comments
 (0)