Skip to content

Commit 1326e5d

Browse files
committed
Split bfloat16_example
Signed-off-by: Larsen, Steffen <[email protected]>
1 parent 3e50079 commit 1326e5d

File tree

4 files changed

+88
-61
lines changed

4 files changed

+88
-61
lines changed
Lines changed: 5 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,24 @@
11
///
2-
/// Check if bfloat16 example works using fallback libraries
2+
/// Check if bfloat16 example works using fallback libraries AOT compiled for
3+
/// both GPU and CPU.
34
///
45

56
// REQUIRES: opencl-aot, ocloc, gpu-intel-gen12
67

7-
// CUDA is not compatible with SPIR.
8-
// UNSUPPORTED: cuda
9-
108
// RUN: %clangxx -fsycl %s -o %t.out
119
// RUN: %{run} %t.out
1210

1311
// RUN: %clangxx -fsycl -fsycl-targets=spir64 %s -o %t.out
1412
// RUN: %{run} %t.out
1513

16-
// RUN: %clangxx -fsycl -fsycl-targets=spir64_gen -Xsycl-target-backend "-device gen12lp" %s -o %t.out
17-
// RUN: %{run} %t.out
18-
19-
// RUN: %clangxx -fsycl -fsycl-targets=spir64_gen -Xsycl-target-backend "-device *" %s -o %t.out
20-
// RUN: %if gpu %{ %{run} %t.out %}
21-
2214
// RUN: %clangxx -fsycl -fsycl-targets=spir64,spir64_gen -Xsycl-target-backend=spir64_gen "-device gen12lp" %s -o %t.out
2315
// RUN: %{run} %t.out
2416

2517
// RUN: %clangxx -fsycl -fsycl-targets=spir64_x86_64,spir64_gen -Xsycl-target-backend=spir64_gen "-device gen12lp" %s -o %t.out
2618
// RUN: %{run} %t.out
2719

