一、DPC++简介
DPC++是一款由Intel开发的开源编译软件,致力于在C++方面提供强大的数据并行性。DPC++编译器基于SYCL,带有一些扩展和广泛的异构支持,包括GPU、CPU和FPGA设备。
二、DevCloud注册
本实验主要是在Intel DevCloud环境下运行,因此需要注册一个DevCloud账户
1.登录网址https://idzcn.com/devcloud.htm
2.点击创建账户
3.按要求填写信息即可
三、基于Intel DevCloud的实践环境
1.在注册完成后,跳转至网址Overview | Intel® DevCloud并登录账户,选择Get Started
2.下滑至底部,找到JupyterLab
3.点击Launch JupyerLab,进入页面即为Intel DevCloud的云实践环境
4.在左侧一栏空白处右键创建一个文件夹mycode
5.创建一个Notebook,点击File->New->Notebook
6.选择Python3内核
7.创建一个Terminal,File->New->Terminal
四、代码实践
下面给出几段基于Intel DevCloud的实践环境下的DPC++示例代码运行
1.HelloWorld程序
(1)在Notebook中填写示例代码
%%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;
}
(2)点击运行
(3)切换至Terminal,切换至mycode文件夹下,依次执行以下命令
icpx -fsycl hello.cpp -o hello
./hello
(4) 输出结果:
2.DPC++设备选择方式1:默认的设备选择器
(1)同上述方式操作,并执行程序
%%writefile mycode/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)输出结果:
(3)结果分析:输出内容即为DPC++运行时环境采用的默认的设备选择器进行设备选择
3.DPC++设备选择方式2:使用host_selector
(1)代码:
%%writefile mycode/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;
}
(2)输出结果:
(3)结果分析: 由于host_selector已经被新版本的SYCL弃用,因此编译并未通过。
4.DPC++设备选择方式3:使用cpu_selector
(1)代码:
%%writefile mycode/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;
}
(2)输出结果:
(3)结果分析: 输出内容即为DPC++运行时环境采用cpu_selector进行设备选择
5.DPC++设备选择方式4:使用多个selector
(1)代码:
%%writefile mycode/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;
}
(2)输出结果:
(3)结果分析:输出内容即为DPC++运行环境时采用多个device selectior进行设备选择
6.Host code程序段
(1)代码:
%%writefile mycode/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)输出结果:
(3)结果分析:该段程序创建了一个绑定到buffer上的数组data,Host code部分设置了任物图结点,使得设备在未来满足依赖时执行循环赋值操作。
五、DPCPP基于本地环境的实践操作
上述实验过程是基于Intel DevCloud的实践环境,下面将介绍基于本地编译环境的搭建及代码运行
1.编译器下载Releases · intel/llvm · GitHub,(如网站进不去,多尝试几次或使用辅助工具)选择一个最新的win类型DPCPP编译器即可,并解压文件
2.配置本地环境变量
(1)右键此电脑->属性->高级系统设置
(2)环境变量->双击系统变量中path->新建->添加一条环境变量,变量格式为:编译器安装目录+dpcpp_compiler\bin
3.在本地某特定文件夹下创建一个CPP文件,以同前面的HelloWorld程序相同代码为例
4.在cmd窗口中切换至CPP文件存储的目录下,执行命令
clang++ -fsycl HelloWorld.cpp –o HelloWorld.exe
HelloWorld.exe
5.其他程序的执行操作类似,但由于硬件原因,在不同硬件下可能存在无法执行的情况,仅供参考。