Skip to content

Commit

Permalink
Sort Adapters by performance
Browse files Browse the repository at this point in the history
  • Loading branch information
saddam213 committed Jun 12, 2024
1 parent e4f9bca commit de788da
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 50 deletions.
52 changes: 5 additions & 47 deletions OnnxStack.Adapter/AdapterInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,63 +38,20 @@ class DxgiModule
}
};

extern "C" __declspec(dllexport) int __cdecl GetAdapter(bool preferHighPerformance)
{
DxgiModule dxgi;

ComPtr<IDXGIFactory6> factory = dxgi.CreateFactory();;
if (!factory)
{
return 0;
}

ComPtr<IDXGIAdapter1> adapter;

// Store LUIDs for hardware adapters in original unsorted order.
std::vector<std::pair<int, LUID>> adaptersUnsortedIndexAndLuid;
for (int i = 0; factory->EnumAdapters1(i, adapter.ReleaseAndGetAddressOf()) != DXGI_ERROR_NOT_FOUND; i++)
{
DXGI_ADAPTER_DESC desc = {};
RETURN_IF_FAILED(adapter->GetDesc(&desc));
adaptersUnsortedIndexAndLuid.emplace_back(i, desc.AdapterLuid);
}

// Find the first adapter meeting GPU preference.
DXGI_ADAPTER_DESC preferredAdapterDesc = {};
{
DXGI_GPU_PREFERENCE gpuPreference = preferHighPerformance ? DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE : DXGI_GPU_PREFERENCE_MINIMUM_POWER;
RETURN_IF_FAILED(factory->EnumAdapterByGpuPreference(0, gpuPreference, IID_PPV_ARGS(adapter.ReleaseAndGetAddressOf())));
RETURN_IF_FAILED(adapter->GetDesc(&preferredAdapterDesc));
}

// Look up the index of the preferred adapter in the unsorted list order.
for (auto& hardwareAdapterEntry : adaptersUnsortedIndexAndLuid)
{
if (hardwareAdapterEntry.second.HighPart == preferredAdapterDesc.AdapterLuid.HighPart &&
hardwareAdapterEntry.second.LowPart == preferredAdapterDesc.AdapterLuid.LowPart)
{
return hardwareAdapterEntry.first;
}
}

return 0;
}


extern "C" __declspec(dllexport) void __cdecl GetAdapters(AdapterInfo * adapterArray)
{
DxgiModule dxgi;

ComPtr<IDXGIFactory6> factory = dxgi.CreateFactory();;
ComPtr<IDXGIFactory6> factory = dxgi.CreateFactory();
if (!factory)
return;

int adapterCount = 0;
ComPtr<IDXGIAdapter1> adapter;
for (int i = 0; factory->EnumAdapters1(i, adapter.ReleaseAndGetAddressOf()) != DXGI_ERROR_NOT_FOUND; i++)
for (int i = 0; factory->EnumAdapterByGpuPreference(i, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IID_PPV_ARGS(adapter.ReleaseAndGetAddressOf())) != DXGI_ERROR_NOT_FOUND; i++)
{
DXGI_ADAPTER_DESC desc = {};
HRESULT hr = adapter->GetDesc(&desc);
DXGI_ADAPTER_DESC1 desc = {};
HRESULT hr = adapter->GetDesc1(&desc);
if (SUCCEEDED(hr))
{
AdapterInfo info{};
Expand All @@ -107,6 +64,7 @@ extern "C" __declspec(dllexport) void __cdecl GetAdapters(AdapterInfo * adapterA
info.SharedSystemMemory = desc.SharedSystemMemory;
info.SubSysId = desc.SubSysId;
info.VendorId = desc.VendorId;
info.Flags = desc.Flags;
wcsncpy_s(info.Description, desc.Description, _TRUNCATE);
adapterArray[adapterCount] = info;
++adapterCount;
Expand Down
4 changes: 1 addition & 3 deletions OnnxStack.Adapter/AdapterInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ struct AdapterInfo
SIZE_T DedicatedSystemMemory;
SIZE_T SharedSystemMemory;
LUID AdapterLuid;
UINT Flags;
WCHAR Description[128];
};


extern "C" __declspec(dllexport) int __cdecl GetAdapter(bool preferHighPerformance);

extern "C" __declspec(dllexport) void __cdecl GetAdapters(AdapterInfo * adapterArray);

0 comments on commit de788da

Please sign in to comment.