28-
// RUN: %clangxx -fsycl -fsycl-targets=spir64,spir64_gen -Xsycl-target-backend=spir64_gen "-device pvc" %s -o %t.out
29-
// RUN: %if cpu %{ %{run} %t.out %}
30-
31-
// RUN: %clangxx -fsycl -fsycl-targets=spir64_x86_64,spir64_gen -Xsycl-target-backend=spir64_gen "-device pvc" %s -o %t.out
32-
// RUN: %if cpu %{ %{run} %t.out %}
33-
34-
#include <sycl/detail/core.hpp>
35-
#include <sycl/ext/oneapi/bfloat16.hpp>
36-
37-
using namespace sycl;
38-
using sycl::ext::oneapi::bfloat16;
39-
40-
float foo(float a, float b) {
41-
// Convert from float to bfloat16.
42-
bfloat16 A{a};
43-
bfloat16 B{b};
44-
45-
// Convert A and B from bfloat16 to float, do addition on floating-point
46-
// numbers, then convert the result to bfloat16 and store it in C.
47-
bfloat16 C = A + B;
48-
49-
// Return the result converted from bfloat16 to float.
50-
return C;
51-
}
52-
53-
int main(int argc, char *argv[]) {
54-
float data[3] = {7.0f, 8.1f, 0.0f};
55-
56-
float result_host = foo(7.0f, 8.1f);
57-
std::cout << "CPU Result = " << result_host << std::endl;
58-
if (std::abs(15.1f - result_host) > 0.1f) {
59-
std::cout << "Test failed. Expected CPU Result ~= 15.1" << std::endl;
60-
return 1;
61-
}
62-
63-
queue deviceQueue;
64-
buffer<float, 1> buf{data, 3};
65-
66-
deviceQueue.submit([&](handler &cgh) {
67-
accessor numbers{buf, cgh, read_write};
68-
cgh.single_task([=]() { numbers[2] = foo(numbers[0], numbers[1]); });
69-
});
70-
71-
host_accessor hostOutAcc{buf, read_only};
72-
float result_device = hostOutAcc[2];
73-
std::cout << "GPU Result = " << result_device << std::endl;
74-
if (std::abs(result_host - result_device) > 0.1f) {
75-
std::cout << "Test failed. CPU Result !~= GPU result" << std::endl;
76-
return 1;
77-
}
20+
#include "bfloat16_example.hpp"
7821

79-
return 0;
22+
int main() {
23+
return runTest();
8024
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <sycl/detail/core.hpp>
2+
#include <sycl/ext/oneapi/bfloat16.hpp>
3+
4+
using namespace sycl;
5+
using sycl::ext::oneapi::bfloat16;
6+
7+
float foo(float a, float b) {
8+
// Convert from float to bfloat16.
9+
bfloat16 A{a};
10+
bfloat16 B{b};
11+
12+
// Convert A and B from bfloat16 to float, do addition on floating-point
13+
// numbers, then convert the result to bfloat16 and store it in C.
14+
bfloat16 C = A + B;
15+
16+
// Return the result converted from bfloat16 to float.
17+
return C;
18+
}
19+
20+
int runTest() {
21+
float data[3] = {7.0f, 8.1f, 0.0f};
22+
23+
float result_host = foo(7.0f, 8.1f);
24+
std::cout << "Host Result = " << result_host << std::endl;
25+
if (std::abs(15.1f - result_host) > 0.1f) {
26+
std::cout << "Test failed. Expected Host Result ~= 15.1" << std::endl;
27+
return 1;
28+
}
29+
30+
queue deviceQueue;
31+
buffer<float, 1> buf{data, 3};
32+
33+
deviceQueue.submit([&](handler &cgh) {
34+
accessor numbers{buf, cgh, read_write};
35+
cgh.single_task([=]() { numbers[2] = foo(numbers[0], numbers[1]); });
36+
});
37+
38+
host_accessor hostOutAcc{buf, read_only};
39+
float result_device = hostOutAcc[2];
40+
std::cout << "Device Result = " << result_device << std::endl;
41+
if (std::abs(result_host - result_device) > 0.1f) {
42+
std::cout << "Test failed. Host Result !~= Device result" << std::endl;
43+
return 1;
44+
}
45+
46+
return 0;
47+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
///
2+
/// Check if bfloat16 example works using fallback libraries AOT compiled for
3+
/// CPU.
4+
///
5+
6+
// REQUIRES: opencl-aot, ocloc, gpu-intel-gen12, cpu
7+
8+
// RUN: %clangxx -fsycl -fsycl-targets=spir64,spir64_gen -Xsycl-target-backend=spir64_gen "-device pvc" %s -o %t.out
9+
// RUN: %{run} %t.out
10+
11+
// RUN: %clangxx -fsycl -fsycl-targets=spir64_x86_64,spir64_gen -Xsycl-target-backend=spir64_gen "-device pvc" %s -o %t.out
12+
// RUN: %{run} %t.out
13+
14+
#include "bfloat16_example.hpp"
15+
16+
int main() {
17+
return runTest();
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
///
2+
/// Check if bfloat16 example works using fallback libraries AOT compiled for
3+
/// GPU.
4+
///
5+
6+
// REQUIRES: opencl-aot, ocloc, gpu-intel-gen12, gpu
7+
8+
// RUN: %clangxx -fsycl -fsycl-targets=spir64_gen -Xsycl-target-backend "-device gen12lp" %s -o %t.out
9+
// RUN: %{run} %t.out
10+
11+
// RUN: %clangxx -fsycl -fsycl-targets=spir64_gen -Xsycl-target-backend "-device *" %s -o %t.out
12+
// RUN: %{run} %t.out
13+
14+
#include "bfloat16_example.hpp"
15+
16+
int main() {
17+
return runTest();
18+
}

0 commit comments

Comments
 (0)