1.基于Intel DevCloud环境
1.环境搭建
1.访问Overview | Intel® DevCloud点击 Enroll 或 Get Free Access 开始注册
2.按照要求填写邮箱和个人信息,然后点击提交当出现如下情况表示注册成功
3.一般情况下,系统会同时自动跳转到账号激活步骤进行账号激活。如果系统未能自动跳转,请点击POST data进行账号激活。 出现如下字样时表示激活成功:
4.激活完成,系统会跳转到 Intel Developer Cloud 主站。点击主站右侧oneAPI相关服务的链接,跳转 进入 DevCloud for oneAPI 专项服务的登录页面。如果系统未能自动跳转,可以通过以下链接:Overview | Intel® DevCloud
5.在 DevCloud for oneAPI 专项服务页面中,点击右上角或页面中的“Sign In”链接,输入完成登录。
6.然后单击getstart标签
然后下滑至最下方的位置
点击Launch JupyterLab*,然后回显示如下界面
2.编写代码并运行
1.输出“Hello world”
1.在左侧目录中新建一个名为lab的目录用来存储实验用到的文件
2.然后新建一个文件
在选择Kernel时选择Python
3.在新建的文件中输入一个“Hello World”的代码
%%writefile lab/hello.cpp
#include <CL/sycl.hpp>
#include <iostream>
using namespace sycl;
const std::string secret
{
"Ifmmp-!xpsme\"\012J(n!tpssz-!Ebwf/!"
"J(n!bgsbje!J!dbo(u!ep!uibu/!.!IBM\01"
};
const auto sz = secret.size();
int main()
{
queue Q;
char *result = malloc_shared<char>(sz, Q);
std::memcpy(result, secret.data(), sz);
Q.parallel_for(sz, [=] (auto &i) {result[i] -= 1;}).wait();
std::cout << result << "\n";
return 0;
}
将代码粘贴后点击三角形符号运行代码。
4.然后再新建一个终端
在终端中跳转至lab文件夹
编译文件并且运行
2.DPC++设备选择方式
1.默认的设备选择器
在左侧窗口中输入如下代码并运行
%%writefile lab/demo_fig2_7.cpp
#include <CL/sycl.hpp>
#include <iostream>
using namespace sycl;
int main(){
queue Q;
std::cout << "Select device: "<<
Q.get_device().get_info<info::device::name>() << "\n";
return 0;
}
切换至右侧命令窗口对代码进行编译和运行
2.使用host_selector
在左侧窗口中输入如下代码并运行
%%writefile lab/demo_fig2_9.cpp
#include <CL/sycl.hpp>
#include <iostream>
using namespace sycl;
int main(){
queue Q (host_selector{} );
std::cout << "Select device: "<<
Q.get_device().get_info<info::device::name>() << "\n";
std::cout <<" -> Device vendor: " <<
Q.get_device().get_info<info::device::vendor>() << "\n";
return 0;
}
这段代码在云端无法运行,显示已经不再支持
3.使用cpu_selector
在左侧窗口中输入如下代码并运行
%%writefile lab/demo_fig2_10.cpp
#include <CL/sycl.hpp>
#include <iostream>
using namespace sycl;
int main(){
queue Q ( cpu_selector_v );
std::cout << "Select device: "<<
Q.get_device().get_info<info::device::name>() << "\n";
std::cout <<" -> Device vendor: " <<
Q.get_device().get_info<info::device::vendor>() << "\n";
return 0;
}
切换至右侧命令窗口对代码进行编译和运行
4.使用多个selector
在左侧窗口中输入如下代码并运行
%%writefile lab/demo_fig2_13.cpp
#include <CL/sycl.hpp>
#include <iostream>
using namespace sycl;
int main(){
queue my_default_queue(default_selector_v);
queue my_accelerator_queue(accelerator_selector_v);
std::cout << "Selected device 1:" << my_default_queue.get_device().get_info<info::device::name>() << "\n";
std::cout << "Selected device 2:" << my_accelerator_queue.get_device().get_info<info::device::name>() << "\n";
return 0;
}
切换至右侧命令窗口对代码进行编译和运行
5.另一个既包含host code,也包含host code的代码示例
在左侧窗口中输入如下代码并运行
%%writefile lab/demo_fig2_22.cpp
#include <CL/sycl.hpp>
#include <array>
#include <iostream>
using namespace sycl;
int main()
{
constexpr int size=16;
std::array<int, size> data;
buffer B{data};
queue Q{};
std::cout << "Selected device is:" <<
Q.get_device () .get_info<info::device::name>()<<"\n";
Q.submit( [&] (handler& h){
accessor acc{B,h};
h.parallel_for (size , [=] (auto&idx){
acc [idx] = idx;
});
});
return 0;
}
切换至右侧命令窗口对代码进行编译和运行
2.Dpcpp编译器的本地环境
1.访问如下网址:https://github.com/intel/llvm/releases
进入界面后点击第一个 下载下面的第一项:
下载完成后解压到一个自定义的目录中
2.在桌面上的“此电脑”处单机右键,选择“属性”在弹出的窗口中点击高级系统设置然后点击环境变量在其中的系统变量中选择Path双击Path在其中添加刚刚解压到的位置的路径并点击确认。
按住win+R后输入cmd,然后按回车键,打开黑色窗口,在窗口中跳转到编写代码的文件夹,输入命令对代码进行编译
clang++ -fsycl hello.cpp –o hello.exe
可能由于电脑硬件原因无法进行编译,运行示例代码均在云端完成