commit 949438b79410260982a55c481273caf16e8d5d36 Author: 源文雨 <41315874+fumiama@users.noreply.github.com> Date: Fri Sep 26 17:27:44 2025 +0800 init: project framework diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d76b74e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build +.DS_Store diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..446c9d6 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,52 @@ +{ + "configurations": [ + { + "name": "Win32", + "includePath": [ + "${workspaceFolder}/**", + "${config:oneapi.root.windows}/compiler/latest/include", + "${config:oneapi.root.windows}/compiler/latest/include/sycl" + ], + "defines": [ + "_DEBUG", + "UNICODE", + "_UNICODE", + "__SYCL_DEVICE_ONLY__", + "SYCL_LANGUAGE_VERSION=2020" + ], + "windowsSdkVersion": "10.0.22621.0", + "compilerPath": "${config:oneapi.root.windows}/compiler/latest/bin/icx.exe", + "compilerArgs": [ + "-fsycl" + ], + "cStandard": "c17", + "cppStandard": "c++17", + "intelliSenseMode": "windows-clang-x64", + "configurationProvider": "ms-vscode.cmake-tools" + }, + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "${config:oneapi.root.linux}/compiler/latest/include", + "${config:oneapi.root.linux}/compiler/latest/include/sycl", + "/usr/include", + "/usr/local/include" + ], + "defines": [ + "_DEBUG", + "__SYCL_DEVICE_ONLY__", + "SYCL_LANGUAGE_VERSION=2020" + ], + "compilerPath": "${config:oneapi.root.linux}/compiler/latest/bin/icpx", + "compilerArgs": [ + "-fsycl" + ], + "cStandard": "c17", + "cppStandard": "c++17", + "intelliSenseMode": "linux-gcc-x64", + "configurationProvider": "ms-vscode.cmake-tools" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..17c1116 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,100 @@ +{ + "oneapi.root.windows": "C:/Program Files (x86)/Intel/oneAPI", + "oneapi.root.linux": "/opt/intel/oneapi", + "files.associations": { + "vector": "cpp", + "iostream": "cpp", + "string": "cpp", + "array": "cpp", + "algorithm": "cpp", + "atomic": "cpp", + "bit": "cpp", + "bitset": "cpp", + "cctype": "cpp", + "cfenv": "cpp", + "charconv": "cpp", + "cinttypes": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "exception": "cpp", + "format": "cpp", + "functional": "cpp", + "initializer_list": "cpp", + "ios": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "iterator": "cpp", + "limits": "cpp", + "list": "cpp", + "locale": "cpp", + "memory": "cpp", + "mutex": "cpp", + "new": "cpp", + "numeric": "cpp", + "optional": "cpp", + "ostream": "cpp", + "ratio": "cpp", + "span": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "system_error": "cpp", + "thread": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "typeinfo": "cpp", + "unordered_map": "cpp", + "utility": "cpp", + "variant": "cpp", + "xfacet": "cpp", + "xhash": "cpp", + "xiosbase": "cpp", + "xlocale": "cpp", + "xlocbuf": "cpp", + "xlocinfo": "cpp", + "xlocmes": "cpp", + "xlocmon": "cpp", + "xlocnum": "cpp", + "xloctime": "cpp", + "xmemory": "cpp", + "xstring": "cpp", + "xtr1common": "cpp", + "xutility": "cpp" + }, + "terminal.integrated.defaultProfile.windows": "Command Prompt with oneAPI", + "terminal.integrated.defaultProfile.linux": "bash with oneAPI", + "terminal.integrated.profiles.windows": { + "Command Prompt with oneAPI": { + "path": "cmd.exe", + "args": ["/k", "${config:oneapi.root.windows}/setvars.bat", "&&", "powershell"] + } + }, + "terminal.integrated.profiles.linux": { + "bash with oneAPI": { + "path": "/bin/bash", + "args": ["-c", "source ${config:oneapi.root.linux}/setvars.sh && exec bash"] + } + }, + "C_Cpp.intelliSenseEngine": "default", + "C_Cpp.errorSquiggles": "enabled", + "C_Cpp.autocomplete": "default", + "C_Cpp.formatting": "clangFormat", + "C_Cpp.clang_format_style": "{ BasedOnStyle: Google, IndentWidth: 2, TabWidth: 2, UseTab: Never, ColumnLimit: 100 }", + "C_Cpp.default.cppStandard": "c++20", + "C_Cpp.default.cStandard": "c17", + "C_Cpp.workspaceParsingPriority": "highest", + "C_Cpp.enhancedColorization": "enabled", + "C_Cpp.suggestSnippets": true, + "editor.formatOnSave": true, + "editor.formatOnType": true +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..61919ba --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,50 @@ +# Please run setvars.sh in your oneAPI root dir first before CMake. +# In Windows, you need to run setvars.bat and CMake in CMD (not PS). + +if(UNIX) + # Direct CMake to use icpx rather than the default C++ compiler/linker + set(CMAKE_C_COMPILER icx) + set(CMAKE_CXX_COMPILER icpx) + # Also set the linker to use icpx + set(CMAKE_CXX_LINKER icpx) + set(CMAKE_LINKER icpx) +endif() + +if(WIN32) + include (Platform/Windows-Clang) + # This is a Windows-specific flag that enables exception handling in host code + set(WIN_FLAG "/EHsc") + # Set CMake to use icx-cl rather than the default C++ compiler/linker + set(CMAKE_C_COMPILER icx-cl) + set(CMAKE_CXX_COMPILER icx-cl) + # Also set the linker to use icpx + set(CMAKE_CXX_LINKER icx-cl) + set(CMAKE_LINKER icx-cl) +endif() + +cmake_minimum_required(VERSION 3.4...4.1.1) +if (POLICY CMP0048) + cmake_policy(SET CMP0048 NEW) +endif (POLICY CMP0048) + +project( + base16384 + VERSION 2.3.2 + LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +if(WIN32) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8 /execution-charset:utf-8 /source-charset:utf-8") +else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -finput-charset=utf-8 -fexec-charset=utf-8") +endif() + +find_package(IntelSYCL REQUIRED) + +set(COMPILE_FLAGS "-fsycl -Wall ${WIN_FLAG}") +set(LINK_FLAGS "-fsycl") + +enable_testing() +add_subdirectory(tests) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..2d07c28 --- /dev/null +++ b/tests/CMakeLists.txt @@ -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() diff --git a/tests/basic.cpp b/tests/basic.cpp new file mode 100644 index 0000000..ef2a62c --- /dev/null +++ b/tests/basic.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#ifdef _WIN32 +#include +#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() << 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(N, q); + for (int i = 0; i < N; i++) data[i] = i; + + try { + q.single_task([=]() { + 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; +}