|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "benchmark/benchmark.h" |
| 19 | + |
| 20 | +#include <cstdint> |
| 21 | +#include <memory> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +#include "arrow/array.h" |
| 25 | +#include "arrow/memory_pool.h" |
| 26 | +#include "arrow/test-util.h" |
| 27 | + |
| 28 | +#include "arrow/gpu/cuda_memory.h" |
| 29 | + |
| 30 | +namespace arrow { |
| 31 | +namespace gpu { |
| 32 | + |
| 33 | +constexpr int64_t kGpuNumber = 0; |
| 34 | + |
| 35 | +static void CudaBufferWriterBenchmark(benchmark::State& state, const int64_t total_bytes, |
| 36 | + const int64_t chunksize, |
| 37 | + const int64_t buffer_size) { |
| 38 | + std::shared_ptr<CudaBuffer> device_buffer; |
| 39 | + ABORT_NOT_OK(AllocateCudaBuffer(kGpuNumber, total_bytes, &device_buffer)); |
| 40 | + CudaBufferWriter writer(device_buffer); |
| 41 | + |
| 42 | + if (buffer_size > 0) { |
| 43 | + ABORT_NOT_OK(writer.SetBufferSize(buffer_size)); |
| 44 | + } |
| 45 | + |
| 46 | + std::shared_ptr<PoolBuffer> buffer; |
| 47 | + ASSERT_OK(test::MakeRandomBytePoolBuffer(total_bytes, default_memory_pool(), &buffer)); |
| 48 | + |
| 49 | + const uint8_t* host_data = buffer->data(); |
| 50 | + while (state.KeepRunning()) { |
| 51 | + int64_t bytes_written = 0; |
| 52 | + ABORT_NOT_OK(writer.Seek(0)); |
| 53 | + while (bytes_written < total_bytes) { |
| 54 | + int64_t bytes_to_write = std::min(chunksize, total_bytes - bytes_written); |
| 55 | + ABORT_NOT_OK(writer.Write(host_data + bytes_written, bytes_to_write)); |
| 56 | + bytes_written += bytes_to_write; |
| 57 | + } |
| 58 | + } |
| 59 | + state.SetBytesProcessed(int64_t(state.iterations()) * total_bytes); |
| 60 | +} |
| 61 | + |
| 62 | +static void BM_Writer_Buffered(benchmark::State& state) { |
| 63 | + // 128MB |
| 64 | + const int64_t kTotalBytes = 1 << 27; |
| 65 | + |
| 66 | + // 8MB |
| 67 | + const int64_t kBufferSize = 1 << 23; |
| 68 | + |
| 69 | + CudaBufferWriterBenchmark(state, kTotalBytes, state.range(0), kBufferSize); |
| 70 | +} |
| 71 | + |
| 72 | +static void BM_Writer_Unbuffered(benchmark::State& state) { |
| 73 | + // 128MB |
| 74 | + const int64_t kTotalBytes = 1 << 27; |
| 75 | + CudaBufferWriterBenchmark(state, kTotalBytes, state.range(0), 0); |
| 76 | +} |
| 77 | + |
| 78 | +// Vary chunk write size from 256 bytes to 64K |
| 79 | +BENCHMARK(BM_Writer_Buffered) |
| 80 | + ->RangeMultiplier(16) |
| 81 | + ->Range(1 << 8, 1 << 16) |
| 82 | + ->MinTime(1.0) |
| 83 | + ->UseRealTime(); |
| 84 | + |
| 85 | +BENCHMARK(BM_Writer_Unbuffered) |
| 86 | + ->RangeMultiplier(4) |
| 87 | + ->RangeMultiplier(16) |
| 88 | + ->Range(1 << 8, 1 << 16) |
| 89 | + ->MinTime(1.0) |
| 90 | + ->UseRealTime(); |
| 91 | + |
| 92 | +} // namespace gpu |
| 93 | +} // namespace arrow |
0 commit comments