mirror of
https://github.com/fumiama/base16384-sycl.git
synced 2026-06-05 08:40:34 +08:00
56 lines
1.3 KiB
C++
56 lines
1.3 KiB
C++
#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;
|
|
}
|