Program Listing for File gpu-id.h

Return to documentation for file (include\metrics\gpu-id.h)

/*
    © Intel Corporation.

    This software and the related documents are Intel copyrighted materials,
    and your use of them is governed by the express license under which they
    were provided to you ("License"). Unless the License provides otherwise,
    you may not use, modify, copy, publish, distribute, disclose or transmit
    this software or the related documents without Intel's prior written
    permission.

    This software and the related documents are provided as is, with no express
    or implied warranties, other than those that are expressly stated in the
    License.
*/

// clang-format off

#pragma once

#include <cstdint>
#include <cstring>

namespace gpa
{
namespace metrics
{

struct GpuId
{
    enum class Type
    {
        Undefined = 0,
        Luid,
        MajorMinor
    };

    static GpuId BuildByLuid(uint32_t low_part, int32_t high_part)
    {
        GpuId gpu_id{};

        gpu_id.type_id = Type::Luid;
        gpu_id.luid.low_part = low_part;
        gpu_id.luid.high_part = high_part;

        return gpu_id;
    }

    static GpuId BuildByMajorMinor(int32_t major, int32_t minor)
    {
        GpuId gpu_id{};

        gpu_id.type_id = Type::MajorMinor;
        gpu_id.major_minor.major = major;
        gpu_id.major_minor.minor = minor;

        return gpu_id;
    }

    struct Luid
    {
        uint32_t low_part;
        int32_t  high_part;
    };

    struct MajorMinor
    {
        int32_t major;
        int32_t minor;
    };

    Type type_id = Type::Undefined;

    union
    {
        Luid       luid;
        MajorMinor major_minor;
        uint8_t    bytes[8] = {};
    };

    bool operator ==(const GpuId& rhs) const noexcept;
    bool operator !=(const GpuId& rhs) const noexcept;
    bool operator <(const GpuId& rhs) const noexcept;

    operator bool() const noexcept;
};

inline bool GpuId::operator ==(const GpuId& rhs) const noexcept
{
    if (type_id != rhs.type_id)
        return false;

    return type_id == Type::Undefined || std::memcmp(bytes, rhs.bytes, sizeof(GpuId::bytes)) == 0;
}

inline bool GpuId::operator !=(const GpuId& rhs) const noexcept
{
    return !operator==(rhs);
}

inline bool GpuId::operator <(const GpuId& rhs) const noexcept
{
    if (type_id != rhs.type_id)
        return type_id < rhs.type_id;

    return type_id != Type::Undefined && std::memcmp(bytes, rhs.bytes, sizeof(GpuId::bytes)) < 0;
}

inline GpuId::operator bool() const noexcept
{
    return type_id != Type::Undefined && luid.low_part;
}

} // namespace metrics
} // namespace gpa

// clang-format on