|
| 1 | +/* |
| 2 | + * Copyright 2023 NVIDIA Corporation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 with the LLVM exception |
| 5 | + * (the "License"); you may not use this file except in compliance with |
| 6 | + * the License. |
| 7 | + * |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://llvm.org/foundation/relicensing/LICENSE.txt |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <nvbench/config.cuh> |
| 20 | +#include <nvbench/cuda_stream.cuh> |
| 21 | +#include <nvbench/device_manager.cuh> |
| 22 | +#include <nvbench/types.cuh> |
| 23 | + |
| 24 | +#include <fmt/format.h> |
| 25 | + |
| 26 | +#include "test_asserts.cuh" |
| 27 | + |
| 28 | +namespace |
| 29 | +{ |
| 30 | +#ifdef NVBENCH_HAS_CUPTI |
| 31 | +/** |
| 32 | + * @brief Queries and returns the device id that the given \p cuda_stream is associated with |
| 33 | + * |
| 34 | + * @param cuda_stream The stream to get the device id for |
| 35 | + * @return The device id that \p cuda_stream is associated with |
| 36 | + */ |
| 37 | +int get_device_of_stream(cudaStream_t cuda_stream) |
| 38 | +{ |
| 39 | + CUcontext ctx; |
| 40 | + NVBENCH_DRIVER_API_CALL(cuStreamGetCtx(CUstream{cuda_stream}, &ctx)); |
| 41 | + NVBENCH_DRIVER_API_CALL(cuCtxPushCurrent(ctx)); |
| 42 | + CUdevice device_id{}; |
| 43 | + NVBENCH_DRIVER_API_CALL(cuCtxGetDevice(&device_id)); |
| 44 | + NVBENCH_DRIVER_API_CALL(cuCtxPopCurrent(&ctx)); |
| 45 | + return static_cast<int>(device_id); |
| 46 | +} |
| 47 | +#endif |
| 48 | +} // namespace |
| 49 | + |
| 50 | +void test_basic() |
| 51 | +{ |
| 52 | +#ifdef NVBENCH_HAS_CUPTI |
| 53 | + // Get devices |
| 54 | + auto devices = nvbench::device_manager::get().get_devices(); |
| 55 | + |
| 56 | + // Iterate over devices |
| 57 | + for (auto const &device_info : devices) |
| 58 | + { |
| 59 | + // Create stream on the device before it becomes the active device |
| 60 | + nvbench::cuda_stream device_stream(device_info); |
| 61 | + |
| 62 | + // Verify cuda stream is associated with the correct cuda device |
| 63 | + ASSERT(get_device_of_stream(device_stream.get_stream()) == device_info.get_id()); |
| 64 | + |
| 65 | + // Set the device as active device |
| 66 | + device_info.set_active(); |
| 67 | + |
| 68 | + // Create the stream (implicitly) on the device that is currently active |
| 69 | + nvbench::cuda_stream current_device_stream{}; |
| 70 | + |
| 71 | + // Verify the cuda stream was in fact associated with the currently active device |
| 72 | + ASSERT(get_device_of_stream(current_device_stream.get_stream()) == device_info.get_id()); |
| 73 | + } |
| 74 | +#endif |
| 75 | +} |
| 76 | + |
| 77 | +int main() { test_basic(); } |
0 commit comments