|
| 1 | +// Copyright 2023 The gVisor Authors. |
| 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 <cuda_runtime.h> |
| 16 | +#include <err.h> |
| 17 | +#include <errno.h> |
| 18 | +#include <stdlib.h> |
| 19 | +#include <unistd.h> |
| 20 | + |
| 21 | +#include <cstdint> |
| 22 | +#include <iostream> |
| 23 | +#include <random> |
| 24 | + |
| 25 | +#include "cuda_test_util.h" // NOLINT(build/include) |
| 26 | + |
| 27 | +__global__ void addKernel(std::uint32_t* data) { |
| 28 | + size_t index = blockIdx.x * blockDim.x + threadIdx.x; |
| 29 | + data[index] += static_cast<std::uint32_t>(index); |
| 30 | +} |
| 31 | + |
| 32 | +void TestMallocManagedRoundTrip(int device, unsigned int malloc_flags, |
| 33 | + bool prefetch) { |
| 34 | + constexpr size_t kNumBlocks = 32; |
| 35 | + constexpr size_t kNumThreads = 64; |
| 36 | + constexpr size_t kNumElems = kNumBlocks * kNumThreads; |
| 37 | + |
| 38 | + std::uint32_t* data = nullptr; |
| 39 | + constexpr size_t kNumBytes = kNumElems * sizeof(*data); |
| 40 | + CHECK_CUDA(cudaMallocManaged(&data, kNumBytes, malloc_flags)); |
| 41 | + |
| 42 | + // Initialize all elements in the array with a random value on the host. |
| 43 | + std::default_random_engine rd; |
| 44 | + const std::uint32_t init_val = |
| 45 | + std::uniform_int_distribution<std::uint32_t>()(rd); |
| 46 | + for (size_t i = 0; i < kNumElems; i++) { |
| 47 | + data[i] = init_val; |
| 48 | + } |
| 49 | + |
| 50 | + if (prefetch) { |
| 51 | + CHECK_CUDA(cudaMemPrefetchAsync(data, kNumBytes, device)); |
| 52 | + } |
| 53 | + |
| 54 | + // Mutate the array on the device. |
| 55 | + addKernel<<<kNumBlocks, kNumThreads>>>(data); |
| 56 | + CHECK_CUDA(cudaGetLastError()); |
| 57 | + CHECK_CUDA(cudaDeviceSynchronize()); |
| 58 | + |
| 59 | + if (prefetch) { |
| 60 | + CHECK_CUDA(cudaMemPrefetchAsync(data, kNumBytes, cudaCpuDeviceId)); |
| 61 | + } |
| 62 | + |
| 63 | + // Check that the array has the expected result. |
| 64 | + for (size_t i = 0; i < kNumElems; i++) { |
| 65 | + std::uint32_t want = init_val + static_cast<std::uint32_t>(i); |
| 66 | + if (data[i] != want) { |
| 67 | + std::cout << "data[" << i << "]: got " << data[i] << ", wanted " << want |
| 68 | + << " = " << init_val << " + " << i << std::endl; |
| 69 | + abort(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + CHECK_CUDA(cudaFree(data)); |
| 74 | +} |
| 75 | + |
| 76 | +void TestMallocManagedReadWrite(int device) { |
| 77 | + constexpr size_t kNumBlocks = 32; |
| 78 | + constexpr size_t kNumThreads = 64; |
| 79 | + constexpr size_t kNumElems = kNumBlocks * kNumThreads; |
| 80 | + |
| 81 | + std::uint32_t* data = nullptr; |
| 82 | + constexpr size_t kNumBytes = kNumElems * sizeof(*data); |
| 83 | + CHECK_CUDA(cudaMallocManaged(&data, kNumBytes, cudaMemAttachGlobal)); |
| 84 | + |
| 85 | + // Initialize all elements in the array with a random value on the host. |
| 86 | + std::default_random_engine rd; |
| 87 | + const std::uint32_t init_val = |
| 88 | + std::uniform_int_distribution<std::uint32_t>()(rd); |
| 89 | + for (size_t i = 0; i < kNumElems; i++) { |
| 90 | + data[i] = init_val; |
| 91 | + } |
| 92 | + |
| 93 | + // Write the array's contents to a temporary file. |
| 94 | + char filename[] = "/tmp/cudaMallocManagedTest.XXXXXX"; |
| 95 | + int fd = mkstemp(filename); |
| 96 | + if (fd < 0) { |
| 97 | + err(1, "mkstemp"); |
| 98 | + } |
| 99 | + size_t done = 0; |
| 100 | + while (done < kNumBytes) { |
| 101 | + ssize_t n = write(fd, reinterpret_cast<char*>(data) + done, |
| 102 | + kNumBytes - done); |
| 103 | + if (n >= 0) { |
| 104 | + done += n; |
| 105 | + } else if (n < 0 && errno != EINTR) { |
| 106 | + err(1, "write"); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // Mutate the array on the device. |
| 111 | + addKernel<<<kNumBlocks, kNumThreads>>>(data); |
| 112 | + CHECK_CUDA(cudaGetLastError()); |
| 113 | + CHECK_CUDA(cudaDeviceSynchronize()); |
| 114 | + |
| 115 | + // Check that the array has the expected result. |
| 116 | + for (size_t i = 0; i < kNumElems; i++) { |
| 117 | + std::uint32_t want = init_val + static_cast<std::uint32_t>(i); |
| 118 | + if (data[i] != want) { |
| 119 | + std::cout << "data[" << i << "]: got " << data[i] << ", wanted " << want |
| 120 | + << " = " << init_val << " + " << i << std::endl; |
| 121 | + abort(); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + // Read the array's original contents back from the temporary file. |
| 126 | + if (lseek(fd, 0, SEEK_SET) < 0) { |
| 127 | + err(1, "lseek"); |
| 128 | + } |
| 129 | + done = 0; |
| 130 | + while (done < kNumBytes) { |
| 131 | + ssize_t n = read(fd, reinterpret_cast<char*>(data) + done, |
| 132 | + kNumBytes - done); |
| 133 | + if (n > 0) { |
| 134 | + done += n; |
| 135 | + } else if (n == 0) { |
| 136 | + errx(1, "read: unexpected EOF after %zu bytes", done); |
| 137 | + } else if (n < 0 && errno != EINTR) { |
| 138 | + err(1, "read"); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + // Check that the array matches what we originally wrote. |
| 143 | + for (size_t i = 0; i < kNumElems; i++) { |
| 144 | + std::uint32_t want = init_val; |
| 145 | + if (data[i] != want) { |
| 146 | + std::cout << "data[" << i << "]: got " << data[i] << ", wanted " << want |
| 147 | + << " = " << init_val << " + " << i << std::endl; |
| 148 | + abort(); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + // Mutate the array on the device again. |
| 153 | + addKernel<<<kNumBlocks, kNumThreads>>>(data); |
| 154 | + CHECK_CUDA(cudaGetLastError()); |
| 155 | + CHECK_CUDA(cudaDeviceSynchronize()); |
| 156 | + |
| 157 | + // Check that the array has the expected result again. |
| 158 | + for (size_t i = 0; i < kNumElems; i++) { |
| 159 | + std::uint32_t want = init_val + static_cast<std::uint32_t>(i); |
| 160 | + if (data[i] != want) { |
| 161 | + std::cout << "data[" << i << "]: got " << data[i] << ", wanted " << want |
| 162 | + << " = " << init_val << " + " << i << std::endl; |
| 163 | + abort(); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + close(fd); |
| 168 | + CHECK_CUDA(cudaFree(data)); |
| 169 | +} |
| 170 | + |
| 171 | +int main() { |
| 172 | + int device; |
| 173 | + CHECK_CUDA(cudaGetDevice(&device)); |
| 174 | + |
| 175 | + std::cout << "Testing cudaMallocManaged(flags=cudaMemAttachGlobal)" |
| 176 | + << std::endl; |
| 177 | + TestMallocManagedRoundTrip(device, cudaMemAttachGlobal, false); |
| 178 | + |
| 179 | + int cma = 0; |
| 180 | + CHECK_CUDA( |
| 181 | + cudaDeviceGetAttribute(&cma, cudaDevAttrConcurrentManagedAccess, device)); |
| 182 | + if (!cma) { |
| 183 | + std::cout << "cudaDevAttrConcurrentManagedAccess not available" |
| 184 | + << std::endl; |
| 185 | + } else { |
| 186 | + std::cout << "Testing cudaMallocManaged(flags=cudaMemAttachGlobal) " |
| 187 | + "with prefetching" |
| 188 | + << std::endl; |
| 189 | + TestMallocManagedRoundTrip(device, cudaMemAttachGlobal, true); |
| 190 | + std::cout << "Testing cudaMallocManaged(flags=cudaMemAttachHost)" |
| 191 | + << std::endl; |
| 192 | + TestMallocManagedRoundTrip(device, cudaMemAttachHost, false); |
| 193 | + std::cout << "Testing cudaMallocManaged(flags=cudaMemAttachHost) " |
| 194 | + "with prefetching" |
| 195 | + << std::endl; |
| 196 | + TestMallocManagedRoundTrip(device, cudaMemAttachHost, true); |
| 197 | + } |
| 198 | + |
| 199 | + std::cout << "Testing read/write syscalls on cudaMallocManaged memory" |
| 200 | + << std::endl; |
| 201 | + TestMallocManagedReadWrite(device); |
| 202 | + |
| 203 | + std::cout << "All tests passed" << std::endl; |
| 204 | + return 0; |
| 205 | +} |
0 commit comments