Skip to content
66 changes: 5 additions & 61 deletions sycl/test-e2e/BFloat16/bfloat16_example.cpp
Original file line number Diff line number Diff line change
@@ -1,80 +1,24 @@
///
/// Check if bfloat16 example works using fallback libraries
/// Check if bfloat16 example works using fallback libraries AOT compiled for
/// both GPU and CPU.
///

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

// CUDA is not compatible with SPIR.
// UNSUPPORTED: cuda

// RUN: %clangxx -fsycl %s -o %t.out
// RUN: %{run} %t.out

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

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

// RUN: %clangxx -fsycl -fsycl-targets=spir64_gen -Xsycl-target-backend "-device *" %s -o %t.out
// RUN: %if gpu %{ %{run} %t.out %}

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

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

// RUN: %clangxx -fsycl -fsycl-targets=spir64,spir64_gen -Xsycl-target-backend=spir64_gen "-device pvc" %s -o %t.out
// RUN: %if cpu %{ %{run} %t.out %}

// RUN: %clangxx -fsycl -fsycl-targets=spir64_x86_64,spir64_gen -Xsycl-target-backend=spir64_gen "-device pvc" %s -o %t.out
// RUN: %if cpu %{ %{run} %t.out %}

#include <sycl/detail/core.hpp>
#include <sycl/ext/oneapi/bfloat16.hpp>

using namespace sycl;
using sycl::ext::oneapi::bfloat16;

float foo(float a, float b) {
// Convert from float to bfloat16.
bfloat16 A{a};
bfloat16 B{b};

// Convert A and B from bfloat16 to float, do addition on floating-point
// numbers, then convert the result to bfloat16 and store it in C.
bfloat16 C = A + B;

// Return the result converted from bfloat16 to float.
return C;
}

int main(int argc, char *argv[]) {
float data[3] = {7.0f, 8.1f, 0.0f};

float result_host = foo(7.0f, 8.1f);
std::cout << "CPU Result = " << result_host << std::endl;
if (std::abs(15.1f - result_host) > 0.1f) {
std::cout << "Test failed. Expected CPU Result ~= 15.1" << std::endl;
return 1;
}

queue deviceQueue;
buffer<float, 1> buf{data, 3};

deviceQueue.submit([&](handler &cgh) {
accessor numbers{buf, cgh, read_write};
cgh.single_task([=]() { numbers[2] = foo(numbers[0], numbers[1]); });
});

host_accessor hostOutAcc{buf, read_only};
float result_device = hostOutAcc[2];
std::cout << "GPU Result = " << result_device << std::endl;
if (std::abs(result_host - result_device) > 0.1f) {
std::cout << "Test failed. CPU Result !~= GPU result" << std::endl;
return 1;
}
#include "bfloat16_example.hpp"

return 0;
int main() {
return runTest();
}
47 changes: 47 additions & 0 deletions sycl/test-e2e/BFloat16/bfloat16_example.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <sycl/detail/core.hpp>
#include <sycl/ext/oneapi/bfloat16.hpp>

using namespace sycl;
using sycl::ext::oneapi::bfloat16;

float foo(float a, float b) {
// Convert from float to bfloat16.
bfloat16 A{a};
bfloat16 B{b};

// Convert A and B from bfloat16 to float, do addition on floating-point
// numbers, then convert the result to bfloat16 and store it in C.
bfloat16 C = A + B;

// Return the result converted from bfloat16 to float.
return C;
}

int runTest() {
float data[3] = {7.0f, 8.1f, 0.0f};

float result_host = foo(7.0f, 8.1f);
std::cout << "Host Result = " << result_host << std::endl;
if (std::abs(15.1f - result_host) > 0.1f) {
std::cout << "Test failed. Expected Host Result ~= 15.1" << std::endl;
return 1;
}

queue deviceQueue;
buffer<float, 1> buf{data, 3};

deviceQueue.submit([&](handler &cgh) {
accessor numbers{buf, cgh, read_write};
cgh.single_task([=]() { numbers[2] = foo(numbers[0], numbers[1]); });
});

host_accessor hostOutAcc{buf, read_only};
float result_device = hostOutAcc[2];
std::cout << "Device Result = " << result_device << std::endl;
if (std::abs(result_host - result_device) > 0.1f) {
std::cout << "Test failed. Host Result !~= Device result" << std::endl;
return 1;
}

return 0;
}
18 changes: 18 additions & 0 deletions sycl/test-e2e/BFloat16/bfloat16_example_cpu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
///
/// Check if bfloat16 example works using fallback libraries AOT compiled for
/// CPU.
///

// REQUIRES: opencl-aot, ocloc, gpu-intel-gen12, cpu

// RUN: %clangxx -fsycl -fsycl-targets=spir64,spir64_gen -Xsycl-target-backend=spir64_gen "-device pvc" %s -o %t.out
// RUN: %{run} %t.out

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

#include "bfloat16_example.hpp"

int main() {
return runTest();
}
18 changes: 18 additions & 0 deletions sycl/test-e2e/BFloat16/bfloat16_example_gpu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
///
/// Check if bfloat16 example works using fallback libraries AOT compiled for
/// GPU.
///

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

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

// RUN: %clangxx -fsycl -fsycl-targets=spir64_gen -Xsycl-target-backend "-device *" %s -o %t.out
// RUN: %{run} %t.out

#include "bfloat16_example.hpp"

int main() {
return runTest();
}
Loading