1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2018-2022 Clemens Lutz
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::data_point::DataPoint;
use super::{Benchmark, ItemBytes, MemoryOperation, TileSize};
use crate::types::{Block, Cycles, Grid};
use itertools::{iproduct, izip};
use numa_gpu::runtime::memory::Mem;
use numa_gpu::runtime::nvml::{DeviceClocks, ThrottleReasons};
use nvml_wrapper::device::Device as NvmlDevice;
use nvml_wrapper::NVML;
use std::iter;

#[allow(dead_code)]
pub(super) struct GpuMeasurementParameters {
    pub(super) grid_size: Grid,
    pub(super) block_size: Block,
}

#[allow(dead_code)]
pub(super) struct GpuMeasurement {
    device_id: u32,
    grid_sizes: Vec<Grid>,
    block_sizes: Vec<Block>,
    template: DataPoint,
    nvml: nvml_wrapper::NVML,
}

impl GpuMeasurement {
    pub(super) fn new(
        device_id: u32,
        grid_sizes: Vec<Grid>,
        block_sizes: Vec<Block>,
        template: DataPoint,
    ) -> Self {
        let nvml = NVML::init().expect("Couldn't initialize NVML");

        Self {
            device_id,
            grid_sizes,
            block_sizes,
            template,
            nvml,
        }
    }

    pub(super) fn measure<R, S>(
        &self,
        mem: &Mem<u32>,
        mut state: S,
        run: R,
        benches: Vec<Benchmark>,
        ops: Vec<MemoryOperation>,
        item_bytes: Vec<ItemBytes>,
        tile_sizes: Vec<TileSize>,
        warp_aligned: bool,
        repeat: u32,
    ) -> Vec<DataPoint>
    where
        R: Fn(
            Benchmark,
            MemoryOperation,
            ItemBytes,
            TileSize,
            &mut S,
            &Mem<u32>,
            &GpuMeasurementParameters,
            &NvmlDevice,
        ) -> Option<(u32, Option<ThrottleReasons>, u64, Cycles, u64)>,
    {
        let mut nvml_device = self
            .nvml
            .device_by_index(self.device_id as u32)
            .expect("Couldn't get NVML device");

        // Set a stable GPU clock rate to make the measurements more accurate
        if let Err(e) = nvml_device.set_max_gpu_clocks() {
            eprintln!("Warning: Failed to set the maximum GPU clockrate [{}]", e);
        }

        let data_points: Vec<_> = iproduct!(
            iproduct!(
                benches.iter(),
                ops.iter(),
                item_bytes.iter(),
                tile_sizes.iter(),
                self.grid_sizes.iter(),
                self.block_sizes.iter()
            ),
            izip!(iter::once(true).chain(iter::repeat(false)), 0..repeat)
        )
        .filter_map(
            |((&bench, &op, &item_bytes, &tile_size, &grid_size, &block_size), (warm_up, _run))| {
                let mp = GpuMeasurementParameters {
                    grid_size,
                    block_size,
                };

                if let Some((clock_rate_mhz, throttle_reasons, memory_accesses, cycles, ns)) = run(
                    bench,
                    op,
                    item_bytes,
                    tile_size,
                    &mut state,
                    mem,
                    &mp,
                    &nvml_device,
                ) {
                    Some(DataPoint {
                        benchmark: Some(bench),
                        memory_operation: Some(op),
                        item_bytes: Some(item_bytes),
                        tile_size: Some(tile_size),
                        warp_aligned: Some(warp_aligned || tile_size == TileSize::Threads1),
                        warm_up,
                        grid_size: Some(grid_size),
                        block_size: Some(block_size),
                        throttle_reasons: throttle_reasons.map(|r| r.to_string()),
                        clock_rate_mhz: Some(clock_rate_mhz),
                        memory_accesses,
                        cycles,
                        ns,
                        ..self.template.clone()
                    })
                } else {
                    None
                }
            },
        )
        .collect();
        data_points
    }
}

impl Drop for GpuMeasurement {
    fn drop(&mut self) {
        self.nvml
            .device_by_index(self.device_id as u32)
            .unwrap()
            .set_default_gpu_clocks()
            .expect("Failed to reset default GPU clock rates");
    }
}