利用cuda opencl加速guetzli的项目地址是: https://github.com/ianhuang-777/guetzli-cuda-opencl

使用cuda来加速guetzli

找一台支持cuda的机器,并安装好GPU驱动

使用cuda-docker,而不在本机安装cuda

在启动docker时,将guetzli项目引入进去。

1
docker run -it --runtime=nvidia -v /home/liu/code/guetzli-cuda-opencl:/guetzli nvdia/cuda:9.0-devel

安装依赖

进入docker之后,需要安装几项依赖

1
2
3
apt update
apt install libpng-dev
apt install pkg-config

修改premake5.lua

premake5是一个跨平台编译系统,通过lua脚本来生成适用于各个平台的编译脚本,linux平台生成Makefile,win平台生成vcproj文件

  • Edit premake5.lua, add $(CUDA_PATH)\include to includedirs under workspace “guetzli”, add defines { "__USE_CUDA__" } and links { "cuda" } under filter "action:gmake". Then do premake5 --os=linux gmake to update the makefile.
  • Edit clguetzli/clguetzli.cl and add #define __USE_CUDA__ at first line.

编译

  • Run make and wait the binary to be created in bin/Release/guetzli.
  • mkdir -p bin/Release/clguetzli/
  • Run ./compile.sh 64 or ./compile.sh 32 to build the 64 or 32 bits ptx file, and the ptx file will be copied to bin/Release/clguetzli.

如果编译时遇到什么问题,记得再次编译前用make clean清理环境。

运行

编译成功后,会生成可执行文件在bin/Release/guetzli,找一张图片测试一下

1
bin/Release/guetzli --cuda 1.jpg 1_out.jpg

可以比较一下两张图片的清晰度和size。

如果无法使用cuda,可以使用opencl来加速guetzli

找一台支持opencl的机器

其实intel和AMD基本都支持了opencl, 所以任意一台都可以。

使用opencl-docker,而不在本机安装opencl

在启动docker时,将guetzli项目引入进去。

1
docker run -it -v /Users/liu/code/guetzli-cuda-opencl:/guetzli  pkienzle/opencl_docker bash

安装依赖

进入docker之后,需要安装几项依赖

1
2
3
apt update
apt install libpng-dev
apt install pkg-config

修改premake5.lua

premake5是一个跨平台编译系统,通过lua脚本来生成适用于各个平台的编译脚本,linux平台生成Makefile,win平台生成vcproj文件

注意:pkienzle/opencl_docker里没有opencl的include文件夹,需要从其他地方拷贝过来,
且还需要设置LIBRARY_PATH

1
2
3
mkdir -p /opt/intel/opencl-1.2-6.4.0.25/include
cp -rf guetzli/CL /opt/intel/opencl-1.2-6.4.0.25/include
export LIBRARY_PATH=$LIBRARY_PATH:/opt/intel/opencl-1.2-6.4.0.25/lib64/
  • Edit premake5.lua, add $(OPENCL_SDK_PATH)\include to includedirs under workspace “guetzli”, add defines { "__USE_OPENCL__" } and links { "**" } under filter "action:gmake". Then execute premake5 --os=linux gmake to update the makefile.
  • Edit clguetzli/clguetzli.cl and add #define __USE_OPENCL__ at first line.
  • Run make and wait the binary to be created in bin/Release/guetzli.
  • Copy clguetzli/clguetzli.cl to bin/Release/clguetzli before running.