|
| 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 "utils_level_zero.h" |
| 11 | + |
| 12 | +#include <umf/memory_pool.h> |
| 13 | +#include <umf/pools/pool_disjoint.h> |
| 14 | +#include <umf/providers/provider_level_zero.h> |
| 15 | + |
| 16 | +int main(void) { |
| 17 | + // A result object for storing UMF API result status |
| 18 | + umf_result_t res; |
| 19 | + |
| 20 | + uint32_t driverId = 0; |
| 21 | + ze_driver_handle_t hDriver = NULL; |
| 22 | + ze_device_handle_t hDevice = NULL; |
| 23 | + ze_context_handle_t hContext = NULL; |
| 24 | + |
| 25 | + // Initialize Level Zero |
| 26 | + int ret = init_level_zero(); |
| 27 | + if (ret != 0) { |
| 28 | + fprintf(stderr, "Failed to init Level 0!\n"); |
| 29 | + return ret; |
| 30 | + } |
| 31 | + |
| 32 | + ret = find_driver_with_gpu(&driverId, &hDriver); |
| 33 | + if (ret || hDriver == NULL) { |
| 34 | + fprintf(stderr, "Cannot find L0 driver with GPU device!\n"); |
| 35 | + return ret; |
| 36 | + } |
| 37 | + |
| 38 | + ret = find_gpu_device(hDriver, &hDevice); |
| 39 | + if (ret || hDevice == NULL) { |
| 40 | + fprintf(stderr, "Cannot find GPU device!\n"); |
| 41 | + return ret; |
| 42 | + } |
| 43 | + |
| 44 | + ret = create_context(hDriver, &hContext); |
| 45 | + if (ret != 0) { |
| 46 | + fprintf(stderr, "Failed to create L0 context!\n"); |
| 47 | + return ret; |
| 48 | + } |
| 49 | + |
| 50 | + // Setup parameters for the Level Zero memory provider. It will be used for |
| 51 | + // allocating memory from Level Zero devices. |
| 52 | + level_zero_memory_provider_params_t ze_memory_provider_params; |
| 53 | + ze_memory_provider_params.level_zero_context_handle = hContext; |
| 54 | + ze_memory_provider_params.level_zero_device_handle = hDevice; |
| 55 | + // Set the memory type to shared to allow the memory to be accessed on both |
| 56 | + // CPU and GPU. |
| 57 | + ze_memory_provider_params.memory_type = UMF_MEMORY_TYPE_SHARED; |
| 58 | + |
| 59 | + // Create Level Zero memory provider |
| 60 | + umf_memory_provider_handle_t ze_memory_provider; |
| 61 | + res = umfMemoryProviderCreate(umfLevelZeroMemoryProviderOps(), |
| 62 | + &ze_memory_provider_params, |
| 63 | + &ze_memory_provider); |
| 64 | + if (res != UMF_RESULT_SUCCESS) { |
| 65 | + fprintf(stderr, "Failed to create a memory provider!\n"); |
| 66 | + goto level_zero_destroy; |
| 67 | + } |
| 68 | + |
| 69 | + printf("Level Zero memory provider created at %p\n", |
| 70 | + (void *)ze_memory_provider); |
| 71 | + |
| 72 | + // Setup parameters for the Disjoint Pool. It will be used for managing the |
| 73 | + // memory allocated using memory provider. |
| 74 | + umf_disjoint_pool_params_t disjoint_memory_pool_params = |
| 75 | + umfDisjointPoolParamsDefault(); |
| 76 | + // Set the Slab Min Size to 64KB - the page size for GPU allocations |
| 77 | + disjoint_memory_pool_params.SlabMinSize = 64 * 1024L; |
| 78 | + // We would keep only single slab per each allocation bucket |
| 79 | + disjoint_memory_pool_params.Capacity = 1; |
| 80 | + // Set the maximum poolable size to 64KB - objects with size above this |
| 81 | + // limit will not be stored/allocated from the pool. |
| 82 | + disjoint_memory_pool_params.MaxPoolableSize = 64 * 1024L; |
| 83 | + // Enable tracing |
| 84 | + disjoint_memory_pool_params.PoolTrace = 1; |
| 85 | + |
| 86 | + // Create Disjoint Pool memory pool. |
| 87 | + umf_memory_pool_handle_t ze_disjoint_memory_pool; |
| 88 | + res = umfPoolCreate(umfDisjointPoolOps(), ze_memory_provider, |
| 89 | + &disjoint_memory_pool_params, UMF_POOL_CREATE_FLAG_NONE, |
| 90 | + &ze_disjoint_memory_pool); |
| 91 | + if (res != UMF_RESULT_SUCCESS) { |
| 92 | + fprintf(stderr, "Failed to create a memory pool!\n"); |
| 93 | + goto memory_provider_destroy; |
| 94 | + } |
| 95 | + |
| 96 | + printf("Disjoint Pool created at %p\n", (void *)ze_disjoint_memory_pool); |
| 97 | + |
| 98 | + // Allocate some memory from the pool |
| 99 | + int *ptr = (int *)umfPoolMalloc(ze_disjoint_memory_pool, sizeof(int)); |
| 100 | + if (res != UMF_RESULT_SUCCESS) { |
| 101 | + fprintf(stderr, "Failed to allocate memory from the memory pool!\n"); |
| 102 | + goto memory_pool_destroy; |
| 103 | + } |
| 104 | + |
| 105 | + // Use allocated memory |
| 106 | + *ptr = 1; |
| 107 | + |
| 108 | + // Free allocated memory |
| 109 | + res = umfFree(ptr); |
| 110 | + if (res != UMF_RESULT_SUCCESS) { |
| 111 | + fprintf(stderr, "Failed to free memory to the pool!\n"); |
| 112 | + goto memory_pool_destroy; |
| 113 | + } |
| 114 | + printf("Freed memory at %p\n", (void *)ptr); |
| 115 | + |
| 116 | + // Cleanup |
| 117 | + umfPoolDestroy(ze_disjoint_memory_pool); |
| 118 | + umfMemoryProviderDestroy(ze_memory_provider); |
| 119 | + zeContextDestroy(hContext); |
| 120 | + return 0; |
| 121 | + |
| 122 | +memory_pool_destroy: |
| 123 | + umfPoolDestroy(ze_disjoint_memory_pool); |
| 124 | + |
| 125 | +memory_provider_destroy: |
| 126 | + umfMemoryProviderDestroy(ze_memory_provider); |
| 127 | + |
| 128 | +level_zero_destroy: |
| 129 | + zeContextDestroy(hContext); |
| 130 | + return -1; |
| 131 | +} |
0 commit comments