WorkWithADL

Demonstartes how to work with ADLX along ADL when programming with ADLX.

Sample Path

/Samples/CPP/Generic/WorkWithADL

Code

Copied!

//
// Copyright (c) 2021 - 2025 Advanced Micro Devices, Inc. All rights reserved.
//
//-------------------------------------------------------------------------------------------------


#include "SDK/ADLXHelper/Windows/Cpp/ADLXHelper.h"
#include <iostream>

// ADL dll name
#ifndef _WIN64
#define ADL_DLL_Name L"atiadlxy.dll"
#else // WIN32
#define ADL_DLL_Name L"atiadlxx.dll"
#endif

typedef void*(__stdcall* ADL_MALLOC_CALL)(int);
typedef int (*ADL_MAIN_CONTROL_CREATE)(ADL_MALLOC_CALL, int iEnumConnectedAdapter, void** context, int);
typedef int (*ADL_MAIN_CONTROL_DESTROY)(void*);

// Callback ADL for memory allocation
// iSize_ - Number of bytes to be allocated
static void* __stdcall ADL_Client_Main_Memory_Alloc(int iSize_)
{
    void* lpBuffer = malloc(iSize_);
    return lpBuffer;
}

// Callback ADL for memory release
// lpBuffer_ - The memory buffer to dealocate and to make NULL.
static void __stdcall ADL_Main_Memory_Free_Client(void** lpBuffer_)
{
    if (nullptr != lpBuffer_ && nullptr != *lpBuffer_)
    {
        free(*lpBuffer_);
        *lpBuffer_ = nullptr;
    }
}

// Use ADLX namespace
using namespace adlx;

// Wait for exit with error message
int WaitAndExit(const char* msg, const int retCode)
{
    // Printout the message and pause to see it before returning the desired code
    if (nullptr != msg)
        std::cout << msg << std::endl;

    system("Pause");
    return retCode;
}

int main()
{
    // Define return code
    ADLX_RESULT  res = ADLX_FAIL ;

    // Create ADLXHelper instance
    ADLXHelper ADLXHelp;

    // Define ADL context and ADL dll handl
    void* adlContext = nullptr;
    HINSTANCE adlDll = nullptr;

    // Initialize ADL
    adlDll = LoadLibraryW(ADL_DLL_Name);
    if (nullptr == adlDll)
        return WaitAndExit("ADL initialization failed", 0);
    ADL_MAIN_CONTROL_CREATE adlCreate = (ADL_MAIN_CONTROL_CREATE)GetProcAddress(adlDll, "ADL2_Main_ControlX2_Create");
    if (adlCreate)
        adlCreate(ADL_Client_Main_Memory_Alloc, 1, &adlContext, 1);
    std::cout << "ADL initialized successfully" << std::endl;

    // Initialize ADLX
    res = ADLXHelp.InitializeWithCallerAdl(adlContext, ADL_Main_Memory_Free_Client);
    if (ADLX_SUCCEEDED (res))
    {
        std::cout << "ADLX initialized successfully" << std::endl;
    }
    else
    {
        std::cout << "ADLX initialization failed" << std::endl;
    }

    const char* adlxVersion = ADLXHelp.QueryVersion();
    std::cout << "ADLX version: " << adlxVersion << std::endl;
    adlx_uint64 adlxfullVersion = ADLXHelp.QueryFullVersion();
    std::cout << "ADLX full version: " << adlxfullVersion << std::endl;

    // Demonstrates mapping between ADL and ADLX data
    IADLXSystem* systemService = ADLXHelp.GetSystemServices();
    IADLMapping* adlMapping = ADLXHelp.GetAdlMapping();

    if (systemService && adlMapping)
    {
        adlx_uint totalRAM = 0;
        systemService->TotalSystemRAM(&totalRAM);
        std::cout << "Total System RAM: " << totalRAM << " MB" << std::endl;

        // Map ADLX to ADL
        IADLXGPUListPtr gpus;
        systemService->GetGPUs(&gpus);
        IADLXGPUPtr gpu;
        adlx_uint it = 0;
        gpus->At(it, &gpu);
        const char* name = nullptr;
        gpu->Name(&name);
        adlx_int adapterIndex;
        adlMapping->AdlAdapterIndexFromADLXGPU(gpu, &adapterIndex);
        std::cout << "ADL get adapterIndex from ADLXGPU named: " << name << ", adapterIndex is : " << adapterIndex << std::endl;

        // Map ADL to ADLX, to get adapterIndex via ADL
        IADLXGPUPtr gpuInfo;
        adlMapping->GetADLXGPUFromAdlAdapterIndex(adapterIndex, &gpuInfo);
        gpuInfo->Name(&name);
        std::cout << "ADLX get ADLXGPU from adapterIndex: " << adapterIndex << ", the gpu name is : " << name << std::endl;

        adlx_int bus = 0;
        adlx_int device = 0;
        adlx_int function = 0;
        res = adlMapping->BdfFromADLXGPU (gpu, &bus, &device, &function);
        std::cout << "Get BDF from ADLXGPU bus: " << bus << ", device: " << device << ", function: " << function << std::endl;
    }

    // Destroy ADLX
    res = ADLXHelp.Terminate();
    std::cout << "Destroy ADLX result: " << res << std::endl;

    // Destroy ADL
    ADL_MAIN_CONTROL_DESTROY adlDestroy = (ADL_MAIN_CONTROL_DESTROY)GetProcAddress(adlDll, "ADL2_Main_Control_Destroy");
    if (nullptr != adlDestroy)
        adlDestroy(adlContext);
    FreeLibrary(adlDll);
    adlDll = nullptr;
    adlContext = nullptr;
    std::cout << "Destroy ADL" << std::endl;

    // Pause to see the print out
    system("pause");
    return 0;
}

Related pages

  • Visit the Adlx product page for download links and more information.

Looking for more documentation on GPUOpen?

AMD GPUOpen software blogs

Our handy software release blogs will help you make good use of our tools, SDKs, and effects, as well as sharing the latest features with new releases.

GPUOpen Manuals

Don’t miss our manual documentation! And if slide decks are what you’re after, you’ll find 100+ of our finest presentations here.

AMD GPUOpen Performance Guides

The home of great performance and optimization advice for AMD RDNA™ 2 GPUs, AMD Ryzen™ CPUs, and so much more.

Getting started: AMD GPUOpen software

New or fairly new to AMD’s tools, libraries, and effects? This is the best place to get started on GPUOpen!

AMD GPUOpen Getting Started Development and Performance

Looking for tips on getting started with developing and/or optimizing your game, whether on AMD hardware or generally? We’ve got you covered!

AMD GPUOpen Technical blogs

Browse our technical blogs, and find valuable advice on developing with AMD hardware, ray tracing, Vulkan®, DirectX®, Unreal Engine, and lots more.

Find out more about our software!

AMD GPUOpen Effects - AMD FidelityFX technologies

Create wonder. No black boxes. Meet the AMD FidelityFX SDK!

AMD GPUOpen Samples

Browse all our useful samples. Perfect for when you’re needing to get started, want to integrate one of our libraries, and much more.

AMD GPUOpen developer SDKs

Discover what our SDK technologies can offer you. Query hardware or software, manage memory, create rendering applications or machine learning, and much more!

AMD GPUOpen Developer Tools

Analyze, Optimize, Profile, Benchmark. We provide you with the developer tools you need to make sure your game is the best it can be!