|
| 1 | +// RUN: %clangxx -fsycl %s -o %t.out |
| 2 | +// RUN: env SYCL_DEVICE_TYPE=HOST %t.out | FileCheck %s |
| 3 | +// RUN: %CPU_RUN_PLACEHOLDER %t.out %CPU_CHECK_PLACEHOLDER |
| 4 | +// RUN: %GPU_RUN_PLACEHOLDER %t.out %GPU_CHECK_PLACEHOLDER |
| 5 | +// RUN: %ACC_RUN_PLACEHOLDER %t.out %ACC_CHECK_PLACEHOLDER |
| 6 | + |
| 7 | +#include <CL/sycl.hpp> |
| 8 | + |
| 9 | +#include <cstdint> |
| 10 | +#include <iostream> |
| 11 | + |
| 12 | +using namespace cl::sycl; |
| 13 | + |
| 14 | +// According to OpenCL C spec, the format string must be in constant address |
| 15 | +// space |
| 16 | +#ifdef __SYCL_DEVICE_ONLY__ |
| 17 | +#define CONSTANT __attribute__((opencl_constant)) |
| 18 | +#else |
| 19 | +#define CONSTANT |
| 20 | +#endif |
| 21 | + |
| 22 | +// This is one of the possible ways to define a format string in a correct |
| 23 | +// address space |
| 24 | +static const CONSTANT char format_hello_world[] = "Hello, World!\n"; |
| 25 | + |
| 26 | +// Static isn't really needed if you define it in global scope |
| 27 | +const CONSTANT char format_int[] = "%d\n"; |
| 28 | + |
| 29 | +static const CONSTANT char format_vec[] = "%d,%d,%d,%d\n"; |
| 30 | + |
| 31 | +const CONSTANT char format_hello_world_2[] = "%lu: Hello, World!\n"; |
| 32 | + |
| 33 | +int main() { |
| 34 | + { |
| 35 | + default_selector Selector; |
| 36 | + queue Queue(Selector); |
| 37 | + |
| 38 | + Queue.submit([&](handler &CGH) { |
| 39 | + CGH.single_task<class integral>([=]() { |
| 40 | + // String |
| 41 | + intel::experimental::printf(format_hello_world); |
| 42 | + // Due to a bug in Intel CPU Runtime for OpenCL on Windows, information |
| 43 | + // printed using such format strings (without %-specifiers) might |
| 44 | + // appear in different order if output is redirected to a file or |
| 45 | + // another app |
| 46 | + // FIXME: strictly check output order once the bug is fixed |
| 47 | + // CHECK: {{(Hello, World!)?}} |
| 48 | + |
| 49 | + // Integral types |
| 50 | + intel::experimental::printf(format_int, (int32_t)123); |
| 51 | + intel::experimental::printf(format_int, (int32_t)-123); |
| 52 | + // CHECK: 123 |
| 53 | + // CHECK-NEXT: -123 |
| 54 | + |
| 55 | + // Floating point types |
| 56 | + { |
| 57 | + // You can declare format string in non-global scope, but in this case |
| 58 | + // static keyword is required |
| 59 | + static const CONSTANT char format[] = "%f\n"; |
| 60 | + intel::experimental::printf(format, 33.4f); |
| 61 | + intel::experimental::printf(format, -33.4f); |
| 62 | + } |
| 63 | + // CHECK-NEXT: 33.4 |
| 64 | + // CHECK-NEXT: -33.4 |
| 65 | + |
| 66 | + // Vectors |
| 67 | + cl::sycl::vec<int, 4> v4{5, 6, 7, 8}; |
| 68 | +#ifdef __SYCL_DEVICE_ONLY__ |
| 69 | + // On device side, vectors can be printed via native OpenCL types: |
| 70 | + using ocl_int4 = cl::sycl::vec<int, 4>::vector_t; |
| 71 | + { |
| 72 | + static const CONSTANT char format[] = "%v4d\n"; |
| 73 | + intel::experimental::printf(format, (ocl_int4)v4); |
| 74 | + } |
| 75 | + |
| 76 | + // However, you are still able to print them by-element: |
| 77 | + { |
| 78 | + intel::experimental::printf(format_vec, (int32_t)v4.w(), |
| 79 | + (int32_t)v4.z(), (int32_t)v4.y(), |
| 80 | + (int32_t)v4.x()); |
| 81 | + } |
| 82 | +#else |
| 83 | + // On host side you always have to print them by-element: |
| 84 | + intel::experimental::printf(format_vec, (int32_t)v4.x(), |
| 85 | + (int32_t)v4.y(), (int32_t)v4.z(), |
| 86 | + (int32_t)v4.w()); |
| 87 | + intel::experimental::printf(format_vec, (int32_t)v4.w(), |
| 88 | + (int32_t)v4.z(), (int32_t)v4.y(), |
| 89 | + (int32_t)v4.x()); |
| 90 | +#endif // __SYCL_DEVICE_ONLY__ |
| 91 | + // CHECK-NEXT: 5,6,7,8 |
| 92 | + // CHECK-NEXT: 8,7,6,5 |
| 93 | + |
| 94 | + // Pointers |
| 95 | + int a = 5; |
| 96 | + int *Ptr = &a; |
| 97 | + // According to OpenCL spec, argument should be a void pointer |
| 98 | + { |
| 99 | + static const CONSTANT char format[] = "%p\n"; |
| 100 | + intel::experimental::printf(format, (void *)Ptr); |
| 101 | + } |
| 102 | + // CHECK-NEXT: {{(0x)?[0-9a-fA-F]+$}} |
| 103 | + }); |
| 104 | + }); |
| 105 | + Queue.wait(); |
| 106 | + |
| 107 | + // printf in parallel_for |
| 108 | + Queue.submit([&](handler &CGH) { |
| 109 | + CGH.parallel_for<class stream_string>(range<1>(10), [=](id<1> i) { |
| 110 | + // cast to uint64_t to be sure that we pass 64-bit unsigned value |
| 111 | + intel::experimental::printf(format_hello_world_2, (uint64_t)i.get(0)); |
| 112 | + }); |
| 113 | + }); |
| 114 | + Queue.wait(); |
| 115 | + // CHECK-NEXT: {{[0-9]+}}: Hello, World! |
| 116 | + // CHECK-NEXT: {{[0-9]+}}: Hello, World! |
| 117 | + // CHECK-NEXT: {{[0-9]+}}: Hello, World! |
| 118 | + // CHECK-NEXT: {{[0-9]+}}: Hello, World! |
| 119 | + // CHECK-NEXT: {{[0-9]+}}: Hello, World! |
| 120 | + // CHECK-NEXT: {{[0-9]+}}: Hello, World! |
| 121 | + // CHECK-NEXT: {{[0-9]+}}: Hello, World! |
| 122 | + // CHECK-NEXT: {{[0-9]+}}: Hello, World! |
| 123 | + // CHECK-NEXT: {{[0-9]+}}: Hello, World! |
| 124 | + // CHECK-NEXT: {{[0-9]+}}: Hello, World! |
| 125 | + } |
| 126 | + |
| 127 | +// FIXME: strictly check output order once the bug mentioned above is fixed |
| 128 | +// CHECK: {{(Hello, World!)?}} |
| 129 | + |
| 130 | + return 0; |
| 131 | +} |
0 commit comments