1
0
mirror of https://github.com/fumiama/base16384-sycl.git synced 2026-06-05 00:32:49 +08:00

optimize: project structure

This commit is contained in:
源文雨
2025-09-28 16:12:45 +08:00
parent 949438b794
commit ad1df2bb98
7 changed files with 87 additions and 16 deletions

View File

@@ -20,7 +20,7 @@
"-fsycl"
],
"cStandard": "c17",
"cppStandard": "c++17",
"cppStandard": "c++20",
"intelliSenseMode": "windows-clang-x64",
"configurationProvider": "ms-vscode.cmake-tools"
},
@@ -43,7 +43,7 @@
"-fsycl"
],
"cStandard": "c17",
"cppStandard": "c++17",
"cppStandard": "c++20",
"intelliSenseMode": "linux-gcc-x64",
"configurationProvider": "ms-vscode.cmake-tools"
}

View File

@@ -43,8 +43,18 @@ endif()
find_package(IntelSYCL REQUIRED)
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include")
set(COMPILE_FLAGS "-fsycl -Wall ${WIN_FLAG}")
set(LINK_FLAGS "-fsycl")
add_subdirectory(libs)
message(STATUS "Collected libs: ${B14LIBS}")
enable_testing()
add_subdirectory(tests)
message(STATUS "Collected tests: ${B14TESTS}")
foreach(TARGET_NAME ${B14TESTS})
target_link_libraries(${TARGET_NAME} ${B14LIBS})
endforeach()

16
include/errors.hpp Normal file
View File

@@ -0,0 +1,16 @@
#ifndef _ERRORS_HPP_
#define _ERRORS_HPP_
#include <functional>
typedef enum {
errors_code_ok,
errors_code_sync_sycl_exception,
errors_code_std_exception,
errors_code_unknown_exception,
} errors_code_enum_t;
// failed try to exec fn, catch and print .what() when exception is thrown.
errors_code_enum_t failed(std::function<void(void)> fn);
#endif

17
libs/CMakeLists.txt Normal file
View File

@@ -0,0 +1,17 @@
file(GLOB CPP_FILES "*.cpp")
set(LOCAL_B14LIBS "")
foreach(CPP_FILE ${CPP_FILES})
# name without .cpp
get_filename_component(TARGET_NAME ${CPP_FILE} NAME_WE)
message(STATUS "Add lib: ${TARGET_NAME}")
add_library(${TARGET_NAME} STATIC ${CPP_FILE})
set_target_properties(${TARGET_NAME} PROPERTIES COMPILE_FLAGS "${COMPILE_FLAGS}")
set_target_properties(${TARGET_NAME} PROPERTIES LINK_FLAGS "${LINK_FLAGS}")
list(APPEND LOCAL_B14LIBS ${TARGET_NAME})
endforeach()
set(B14LIBS ${LOCAL_B14LIBS} PARENT_SCOPE)

29
libs/errors.cpp Normal file
View File

@@ -0,0 +1,29 @@
#include "errors.hpp"
#include <functional>
#include <iostream>
#include <sycl/sycl.hpp>
template <typename T>
concept has_what_concept_t = requires(T t) { t.what(); };
template <has_what_concept_t T>
void print_what(T e, std::string msg) {
std::cerr << msg << e.what() << std::endl;
}
errors_code_enum_t failed(std::function<void(void)> fn) {
try {
fn();
} catch (sycl::exception &e) {
print_what(e, "Caught sync SYCL exception: ");
return errors_code_sync_sycl_exception;
} catch (std::exception &e) {
print_what(e, "Caught std exception: ");
return errors_code_std_exception;
} catch (...) {
std::cerr << "Caught unknown exception." << std::endl;
return errors_code_unknown_exception;
}
return errors_code_ok;
}

View File

@@ -1,13 +1,18 @@
file(GLOB CPP_FILES "*.cpp")
set(LOCAL_B14TESTS "")
foreach(CPP_FILE ${CPP_FILES})
# name without .cpp
get_filename_component(TARGET_NAME ${CPP_FILE} NAME_WE)
message(STATUS "Add test ${TARGET_NAME}")
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})
list(APPEND LOCAL_B14TESTS ${TARGET_NAME})
endforeach()
set(B14TESTS ${LOCAL_B14TESTS} PARENT_SCOPE)

View File

@@ -5,6 +5,8 @@
#include <windows.h>
#endif
#include "errors.hpp"
static const int N = 4;
int main() {
@@ -29,23 +31,15 @@ int main() {
int *data = sycl::malloc_shared<int>(N, q);
for (int i = 0; i < N; i++) data[i] = i;
try {
q.single_task<class MyClass>([=]() {
auto errn = failed([&]() {
q.parallel_for(sycl::range<1>(1), [=](sycl::id<1>) {
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;
}
});
if (errn) return errn;
for (int i = 0; i < N; i++) std::cout << data[i] << std::endl;