|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include "paddle/memory/detail/system_allocator.h" |
| 16 | + |
| 17 | +#include <stdlib.h> // for malloc and free |
| 18 | +#include <sys/mman.h> // for mlock and munlock |
| 19 | + |
| 20 | +#include "gflags/gflags.h" |
| 21 | +#include "paddle/platform/assert.h" |
| 22 | +#include "paddle/platform/cuda.h" |
| 23 | + |
| 24 | +// If use_pinned_memory is true, CPUAllocator calls mlock, which |
| 25 | +// returns pinned and locked memory as staging areas for data exchange |
| 26 | +// between host and device. Allocates too much would reduce the amount |
| 27 | +// of memory available to the system for paging. So, by default, we |
| 28 | +// should set false to use_pinned_memory. |
| 29 | +DEFINE_bool(use_pinned_memory, false, |
| 30 | + "If set, allocate cpu/gpu pinned memory."); |
| 31 | + |
| 32 | +namespace paddle { |
| 33 | +namespace memory { |
| 34 | +namespace detail { |
| 35 | + |
| 36 | +void* CPUAllocator::Alloc(size_t size) { |
| 37 | + // According to http://www.cplusplus.com/reference/cstdlib/malloc/, |
| 38 | + // malloc might not return nullptr if size is zero, but the returned |
| 39 | + // pointer shall not be dereferenced -- so we make it nullptr. |
| 40 | + if (size <= 0) return nullptr; |
| 41 | + |
| 42 | + void* p = malloc(size); |
| 43 | + if (p != nullptr && FLAGS_use_pinned_memory) { |
| 44 | + mlock(p, size); |
| 45 | + } |
| 46 | + return p; |
| 47 | +} |
| 48 | + |
| 49 | +void CPUAllocator::Free(void* p, size_t size) { |
| 50 | + if (p != nullptr && FLAGS_use_pinned_memory) { |
| 51 | + munlock(p, size); |
| 52 | + } |
| 53 | + free(p); |
| 54 | +} |
| 55 | + |
| 56 | +#ifndef PADDLE_ONLY_CPU |
| 57 | + |
| 58 | +void* GPUAllocator::Alloc(size_t size) { |
| 59 | + // CUDA documentation doesn't explain if cudaMalloc returns nullptr |
| 60 | + // if size is 0. We just make sure it does. |
| 61 | + if (size <= 0) { |
| 62 | + return nullptr; |
| 63 | + } |
| 64 | + |
| 65 | + void* p = 0; |
| 66 | + cudaError_t result = |
| 67 | + FLAGS_use_pinned_memory ? cudaMallocHost(&p, size) : cudaMalloc(&p, size); |
| 68 | + if (result != cudaSuccess) { |
| 69 | + cudaGetLastError(); // clear error if there is any. |
| 70 | + } |
| 71 | + return result == cudaSuccess ? p : nullptr; |
| 72 | +} |
| 73 | + |
| 74 | +void GPUAllocator::Free(void* p, size_t size) { |
| 75 | + // Purposefully allow cudaErrorCudartUnloading, because |
| 76 | + // that is returned if you ever call cudaFree after the |
| 77 | + // driver has already shutdown. This happens only if the |
| 78 | + // process is terminating, in which case we don't care if |
| 79 | + // cudaFree succeeds. |
| 80 | + cudaError_t err = FLAGS_use_pinned_memory ? cudaFreeHost(p) : cudaFree(p); |
| 81 | + if (err != cudaErrorCudartUnloading) { |
| 82 | + platform::throw_on_error(err, "cudaFree{Host} failed"); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +#endif // PADDLE_ONLY_CPU |
| 87 | + |
| 88 | +} // namespace detail |
| 89 | +} // namespace memory |
| 90 | +} // namespace paddle |
0 commit comments