|  | 
|  | 1 | +/* | 
|  | 2 | + * | 
|  | 3 | + * Copyright (C) 2024 Intel Corporation | 
|  | 4 | + * | 
|  | 5 | + * Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. | 
|  | 6 | + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | 
|  | 7 | + * | 
|  | 8 | + */ | 
|  | 9 | + | 
|  | 10 | +#include <assert.h> | 
|  | 11 | +#include <stdio.h> | 
|  | 12 | +#include <stdlib.h> | 
|  | 13 | +#include <ze_api.h> | 
|  | 14 | + | 
|  | 15 | +#include "umf/memory_pool.h" | 
|  | 16 | +#include "umf/pools/pool_disjoint.h" | 
|  | 17 | +#include "umf/providers/provider_level_zero.h" | 
|  | 18 | + | 
|  | 19 | +// This function initializes the Level Zero driver and creates a context in the | 
|  | 20 | +// first GPU device found. | 
|  | 21 | +int level_zero_init(ze_driver_handle_t *driver, ze_device_handle_t *device, | 
|  | 22 | +                    ze_context_handle_t *context) { | 
|  | 23 | + | 
|  | 24 | +    // Initialize the Level Zero driver | 
|  | 25 | +    ze_result_t ze_result = zeInit(0); | 
|  | 26 | +    assert(ze_result == ZE_RESULT_SUCCESS); | 
|  | 27 | + | 
|  | 28 | +    // Discover all the driver instances | 
|  | 29 | +    uint32_t driverCount = 0; | 
|  | 30 | +    ze_result = zeDriverGet(&driverCount, NULL); | 
|  | 31 | +    assert(ze_result == ZE_RESULT_SUCCESS); | 
|  | 32 | +    assert(driverCount > 0); | 
|  | 33 | + | 
|  | 34 | +    ze_driver_handle_t *all_drivers = | 
|  | 35 | +        (ze_driver_handle_t *)calloc(driverCount, sizeof(ze_driver_handle_t)); | 
|  | 36 | +    ze_result = zeDriverGet(&driverCount, all_drivers); | 
|  | 37 | +    assert(ze_result == ZE_RESULT_SUCCESS); | 
|  | 38 | + | 
|  | 39 | +    // Find a driver instance with a GPU device | 
|  | 40 | +    for (uint32_t i = 0; i < driverCount; ++i) { | 
|  | 41 | +        assert(all_drivers[i] != NULL); | 
|  | 42 | + | 
|  | 43 | +        // Discover all the devices | 
|  | 44 | +        uint32_t deviceCount = 0; | 
|  | 45 | +        ze_result = zeDeviceGet(all_drivers[i], &deviceCount, NULL); | 
|  | 46 | +        assert(ze_result == ZE_RESULT_SUCCESS); | 
|  | 47 | +        assert(deviceCount > 0); | 
|  | 48 | + | 
|  | 49 | +        ze_device_handle_t *all_devices = (ze_device_handle_t *)calloc( | 
|  | 50 | +            deviceCount, sizeof(ze_device_handle_t)); | 
|  | 51 | +        ze_result = zeDeviceGet(all_drivers[i], &deviceCount, all_devices); | 
|  | 52 | +        assert(ze_result == ZE_RESULT_SUCCESS); | 
|  | 53 | + | 
|  | 54 | +        // Loop through the devices to find a GPU device | 
|  | 55 | +        for (uint32_t d = 0; d < deviceCount; ++d) { | 
|  | 56 | +            assert(all_devices[d] != NULL); | 
|  | 57 | + | 
|  | 58 | +            ze_device_properties_t device_properties = {0}; | 
|  | 59 | +            device_properties.stype = ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES; | 
|  | 60 | +            ze_result = | 
|  | 61 | +                zeDeviceGetProperties(all_devices[d], &device_properties); | 
|  | 62 | +            assert(ze_result == ZE_RESULT_SUCCESS); | 
|  | 63 | + | 
|  | 64 | +            if (ZE_DEVICE_TYPE_GPU == device_properties.type) { | 
|  | 65 | +                *driver = all_drivers[i]; | 
|  | 66 | +                *device = all_devices[d]; | 
|  | 67 | +                printf("GPU device found: %p\n", device); | 
|  | 68 | +                break; | 
|  | 69 | +            } | 
|  | 70 | +        } | 
|  | 71 | + | 
|  | 72 | +        free(all_devices); | 
|  | 73 | +        if (NULL != driver) { | 
|  | 74 | +            break; | 
|  | 75 | +        } | 
|  | 76 | +    } | 
|  | 77 | + | 
|  | 78 | +    // In case no suitable device was found, print an error message and exit | 
|  | 79 | +    if (NULL == device) { | 
|  | 80 | +        printf("Failed to find GPU device!"); | 
|  | 81 | +        return -1; | 
|  | 82 | +    } | 
|  | 83 | + | 
|  | 84 | +    // Create context | 
|  | 85 | +    ze_context_desc_t ctxtDesc = {ZE_STRUCTURE_TYPE_CONTEXT_DESC, NULL, 0}; | 
|  | 86 | +    ze_result = zeContextCreate(*driver, &ctxtDesc, context); | 
|  | 87 | +    assert(ze_result == ZE_RESULT_SUCCESS); | 
|  | 88 | +    assert(*context != NULL); | 
|  | 89 | + | 
|  | 90 | +    // Free the memory | 
|  | 91 | +    free(all_drivers); | 
|  | 92 | +    return 0; | 
|  | 93 | +} | 
|  | 94 | + | 
|  | 95 | +int main(void) { | 
|  | 96 | + | 
|  | 97 | +    // A result object for storing UMF API result status | 
|  | 98 | +    umf_result_t res; | 
|  | 99 | + | 
|  | 100 | +    // Initialize Level Zero | 
|  | 101 | +    ze_driver_handle_t hDriver; | 
|  | 102 | +    ze_device_handle_t hDevice; | 
|  | 103 | +    ze_context_handle_t hContext; | 
|  | 104 | +    int ret = level_zero_init(&hDriver, &hDevice, &hContext); | 
|  | 105 | +    if (ret < 0) { | 
|  | 106 | +        // Initialization failed or no GPU devices was found | 
|  | 107 | +        return -1; | 
|  | 108 | +    } | 
|  | 109 | + | 
|  | 110 | +    // Setup parameters for the Level Zero memory provider. It will be used for | 
|  | 111 | +    // allocating memory from Level Zero devices. | 
|  | 112 | +    level_zero_memory_provider_params_t ze_memory_provider_params; | 
|  | 113 | +    ze_memory_provider_params.level_zero_context_handle = hContext; | 
|  | 114 | +    ze_memory_provider_params.level_zero_device_handle = hDevice; | 
|  | 115 | +    // Set the memory type to shared to allow the memory to be accessed on both | 
|  | 116 | +    // CPU and GPU. | 
|  | 117 | +    ze_memory_provider_params.memory_type = UMF_MEMORY_TYPE_SHARED; | 
|  | 118 | + | 
|  | 119 | +    // Create Level Zero memory provider | 
|  | 120 | +    umf_memory_provider_handle_t ze_memory_provider; | 
|  | 121 | +    res = umfMemoryProviderCreate(umfLevelZeroMemoryProviderOps(), | 
|  | 122 | +                                  &ze_memory_provider_params, | 
|  | 123 | +                                  &ze_memory_provider); | 
|  | 124 | +    if (res != UMF_RESULT_SUCCESS) { | 
|  | 125 | +        printf("Failed to create a memory provider!"); | 
|  | 126 | +        goto level_zero_destroy; | 
|  | 127 | +    } | 
|  | 128 | + | 
|  | 129 | +    printf("Level Zero memory provider created at %p\n", | 
|  | 130 | +           (void *)ze_memory_provider); | 
|  | 131 | + | 
|  | 132 | +    // Setup parametes for the Disjoint Pool. It will be used for managing the | 
|  | 133 | +    // memory allocated using memory provider. | 
|  | 134 | +    umf_disjoint_pool_params_t disjoint_memory_pool_params = | 
|  | 135 | +        umfDisjointPoolParamsDefault(); | 
|  | 136 | +    // Set the Slab Min Size to 64KB - the page size for GPU allocations | 
|  | 137 | +    disjoint_memory_pool_params.SlabMinSize = 64 * 1024L; | 
|  | 138 | +    // We would keep only single slab per each allocation bucket | 
|  | 139 | +    disjoint_memory_pool_params.Capacity = 1; | 
|  | 140 | +    // Set the maximum poolable size to 64KB - objects with size above this | 
|  | 141 | +    // limit will not be stored/allocated from the pool. | 
|  | 142 | +    disjoint_memory_pool_params.MaxPoolableSize = 64 * 1024L; | 
|  | 143 | +    // Enable tracing | 
|  | 144 | +    disjoint_memory_pool_params.PoolTrace = 1; | 
|  | 145 | + | 
|  | 146 | +    // Create Disjoint Pool memory pool. | 
|  | 147 | +    umf_memory_pool_handle_t ze_disjoint_memory_pool; | 
|  | 148 | +    res = umfPoolCreate(umfDisjointPoolOps(), ze_memory_provider, | 
|  | 149 | +                        &disjoint_memory_pool_params, UMF_POOL_CREATE_FLAG_NONE, | 
|  | 150 | +                        &ze_disjoint_memory_pool); | 
|  | 151 | +    if (res != UMF_RESULT_SUCCESS) { | 
|  | 152 | +        printf("Failed to create a memory pool!"); | 
|  | 153 | +        goto memory_provider_destroy; | 
|  | 154 | +    } | 
|  | 155 | + | 
|  | 156 | +    printf("Disjoint Pool created at %p\n", (void *)ze_disjoint_memory_pool); | 
|  | 157 | + | 
|  | 158 | +    // Allocate some memory from the pool | 
|  | 159 | +    void *ptr = umfPoolMalloc(ze_disjoint_memory_pool, sizeof(int)); | 
|  | 160 | +    if (res != UMF_RESULT_SUCCESS) { | 
|  | 161 | +        printf("Failed to allocate memory from the memory pool!"); | 
|  | 162 | +        goto memory_pool_destroy; | 
|  | 163 | +    } | 
|  | 164 | + | 
|  | 165 | +    // Use allocated memory | 
|  | 166 | +    *(int *)ptr = 1; | 
|  | 167 | + | 
|  | 168 | +    // Free allocated memory | 
|  | 169 | +    res = umfFree(ptr); | 
|  | 170 | +    if (res != UMF_RESULT_SUCCESS) { | 
|  | 171 | +        printf("Failed to free memory to the pool!"); | 
|  | 172 | +        goto memory_pool_destroy; | 
|  | 173 | +    } | 
|  | 174 | +    printf("Freed memory at %p\n", ptr); | 
|  | 175 | + | 
|  | 176 | +    // Cleanup | 
|  | 177 | +    umfPoolDestroy(ze_disjoint_memory_pool); | 
|  | 178 | +    umfMemoryProviderDestroy(ze_memory_provider); | 
|  | 179 | +    zeContextDestroy(hContext); | 
|  | 180 | +    return 0; | 
|  | 181 | + | 
|  | 182 | +memory_pool_destroy: | 
|  | 183 | +    umfPoolDestroy(ze_disjoint_memory_pool); | 
|  | 184 | + | 
|  | 185 | +memory_provider_destroy: | 
|  | 186 | +    umfMemoryProviderDestroy(ze_memory_provider); | 
|  | 187 | + | 
|  | 188 | +level_zero_destroy: | 
|  | 189 | +    zeContextDestroy(hContext); | 
|  | 190 | +    return -1; | 
|  | 191 | +} | 
0 commit comments