opencl之code builder学习教程4

来源:互联网 发布:numbers是什么软件 编辑:程序博客网 时间:2024/06/06 20:39


以下文章中缺少图片,请在百度网盘下载word观看

https://pan.baidu.com/s/1nuI2dRV

Debugging OpenCL™ Kernels on GPU

Intel® SDK for OpenCL™ Applications delivers aseamless debugging experience that enables you to debug both CPU and GPUportions of your application simultaneously.

This chapter provides OpenCL GPU Debuggerinstallation walkthrough and demonstrates how to use the debugger to debug anOpenCL sample application.

Usually the process requires two separatemachines (host machine and target machine) todebug OpenCL kernels on the GPU (unless you have a discrete graphics card, whenhost and target machines can be the same machine physically):

·        HostMachine - The machineused for running the debugger (GDB or Microsoft Visual Studio* 2015/2017) andcontrolling the debugging process.

·        TargetMachine - The machine usedfor running the OpenCL host app and kernel under debug.

NOTE: The reason forusing two separate machines is because the GPU is hung on the OpenCL kernelwhile it is under debugging. Therefore, the UI rendering seems frozen while itwaits for the GPU to render UI/desktop.

For more information, see the followingtopics:

·        OpenCL™GPU Kernel Debugger for Windows*

·        OpenCL™GPU Kernel Debugger for Linux* Beta

OpenCL™ GPU Kernel Debugger forWindows*

This chapter describes how to install theOpenCL™ Kernel Debugger on Microsoft Windows* OS and demonstrates how to use itfor debugging an OpenCL application.

For details, see the following topics:

·        InstallingOpenCL™ Kernel Debugger for Windows*

·        DebuggingOpenCL™ Kernels

Installing OpenCL™ KernelDebugger for Windows*

This topic provides a walkthrough for the installation of theOpenCL™ GPU Debugger on a Microsoft Windows* OS.

Supported OSes andPrerequisites

OpenCL™ Kernel Debugger for Windows* can be installed and run onthe following operating systems:

·        Windows 7 (64-bit)

·        Windows 8.1 (64-bit)

·        Windows 10 (64-bit) (preferred)

Host machine

As a host machine you can use any machine with an Intel® CPUrunning a supported Windows* OS.

Target machine

A target machine is a 6th or 7th Generation Intel® Core™Processors machine running a supported Windows OS (Windows 10 is preferred)with the latest Graphics Driver is installed.

Installing OpenCL™ KernelDebugger

Installation of the OpenCL™ Kernel Debugger on Windows machinerequires installation on both host and target machines.

Installing on the Host

To install the debugger on the host machine, use the followingsteps:

1.    Launch the Intel® SDK for OpenCL™ Applications installer andensure that IntelDebugger for Heterogeneous Compute is selected in the listof components to install.

2.    Select either option you like and click Install:

