|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Benchmarks for atomic operations under high thread contention. |
| 17 | +
|
| 18 | +All threads write to a single output location (index 0) to maximize contention |
| 19 | +and measure worst-case atomic operation performance. |
| 20 | +""" |
| 21 | + |
| 22 | +from typing import Any |
| 23 | + |
| 24 | +import numpy as np |
| 25 | + |
| 26 | +import warp as wp |
| 27 | + |
| 28 | +# Map string parameter names to warp dtypes |
| 29 | +DTYPE_MAP = { |
| 30 | + "float32": wp.float32, |
| 31 | + "int32": wp.int32, |
| 32 | +} |
| 33 | + |
| 34 | +NUM_ELEMENTS = 32 * 1024 * 1024 |
| 35 | + |
| 36 | + |
| 37 | +@wp.kernel |
| 38 | +def max_kernel( |
| 39 | + vals: wp.array(dtype=Any), |
| 40 | + out: wp.array(dtype=Any), |
| 41 | +): |
| 42 | + tid = wp.tid() |
| 43 | + val = vals[tid] |
| 44 | + wp.atomic_max(out, 0, val) # All threads contend on out[0] |
| 45 | + |
| 46 | + |
| 47 | +@wp.kernel |
| 48 | +def min_kernel( |
| 49 | + vals: wp.array(dtype=Any), |
| 50 | + out: wp.array(dtype=Any), |
| 51 | +): |
| 52 | + tid = wp.tid() |
| 53 | + val = vals[tid] |
| 54 | + wp.atomic_min(out, 0, val) # All threads contend on out[0] |
| 55 | + |
| 56 | + |
| 57 | +class AtomicMax: |
| 58 | + """Benchmark wp.atomic_max() with high thread contention. |
| 59 | +
|
| 60 | + Uses 4x larger arrays (128M elements) to reduce measurement variation, |
| 61 | + as atomic_max showed ~10% variation with the default 32M elements. |
| 62 | + """ |
| 63 | + |
| 64 | + params = ["float32", "int32"] |
| 65 | + param_names = ["dtype"] |
| 66 | + |
| 67 | + repeat = 50 |
| 68 | + number = 15 |
| 69 | + |
| 70 | + # Use 4x more elements to reduce measurement variation |
| 71 | + num_elements = 4 * NUM_ELEMENTS |
| 72 | + |
| 73 | + def setup_cache(self): |
| 74 | + rng = np.random.default_rng(42) |
| 75 | + # Generate vals_np for each dtype in DTYPE_MAP |
| 76 | + vals_np_dict = {} |
| 77 | + for dtype_str_key, dtype in DTYPE_MAP.items(): |
| 78 | + if dtype == wp.float32: |
| 79 | + vals_np = rng.random(self.num_elements).astype(np.float32) |
| 80 | + elif dtype == wp.int32: |
| 81 | + vals_np = rng.integers(0, 2**31 - 1, size=self.num_elements, dtype=np.int32) |
| 82 | + else: |
| 83 | + vals_np = None |
| 84 | + vals_np_dict[dtype_str_key] = vals_np |
| 85 | + |
| 86 | + return vals_np_dict |
| 87 | + |
| 88 | + def setup(self, vals_np_dict, dtype_str): |
| 89 | + wp.init() |
| 90 | + self.device = wp.get_device("cuda:0") |
| 91 | + |
| 92 | + dtype = DTYPE_MAP[dtype_str] |
| 93 | + |
| 94 | + self.vals = wp.array(vals_np_dict[dtype_str], dtype=dtype, device=self.device) |
| 95 | + self.out = wp.zeros(shape=(1,), dtype=dtype, device=self.device) |
| 96 | + |
| 97 | + self.cmd = wp.launch( |
| 98 | + max_kernel, |
| 99 | + (self.num_elements,), |
| 100 | + inputs=[self.vals], |
| 101 | + outputs=[self.out], |
| 102 | + device=self.device, |
| 103 | + record_cmd=True, |
| 104 | + ) |
| 105 | + |
| 106 | + # Launch once to compile |
| 107 | + self.cmd.launch() |
| 108 | + wp.synchronize_device(self.device) |
| 109 | + |
| 110 | + def time_cuda(self, vals_np_dict, dtype_str): |
| 111 | + self.out.zero_() |
| 112 | + self.cmd.launch() |
| 113 | + wp.synchronize_device(self.device) |
| 114 | + |
| 115 | + |
| 116 | +class AtomicMin: |
| 117 | + """Benchmark wp.atomic_min() with high thread contention. |
| 118 | +
|
| 119 | + Uses standard array size (32M elements) as measurements are already stable. |
| 120 | + """ |
| 121 | + |
| 122 | + params = ["float32", "int32"] |
| 123 | + param_names = ["dtype"] |
| 124 | + |
| 125 | + repeat = 100 |
| 126 | + number = 25 |
| 127 | + |
| 128 | + def setup_cache(self): |
| 129 | + rng = np.random.default_rng(42) |
| 130 | + # Generate vals_np for each dtype in DTYPE_MAP |
| 131 | + vals_np_dict = {} |
| 132 | + for dtype_str_key, dtype in DTYPE_MAP.items(): |
| 133 | + if dtype == wp.float32: |
| 134 | + vals_np = rng.random(NUM_ELEMENTS).astype(np.float32) |
| 135 | + elif dtype == wp.int32: |
| 136 | + vals_np = rng.integers(0, 2**31 - 1, size=NUM_ELEMENTS, dtype=np.int32) |
| 137 | + else: |
| 138 | + vals_np = None |
| 139 | + vals_np_dict[dtype_str_key] = vals_np |
| 140 | + |
| 141 | + return vals_np_dict |
| 142 | + |
| 143 | + def setup(self, vals_np_dict, dtype_str): |
| 144 | + wp.init() |
| 145 | + self.device = wp.get_device("cuda:0") |
| 146 | + |
| 147 | + dtype = DTYPE_MAP[dtype_str] |
| 148 | + |
| 149 | + self.vals = wp.array(vals_np_dict[dtype_str], dtype=dtype, device=self.device) |
| 150 | + self.out = wp.zeros(shape=(1,), dtype=dtype, device=self.device) |
| 151 | + |
| 152 | + self.cmd = wp.launch( |
| 153 | + min_kernel, |
| 154 | + (NUM_ELEMENTS,), |
| 155 | + inputs=[self.vals], |
| 156 | + outputs=[self.out], |
| 157 | + device=self.device, |
| 158 | + record_cmd=True, |
| 159 | + ) |
| 160 | + |
| 161 | + # Launch once to compile |
| 162 | + self.cmd.launch() |
| 163 | + wp.synchronize_device(self.device) |
| 164 | + |
| 165 | + def time_cuda(self, vals_np_dict, dtype_str): |
| 166 | + self.out.zero_() |
| 167 | + self.cmd.launch() |
| 168 | + wp.synchronize_device(self.device) |
0 commit comments