OneAPI

#!/bin/bash

USE_CUDA=false
WORK_SPACE=~/xyt/

install_packages() {
	echo -e "-------------------- Install packages --------------------"
	sudo apt-get install git
	sudo apt-get install cmake
	sudo apt-get install python3
	sudo apt-get install ninja-build
	pip install psutil
}

install_onemkl() {
	echo -e "-------------------- Install OneMKL --------------------"
	if [ -d ~/intel/oneapi/mkl/2021.3.0/ ]; then
		echo "OneMKL found"
		echo "~/intel/oneapi/mkl/2021.3.0/"
	else
		wget https://registrationcenter-download.intel.com/akdlm/irc_nas/17901/l_onemkl_p_2021.3.0.520_offline.sh
		sh l_onemkl_p_2021.3.0.520_offline.sh
		rm -rf l_onemkl_p_2021.3.0.520_offline.sh
	fi
}

install_dpcpp() {
	echo -e "-------------------- Install CLANG++ and DPCPP --------------------"
	export DPCPP_HOME=~/sycl_workspace/
	if [ -d $DPCPP_HOME ]; then
		echo "CLANG++ and DPCPP found"
		echo $DPCPP_HOME
	else
		mkdir $DPCPP_HOME
		cd $DPCPP_HOME
		git clone https://github.com/intel/llvm -b sycl
		if [ $USE_CUDA ]; then
			python $DPCPP_HOME/llvm/buildbot/configure.py --cuda
		else
			python $DPCPP_HOME/llvm/buildbot/configure.py
		fi
		python $DPCPP_HOME/llvm/buildbot/configure.py
		python $DPCPP_HOME/llvm/buildbot/compile.py
		python $DPCPP_HOME/llvm/buildbot/check.py
	fi
}

create_env() {
	echo -e "-------------------- Create OneAPI env --------------------"
	cd $WORK_SPACE
	rm -rf env_oneapi.sh
	echo "# TODO: configure DPCPP_ROOT/MKL_DPCPP_ROOT
export DPCPP_ROOT=${DPCPP_HOME}llvm/build
export MKL_DPCPP_ROOT=~/intel/oneapi/mkl/2021.3.0
export LD_LIBRARY_PATH=\${DPCPP_ROOT}/lib:\${LD_LIBRARY_PATH}
export LD_LIBRARY_PATH=\${MKL_DPCPP_ROOT}/lib/ia32:\${MKL_DPCPP_ROOT}/lib/intel64:\${LD_LIBRARY_PATH}
export PATH=\${DPCPP_ROOT}/bin:\$PATH
export INTELOCLSDKROOT=\${DPCPP_ROOT}
export SYCLROOT=\${DPCPP_ROOT}" >> env_oneapi.sh
}

runtime_test() {
	echo -e "-------------------- Runtime test --------------------"
	cd $WORK_SPACE
	rm -rf simple-sycl-app.cpp
	rm -rf simple-sycl-app.exe
	source env_oneapi.sh
	echo "
#include <iostream>
#include <ctime>
#include <math.h>
#include <string>
#include <CL/sycl.hpp>
using namespace std;
using namespace sycl;
clock_t start;
const size_t len = 2048*2048*100;
void output_dev_info( const device& dev, const std::string& selector_name) {
	std::cout << selector_name << \": Selected device: \" <<
	dev.get_info<info::device::name>() << \"\n\";
	std::cout << \" -> Device vendor: \" <<
	dev.get_info<info::device::vendor>() << \"\n\";
}
int main() {
	output_dev_info( device{ default_selector{}}, \"default_selector\" );
	output_dev_info( device{ host_selector{}}, \"host_selector\" );
	output_dev_info( device{ gpu_selector{}}, \"gpu_selector\" );
	
	float *a = new float[len]; memset(a, 123, len*sizeof(float));
	float *b = new float[len]; memset(b, 22, len*sizeof(float));
	float *c = new float[len];
	queue Q(gpu_selector{});
	float *a_dev = malloc_device<float>(len, Q);
	float *b_dev = malloc_device<float>(len, Q);
	float *c_dev = malloc_device<float>(len, Q);
		
	start=clock();
	for(int i=0;i<len;i++) c[i] = sqrt(a[i]) * b[i];
	long time_cpu = clock() - start;
	cout << time_cpu << std::endl;
	
	start=clock();
	Q.submit([&](handler &h) {
		h.parallel_for(len, [=](id<1> i) {
			c_dev[i] = sqrt(a_dev[i]) * b_dev[i];
		});
	}).wait();
	long time_xpu = clock() - start;
	cout << time_xpu << std::endl;
	cout << (float)time_cpu / time_xpu << std::endl;
	
	return 0;
}
" >> simple-sycl-app.cpp
	if [ $USE_CUDA ]; then
		clang++ -fsycl simple-sycl-app.cpp -o simple-sycl-app.exe
	else
		clang++ -fsycl -fsycl-targets=nvptx64-nvidia-cuda-sycldevice simple-sycl-app.cpp -o simple-sycl-app.exe
	fi
	./simple-sycl-app.exe
}


install_packages
install_onemkl
install_dpcpp
create_env
runtime_test

OneAPI

上一篇:win10 wsl buildroot syntax error near unexpected token `x86'


下一篇:53.(c#)相关工具的使用