|
| 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 <stdio.h> |
| 11 | + |
| 12 | +#include "umf/ipc.h" |
| 13 | +#include "umf/memory_pool.h" |
| 14 | +#include "umf/pools/pool_disjoint.h" |
| 15 | +#include "umf/providers/provider_level_zero.h" |
| 16 | + |
| 17 | +#include "utils_level_zero.h" |
| 18 | + |
| 19 | +int create_level_zero_pool(ze_context_handle_t context, |
| 20 | + ze_device_handle_t device, |
| 21 | + umf_memory_pool_handle_t *pool) { |
| 22 | + // setup params |
| 23 | + level_zero_memory_provider_params_t params = {0}; |
| 24 | + params.level_zero_context_handle = context; |
| 25 | + params.level_zero_device_handle = device; |
| 26 | + params.memory_type = UMF_MEMORY_TYPE_DEVICE; |
| 27 | + // create Level Zero provider |
| 28 | + umf_memory_provider_handle_t provider = 0; |
| 29 | + umf_result_t umf_result = umfMemoryProviderCreate( |
| 30 | + umfLevelZeroMemoryProviderOps(), ¶ms, &provider); |
| 31 | + if (umf_result != UMF_RESULT_SUCCESS) { |
| 32 | + fprintf(stderr, "Failed to create Level Zero memory provider!\n"); |
| 33 | + return -1; |
| 34 | + } |
| 35 | + |
| 36 | + // create pool |
| 37 | + umf_pool_create_flags_t flags = UMF_POOL_CREATE_FLAG_OWN_PROVIDER; |
| 38 | + umf_disjoint_pool_params_t disjoint_params = umfDisjointPoolParamsDefault(); |
| 39 | + umf_result = umfPoolCreate(umfDisjointPoolOps(), provider, &disjoint_params, |
| 40 | + flags, pool); |
| 41 | + if (umf_result != UMF_RESULT_SUCCESS) { |
| 42 | + fprintf(stderr, "Failed to create pool!\n"); |
| 43 | + return -1; |
| 44 | + } |
| 45 | + |
| 46 | + return 0; |
| 47 | +} |
| 48 | + |
| 49 | +int main(void) { |
| 50 | + uint32_t driver_idx = 0; |
| 51 | + ze_driver_handle_t driver = NULL; |
| 52 | + ze_device_handle_t device = NULL; |
| 53 | + ze_context_handle_t producer_context = NULL; |
| 54 | + ze_context_handle_t consumer_context = NULL; |
| 55 | + int ret = init_level_zero(); |
| 56 | + if (ret != 0) { |
| 57 | + fprintf(stderr, "Failed to init Level 0!\n"); |
| 58 | + return ret; |
| 59 | + } |
| 60 | + |
| 61 | + ret = find_driver_with_gpu(&driver_idx, &driver); |
| 62 | + if (ret || driver == NULL) { |
| 63 | + fprintf(stderr, "Cannot find L0 driver with GPU device!\n"); |
| 64 | + return ret; |
| 65 | + } |
| 66 | + |
| 67 | + ret = create_context(driver, &producer_context); |
| 68 | + if (ret != 0) { |
| 69 | + fprintf(stderr, "Failed to create L0 context!\n"); |
| 70 | + return ret; |
| 71 | + } |
| 72 | + |
| 73 | + ret = create_context(driver, &consumer_context); |
| 74 | + if (ret != 0) { |
| 75 | + fprintf(stderr, "Failed to create L0 context!\n"); |
| 76 | + return ret; |
| 77 | + } |
| 78 | + |
| 79 | + ret = find_gpu_device(driver, &device); |
| 80 | + if (ret || device == NULL) { |
| 81 | + fprintf(stderr, "Cannot find GPU device!\n"); |
| 82 | + return ret; |
| 83 | + } |
| 84 | + |
| 85 | + // create producer pool |
| 86 | + umf_memory_pool_handle_t producer_pool = 0; |
| 87 | + ret = create_level_zero_pool(producer_context, device, &producer_pool); |
| 88 | + if (ret != 0) { |
| 89 | + fprintf(stderr, "Failed to create producer pool!\n"); |
| 90 | + return ret; |
| 91 | + } |
| 92 | + |
| 93 | + fprintf(stdout, "Producer pool created.\n"); |
| 94 | + |
| 95 | + void *initial_buf = umfPoolMalloc(producer_pool, 1024); |
| 96 | + if (!initial_buf) { |
| 97 | + fprintf(stderr, "Failed to allocate buffer from UMF pool!\n"); |
| 98 | + return -1; |
| 99 | + } |
| 100 | + |
| 101 | + fprintf(stdout, "Buffer allocated from the producer pool.\n"); |
| 102 | + |
| 103 | + umf_ipc_handle_t ipc_handle = NULL; |
| 104 | + size_t handle_size = 0; |
| 105 | + umf_result_t umf_result = |
| 106 | + umfGetIPCHandle(initial_buf, &ipc_handle, &handle_size); |
| 107 | + if (umf_result != UMF_RESULT_SUCCESS) { |
| 108 | + fprintf(stderr, "Failed to get IPC handle!\n"); |
| 109 | + return -1; |
| 110 | + } |
| 111 | + |
| 112 | + fprintf(stdout, "IPC handle obtained.\n"); |
| 113 | + |
| 114 | + // create consumer pool |
| 115 | + umf_memory_pool_handle_t consumer_pool = 0; |
| 116 | + ret = create_level_zero_pool(consumer_context, device, &consumer_pool); |
| 117 | + if (ret != 0) { |
| 118 | + fprintf(stderr, "Failed to create consumer pool!\n"); |
| 119 | + return ret; |
| 120 | + } |
| 121 | + |
| 122 | + fprintf(stdout, "Consumer pool created.\n"); |
| 123 | + |
| 124 | + void *mapped_buf = NULL; |
| 125 | + umf_result = umfOpenIPCHandle(consumer_pool, ipc_handle, &mapped_buf); |
| 126 | + if (umf_result != UMF_RESULT_SUCCESS) { |
| 127 | + fprintf(stderr, "Failed to open IPC handle!\n"); |
| 128 | + return -1; |
| 129 | + } |
| 130 | + |
| 131 | + fprintf(stdout, "IPC handle opened in the consumer pool.\n"); |
| 132 | + |
| 133 | + // Now we have two mappings (belongs to different Level Zero contexts) to the same physical memory region: |
| 134 | + // * the initial mapping, pointed by initial_buf, we get by allocating memory from the producer pool. |
| 135 | + // * the second mapping, pointed by mapped_buf, we get by opening the IPC handle in the consumer pool. |
| 136 | + |
| 137 | + umf_result = umfPutIPCHandle(ipc_handle); |
| 138 | + if (umf_result != UMF_RESULT_SUCCESS) { |
| 139 | + fprintf(stderr, "Failed to put IPC handle!\n"); |
| 140 | + return -1; |
| 141 | + } |
| 142 | + |
| 143 | + fprintf(stdout, "IPC handle released in the producer pool.\n"); |
| 144 | + |
| 145 | + umf_result = umfCloseIPCHandle(mapped_buf); |
| 146 | + if (umf_result != UMF_RESULT_SUCCESS) { |
| 147 | + fprintf(stderr, "Failed to close IPC handle!\n"); |
| 148 | + return -1; |
| 149 | + } |
| 150 | + |
| 151 | + fprintf(stdout, "IPC handle closed in the consumer pool.\n"); |
| 152 | + |
| 153 | + umfFree(initial_buf); |
| 154 | + |
| 155 | + umfPoolDestroy(producer_pool); |
| 156 | + umfPoolDestroy(consumer_pool); |
| 157 | + |
| 158 | + ret = destroy_context(producer_context); |
| 159 | + if (ret != 0) { |
| 160 | + fprintf(stderr, "Failed to destroy L0 context!\n"); |
| 161 | + return ret; |
| 162 | + } |
| 163 | + |
| 164 | + ret = destroy_context(consumer_context); |
| 165 | + if (ret != 0) { |
| 166 | + fprintf(stderr, "Failed to destroy L0 context!\n"); |
| 167 | + return ret; |
| 168 | + } |
| 169 | + return 0; |
| 170 | +} |
0 commit comments