1
0
mirror of https://github.com/fumiama/base16384-sycl.git synced 2026-06-27 16:20:29 +08:00

init: project framework

This commit is contained in:
源文雨
2025-09-26 17:27:44 +08:00
commit 949438b794
6 changed files with 272 additions and 0 deletions

13
tests/CMakeLists.txt Normal file
View File

@@ -0,0 +1,13 @@
file(GLOB CPP_FILES "*.cpp")
foreach(CPP_FILE ${CPP_FILES})
# name without .cpp
get_filename_component(TARGET_NAME ${CPP_FILE} NAME_WE)
message(STATUS "Add test ${TARGET_NAME}")
add_executable(${TARGET_NAME} ${CPP_FILE})
set_target_properties(${TARGET_NAME} PROPERTIES COMPILE_FLAGS "${COMPILE_FLAGS}")
set_target_properties(${TARGET_NAME} PROPERTIES LINK_FLAGS "${LINK_FLAGS}")
add_test(NAME test_${TARGET_NAME} COMMAND ${TARGET_NAME})
endforeach()

55
tests/basic.cpp Normal file
View File

@@ -0,0 +1,55 @@
#include <iostream>
#include <sycl/sycl.hpp>
#include <vector>
#ifdef _WIN32
#include <windows.h>
#endif
static const int N = 4;
int main() {
#ifdef _WIN32
// Set console code page to UTF-8
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
#endif
sycl::queue q;
auto device = q.get_device();
std::cout << "执行设备: " << device.get_info<sycl::info::device::name>() << std::endl;
std::cout << "设备类型: ";
if (device.is_cpu()) {
std::cout << "CPU" << std::endl;
} else if (device.is_gpu()) {
std::cout << "GPU" << std::endl;
} else {
std::cout << "其他" << std::endl;
}
int *data = sycl::malloc_shared<int>(N, q);
for (int i = 0; i < N; i++) data[i] = i;
try {
q.single_task<class MyClass>([=]() {
for (int i = 0; i < N; i++) {
data[i] *= 2;
}
}).wait();
} catch (sycl::exception &e) {
// Do something to output or handle the exception
std::cout << "Caught sync SYCL exception: " << e.what() << "\n";
return 1;
} catch (std::exception &e) {
std::cout << "Caught std exception: " << e.what() << "\n";
return 2;
} catch (...) {
std::cout << "Caught unknown exception\n";
return 3;
}
for (int i = 0; i < N; i++) std::cout << data[i] << std::endl;
sycl::free(data, q);
return 0;
}