amd-spl runtime programming guide
DESCRIPTION
AMD-SPL Runtime Programming Guide. Jiawei. Outline. What is AMD-SPL runtime How to use AMD-SPL runtime. What is AMD-SPL Runtime. The Core of SPL. Goal. What is in SPL Runtime. Outline. What is AMD-SPL runtime How to use AMD-SPL runtime. How to use SPL Runtime. Pre-Requirements. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/1.jpg)
AMD-SPL Runtime Programming Guide
Jiawei
![Page 2: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/2.jpg)
Outline
• What is AMD-SPL runtime• How to use AMD-SPL runtime
![Page 3: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/3.jpg)
WHAT IS AMD-SPL RUNTIME
![Page 4: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/4.jpg)
The Core of SPLEncapsulation
Resource management
Workflow control
Optimization
Based on CAL
![Page 5: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/5.jpg)
Goal
Overcome limitations of Brook+
Provide friendly programming
interface for CAL
Support the development of SPL
![Page 6: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/6.jpg)
What is in SPL Runtime
SPL Runtime
Program Manageme
nt
Buffer Manageme
nt
Device Manageme
nt
![Page 7: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/7.jpg)
Outline
• What is AMD-SPL runtime• How to use AMD-SPL runtime
![Page 8: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/8.jpg)
HOW TO USE SPL RUNTIME
![Page 9: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/9.jpg)
Pre-Requirements
Windows
•Visual Studio 2005•AMD Stream SDK 1.4 beta•AMD-SPL 1.0 beta or higher
Linux •……
![Page 10: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/10.jpg)
Add Include Directories
• Add include path in VS2005– CAL: “$(CALROOT)\include\”– SPL: “$(SPLROOT)\include\”– Runtime: “$(SPLROOT)\include\core\cal”
Note: $(SPLROOT) is the root folder of SPL
![Page 11: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/11.jpg)
Add Library Directories
• Add library directories in VS2005– CAL:
• “$(CALROOT)\lib\lh32\” Vista 32bit• “$(CALROOT)\lib\lh64\” Vista 64bit• “$(CALROOT)\lib\xp32\” XP 32bit• “$(CALROOT)\lib\xp64\” XP 64bit
– SPL• “$(SPLROOT)\lib
Note: $(SPLROOT) is the root folder of SPL
![Page 12: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/12.jpg)
Add Library Dependencies
• Add additional dependencies in VS2005– CAL:
• aticalrt.lib aticalcl.lib
– SPL:• amd-spl_d.libDebug version• amd-spl.lib Release version
![Page 13: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/13.jpg)
Header and Namespaces
• Include proper header files– #include “cal.h” CAL header– #include “amdspl.h” SPL header– #include “RuntimeDefs.h” Runtime header
• Using namespaces– using namespace amdspl;– using namespace amdspl::core::cal;
![Page 14: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/14.jpg)
DEFINE THE IL KERNEL
![Page 15: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/15.jpg)
Code in IL
AMD Stream Kernel Analyzer
Generate IL from Brook+ kernel• Easier to program• Difficult to maintain and optimize
Write IL manually• Difficult to program and understand• Easier to optimize• Provide more GPU features
![Page 16: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/16.jpg)
IL Kernel Sample
kernel void k(out float o<>, float i<>, float c){ o = i + c;}
il_ps_2_0dcl_output_generic o0dcl_resource_id(0)_type(2d,unnorm)_fmtx(float)_fmty(float)_fmtz(float)_fmtw(float)dcl_input_position_interp(linear_noperspective) v0.xy__dcl_cb cb0[1]sample_resource(0)_sampler(0) r1, v0.xy00add o0, r1, cb0[0]endmainend
The Brook+ kernel equivalent:
![Page 17: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/17.jpg)
IL Source String
const char * __sample_program_src__ =
"il_ps_2_0\n""dcl_output_generic o0\n""dcl_resource_id(0)_type(2d,unnorm)_fmtx(float)_fmty(float)_fmtz(float)_fmtw(float)\n""dcl_input_position_interp(linear_noperspective) v0.xy__\n""dcl_cb cb0[1]\n""sample_resource(0)_sampler(0) r1, v0.xy00\n""add o0, r1, cb0[0]\n""endmain\n""end\n";
![Page 18: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/18.jpg)
Kernel Information
• Define the kernel using template class ProgramInfo– Kernel Parameters– ID of the Kernel– Source of the Kernel
template <int outputsT, int inputsT = 0, int constantsT = 0, bool globalsT = false> class ProgramInfo{ ProgramInfo(const char* ID, const char* source) {...}...};
![Page 19: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/19.jpg)
Define the IL Kernel in SPL
• Define a global object for the kernel
typedef ProgramInfo<1, 1, 1, false> SampleProgram;
SampleProgram sampleProgInfo = SampleProgram("Sample Program", __sample_program_src__);
![Page 20: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/20.jpg)
INITIALIZE SPL RUNTIME
![Page 21: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/21.jpg)
Initialize SPL Runtime
Get runtime instance
Get device manager
Get buffer manager
Get program manager
Runtime *runtime = Runtime::getInstance(); assert(runtime); DeviceManager *devMgr = runtime->getDeviceManager(); assert(devMgr); BufferManager *bufMgr = runtime->getBufferManager(); assert(bufMgr); ProgramManager* progMgr = runtime->getProgramManager(); assert(progMgr);
![Page 22: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/22.jpg)
Assign Device to SPL
bool r;r = devMgr->assignDevice(0);assert(r);
Assign device to device manager
The device manager will handle device initialization and destroy.
SPL cannot access device which is not assigned to it
![Page 23: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/23.jpg)
DO GPGPU COMPUTING
![Page 24: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/24.jpg)
Initialize CPU Buffer
void fillBuffer(float buf[], int size){ for (int i = 0;i < size; i++) { buf[i] = (float)i; }}float *cpuInBuf = new float[1024 * 512];float *cpuOutBuf = new float[1024 * 512];float constant = 3;
fillBuffer(cpuInBuf, 1024 * 512);
![Page 25: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/25.jpg)
Get Device
• Get the default device• Get device by ID
Device* device = devMgr->getDefaultDevice();
Device* device = devMgr->getDeviceByID(0);
OR
![Page 26: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/26.jpg)
Load Program
• Load the program using program manager– Pass in a ProgramInfo instance
Program *prog = progMgr->loadProgram(sampleProgInfo);
assert(prog);
![Page 27: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/27.jpg)
Create Buffers
• Create local buffer for input• Create remote buffer for output• Get constant buffer from constant buffer pool
Buffer* inBuf = bufMgr-> createLocalBuffer(device, CAL_FORMAT_FLOAT_1, 1024, 512);assert(inBuf); Buffer* outBuf = bufMgr->createRemoteBuffer( CAL_FORMAT_FLOAT_1, 1024, 512);assert(outBuf);ConstBuffer* constBuf = bufMgr->getConstBuffer(1);assert(constBuf);
![Page 28: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/28.jpg)
CPU to GPU Data Transfer
• Read in CPU buffer
• Set Constant
bool r;r = inBuf->readData(cpuInBuf, 1024 * 512);assert(r);
r = constBuf->setConstant<0>(&constant);assert(r);
![Page 29: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/29.jpg)
Bind Buffers
• Bind buffers to the program– Input, Output, Constant, Global
r = prog->bindOutput(outBuf, 0);assert(r);r = prog->bindInput(inBuf, 0);assert(r);r = prog->bindConstant(constBuf, 0);assert(r);
![Page 30: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/30.jpg)
Execute Program
• Define the execution domain• Run program• Check the execution event
CALdomain domain = {0, 0, 1024, 512};
Event *e = prog->run(domain);assert(e);
![Page 31: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/31.jpg)
GPU to CPU Data Transfer
• Write in CPU buffer
r = outBuf->writeData(cpuOutBuf, 1024 * 512);assert(r);
![Page 32: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/32.jpg)
RELEASE RESOURCE AND CLEAN UP
![Page 33: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/33.jpg)
Unload Program
• Destroy program object– Unbind all the buffers
• Call Program::unbindAllBuffers();
– Unload module from context
progMgr->unloadProgram(prog);
![Page 34: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/34.jpg)
Destroy/Release Buffers
• Destroy buffers– InputBuffer, OutputBuffer
• Release ConstBuffer to the pool
bufMgr->destroyBuffer(inBuf);bufMgr->destroyBuffer(outBuf);bufMgr->releaseConstBuffer(constBuf);
![Page 35: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/35.jpg)
Shutdown Runtime
• Not necessary!– Runtime will be destroy when application
exits.
Runtime::destroy();
![Page 36: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/36.jpg)
The Whole Program
#include "cal.h"#include "amdspl.h"#include "RuntimeDefs.h"
using namespace amdspl;using namespace amdspl::core::cal;
void fillBuffer(float buf[], int size){ for (int i = 0;i < size; i++) { buf[i] = (float)i; }}
![Page 37: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/37.jpg)
The Whole Program
const char *__sample_program_src__ = "il_ps_2_0\n""dcl_output_generic o0\n""dcl_resource_id(0)_type(2d,unnorm)_fmtx(float)_fmty(float)_fmtz(float)_fmtw(float)\n""dcl_input_position_interp(linear_noperspective) v0.xy__\n""dcl_cb cb0[1]\n""sample_resource(0)_sampler(0) r1, v0.xy00\n""add o0, r1, cb0[0]\n""endmain\n""end\n";
typedef ProgramInfo<1, 1, 1, false> SampleProgram;SampleProgram sampleProgInfo = SampleProgram("Sample Program", __sample_program_src__);
![Page 38: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/38.jpg)
The Whole Program
int main(void){ float *cpuInBuf = new float[1024 * 512]; float *cpuOutBuf = new float[1024 * 512]; float constant = 3; fillBuffer(cpuInBuf, 1024 * 512);
Runtime *runtime = Runtime::getInstance(); DeviceManager *devMgr = runtime->getDeviceManager(); BufferManager *bufMgr = runtime->getBufferManager(); ProgramManager* progMgr = runtime->getProgramManager(); devMgr->assignDevice(0); Device* device = devMgr->getDefaultDevice();
..........
![Page 39: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/39.jpg)
The Whole Program
...... Program *prog = progMgr->loadProgram(sampleProgInfo); Buffer* inBuf = bufMgr->createLocalBuffer(device, CAL_FORMAT_FLOAT_1, 1024, 512); Buffer* outBuf = bufMgr->createRemoteBuffer(CAL_FORMAT_FLOAT_1, 1024, 512); ConstBuffer* constBuf = bufMgr->getConstBuffer(1); inBuf->readData(cpuInBuf, 1024 * 512); constBuf->setConstant<0>(&constant); prog->bindOutput(outBuf, 0); prog->bindInput(inBuf, 0); prog->bindConstant(constBuf, 0); CALdomain domain = {0, 0, 1024, 512}; Event *e = prog->run(domain); r = outBuf->writeData(cpuOutBuf, 1024 * 512); ......
![Page 40: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/40.jpg)
The Entire Program
..... progMgr->unloadProgram(prog); bufMgr->destroyBuffer(inBuf); bufMgr->destroyBuffer(outBuf); bufMgr->releaseConstBuffer(constBuf);
Runtime::destroy();
delete [] cpuInBuf; delete [] cpuOutBuf;
return 0;}
![Page 41: AMD-SPL Runtime Programming Guide](https://reader035.vdocuments.us/reader035/viewer/2022062520/56815b21550346895dc8df54/html5/thumbnails/41.jpg)
THANK YOU!