<commit_before>#include "pch.h"
#include "NvidiaGPU.h"
#include "nvidia_interface.h"

namespace lib_gpu {

template<typename T>
std::unique_ptr<T> loadNvidiaStruct(NV_PHYSICAL_GPU_HANDLE const& handle, NV_STATUS(*loader)(NV_PHYSICAL_GPU_HANDLE, T*), void(*preparer)(T*) = nullptr)
{
    auto struct_ = std::make_unique<T>();
    UINT32 version = struct_->version;
    memset(struct_.get(), 0, sizeof(T));
    struct_->version = version;
    if (preparer) {
        preparer(struct_.get());
    }
    return (*loader)(handle, struct_.get()) == NVAPI_OK ? std::move(struct_) : nullptr;
}

std::unique_ptr<NVIDIA_CLOCK_FREQUENCIES> loadCLOCK_FREQUENCIES(NV_PHYSICAL_GPU_HANDLE const& handle)
{
    return loadNvidiaStruct<NVIDIA_CLOCK_FREQUENCIES>(handle, NVIDIA_RAW_GetAllClockFrequencies, [](NVIDIA_CLOCK_FREQUENCIES* f) {f->clock_type = 0; });
}

std::unique_ptr<NVIDIA_GPU_THERMAL_SETTINGS_V2> loadGPU_THERMAL_SETTINGS_V2(NV_PHYSICAL_GPU_HANDLE const& handle)
{
    auto loader_wrapper = [](NV_PHYSICAL_GPU_HANDLE handle, NVIDIA_GPU_THERMAL_SETTINGS_V2* settings) {
        return NVIDIA_RAW_GpuGetThermalSettings(handle, NVIDIA_THERMAL_TARGET_ALL, settings);
    };

    return loadNvidiaStruct<NVIDIA_GPU_THERMAL_SETTINGS_V2>(handle, loader_wrapper);
}

#define SIMPLE_NVIDIA_CALL(T_, function_) std::unique_ptr<NVIDIA_##T_> load ## T_(NV_PHYSICAL_GPU_HANDLE const& handle) { return loadNvidiaStruct<NVIDIA_##T_>(handle, function_); }

SIMPLE_NVIDIA_CALL(DYNAMIC_PSTATES, NVIDIA_RAW_GetDynamicPStates);
SIMPLE_NVIDIA_CALL(GPU_PSTATES20_V2, NVIDIA_RAW_GetPstates20);
SIMPLE_NVIDIA_CALL(GPU_POWER_POLICIES_INFO, NVIDIA_RAW_GpuClientPowerPoliciesGetInfo);
SIMPLE_NVIDIA_CALL(GPU_POWER_POLICIES_STATUS, NVIDIA_RAW_GpuClientPowerPoliciesGetStatus);
SIMPLE_NVIDIA_CALL(GPU_VOLTAGE_DOMAINS_STATUS, NVIDIA_RAW_GpuGetVoltageDomainsStatus);

bool NvidiaGPU::poll()
{
    auto frequencies = loadCLOCK_FREQUENCIES(this->handle);
    auto dynamicPstates = loadDYNAMIC_PSTATES(this->handle);
    auto pstates20 = loadGPU_PSTATES20_V2(this->handle);
    //auto perfTable = std::make_shared<NVIDIA_GPU_PERF_TABLE>();
    auto powerPoliciesInfo = loadGPU_POWER_POLICIES_INFO(this->handle);
    auto powerPoliciesStatus = loadGPU_POWER_POLICIES_STATUS(this->handle);
    auto voltageDomainsStatus = loadGPU_VOLTAGE_DOMAINS_STATUS(this->handle);
    auto thermalSettings = loadGPU_THERMAL_SETTINGS_V2(this->handle);

    if (frequencies && dynamicPstates && pstates20 && powerPoliciesInfo &&
        powerPoliciesStatus && voltageDomainsStatus && thermalSettings) {
        this->frequencies = std::move(frequencies);
        this->dynamicPstates = std::move(dynamicPstates);
        this->pstates20 = std::move(pstates20);
        this->powerPoliciesInfo = std::move(powerPoliciesInfo);
        this->powerPoliciesStatus = std::move(powerPoliciesStatus);
        this->voltageDomainsStatus = std::move(voltageDomainsStatus);
        this->thermalSettings = std::move(thermalSettings);
        return true;
    } else {
        return false;
    }
}

std::string NvidiaGPU::getName()
{<commit_msg>Implement ability to fetch specific frequency<commit_after>#include "pch.h"
#include "NvidiaGPU.h"
#include "nvidia_interface.h"

namespace lib_gpu {

template<typename T, typename F>
std::unique_ptr<T> loadNvidiaStruct(NV_PHYSICAL_GPU_HANDLE const& handle, NV_STATUS(*loader)(NV_PHYSICAL_GPU_HANDLE, T*), F preparer)
{
    auto struct_ = std::make_unique<T>();
    UINT32 version = struct_->version;
    memset(struct_.get(), 0, sizeof(T));
    struct_->version = version;
    preparer(struct_.get());
    return (*loader)(handle, struct_.get()) == NVAPI_OK ? std::move(struct_) : nullptr;
}

template<typename T>
std::unique_ptr<T> loadNvidiaStruct(NV_PHYSICAL_GPU_HANDLE const& handle, NV_STATUS(*loader)(NV_PHYSICAL_GPU_HANDLE, T*))
{
    return loadNvidiaStruct<T>(handle, loader, [](void*) {});
}

std::unique_ptr<NVIDIA_CLOCK_FREQUENCIES> loadCLOCK_FREQUENCIES(NV_PHYSICAL_GPU_HANDLE const& handle, NVIDIA_CLOCK_FREQUENCY_TYPE type)
{
    return loadNvidiaStruct<NVIDIA_CLOCK_FREQUENCIES>(handle, NVIDIA_RAW_GetAllClockFrequencies, [&](NVIDIA_CLOCK_FREQUENCIES* f) {f->clock_type = type; });
}

std::unique_ptr<NVIDIA_GPU_THERMAL_SETTINGS_V2> loadGPU_THERMAL_SETTINGS_V2(NV_PHYSICAL_GPU_HANDLE const& handle)
{
    auto loader_wrapper = [](NV_PHYSICAL_GPU_HANDLE handle, NVIDIA_GPU_THERMAL_SETTINGS_V2* settings) {
        return NVIDIA_RAW_GpuGetThermalSettings(handle, NVIDIA_THERMAL_TARGET_ALL, settings);
    };

    return loadNvidiaStruct<NVIDIA_GPU_THERMAL_SETTINGS_V2>(handle, loader_wrapper);
}

#define SIMPLE_NVIDIA_CALL(T_, function_) std::unique_ptr<NVIDIA_##T_> load ## T_(NV_PHYSICAL_GPU_HANDLE const& handle) { return loadNvidiaStruct<NVIDIA_##T_>(handle, function_); }

SIMPLE_NVIDIA_CALL(DYNAMIC_PSTATES, NVIDIA_RAW_GetDynamicPStates);
SIMPLE_NVIDIA_CALL(GPU_PSTATES20_V2, NVIDIA_RAW_GetPstates20);
SIMPLE_NVIDIA_CALL(GPU_POWER_POLICIES_INFO, NVIDIA_RAW_GpuClientPowerPoliciesGetInfo);
SIMPLE_NVIDIA_CALL(GPU_POWER_POLICIES_STATUS, NVIDIA_RAW_GpuClientPowerPoliciesGetStatus);
SIMPLE_NVIDIA_CALL(GPU_VOLTAGE_DOMAINS_STATUS, NVIDIA_RAW_GpuGetVoltageDomainsStatus);

bool NvidiaGPU::poll()
{
    auto frequencies = loadCLOCK_FREQUENCIES(this->handle, NVIDIA_CLOCK_FREQUENCY_TYPE_CURRENT);

    auto dynamicPstates = loadDYNAMIC_PSTATES(this->handle);
    auto pstates20 = loadGPU_PSTATES20_V2(this->handle);
    //auto perfTable = std::make_shared<NVIDIA_GPU_PERF_TABLE>();
    auto powerPoliciesInfo = loadGPU_POWER_POLICIES_INFO(this->handle);
    auto powerPoliciesStatus = loadGPU_POWER_POLICIES_STATUS(this->handle);
    auto voltageDomainsStatus = loadGPU_VOLTAGE_DOMAINS_STATUS(this->handle);
    auto thermalSettings = loadGPU_THERMAL_SETTINGS_V2(this->handle);

    if (frequencies && dynamicPstates && pstates20 && powerPoliciesInfo &&
        powerPoliciesStatus && voltageDomainsStatus && thermalSettings) {
        this->frequencies = std::move(frequencies);
        this->dynamicPstates = std::move(dynamicPstates);
        this->pstates20 = std::move(pstates20);
        this->powerPoliciesInfo = std::move(powerPoliciesInfo);
        this->powerPoliciesStatus = std::move(powerPoliciesStatus);
        this->voltageDomainsStatus = std::move(voltageDomainsStatus);
        this->thermalSettings = std::move(thermalSettings);
        return true;
    } 

    return false;
}

std::string NvidiaGPU::getName()
{
{'fraction_non_alphanumeric': 0.10952084629744867, 'fraction_numerical': 0.007622899813316739, 'mean_word_length': 6.7457831325301205, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4375772', 'n_tokens_mistral': 2367, 'n_tokens_neox': 2304, 'n_words': 303}