Skip to content

Commit 1587105

Browse files
committed
Extend ipc_level_zero.c example to verify the content of original and mmaped buffers
1 parent 6228e5c commit 1587105

File tree

1 file changed

+46
-20
lines changed

1 file changed

+46
-20
lines changed

examples/basic/ipc_level_zero.c

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ int create_level_zero_pool(ze_context_handle_t context,
2929
umf_result_t umf_result = umfMemoryProviderCreate(
3030
umfLevelZeroMemoryProviderOps(), &params, &provider);
3131
if (umf_result != UMF_RESULT_SUCCESS) {
32-
fprintf(stderr, "Failed to create Level Zero memory provider!\n");
32+
fprintf(stderr,
33+
"ERROR: Failed to create Level Zero memory provider!\n");
3334
return -1;
3435
}
3536

@@ -39,7 +40,7 @@ int create_level_zero_pool(ze_context_handle_t context,
3940
umf_result = umfPoolCreate(umfDisjointPoolOps(), provider, &disjoint_params,
4041
flags, pool);
4142
if (umf_result != UMF_RESULT_SUCCESS) {
42-
fprintf(stderr, "Failed to create pool!\n");
43+
fprintf(stderr, "ERROR: Failed to create pool!\n");
4344
return -1;
4445
}
4546

@@ -52,60 +53,71 @@ int main(void) {
5253
ze_device_handle_t device = NULL;
5354
ze_context_handle_t producer_context = NULL;
5455
ze_context_handle_t consumer_context = NULL;
56+
const size_t BUFFER_SIZE = 1024;
57+
const size_t BUFFER_PATTERN = 0x42;
5558
int ret = init_level_zero();
5659
if (ret != 0) {
57-
fprintf(stderr, "Failed to init Level 0!\n");
60+
fprintf(stderr, "ERROR: Failed to init Level 0!\n");
5861
return ret;
5962
}
6063

6164
ret = find_driver_with_gpu(&driver_idx, &driver);
6265
if (ret || driver == NULL) {
63-
fprintf(stderr, "Cannot find L0 driver with GPU device!\n");
66+
fprintf(stderr, "ERROR: Cannot find L0 driver with GPU device!\n");
6467
return ret;
6568
}
6669

6770
ret = create_context(driver, &producer_context);
6871
if (ret != 0) {
69-
fprintf(stderr, "Failed to create L0 context!\n");
72+
fprintf(stderr, "ERROR: Failed to create L0 context!\n");
7073
return ret;
7174
}
7275

7376
ret = create_context(driver, &consumer_context);
7477
if (ret != 0) {
75-
fprintf(stderr, "Failed to create L0 context!\n");
78+
fprintf(stderr, "ERROR: Failed to create L0 context!\n");
7679
return ret;
7780
}
7881

7982
ret = find_gpu_device(driver, &device);
8083
if (ret || device == NULL) {
81-
fprintf(stderr, "Cannot find GPU device!\n");
84+
fprintf(stderr, "ERROR: Cannot find GPU device!\n");
8285
return ret;
8386
}
8487

8588
// create producer pool
8689
umf_memory_pool_handle_t producer_pool = 0;
8790
ret = create_level_zero_pool(producer_context, device, &producer_pool);
8891
if (ret != 0) {
89-
fprintf(stderr, "Failed to create producer pool!\n");
92+
fprintf(stderr, "ERROR: Failed to create producer pool!\n");
9093
return ret;
9194
}
9295

9396
fprintf(stdout, "Producer pool created.\n");
9497

95-
void *initial_buf = umfPoolMalloc(producer_pool, 1024);
98+
void *initial_buf = umfPoolMalloc(producer_pool, BUFFER_SIZE);
9699
if (!initial_buf) {
97-
fprintf(stderr, "Failed to allocate buffer from UMF pool!\n");
100+
fprintf(stderr, "ERROR: Failed to allocate buffer from UMF pool!\n");
98101
return -1;
99102
}
100103

101104
fprintf(stdout, "Buffer allocated from the producer pool.\n");
102105

106+
ret = level_zero_fill(producer_context, device, initial_buf, BUFFER_SIZE,
107+
&BUFFER_PATTERN, sizeof(BUFFER_PATTERN));
108+
if (ret != 0) {
109+
fprintf(stderr, "ERROR: Failed to fill the buffer with pattern!\n");
110+
return ret;
111+
}
112+
113+
fprintf(stdout, "Buffer filled with pattern.\n");
114+
103115
umf_ipc_handle_t ipc_handle = NULL;
104116
size_t handle_size = 0;
105117
umf_result_t umf_result =
106118
umfGetIPCHandle(initial_buf, &ipc_handle, &handle_size);
107119
if (umf_result != UMF_RESULT_SUCCESS) {
108-
fprintf(stderr, "Failed to get IPC handle!\n");
120+
fprintf(stderr, "ERROR: Failed to get IPC handle!\n");
109121
return -1;
110122
}
111123

@@ -115,7 +127,7 @@ int main(void) {
115127
umf_memory_pool_handle_t consumer_pool = 0;
116128
ret = create_level_zero_pool(consumer_context, device, &consumer_pool);
117129
if (ret != 0) {
118-
fprintf(stderr, "Failed to create consumer pool!\n");
130+
fprintf(stderr, "ERROR: Failed to create consumer pool!\n");
119131
return ret;
120132
}
121133

@@ -124,27 +136,41 @@ int main(void) {
124136
void *mapped_buf = NULL;
125137
umf_result = umfOpenIPCHandle(consumer_pool, ipc_handle, &mapped_buf);
126138
if (umf_result != UMF_RESULT_SUCCESS) {
127-
fprintf(stderr, "Failed to open IPC handle!\n");
139+
fprintf(stderr, "ERROR: Failed to open IPC handle!\n");
128140
return -1;
129141
}
130142

131143
fprintf(stdout, "IPC handle opened in the consumer pool.\n");
132144

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.
145+
size_t *tmp_buf = malloc(BUFFER_SIZE);
146+
ret = level_zero_copy(consumer_context, device, tmp_buf, mapped_buf,
147+
BUFFER_SIZE);
148+
if (ret != 0) {
149+
fprintf(stderr, "ERROR: Failed to copy mapped_buf to host!\n");
150+
return ret;
151+
}
152+
153+
// Verify the content of the buffer
154+
for (size_t i = 0; i < BUFFER_SIZE / sizeof(BUFFER_PATTERN); ++i) {
155+
if (tmp_buf[i] != BUFFER_PATTERN) {
156+
fprintf(stderr, "ERROR: mapped_buf does not match initial_buf!\n");
157+
return -1;
158+
}
159+
}
160+
161+
fprintf(stdout, "mapped_buf matches initial_buf.\n");
136162

137163
umf_result = umfPutIPCHandle(ipc_handle);
138164
if (umf_result != UMF_RESULT_SUCCESS) {
139-
fprintf(stderr, "Failed to put IPC handle!\n");
165+
fprintf(stderr, "ERROR: Failed to put IPC handle!\n");
140166
return -1;
141167
}
142168

143169
fprintf(stdout, "IPC handle released in the producer pool.\n");
144170

145171
umf_result = umfCloseIPCHandle(mapped_buf);
146172
if (umf_result != UMF_RESULT_SUCCESS) {
147-
fprintf(stderr, "Failed to close IPC handle!\n");
173+
fprintf(stderr, "ERROR: Failed to close IPC handle!\n");
148174
return -1;
149175
}
150176

@@ -157,13 +183,13 @@ int main(void) {
157183

158184
ret = destroy_context(producer_context);
159185
if (ret != 0) {
160-
fprintf(stderr, "Failed to destroy L0 context!\n");
186+
fprintf(stderr, "ERROR: Failed to destroy L0 context!\n");
161187
return ret;
162188
}
163189

164190
ret = destroy_context(consumer_context);
165191
if (ret != 0) {
166-
fprintf(stderr, "Failed to destroy L0 context!\n");
192+
fprintf(stderr, "ERROR: Failed to destroy L0 context!\n");
167193
return ret;
168194
}
169195
return 0;

0 commit comments

Comments
 (0)