3.    Check the Opentarget installer folder option. Pay attention to thelocation of the target installer (in this instance it's C:\Intel\OpenCL\debugger), youwill need it for installing on the target machine:

NOTE: Rebootis required (usually for environment variables to propagate after setup), somake sure you reboot prior to the first usage.

Installing on the Target

The target machine is the machine used for running the OpenCLhost application under debug. This is usually a different machine (not thehost), unless you have a discrete graphics card used for displaying the WindowsDesktop, in which case host and target can be the same machine. Complete thefollowing steps to install the debugger on your target machine:

1.    Copy the gen_debugger_target_*.msi installerfile from the host machine (from C:\Intel\OpenCL\debuggerbydefault) to your target machine.

2.    Launch the installer and follow the instructions:

3.    Click Finish andreboot the machine:

If you want to (remotely) debug your OpenCL kernels usingMicrosoft Visual Studio* 2015/2017, the following additional steps arerequired.

NOTE: Youcan skip these steps if you already have Visual Studio 2015/2017 installed onyour target machine.

To debug through Visual Studio 2015, do the following:

1.    Open an elevated command prompt on your target machine

2.    Create the RemoteDebugger directory: mkdirC:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Remote Debugger

3.    Copy the entire folder C:\Program Files (x86)\Microsoft Visual Studio14.0\Common7\IDE\Remote Debugger from your host machine toyour target machine (into the same directory hierarchy)

4.    Create a desktop shortcut (optional):

a.    Open C:\Program Files(x86)\Microsoft Visual Studio 14.0\Common7\IDE\Remote Debugger\x64

b.   Right-click on msvsmon.exe > Send to > Desktop (create shortcut),rename the shortcut to VS2015Remote Debugger.

If you wish to debug through Visual Studio 2017, do thefollowing:

1.    Open an elevated command prompt on your target machine

2.    Create the RemoteDebugger directory: mkdirC:\Program Files (x86)\Microsoft VisualStudio\2017\Professional\Common7\IDE\Remote Debugger

3.    Copy the entire folder C:\Program Files (x86)\Microsoft VisualStudio\2017\Professional\Common7\IDE\Remote Debugger fromyour host machine to your target machine (into the same directory hierarchy)

4.    Create a desktop shortcut (optional):

a.    Open C:\Program Files (x86)\MicrosoftVisual Studio\2017\Professional\Common7\IDE\Remote Debugger\x64

b.   Right-click on msvsmon.exe > Send to > Desktop (create shortcut),rename the shortcut to VS2017Remote Debugger.

Debugging OpenCL™ Kernels

This topic demonstrates how to use the OpenCL™GPU Kernel Debugger to debug an OpenCL sample application on a MicrosoftWindows* OS.

To debug your OpenCL™ Kernels on GPU usingOpenCL Kernel Debugger on a Microsoft Windows* OS, perform the following steps:

On the target machine, launch the RemoteDebugger:

NOTE: Make sure thatthe version of the Visual Studio Remote Debugger you run on the target machine,matches the version of the Visual Studio IDE you use on the host machine. Forexample, if you are using Visual Studio 2015 on the host, you should use VisualStudio 2015 Remote Debugger on the target machine.

On the host machine, start Microsoft VisualStudio* 2015/2017, go to Tools > Options > Code Builder > GPUKernel Debugger, set Enable Debugging to True and changethe Server field to the name of the target machine (e.g. localhost in thisexample):

Add the OpenCL kernel file to your project (ifit is not part of the project yet), and set breakpoints where needed.

Build your project and copy the executable and.cl files to the working directory on your target machine.

Configure your project for remote C++Debugging:

1.    Right click on yourproject, choose Properties, open Configuration Properties > Debugging andselect Remote Windows Debugger in the Debugger tolaunch list:

2.    Set RemoteCommand to the full path to your executable on the target machine.

3.    Set WorkingDirectory to the directory where the binary file is located.

4.    Set RemoteServer name to be the host name of your target machine.

5.    Click OK:

After the dialog is closed, click on RemoteWindows Debugger, or press F5:

If you configured the project properly, thedebugger stops execution at the breakpoints you have set:

OpenCL™ GPU Kernel Debugger forLinux* Beta

This chapter describes how to install theOpenCL™ Kernel Debugger on Linux* OS and demonstrates how to use it fordebugging an OpenCL application.

For details, see the following topics:

·        InstallingOpenCL™ Kernel Debugger for Linux*

·        DebuggingOpenCL™ Kernels

Installing OpenCL™ KernelDebugger for Linux*

This topic provides a walkthrough for the installation of theOpenCL™ GPU Debugger on a Linux* machine.

Supported OSes andPrerequisites

OpenCL™ Kernel Debugger for Linux* can be installed and run onthe following Linux* operating systems:

·        CentOS 7.2 or higher

·        Ubuntu 14.04

·        Ubuntu 16.04

NOTE: OpenCL™Kernel Debugger currently does not support a Yocto* operating system on aIntel® Processor formerly known as Broxton.

NOTE: ForCentOS users, sudo-E yum update upgrades the system from 7.1 to 7.2.

Host Machine

As a host machine you can use any machine with an Intel® CPUrunning a Linux* OS.

Target Machine

A machine with 6th or 7th Generation Intel® Core™ Processorsrunning a Linux* OS with the OpenCL™ 2.0 GPU graphics driver (SRB5.0)installed. To install the driver using the installation script, see https://software.intel.com/en-us/articles/sdk-for-opencl-gsg. Formanual downloading and installation instructions, refer to the https://software.intel.com/en-us/articles/opencl-drivers#latest_linux_driver section.

If you are building on CentOS* machine, please use make rpm-pkg insteadof makebinrpm-pkg when building the Linux kernel, and make sure to installthe Linux headers RPM.

Make sure the dkms package is installed. Use the followingcommand to install:

·        On Debian*: sudoapt-get install dkms

·        On CentOS*: sudoyum install epel-release dkms

Using Target Machine as Host

On Linux, the host and target machine can be the same machine ifyou use one of these option:

·        You have a discrete Graphics Card used for rendering the Desktopand the UI

·        You connect remotely to the host machine.

This section describes the process assuming the second option,i.e. you can connect remotely to the host machine.

So for example, you may start a remote SSH (e.g. PuTTY) fromyour laptop to your development machine (onto which the SDK is installed), andlaunch gdb, gdbserver-igfx and your host application via the SSH session.

The example presented in this section uses three PuTTY windowsand two separate machines:

·        A Windows* development machine

·        A Linux host and target machine

The Windows development machine is used for the remote SSHsessions, but you can also start a remote SSH session through a Linux machine.

Sample Application

Download the Median Filter sample, whichwill be used for the debugging demonstration.

The sample is part of the "Code Samples" suiteavailable on Code Samples page: https://software.intel.com/en-us/intel-opencl-support/code-samples.

Installing OpenCL™ KernelDebugger

To install the OpenCL™ GPU Kernel Debugger for Linux* from anIntel® SDK for OpenCL™ applications release package, follow the steps below:

1.    Unpack the installer package and launch:

1

tar zxvf intel_sdk_for_opencl_2017.0.0.2441_x64.tgz

2

cd intel_sdk_for_opencl_2017.0.0.2441_x64

2. 

3

sudo ./install.sh

3.    Accept the EULA and follow the wizard instructions.

4.    When the installation is complete, the following message isshown:

view sourceprint?

1

Step 5 of 5 | Complete

2

--------------------------------------------------------------------------------

 

3

Thank you for installing and for using the Intel(R) SDK for OpenCL(TM)

4

Applications 2017 R1 for Linux*.

 

5

--------------------------------------------------------------------------------

6

Press "Enter" key to quit:

Debugging OpenCL™ Kernels

This topic demonstrates how to use the OpenCL™ GPU KernelDebugger to debug an OpenCL sample application on a Linux* OS. The steps belowuse the "Median Filter" OpenCL sample application to show thedebugging process.

Building and Running theSample Application

Before you continue, make sure you have downloaded the MedianFilter sample application, which is used for furtherdemonstration.

Unpack the sample and build it running the make commandfrom the unpacked MedianFilter directory:

1

-bash-4.2$ cd MedianFilter/

2

-bash-4.2$ make

Run the sample:

01

-bash-4.2$ ./MedianFilter

02

Platforms (1):

 

03

    [0] Intel(R) OpenCL [Selected]

04

Devices (2):

 

05

    [0] Intel(R) HD Graphics [Selected]

06

    [1] Intel(R) Core(TM) m5-6Y57 CPU @ 1.10GHz

 

07

Input size is 4096 X 4096

08

OpenCL data alignment is 4096 bytes.

 

09

Save Image: MedianFilterInput.bmp

10

Executing OpenCL kernel...

 

11

Save Image: MedianFilterOutput.bmp

12

Executing reference...

 

13

Save Image: MedianFilterOutputReference.bmp

14

Performing verification...

 

15

Verification succeeded.

16

NDRange perf. counter time 38.953064 ms.

 

17

-bash-4.2$

If you were able to successfully run it, you will get an outputsimilar to the above, otherwise please consult the documentation for theGraphics Driver installation.

Debug Demonstration

In the demo, one physical Linux* machine is used both as thehost and target machine. The host machine (which is also the target) will beconnected remotely and three PuTTY* terminals will be used for launching gdb,gdbserver-igfx and the sample.

1.    Launch gdbserver in the terminal 1:

1

/usr/bin/gdbserver-igfx :1234 --attach 123

2.    

3.     Launchthe sample under debug in the terminal 2:

1

export IGFXDBG_OVERRIDE_CLIENT_PID=123

2

cd /tmp/intel_ocl_median_filter/MedianFilter

4. 

3

./MedianFilter

5.    

6.    Launch gdb in the terminal 3:

01

# needed for IGA (Intel Graphics Assembly) disassembly

02

#

7. 

03

source /opt/intel/opencl-sdk/gt_debugger_2016.0/bin/debuggervars.sh

04

# launch gdb in TUI mode

8. 

05

#

06

/opt/intel/opencl-sdk/gt_debugger_2016.0/bin/launch_gdb.sh --tui

9. 

07

# execute these commands on the gdb prompt

08

#

10. 

09

target remote :1234

10

# the first continue is always needed in order

11. 

11

# to reach the initial breakpoint of the kernel

12

#

12. 

13

continue

14

# disassemble the first instruction of the kernel

13. 

15

#

16

x/i $pc

Now you stopped at the first instruction of the kernel (alsocalled "the initial breakpoint") and if all went well, you should seethe following screen:

From this point you may use standard gdb commands to examinememory, set breakpoints, change run control, etc.

For example, set a breakpoint at line 52 and continue:

1

thread 2

2

break 52

 

3

continue

It should look like the following:

GDB* Cheat Sheet

Here is a GDB useful commands cheat sheet:

view sourceprint?

01

# Print/examine variables/memory

02

#

 

03

print pSrc - print the value of the pointer passed to the kernel

04

x/4xw pSrc - print 4 WORDs (32-bits, note that it's a gdb WORD) in hexadecimal form

 

05

x/4xw 0x1000 - print 4 DWORDs starting in memory address 0x1000

06

# Modify buffer's/memory contents

 

07

#

08

set pSrc[0]=0xdeadbeef - change the first element in the array to 0xdeadbeef

 

09

set *(unsigned int*)0x1000=0xdeadbeef - change the memory contents at address

10

                                        0x1000 to 0xdeadbeef

 

11

# Print the register file

12

#

 

13

info registers     - show the ARF (Architecture Register File) registers

14

info registers all - show all registers, GRF (General Register File) + ARF

 

15

p/x $r0.v8_int32[0] - show the first 32-bit of the r0 register, i.e. r0.0<8>:uw

16

set $r0.v8_int32[0]=0xdeadbeef - modify r0.0<8>:uw to 0xdeadbeef

 

17

# Breakpoints and run control

18

#

 

19

break 38         - set a breakpoint at line 38

20

continue         - continue execution (after gdb is stopped)

 

21

delete 1         - delete the first breakpoint

22

delete           - delete all breakpoints

 

23

info breakpoints - list all breakpoints

24

step             - step into function

 

25

stepi            - step one machine instruction

26

finish           - step out of current function

 

27

next             - step over current statement

28

# Thread control/info

 

29

# (you should ignore thread #0, as it's a fake/dummy thread)

30

#

 

31

info threads - print all hardware (execution unit) threads

32

               note that on Gen hardware a HW thread is a SIMD thread that

 

33

               usually runs 8/16/32 OpenCL work-items

34

thread    - print the currently thread

 

35

thread 10 - switch the active thread to thread #10

36

# Disassembly

 

37

#

38

x/2i $pc - disassemble 2 instructions from the current program counter value

For more information on GDB commands, see the GDB manual.

 

原创粉丝点击