• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (c) 2017 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //    http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #include <stdio.h>
17 #include <stdlib.h>
18 
19 #include "procs.h"
20 #include "harness/errorHelpers.h"
21 
22 #define MAX_SUB_DEVICES        16        // Limit the sub-devices to ensure no out of resource errors.
23 #define MEM_OBJ_SIZE          1024
24 #define IMAGE_DIM         16
25 
26 // Kernel source code
27 static const char *image_migrate_kernel_code =
28 "__kernel void test_image_migrate(write_only image2d_t dst, read_only image2d_t src1,\n"
29 "                                 read_only image2d_t src2, sampler_t sampler, uint x)\n"
30 "{\n"
31 "  int tidX = get_global_id(0), tidY = get_global_id(1);\n"
32 "  int2 coords = (int2) {tidX, tidY};\n"
33 "  uint4 val = read_imageui(src1, sampler, coords) ^\n"
34 "              read_imageui(src2, sampler, coords) ^\n"
35 "              x;\n"
36 "  write_imageui(dst, coords, val);\n"
37 "}\n";
38 
39 enum migrations { MIGRATE_PREFERRED,           // migrate to the preferred sub-device
40                   MIGRATE_NON_PREFERRED,     // migrate to a randomly chosen non-preferred sub-device
41                   MIGRATE_RANDOM,              // migrate to a randomly chosen sub-device with randomly chosen flags
42                   NUMBER_OF_MIGRATIONS };
43 
init_image(cl_command_queue cmd_q,cl_mem image,cl_uint * data)44 static cl_mem init_image(cl_command_queue cmd_q, cl_mem image, cl_uint *data)
45 {
46   cl_int err;
47 
48   size_t origin[3] = {0, 0, 0};
49   size_t region[3] = {IMAGE_DIM, IMAGE_DIM, 1};
50 
51   if (image) {
52     if ((err = clEnqueueWriteImage(cmd_q, image, CL_TRUE,
53                                    origin, region, 0, 0, data, 0, NULL, NULL)) != CL_SUCCESS) {
54       print_error(err, "Failed on enqueue write of image data.");
55     }
56   }
57 
58   return image;
59 }
60 
migrateMemObject(enum migrations migrate,cl_command_queue * queues,cl_mem * mem_objects,cl_uint num_devices,cl_mem_migration_flags * flags,MTdata d)61 static cl_int migrateMemObject(enum migrations migrate, cl_command_queue *queues, cl_mem *mem_objects,
62                                cl_uint num_devices, cl_mem_migration_flags *flags, MTdata d)
63 {
64   cl_uint i, j;
65   cl_int  err = CL_SUCCESS;
66 
67   for (i=0; i<num_devices; i++) {
68     j = genrand_int32(d) % num_devices;
69     flags[i] = 0;
70     switch (migrate) {
71       case MIGRATE_PREFERRED:
72         // Force the device to be preferred
73         j = i;
74         break;
75       case MIGRATE_NON_PREFERRED:
76         // Coerce the device to be non-preferred
77         if ((j == i) && (num_devices > 1)) j = (j+1) % num_devices;
78         break;
79       case MIGRATE_RANDOM:
80         // Choose a random set of flags
81         flags[i] = (cl_mem_migration_flags)(genrand_int32(d) & (CL_MIGRATE_MEM_OBJECT_HOST | CL_MIGRATE_MEM_OBJECT_CONTENT_UNDEFINED));
82         break;
83       default: log_error("Unhandled migration type: %d\n", migrate); return -1;
84     }
85     if ((err = clEnqueueMigrateMemObjects(queues[j], 1, (const cl_mem *)(&mem_objects[i]),
86                                           flags[i], 0, NULL, NULL)) != CL_SUCCESS) {
87       print_error(err, "Failed migrating memory object.");
88     }
89   }
90   return err;
91 }
92 
restoreImage(cl_command_queue * queues,cl_mem * mem_objects,cl_uint num_devices,cl_mem_migration_flags * flags,cl_uint * buffer)93 static cl_int restoreImage(cl_command_queue *queues, cl_mem *mem_objects, cl_uint num_devices,
94                            cl_mem_migration_flags *flags, cl_uint *buffer)
95 {
96   cl_uint i;
97   cl_int  err;
98 
99   const size_t origin[3] = {0, 0, 0};
100   const size_t region[3] = {IMAGE_DIM, IMAGE_DIM, 1};
101 
102   // If the image was previously migrated with undefined content, reload the content.
103 
104   for (i=0; i<num_devices; i++) {
105     if (flags[i] & CL_MIGRATE_MEM_OBJECT_CONTENT_UNDEFINED) {
106       if ((err = clEnqueueWriteImage(queues[i], mem_objects[i], CL_TRUE,
107                                      origin, region, 0, 0, buffer, 0, NULL, NULL)) != CL_SUCCESS) {
108         print_error(err, "Failed on restoration enqueue write of image data.");
109         return err;
110       }
111     }
112   }
113   return CL_SUCCESS;
114 }
115 
test_image_migrate(cl_device_id deviceID,cl_context context,cl_command_queue queue,int num_elements)116 int test_image_migrate(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements)
117 {
118   int failed = 0;
119   cl_uint i, j;
120   cl_int err;
121   cl_uint max_sub_devices = 0;
122   cl_uint num_devices, num_devices_limited;
123   cl_uint A[MEM_OBJ_SIZE], B[MEM_OBJ_SIZE], C[MEM_OBJ_SIZE];
124   cl_uint test_number = 1;
125   cl_device_affinity_domain domain, domains;
126   cl_device_id *devices;
127   cl_command_queue *queues;
128   cl_mem_migration_flags *flagsA, *flagsB, *flagsC;
129   cl_device_partition_property property[] = {CL_DEVICE_PARTITION_BY_AFFINITY_DOMAIN, 0, 0};
130   cl_mem *imageA, *imageB, *imageC;
131   cl_mem_flags flags;
132   cl_image_format format;
133   cl_sampler sampler = NULL;
134   cl_program program = NULL;
135   cl_kernel kernel = NULL;
136   cl_context ctx = NULL;
137   enum migrations migrateA, migrateB, migrateC;
138   MTdata d = init_genrand(gRandomSeed);
139   const size_t wgs[2] = {IMAGE_DIM, IMAGE_DIM};
140   const size_t wls[2] = {1, 1};
141 
142   // Check for image support.
143   if(checkForImageSupport(deviceID) == CL_IMAGE_FORMAT_NOT_SUPPORTED) {
144     log_info("Device does not support images. Skipping test.\n");
145     return 0;
146   }
147 
148   // Allocate arrays whose size varies according to the maximum number of sub-devices.
149   if ((err = clGetDeviceInfo(deviceID, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(max_sub_devices), &max_sub_devices, NULL)) != CL_SUCCESS) {
150     print_error(err, "clGetDeviceInfo(CL_DEVICE_MAX_COMPUTE_UNITS) failed");
151     return -1;
152   }
153   if (max_sub_devices < 1) {
154     log_error("ERROR: Invalid number of compute units returned.\n");
155     return -1;
156   }
157 
158   devices = (cl_device_id *)malloc(max_sub_devices * sizeof(cl_device_id));
159   queues = (cl_command_queue *)malloc(max_sub_devices * sizeof(cl_command_queue));
160   flagsA = (cl_mem_migration_flags *)malloc(max_sub_devices * sizeof(cl_mem_migration_flags));
161   flagsB = (cl_mem_migration_flags *)malloc(max_sub_devices * sizeof(cl_mem_migration_flags));
162   flagsC = (cl_mem_migration_flags *)malloc(max_sub_devices * sizeof(cl_mem_migration_flags));
163   imageA = (cl_mem *)malloc(max_sub_devices * sizeof(cl_mem));
164   imageB = (cl_mem *)malloc(max_sub_devices * sizeof(cl_mem));
165   imageC = (cl_mem *)malloc(max_sub_devices * sizeof(cl_mem));
166 
167   if ((devices == NULL) || (queues  == NULL) ||
168       (flagsA  == NULL) || (flagsB  == NULL) || (flagsC == NULL) ||
169       (imageA  == NULL) || (imageB == NULL)  || (imageC == NULL)) {
170     log_error("ERROR: Failed to successfully allocate required local buffers.\n");
171     failed = -1;
172     goto cleanup_allocations;
173   }
174 
175   for (i=0; i<max_sub_devices; i++) {
176     devices[i] = NULL;
177     queues [i] = NULL;
178     imageA[i] = imageB[i] = imageC[i] = NULL;
179   }
180 
181   for (i=0; i<MEM_OBJ_SIZE; i++) {
182     A[i] = genrand_int32(d);
183     B[i] = genrand_int32(d);
184   }
185 
186   // Set image format.
187   format.image_channel_order = CL_RGBA;
188   format.image_channel_data_type = CL_UNSIGNED_INT32;
189 
190 
191   // Attempt to partition the device along each of the allowed affinity domain.
192   if ((err = clGetDeviceInfo(deviceID, CL_DEVICE_PARTITION_AFFINITY_DOMAIN, sizeof(domains), &domains, NULL)) != CL_SUCCESS) {
193     print_error(err, "clGetDeviceInfo(CL_PARTITION_AFFINITY_DOMAIN) failed");
194     return -1;
195   }
196 
197   domains &= (CL_DEVICE_AFFINITY_DOMAIN_L4_CACHE | CL_DEVICE_AFFINITY_DOMAIN_L3_CACHE |
198               CL_DEVICE_AFFINITY_DOMAIN_L2_CACHE | CL_DEVICE_AFFINITY_DOMAIN_L1_CACHE | CL_DEVICE_AFFINITY_DOMAIN_NUMA);
199 
200   do {
201     if (domains) {
202       for (domain = 1; (domain & domains) == 0; domain <<= 1) {};
203       domains &= ~domain;
204     } else {
205       domain = 0;
206     }
207 
208     // Determine the number of partitions for the device given the specific domain.
209     if (domain) {
210       property[1] = domain;
211       err = clCreateSubDevices(deviceID, (const cl_device_partition_property *)property, -1, NULL, &num_devices);
212       if ((err != CL_SUCCESS) || (num_devices == 0)) {
213         print_error(err, "Obtaining the number of partions by affinity failed.");
214         failed = 1;
215         goto cleanup;
216       }
217     } else {
218       num_devices = 1;
219     }
220 
221     if (num_devices > 1) {
222       // Create each of the sub-devices and a corresponding context.
223       if ((err = clCreateSubDevices(deviceID, (const cl_device_partition_property *)property, num_devices, devices, &num_devices)) != CL_SUCCESS) {
224         print_error(err, "Failed creating sub devices.");
225         failed = 1;
226         goto cleanup;
227       }
228 
229       // Create a context containing all the sub-devices
230       ctx = clCreateContext(NULL, num_devices, devices, notify_callback, NULL, &err);
231       if (ctx == NULL) {
232     print_error(err, "Failed creating context containing the sub-devices.");
233     failed = 1;
234     goto cleanup;
235       }
236 
237       // Create a command queue for each sub-device
238       for (i=0; i<num_devices; i++) {
239         if (devices[i]) {
240           if ((queues[i] = clCreateCommandQueue(ctx, devices[i], 0, &err)) == NULL) {
241             print_error(err, "Failed creating command queues.");
242             failed = 1;
243             goto cleanup;
244           }
245         }
246       }
247     } else {
248       // No partitioning available. Just exercise the APIs on a single device.
249       devices[0] = deviceID;
250       queues[0] = queue;
251       ctx = context;
252     }
253 
254     // Build the kernel program.
255     if ((err = create_single_kernel_helper(ctx, &program, &kernel, 1,
256                                            &image_migrate_kernel_code,
257                                            "test_image_migrate")))
258     {
259         print_error(err, "Failed creating kernel.");
260         failed = 1;
261         goto cleanup;
262     }
263 
264     // Create sampler.
265     sampler = clCreateSampler(ctx, CL_FALSE, CL_ADDRESS_CLAMP, CL_FILTER_NEAREST, &err );
266     if ((err != CL_SUCCESS) || !sampler) {
267       print_error(err, "Failed to create a sampler.");
268       failed = 1;
269       goto cleanup;
270     }
271 
272     num_devices_limited = num_devices;
273 
274     // Allocate memory buffers. 3 buffers (2 input, 1 output) for each sub-device.
275     // If we run out of memory, then restrict the number of sub-devices to be tested.
276     for (i=0; i<num_devices; i++) {
277       imageA[i] = init_image(queues[i], create_image_2d(ctx, (CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR),
278                                                         &format, IMAGE_DIM, IMAGE_DIM, 0, NULL, &err), A);
279       imageB[i] = init_image(queues[i], create_image_2d(ctx, (CL_MEM_READ_ONLY | CL_MEM_ALLOC_HOST_PTR),
280                                                         &format, IMAGE_DIM, IMAGE_DIM, 0, NULL, &err), B);
281       imageC[i] = create_image_2d(ctx, (CL_MEM_WRITE_ONLY | CL_MEM_ALLOC_HOST_PTR),
282                                   &format, IMAGE_DIM, IMAGE_DIM, 0, NULL, &err);
283 
284       if ((imageA[i] == NULL) || (imageB[i] == NULL) || (imageC[i] == NULL)) {
285         if (i == 0) {
286           log_error("Failed to allocate even 1 set of buffers.\n");
287           failed = 1;
288           goto cleanup;
289         }
290         num_devices_limited = i;
291         break;
292       }
293     }
294 
295     // For each partition, we will execute the test kernel with each of the 3 buffers migrated to one of the migrate options
296     for (migrateA=(enum migrations)(0); migrateA<NUMBER_OF_MIGRATIONS; migrateA = (enum migrations)((int)migrateA + 1)) {
297       if (migrateMemObject(migrateA, queues, imageA, num_devices_limited, flagsA, d) != CL_SUCCESS) {
298         failed = 1;
299         goto cleanup;
300       }
301       for (migrateC=(enum migrations)(0); migrateC<NUMBER_OF_MIGRATIONS; migrateC = (enum migrations)((int)migrateC + 1)) {
302         if (migrateMemObject(migrateC, queues, imageC, num_devices_limited, flagsC, d) != CL_SUCCESS) {
303           failed = 1;
304           goto cleanup;
305         }
306         for (migrateB=(enum migrations)(0); migrateB<NUMBER_OF_MIGRATIONS; migrateB = (enum migrations)((int)migrateB + 1)) {
307           if (migrateMemObject(migrateB, queues, imageB, num_devices_limited, flagsB, d) != CL_SUCCESS) {
308             failed = 1;
309             goto cleanup;
310           }
311           // Run the test on each of the partitions.
312           for (i=0; i<num_devices_limited; i++) {
313             cl_uint x;
314 
315             x = i + test_number;
316 
317             if ((err = clSetKernelArg(kernel, 0, sizeof(cl_mem), (const void *)&imageC[i])) != CL_SUCCESS) {
318               print_error(err, "Failed set kernel argument 0.");
319               failed = 1;
320               goto cleanup;
321             }
322 
323             if ((err = clSetKernelArg(kernel, 1, sizeof(cl_mem), (const void *)&imageA[i])) != CL_SUCCESS) {
324               print_error(err, "Failed set kernel argument 1.");
325               failed = 1;
326               goto cleanup;
327             }
328 
329             if ((err = clSetKernelArg(kernel, 2, sizeof(cl_mem), (const void *)&imageB[i])) != CL_SUCCESS) {
330               print_error(err, "Failed set kernel argument 2.");
331               failed = 1;
332               goto cleanup;
333             }
334 
335             if ((err = clSetKernelArg(kernel, 3, sizeof(cl_sampler), (const void *)&sampler)) != CL_SUCCESS) {
336               print_error(err, "Failed set kernel argument 3.");
337               failed = 1;
338               goto cleanup;
339             }
340 
341             if ((err = clSetKernelArg(kernel, 4, sizeof(cl_uint), (const void *)&x)) != CL_SUCCESS) {
342               print_error(err, "Failed set kernel argument 4.");
343               failed = 1;
344               goto cleanup;
345             }
346 
347             if ((err = clEnqueueNDRangeKernel(queues[i], kernel, 2, NULL, wgs, wls, 0, NULL, NULL)) != CL_SUCCESS) {
348                 print_error(err, "Failed enqueuing the NDRange kernel.");
349                 failed = 1;
350                 goto cleanup;
351             }
352           }
353           // Verify the results as long as neither input is an undefined migration
354           const size_t origin[3] = {0, 0, 0};
355           const size_t region[3] = {IMAGE_DIM, IMAGE_DIM, 1};
356 
357           for (i=0; i<num_devices_limited; i++, test_number++) {
358             if (((flagsA[i] | flagsB[i]) & CL_MIGRATE_MEM_OBJECT_CONTENT_UNDEFINED) == 0) {
359               if ((err = clEnqueueReadImage(queues[i], imageC[i], CL_TRUE,
360                                             origin, region, 0, 0, C, 0, NULL, NULL)) != CL_SUCCESS) {
361                 print_error(err, "Failed reading output buffer.");
362                 failed = 1;
363                 goto cleanup;
364               }
365               for (j=0; j<MEM_OBJ_SIZE; j++) {
366                 cl_uint expected;
367 
368                 expected = A[j] ^ B[j] ^ test_number;
369                 if (C[j] != expected) {
370                   log_error("Failed on device %d,  work item %4d,  expected 0x%08x got 0x%08x (0x%08x ^ 0x%08x ^ 0x%08x)\n", i, j, expected, C[j], A[j], B[j], test_number);
371                   failed = 1;
372                 }
373               }
374               if (failed) goto cleanup;
375             }
376           }
377 
378           if (restoreImage(queues, imageB, num_devices_limited, flagsB, B) != CL_SUCCESS) {
379             failed = 1;
380             goto cleanup;
381           }
382         }
383       }
384       if (restoreImage(queues, imageA, num_devices_limited, flagsA, A) != CL_SUCCESS) {
385         failed = 1;
386         goto cleanup;
387       }
388     }
389 
390   cleanup:
391     // Clean up all the allocted resources create by the test. This includes sub-devices,
392     // command queues, and memory buffers.
393 
394     for (i=0; i<max_sub_devices; i++) {
395       // Memory buffer cleanup
396       if (imageA[i]) {
397         if ((err = clReleaseMemObject(imageA[i])) != CL_SUCCESS) {
398           print_error(err, "Failed releasing memory object.");
399           failed = 1;
400         }
401       }
402       if (imageB[i]) {
403         if ((err = clReleaseMemObject(imageB[i])) != CL_SUCCESS) {
404           print_error(err, "Failed releasing memory object.");
405           failed = 1;
406         }
407       }
408       if (imageC[i]) {
409         if ((err = clReleaseMemObject(imageC[i])) != CL_SUCCESS) {
410           print_error(err, "Failed releasing memory object.");
411           failed = 1;
412         }
413       }
414 
415       if (num_devices > 1) {
416         // Command queue cleanup
417         if (queues[i]) {
418           if ((err = clReleaseCommandQueue(queues[i])) != CL_SUCCESS) {
419             print_error(err, "Failed releasing command queue.");
420             failed = 1;
421           }
422         }
423 
424         // Sub-device cleanup
425         if (devices[i]) {
426           if ((err = clReleaseDevice(devices[i])) != CL_SUCCESS) {
427             print_error(err, "Failed releasing sub device.");
428             failed = 1;
429           }
430         }
431         devices[i] = 0;
432       }
433     }
434 
435     // Sampler cleanup
436     if (sampler) {
437       if ((err = clReleaseSampler(sampler)) != CL_SUCCESS) {
438     print_error(err, "Failed releasing sampler.");
439     failed = 1;
440       }
441       sampler = NULL;
442     }
443 
444     // Context, program, and kernel cleanup
445     if (program) {
446       if ((err = clReleaseProgram(program)) != CL_SUCCESS) {
447     print_error(err, "Failed releasing program.");
448     failed = 1;
449       }
450       program = NULL;
451     }
452 
453     if (kernel) {
454       if ((err = clReleaseKernel(kernel)) != CL_SUCCESS) {
455     print_error(err, "Failed releasing kernel.");
456     failed = 1;
457       }
458       kernel = NULL;
459     }
460 
461     if (ctx && (ctx != context)) {
462       if ((err = clReleaseContext(ctx)) != CL_SUCCESS) {
463     print_error(err, "Failed releasing context.");
464     failed = 1;
465       }
466     }
467     ctx = NULL;
468 
469     if (failed) goto cleanup_allocations;
470   } while (domains);
471 
472 cleanup_allocations:
473   if (devices) free(devices);
474   if (queues)  free(queues);
475   if (flagsA)  free(flagsA);
476   if (flagsB)  free(flagsB);
477   if (flagsC)  free(flagsC);
478   if (imageA)  free(imageA);
479   if (imageB)  free(imageB);
480   if (imageC)  free(imageC);
481 
482   return ((failed) ? -1 : 0);
483 }
484