@@ -29,7 +29,8 @@ int create_level_zero_pool(ze_context_handle_t context,
29
29
umf_result_t umf_result = umfMemoryProviderCreate (
30
30
umfLevelZeroMemoryProviderOps (), & params , & provider );
31
31
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" );
33
34
return -1 ;
34
35
}
35
36
@@ -39,7 +40,7 @@ int create_level_zero_pool(ze_context_handle_t context,
39
40
umf_result = umfPoolCreate (umfDisjointPoolOps (), provider , & disjoint_params ,
40
41
flags , pool );
41
42
if (umf_result != UMF_RESULT_SUCCESS ) {
42
- fprintf (stderr , "Failed to create pool!\n" );
43
+ fprintf (stderr , "ERROR: Failed to create pool!\n" );
43
44
return -1 ;
44
45
}
45
46
@@ -52,60 +53,71 @@ int main(void) {
52
53
ze_device_handle_t device = NULL ;
53
54
ze_context_handle_t producer_context = NULL ;
54
55
ze_context_handle_t consumer_context = NULL ;
56
+ const size_t BUFFER_SIZE = 1024 ;
57
+ const size_t BUFFER_PATTERN = 0x42 ;
55
58
int ret = init_level_zero ();
56
59
if (ret != 0 ) {
57
- fprintf (stderr , "Failed to init Level 0!\n" );
60
+ fprintf (stderr , "ERROR: Failed to init Level 0!\n" );
58
61
return ret ;
59
62
}
60
63
61
64
ret = find_driver_with_gpu (& driver_idx , & driver );
62
65
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" );
64
67
return ret ;
65
68
}
66
69
67
70
ret = create_context (driver , & producer_context );
68
71
if (ret != 0 ) {
69
- fprintf (stderr , "Failed to create L0 context!\n" );
72
+ fprintf (stderr , "ERROR: Failed to create L0 context!\n" );
70
73
return ret ;
71
74
}
72
75
73
76
ret = create_context (driver , & consumer_context );
74
77
if (ret != 0 ) {
75
- fprintf (stderr , "Failed to create L0 context!\n" );
78
+ fprintf (stderr , "ERROR: Failed to create L0 context!\n" );
76
79
return ret ;
77
80
}
78
81
79
82
ret = find_gpu_device (driver , & device );
80
83
if (ret || device == NULL ) {
81
- fprintf (stderr , "Cannot find GPU device!\n" );
84
+ fprintf (stderr , "ERROR: Cannot find GPU device!\n" );
82
85
return ret ;
83
86
}
84
87
85
88
// create producer pool
86
89
umf_memory_pool_handle_t producer_pool = 0 ;
87
90
ret = create_level_zero_pool (producer_context , device , & producer_pool );
88
91
if (ret != 0 ) {
89
- fprintf (stderr , "Failed to create producer pool!\n" );
92
+ fprintf (stderr , "ERROR: Failed to create producer pool!\n" );
90
93
return ret ;
91
94
}
92
95
93
96
fprintf (stdout , "Producer pool created.\n" );
94
97
95
- void * initial_buf = umfPoolMalloc (producer_pool , 1024 );
98
+ void * initial_buf = umfPoolMalloc (producer_pool , BUFFER_SIZE );
96
99
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" );
98
101
return -1 ;
99
102
}
100
103
101
104
fprintf (stdout , "Buffer allocated from the producer pool.\n" );
102
105
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
+
103
115
umf_ipc_handle_t ipc_handle = NULL ;
104
116
size_t handle_size = 0 ;
105
117
umf_result_t umf_result =
106
118
umfGetIPCHandle (initial_buf , & ipc_handle , & handle_size );
107
119
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" );
109
121
return -1 ;
110
122
}
111
123
@@ -115,7 +127,7 @@ int main(void) {
115
127
umf_memory_pool_handle_t consumer_pool = 0 ;
116
128
ret = create_level_zero_pool (consumer_context , device , & consumer_pool );
117
129
if (ret != 0 ) {
118
- fprintf (stderr , "Failed to create consumer pool!\n" );
130
+ fprintf (stderr , "ERROR: Failed to create consumer pool!\n" );
119
131
return ret ;
120
132
}
121
133
@@ -124,27 +136,41 @@ int main(void) {
124
136
void * mapped_buf = NULL ;
125
137
umf_result = umfOpenIPCHandle (consumer_pool , ipc_handle , & mapped_buf );
126
138
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" );
128
140
return -1 ;
129
141
}
130
142
131
143
fprintf (stdout , "IPC handle opened in the consumer pool.\n" );
132
144
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" );
136
162
137
163
umf_result = umfPutIPCHandle (ipc_handle );
138
164
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" );
140
166
return -1 ;
141
167
}
142
168
143
169
fprintf (stdout , "IPC handle released in the producer pool.\n" );
144
170
145
171
umf_result = umfCloseIPCHandle (mapped_buf );
146
172
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" );
148
174
return -1 ;
149
175
}
150
176
@@ -157,13 +183,13 @@ int main(void) {
157
183
158
184
ret = destroy_context (producer_context );
159
185
if (ret != 0 ) {
160
- fprintf (stderr , "Failed to destroy L0 context!\n" );
186
+ fprintf (stderr , "ERROR: Failed to destroy L0 context!\n" );
161
187
return ret ;
162
188
}
163
189
164
190
ret = destroy_context (consumer_context );
165
191
if (ret != 0 ) {
166
- fprintf (stderr , "Failed to destroy L0 context!\n" );
192
+ fprintf (stderr , "ERROR: Failed to destroy L0 context!\n" );
167
193
return ret ;
168
194
}
169
195
return 0 ;
0 commit comments