SlideShare ist ein Scribd-Unternehmen logo
1 von 55
Introduction to OpenCL Perhaad Mistry & Dana Schaa, Northeastern University Computer Architecture Research Lab, with Benedict R. Gaster, AMD © 2011
Instructor Notes This is a straight-forward lecture.  It introduces the OpenCL specification while building a simple vector addition program The Mona Lisa images in the slides may be misleading in that we are not actually using OpenCL images, but they were nicer to look at than diagrams of buffers It would probably be a good idea to open up the example code and walk through it along with the lecture 2 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
OpenCL Architecture OpenCL allows parallel computing on heterogeneous devices CPUs, GPUs, other processors (Cell, DSPs, etc) Provides portable accelerated code Defined in four parts Platform Model Execution Model Memory Model Programming Model (We’re going to diverge from this structure a bit) 3 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Platform Model Each OpenCL implementation (i.e. an OpenCL library from AMD, NVIDIA, etc.) defines platforms which enable the host system to interact with OpenCL-capable devices Currently each vendor supplies only a single platform per implementation OpenCL uses an “Installable Client Driver” model The goal is to allow platforms from different vendors to co-exist Current systems’ device driver model will not allow different vendors’ GPUs to run at the same time 4 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Platform Model The model consists of a host connected to one or more OpenCL devices A device is divided into one or more compute units Compute units are divided into one or more processing elements Each processing element maintains its own program counter 5 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Host/Devices The host is whatever the OpenCL library runs on   x86 CPUs for both NVIDIA and AMD Devices are processors that the library can talk to  CPUs, GPUs, and generic accelerators For AMD  All CPUs are combined into a single device (each core is a compute unit and processing element) Each GPU is a separate device 6 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Selecting a Platform This function is usually called twice The first call is used to get the number of platforms available to the implementation Space is then allocated for the platform objects The second call is used to retrieve the platform objects 7 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Selecting Devices ,[object Object],We can specify which types of devices we are interested in (e.g. all devices, CPUs only, GPUs only)  This call is performed twice as with clGetPlatformIDs The first call is to determine the number of devices, the second retrieves the device objects 8 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Contexts A context refers to the environment for managing OpenCL objects and resources To manage OpenCL programs, the following are associated with a context Devices: the things doing the execution Program objects: the program source that implements the kernels Kernels: functions that run on OpenCL devices Memory objects: data that are operated on by the device Command queues: mechanisms forinteraction with the devices Memory commands (data transfers) Kernel execution Synchronization 9 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Contexts When you create a context, you will provide a list of devices to associate with it For the rest of the OpenCL resources, you will associate them with the context as they are created Empty context Context 10 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Contexts This function creates a context given a list of devices The properties argument specifies which platform to use (if NULL, the default chosen by the vendor will be used) The function also provides a callback mechanism for reporting errors to the user  11 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Command Queues A command queue is the mechanism for the host to request that an action be performed by the device Perform a memory transfer, begin executing, etc.  A separate command queue is required for each device Commands within the queue can be synchronous or asynchronous Commands can execute in-order or out-of-order 12 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Command Queues A command queue establishes a relationship between a context and a device The command queue properties specify: If out-of-order execution of commands is allowed If profiling is enabled Profiling is done using events (discussed in a later lecture) and will create some overhead 13 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Command Queues Command queues associate a context with a device Despite the figure below, they are not a physical connection Command queues Context 14 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Memory Objects Memory objects are OpenCL data that can be moved on and off devices Objects are classified as either buffers or images Buffers Contiguous chunks of memory – stored sequentially and can be accessed directly (arrays, pointers, structs) Read/write capable Images Opaque objects (2D or 3D) Can only be accessed via read_image() and write_image() Can either be read or written in a kernel, but not both  15 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Creating buffers This function creates a buffer (cl_mem object) for the given context Images are more complex and will be covered in a later lecture The flags specify:  the combination of reading and writing allowed on the data  if the host pointer itself should be used to store the data if the data should be copied from the host pointer 16 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Memory Objects Memory objects are associated with a context They must be explicitly transferred to devices prior to execution (covered later) Uninitialized OpenCL memory objects—the original  data will be transferred later to/from these objects Context Original input/output  data (not OpenCL memory objects) 17 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Transferring Data OpenCL provides commands to transfer data to and from devices  clEnqueue{Read|Write}{Buffer|Image} Copying from the host to a device is considered writing Copying from a device to the host is reading The write command both initializes the memory object with data and places it on a device The validity of memory objects that are present on multiple devices is undefined by the OpenCL spec (i.e. are vendor specific) OpenCL calls also exist to directly map part of a memory object to a host pointer 18 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Transferring Data This command initializes the OpenCL memory object and writes data to the device associated with the command queue The command will write data from a host pointer (ptr) to the device The blocking_write parameter specifies whether or not the command should return before the data transfer is complete Events (discussed in another lecture) can specify which commands should be completed before this one runs 19 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Transferring Data Memory objects are transferred to devices by specifying an action (read or write) and a command queue The validity of memory objects that are present on multiple devices is undefined by the OpenCL spec (i.e. is vendor specific) The images are redundant here to show that they are both part of the  context (on the  host) and  physically on the device Context Images are written to a device 20 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Programs A program object is basically a collection of OpenCL kernels Can be source code (text) or precompiled binary Can also contain constant data and auxiliary functions Creating a program object requires either reading in a string (source code) or a precompiled binary To compile the program Specify which devices are targeted Program is compiled for each device  Pass in compiler flags (optional) Check for compilation errors (optional, output to screen) 21 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Programs A program object is created and compiled by providing source code or a binary file and selecting which devices to target Program Context 22 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Creating Programs This function creates a program object from strings of source code count specifies the number of strings The user must create a function to read in the source code to a string If the strings are not NULL-terminated, the lengths fields are used to specify the string lengths 23 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Compiling Programs This function compiles and links an executable from the program object for each device in the context If device_list is supplied, then only those devices are targeted Optional preprocessor, optimization, and other options can be supplied by the options argument    24 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Reporting Compile Errors If a program fails to compile, OpenCL requires the programmer to explicitly ask for compiler output A compilation failure is determined by an error value returned from clBuildProgram() Calling clGetProgramBuildInfo() with the program object and the parameter CL_PROGRAM_BUILD_STATUS returns a string with the compiler output 25 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Kernels A kernel is a function declared in a program that is executed on an OpenCL device A kernel object is a kernel function along with its associated arguments A kernel object is created from a compiled program Must explicitly associate arguments (memory objects, primitives, etc) with the kernel object 26 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Kernels Kernel objects are created from a program object by specifying the name of the kernel function Kernels Context 27 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Kernels Creates a kernel from the given program The kernel that is created is specified by a string that matches the name of the function within the program 28 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Runtime Compilation There is a high overhead for compiling programs and creating kernels  Each operation only has to be performed once (at the beginning of the program) The kernel objects can be reused any number of times by setting different arguments Read source code into an array clCreateProgramWithSource clBuildProgram  clCreateKernel  clCreateProgramWithBinary  29 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Setting Kernel Arguments Kernel arguments are set by repeated calls to clSetKernelArgs ,[object Object]
The index of the argument as it appears in the function signature, the size, and a pointer to the data
Examples:
clSetKernelArg(kernel, 0, sizeof(cl_mem), (void*)&d_iImage);
clSetKernelArg(kernel, 1, sizeof(int), (void*)&a);30 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Kernel Arguments Memory objects and individual data values can be set as kernel arguments Context Data (e.g. images) are set as kernel arguments 31 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Thread Structure Massively parallel programs are usually written so that each thread computes one part of a problem For vector addition, we will add corresponding elements from two arrays, so each thread will perform one addition If we think about the thread structure visually, the threads will usually be arranged in the same shape as the data 32 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Thread Structure Consider a simple vector addition of 16 elements 2 input buffers (A, B) and 1 output buffer (C) are required Array Indices Vector Addition: A + B = C 33 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Thread Structure Create thread structure to match the problem  1-dimensional problem in this case Thread IDs Thread structure: 14 15 12 13 10 11 8 9 6 7 4 5 2 3 0 1 Vector Addition: A + B = C 34 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Thread Structure Each thread is responsible for adding the indices corresponding to its ID Thread structure: 14 15 12 13 10 11 8 9 6 7 4 5 2 3 0 1 Vector Addition: A + B = C 35 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Thread Structure OpenCL’s thread structure is designed to be scalable Each instance of a kernel is called a work-item (though “thread” is commonly used as well) Work-items are organized as work-groups Work-groups are independent from one-another (this is where scalability comes from) An index space defines a hierarchy of work-groups and work-items 36 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Thread Structure Work-items can uniquely identify themselves based on: A global id (unique within the index space) A work-group ID and a local ID within the work-group 37 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Thread Structure API calls allow threads to identify themselves and their data Threads can determine their global ID in each dimension get_global_id(dim)  get_global_size(dim) Or they can determine their work-group ID and ID within the workgroup get_group_id(dim) get_num_groups(dim) get_local_id(dim) get_local_size(dim) get_global_id(0) = column, get_global_id(1) = row get_num_groups(0) * get_local_size(0) == get_global_size(0) 38 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Memory Model The OpenCL memory model defines the various types of memories (closely related to GPU memory hierarchy) 39 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Memory Model Memory management is explicit  Must move data from host memory to device global memory, from global memory to local memory, and back Work-groups are assigned to execute on compute-units No guaranteed communication/coherency between different work-groups (no software mechanism in the OpenCL specification) 40 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Writing a Kernel One instance of the kernel is created for each thread Kernels: Must begin with keyword __kernel Must have return type void Must declare the address space of each argument that is a memory object (next slide) Use API calls (such as get_global_id()) to determine which data a thread will work on 41 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Address Space Identifiers __global – memory allocated from global address space __constant – a special type of read-only memory __local – memory shared by a work-group __private – private per work-item memory __read_only/__write_only – used for images Kernel arguments that are memory objects must be global, local, or constant 42 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Example Kernel Simple vector addition kernel: __kernel void vecadd(__globalint* A,             __global int* B,             __global int* C) { inttid = get_global_id(0); C[tid] = A[tid] + B[tid]; } 43 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Executing the Kernel Need to set the dimensions of the index space, and (optionally) of the work-group sizes Kernels execute asynchronously from the host  clEnqueueNDRangeKernel just adds is to the queue, but doesn’t guarantee that it will start executing 44 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Executing the Kernel A thread structure defined by the index-space that is created Each thread executes the same kernel on different data Context An index space of  threads is created (dimensions match the data) 45 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Executing the Kernel A thread structure defined by the index-space that is created Each thread executes the same kernel on different data Context Each thread  executes the kernel 46 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Executing the Kernel Tells the device associated with a command queue to begin executing the specified kernel The global (index space) must be specified and the local (work-group) sizes are optionally specified A list of events can be used to specify prerequisite operations that must be complete before executing 47 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Copying Data Back The last step is to copy the data back from the device to the host Similar call as writing a buffer to a device, but data will be transferred back to the host 48 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Copying Data Back The output data is read from the device back to the host Context Copied back from GPU 49 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Releasing Resources Most OpenCL resources/objects are pointers that should be freed after they are done being used There is a clRelase{Resource} command for most OpenCL types Ex: clReleaseProgram(), clReleaseMemObject() 50 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
Error Checking OpenCL commands return error codes as negative integer values Return value of 0 indicates CL_SUCCESS Negative values indicates an error  cl.h defines meaning of each return value Note: Errors are sometimes reported asynchronously CL_DEVICE_NOT_FOUND                      	  	-1 CL_DEVICE_NOT_AVAILABLE                     	-2 CL_COMPILER_NOT_AVAILABLE                   	-3 CL_MEM_OBJECT_ALLOCATION_FAILURE 	-4 CL_OUT_OF_RESOURCES                         	-5 51 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011

Weitere ähnliche Inhalte

Was ist angesagt?

Putting a Fork in Fork (Linux Process and Memory Management)
Putting a Fork in Fork (Linux Process and Memory Management)Putting a Fork in Fork (Linux Process and Memory Management)
Putting a Fork in Fork (Linux Process and Memory Management)David Evans
 
Making a Process
Making a ProcessMaking a Process
Making a ProcessDavid Evans
 
Kqueue : Generic Event notification
Kqueue : Generic Event notificationKqueue : Generic Event notification
Kqueue : Generic Event notificationMahendra M
 
Crossing into Kernel Space
Crossing into Kernel SpaceCrossing into Kernel Space
Crossing into Kernel SpaceDavid Evans
 
Virtual Memory (Making a Process)
Virtual Memory (Making a Process)Virtual Memory (Making a Process)
Virtual Memory (Making a Process)David Evans
 
Contiki introduction I.
Contiki introduction I.Contiki introduction I.
Contiki introduction I.Dingxin Xu
 
USENIX ATC 2017 Performance Superpowers with Enhanced BPF
USENIX ATC 2017 Performance Superpowers with Enhanced BPFUSENIX ATC 2017 Performance Superpowers with Enhanced BPF
USENIX ATC 2017 Performance Superpowers with Enhanced BPFBrendan Gregg
 
Scheduling in Linux and Web Servers
Scheduling in Linux and Web ServersScheduling in Linux and Web Servers
Scheduling in Linux and Web ServersDavid Evans
 
YOW2021 Computing Performance
YOW2021 Computing PerformanceYOW2021 Computing Performance
YOW2021 Computing PerformanceBrendan Gregg
 
Linux BPF Superpowers
Linux BPF SuperpowersLinux BPF Superpowers
Linux BPF SuperpowersBrendan Gregg
 
Performance Analysis: new tools and concepts from the cloud
Performance Analysis: new tools and concepts from the cloudPerformance Analysis: new tools and concepts from the cloud
Performance Analysis: new tools and concepts from the cloudBrendan Gregg
 
Performance and predictability
Performance and predictabilityPerformance and predictability
Performance and predictabilityRichardWarburton
 
Mutual Exclusion
Mutual ExclusionMutual Exclusion
Mutual ExclusionDavid Evans
 
Zookeeper Architecture
Zookeeper ArchitectureZookeeper Architecture
Zookeeper ArchitecturePrasad Wali
 
LISA2010 visualizations
LISA2010 visualizationsLISA2010 visualizations
LISA2010 visualizationsBrendan Gregg
 
Security Monitoring with eBPF
Security Monitoring with eBPFSecurity Monitoring with eBPF
Security Monitoring with eBPFAlex Maestretti
 
Class 1: What is an Operating System?
Class 1: What is an Operating System?Class 1: What is an Operating System?
Class 1: What is an Operating System?David Evans
 

Was ist angesagt? (20)

Putting a Fork in Fork (Linux Process and Memory Management)
Putting a Fork in Fork (Linux Process and Memory Management)Putting a Fork in Fork (Linux Process and Memory Management)
Putting a Fork in Fork (Linux Process and Memory Management)
 
Making a Process
Making a ProcessMaking a Process
Making a Process
 
Kqueue : Generic Event notification
Kqueue : Generic Event notificationKqueue : Generic Event notification
Kqueue : Generic Event notification
 
Crossing into Kernel Space
Crossing into Kernel SpaceCrossing into Kernel Space
Crossing into Kernel Space
 
Virtual Memory (Making a Process)
Virtual Memory (Making a Process)Virtual Memory (Making a Process)
Virtual Memory (Making a Process)
 
Contiki introduction I.
Contiki introduction I.Contiki introduction I.
Contiki introduction I.
 
USENIX ATC 2017 Performance Superpowers with Enhanced BPF
USENIX ATC 2017 Performance Superpowers with Enhanced BPFUSENIX ATC 2017 Performance Superpowers with Enhanced BPF
USENIX ATC 2017 Performance Superpowers with Enhanced BPF
 
Scheduling in Linux and Web Servers
Scheduling in Linux and Web ServersScheduling in Linux and Web Servers
Scheduling in Linux and Web Servers
 
Scheduling
SchedulingScheduling
Scheduling
 
YOW2021 Computing Performance
YOW2021 Computing PerformanceYOW2021 Computing Performance
YOW2021 Computing Performance
 
Linux BPF Superpowers
Linux BPF SuperpowersLinux BPF Superpowers
Linux BPF Superpowers
 
Performance Analysis: new tools and concepts from the cloud
Performance Analysis: new tools and concepts from the cloudPerformance Analysis: new tools and concepts from the cloud
Performance Analysis: new tools and concepts from the cloud
 
Performance and predictability
Performance and predictabilityPerformance and predictability
Performance and predictability
 
Mutual Exclusion
Mutual ExclusionMutual Exclusion
Mutual Exclusion
 
Zookeeper Architecture
Zookeeper ArchitectureZookeeper Architecture
Zookeeper Architecture
 
LISA2010 visualizations
LISA2010 visualizationsLISA2010 visualizations
LISA2010 visualizations
 
Security Monitoring with eBPF
Security Monitoring with eBPFSecurity Monitoring with eBPF
Security Monitoring with eBPF
 
srgoc
srgocsrgoc
srgoc
 
Memory model
Memory modelMemory model
Memory model
 
Class 1: What is an Operating System?
Class 1: What is an Operating System?Class 1: What is an Operating System?
Class 1: What is an Operating System?
 

Ähnlich wie Lec02 03 opencl_intro

Survey of open source cloud architectures
Survey of open source cloud architecturesSurvey of open source cloud architectures
Survey of open source cloud architecturesabhinav vedanbhatla
 
Parallel and Distributed Computing Chapter 8
Parallel and Distributed Computing Chapter 8Parallel and Distributed Computing Chapter 8
Parallel and Distributed Computing Chapter 8AbdullahMunir32
 
Glossary of terms (assignment...)
Glossary of terms (assignment...)Glossary of terms (assignment...)
Glossary of terms (assignment...)gordonpj96
 
Glossary of terms (assignment...)
Glossary of terms (assignment...)Glossary of terms (assignment...)
Glossary of terms (assignment...)gordonpj96
 
Driver Programming Report
Driver Programming ReportDriver Programming Report
Driver Programming ReportShivek Khurana
 
Development of Signal Processing Algorithms using OpenCL for FPGA based Archi...
Development of Signal Processing Algorithms using OpenCL for FPGA based Archi...Development of Signal Processing Algorithms using OpenCL for FPGA based Archi...
Development of Signal Processing Algorithms using OpenCL for FPGA based Archi...Pradeep Singh
 
2023-02-22_Tiberti_CyberX.pdf
2023-02-22_Tiberti_CyberX.pdf2023-02-22_Tiberti_CyberX.pdf
2023-02-22_Tiberti_CyberX.pdfcifoxo
 
Hardware/Software Co-Design for Efficient Microkernel Execution
Hardware/Software Co-Design for Efficient Microkernel ExecutionHardware/Software Co-Design for Efficient Microkernel Execution
Hardware/Software Co-Design for Efficient Microkernel ExecutionMartin Děcký
 
brief intro to Linux device drivers
brief intro to Linux device driversbrief intro to Linux device drivers
brief intro to Linux device driversAlexandre Moreno
 
Open CL For Speedup Workshop
Open CL For Speedup WorkshopOpen CL For Speedup Workshop
Open CL For Speedup WorkshopOfer Rosenberg
 
LO-PHI: Low-Observable Physical Host Instrumentation for Malware Analysis
LO-PHI: Low-Observable Physical Host Instrumentation for Malware AnalysisLO-PHI: Low-Observable Physical Host Instrumentation for Malware Analysis
LO-PHI: Low-Observable Physical Host Instrumentation for Malware AnalysisPietro De Nicolao
 
EMBEDDED SYSTEMS -UNIT-III-TSK-PROF.ECE-ACET.ppt
EMBEDDED SYSTEMS -UNIT-III-TSK-PROF.ECE-ACET.pptEMBEDDED SYSTEMS -UNIT-III-TSK-PROF.ECE-ACET.ppt
EMBEDDED SYSTEMS -UNIT-III-TSK-PROF.ECE-ACET.pptswarna pasupuleti
 
EMBEDDED SYSTEMS -UNIT-III-TSK-PROF.ECE-ACET.ppt
EMBEDDED SYSTEMS -UNIT-III-TSK-PROF.ECE-ACET.pptEMBEDDED SYSTEMS -UNIT-III-TSK-PROF.ECE-ACET.ppt
EMBEDDED SYSTEMS -UNIT-III-TSK-PROF.ECE-ACET.pptswarna pasupuleti
 
Operating Systems Part I-Basics
Operating Systems Part I-BasicsOperating Systems Part I-Basics
Operating Systems Part I-BasicsAjit Nayak
 
Linux Conference Australia 2018 : Device Tree, past, present, future
Linux Conference Australia 2018 : Device Tree, past, present, futureLinux Conference Australia 2018 : Device Tree, past, present, future
Linux Conference Australia 2018 : Device Tree, past, present, futureNeil Armstrong
 
Introduction to char device driver
Introduction to char device driverIntroduction to char device driver
Introduction to char device driverVandana Salve
 
My cool new Slideshow!
My cool new Slideshow!My cool new Slideshow!
My cool new Slideshow!manishamorya
 

Ähnlich wie Lec02 03 opencl_intro (20)

Survey of open source cloud architectures
Survey of open source cloud architecturesSurvey of open source cloud architectures
Survey of open source cloud architectures
 
Parallel and Distributed Computing Chapter 8
Parallel and Distributed Computing Chapter 8Parallel and Distributed Computing Chapter 8
Parallel and Distributed Computing Chapter 8
 
Glossary of terms (assignment...)
Glossary of terms (assignment...)Glossary of terms (assignment...)
Glossary of terms (assignment...)
 
Glossary of terms (assignment...)
Glossary of terms (assignment...)Glossary of terms (assignment...)
Glossary of terms (assignment...)
 
Driver Programming Report
Driver Programming ReportDriver Programming Report
Driver Programming Report
 
Development of Signal Processing Algorithms using OpenCL for FPGA based Archi...
Development of Signal Processing Algorithms using OpenCL for FPGA based Archi...Development of Signal Processing Algorithms using OpenCL for FPGA based Archi...
Development of Signal Processing Algorithms using OpenCL for FPGA based Archi...
 
2023-02-22_Tiberti_CyberX.pdf
2023-02-22_Tiberti_CyberX.pdf2023-02-22_Tiberti_CyberX.pdf
2023-02-22_Tiberti_CyberX.pdf
 
Hardware/Software Co-Design for Efficient Microkernel Execution
Hardware/Software Co-Design for Efficient Microkernel ExecutionHardware/Software Co-Design for Efficient Microkernel Execution
Hardware/Software Co-Design for Efficient Microkernel Execution
 
brief intro to Linux device drivers
brief intro to Linux device driversbrief intro to Linux device drivers
brief intro to Linux device drivers
 
Open CL For Speedup Workshop
Open CL For Speedup WorkshopOpen CL For Speedup Workshop
Open CL For Speedup Workshop
 
Exokernel
ExokernelExokernel
Exokernel
 
LO-PHI: Low-Observable Physical Host Instrumentation for Malware Analysis
LO-PHI: Low-Observable Physical Host Instrumentation for Malware AnalysisLO-PHI: Low-Observable Physical Host Instrumentation for Malware Analysis
LO-PHI: Low-Observable Physical Host Instrumentation for Malware Analysis
 
EMBEDDED SYSTEMS -UNIT-III-TSK-PROF.ECE-ACET.ppt
EMBEDDED SYSTEMS -UNIT-III-TSK-PROF.ECE-ACET.pptEMBEDDED SYSTEMS -UNIT-III-TSK-PROF.ECE-ACET.ppt
EMBEDDED SYSTEMS -UNIT-III-TSK-PROF.ECE-ACET.ppt
 
EMBEDDED SYSTEMS -UNIT-III-TSK-PROF.ECE-ACET.ppt
EMBEDDED SYSTEMS -UNIT-III-TSK-PROF.ECE-ACET.pptEMBEDDED SYSTEMS -UNIT-III-TSK-PROF.ECE-ACET.ppt
EMBEDDED SYSTEMS -UNIT-III-TSK-PROF.ECE-ACET.ppt
 
Case study windows
Case study windowsCase study windows
Case study windows
 
Operating Systems Part I-Basics
Operating Systems Part I-BasicsOperating Systems Part I-Basics
Operating Systems Part I-Basics
 
Linux Conference Australia 2018 : Device Tree, past, present, future
Linux Conference Australia 2018 : Device Tree, past, present, futureLinux Conference Australia 2018 : Device Tree, past, present, future
Linux Conference Australia 2018 : Device Tree, past, present, future
 
Introduction to char device driver
Introduction to char device driverIntroduction to char device driver
Introduction to char device driver
 
Lec12 debugging
Lec12 debuggingLec12 debugging
Lec12 debugging
 
My cool new Slideshow!
My cool new Slideshow!My cool new Slideshow!
My cool new Slideshow!
 

Kürzlich hochgeladen

Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 

Kürzlich hochgeladen (20)

Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 

Lec02 03 opencl_intro

  • 1. Introduction to OpenCL Perhaad Mistry & Dana Schaa, Northeastern University Computer Architecture Research Lab, with Benedict R. Gaster, AMD © 2011
  • 2. Instructor Notes This is a straight-forward lecture. It introduces the OpenCL specification while building a simple vector addition program The Mona Lisa images in the slides may be misleading in that we are not actually using OpenCL images, but they were nicer to look at than diagrams of buffers It would probably be a good idea to open up the example code and walk through it along with the lecture 2 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 3. OpenCL Architecture OpenCL allows parallel computing on heterogeneous devices CPUs, GPUs, other processors (Cell, DSPs, etc) Provides portable accelerated code Defined in four parts Platform Model Execution Model Memory Model Programming Model (We’re going to diverge from this structure a bit) 3 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 4. Platform Model Each OpenCL implementation (i.e. an OpenCL library from AMD, NVIDIA, etc.) defines platforms which enable the host system to interact with OpenCL-capable devices Currently each vendor supplies only a single platform per implementation OpenCL uses an “Installable Client Driver” model The goal is to allow platforms from different vendors to co-exist Current systems’ device driver model will not allow different vendors’ GPUs to run at the same time 4 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 5. Platform Model The model consists of a host connected to one or more OpenCL devices A device is divided into one or more compute units Compute units are divided into one or more processing elements Each processing element maintains its own program counter 5 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 6. Host/Devices The host is whatever the OpenCL library runs on x86 CPUs for both NVIDIA and AMD Devices are processors that the library can talk to CPUs, GPUs, and generic accelerators For AMD All CPUs are combined into a single device (each core is a compute unit and processing element) Each GPU is a separate device 6 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 7. Selecting a Platform This function is usually called twice The first call is used to get the number of platforms available to the implementation Space is then allocated for the platform objects The second call is used to retrieve the platform objects 7 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 8.
  • 9. Contexts A context refers to the environment for managing OpenCL objects and resources To manage OpenCL programs, the following are associated with a context Devices: the things doing the execution Program objects: the program source that implements the kernels Kernels: functions that run on OpenCL devices Memory objects: data that are operated on by the device Command queues: mechanisms forinteraction with the devices Memory commands (data transfers) Kernel execution Synchronization 9 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 10. Contexts When you create a context, you will provide a list of devices to associate with it For the rest of the OpenCL resources, you will associate them with the context as they are created Empty context Context 10 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 11. Contexts This function creates a context given a list of devices The properties argument specifies which platform to use (if NULL, the default chosen by the vendor will be used) The function also provides a callback mechanism for reporting errors to the user 11 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 12. Command Queues A command queue is the mechanism for the host to request that an action be performed by the device Perform a memory transfer, begin executing, etc. A separate command queue is required for each device Commands within the queue can be synchronous or asynchronous Commands can execute in-order or out-of-order 12 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 13. Command Queues A command queue establishes a relationship between a context and a device The command queue properties specify: If out-of-order execution of commands is allowed If profiling is enabled Profiling is done using events (discussed in a later lecture) and will create some overhead 13 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 14. Command Queues Command queues associate a context with a device Despite the figure below, they are not a physical connection Command queues Context 14 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 15. Memory Objects Memory objects are OpenCL data that can be moved on and off devices Objects are classified as either buffers or images Buffers Contiguous chunks of memory – stored sequentially and can be accessed directly (arrays, pointers, structs) Read/write capable Images Opaque objects (2D or 3D) Can only be accessed via read_image() and write_image() Can either be read or written in a kernel, but not both 15 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 16. Creating buffers This function creates a buffer (cl_mem object) for the given context Images are more complex and will be covered in a later lecture The flags specify: the combination of reading and writing allowed on the data if the host pointer itself should be used to store the data if the data should be copied from the host pointer 16 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 17. Memory Objects Memory objects are associated with a context They must be explicitly transferred to devices prior to execution (covered later) Uninitialized OpenCL memory objects—the original data will be transferred later to/from these objects Context Original input/output data (not OpenCL memory objects) 17 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 18. Transferring Data OpenCL provides commands to transfer data to and from devices clEnqueue{Read|Write}{Buffer|Image} Copying from the host to a device is considered writing Copying from a device to the host is reading The write command both initializes the memory object with data and places it on a device The validity of memory objects that are present on multiple devices is undefined by the OpenCL spec (i.e. are vendor specific) OpenCL calls also exist to directly map part of a memory object to a host pointer 18 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 19. Transferring Data This command initializes the OpenCL memory object and writes data to the device associated with the command queue The command will write data from a host pointer (ptr) to the device The blocking_write parameter specifies whether or not the command should return before the data transfer is complete Events (discussed in another lecture) can specify which commands should be completed before this one runs 19 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 20. Transferring Data Memory objects are transferred to devices by specifying an action (read or write) and a command queue The validity of memory objects that are present on multiple devices is undefined by the OpenCL spec (i.e. is vendor specific) The images are redundant here to show that they are both part of the context (on the host) and physically on the device Context Images are written to a device 20 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 21. Programs A program object is basically a collection of OpenCL kernels Can be source code (text) or precompiled binary Can also contain constant data and auxiliary functions Creating a program object requires either reading in a string (source code) or a precompiled binary To compile the program Specify which devices are targeted Program is compiled for each device Pass in compiler flags (optional) Check for compilation errors (optional, output to screen) 21 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 22. Programs A program object is created and compiled by providing source code or a binary file and selecting which devices to target Program Context 22 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 23. Creating Programs This function creates a program object from strings of source code count specifies the number of strings The user must create a function to read in the source code to a string If the strings are not NULL-terminated, the lengths fields are used to specify the string lengths 23 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 24. Compiling Programs This function compiles and links an executable from the program object for each device in the context If device_list is supplied, then only those devices are targeted Optional preprocessor, optimization, and other options can be supplied by the options argument 24 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 25. Reporting Compile Errors If a program fails to compile, OpenCL requires the programmer to explicitly ask for compiler output A compilation failure is determined by an error value returned from clBuildProgram() Calling clGetProgramBuildInfo() with the program object and the parameter CL_PROGRAM_BUILD_STATUS returns a string with the compiler output 25 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 26. Kernels A kernel is a function declared in a program that is executed on an OpenCL device A kernel object is a kernel function along with its associated arguments A kernel object is created from a compiled program Must explicitly associate arguments (memory objects, primitives, etc) with the kernel object 26 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 27. Kernels Kernel objects are created from a program object by specifying the name of the kernel function Kernels Context 27 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 28. Kernels Creates a kernel from the given program The kernel that is created is specified by a string that matches the name of the function within the program 28 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 29. Runtime Compilation There is a high overhead for compiling programs and creating kernels Each operation only has to be performed once (at the beginning of the program) The kernel objects can be reused any number of times by setting different arguments Read source code into an array clCreateProgramWithSource clBuildProgram clCreateKernel clCreateProgramWithBinary 29 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 30.
  • 31. The index of the argument as it appears in the function signature, the size, and a pointer to the data
  • 34. clSetKernelArg(kernel, 1, sizeof(int), (void*)&a);30 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 35. Kernel Arguments Memory objects and individual data values can be set as kernel arguments Context Data (e.g. images) are set as kernel arguments 31 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 36. Thread Structure Massively parallel programs are usually written so that each thread computes one part of a problem For vector addition, we will add corresponding elements from two arrays, so each thread will perform one addition If we think about the thread structure visually, the threads will usually be arranged in the same shape as the data 32 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 37. Thread Structure Consider a simple vector addition of 16 elements 2 input buffers (A, B) and 1 output buffer (C) are required Array Indices Vector Addition: A + B = C 33 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 38. Thread Structure Create thread structure to match the problem 1-dimensional problem in this case Thread IDs Thread structure: 14 15 12 13 10 11 8 9 6 7 4 5 2 3 0 1 Vector Addition: A + B = C 34 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 39. Thread Structure Each thread is responsible for adding the indices corresponding to its ID Thread structure: 14 15 12 13 10 11 8 9 6 7 4 5 2 3 0 1 Vector Addition: A + B = C 35 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 40. Thread Structure OpenCL’s thread structure is designed to be scalable Each instance of a kernel is called a work-item (though “thread” is commonly used as well) Work-items are organized as work-groups Work-groups are independent from one-another (this is where scalability comes from) An index space defines a hierarchy of work-groups and work-items 36 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 41. Thread Structure Work-items can uniquely identify themselves based on: A global id (unique within the index space) A work-group ID and a local ID within the work-group 37 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 42. Thread Structure API calls allow threads to identify themselves and their data Threads can determine their global ID in each dimension get_global_id(dim) get_global_size(dim) Or they can determine their work-group ID and ID within the workgroup get_group_id(dim) get_num_groups(dim) get_local_id(dim) get_local_size(dim) get_global_id(0) = column, get_global_id(1) = row get_num_groups(0) * get_local_size(0) == get_global_size(0) 38 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 43. Memory Model The OpenCL memory model defines the various types of memories (closely related to GPU memory hierarchy) 39 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 44. Memory Model Memory management is explicit Must move data from host memory to device global memory, from global memory to local memory, and back Work-groups are assigned to execute on compute-units No guaranteed communication/coherency between different work-groups (no software mechanism in the OpenCL specification) 40 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 45. Writing a Kernel One instance of the kernel is created for each thread Kernels: Must begin with keyword __kernel Must have return type void Must declare the address space of each argument that is a memory object (next slide) Use API calls (such as get_global_id()) to determine which data a thread will work on 41 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 46. Address Space Identifiers __global – memory allocated from global address space __constant – a special type of read-only memory __local – memory shared by a work-group __private – private per work-item memory __read_only/__write_only – used for images Kernel arguments that are memory objects must be global, local, or constant 42 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 47. Example Kernel Simple vector addition kernel: __kernel void vecadd(__globalint* A, __global int* B, __global int* C) { inttid = get_global_id(0); C[tid] = A[tid] + B[tid]; } 43 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 48. Executing the Kernel Need to set the dimensions of the index space, and (optionally) of the work-group sizes Kernels execute asynchronously from the host clEnqueueNDRangeKernel just adds is to the queue, but doesn’t guarantee that it will start executing 44 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 49. Executing the Kernel A thread structure defined by the index-space that is created Each thread executes the same kernel on different data Context An index space of threads is created (dimensions match the data) 45 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 50. Executing the Kernel A thread structure defined by the index-space that is created Each thread executes the same kernel on different data Context Each thread executes the kernel 46 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 51. Executing the Kernel Tells the device associated with a command queue to begin executing the specified kernel The global (index space) must be specified and the local (work-group) sizes are optionally specified A list of events can be used to specify prerequisite operations that must be complete before executing 47 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 52. Copying Data Back The last step is to copy the data back from the device to the host Similar call as writing a buffer to a device, but data will be transferred back to the host 48 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 53. Copying Data Back The output data is read from the device back to the host Context Copied back from GPU 49 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 54. Releasing Resources Most OpenCL resources/objects are pointers that should be freed after they are done being used There is a clRelase{Resource} command for most OpenCL types Ex: clReleaseProgram(), clReleaseMemObject() 50 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 55. Error Checking OpenCL commands return error codes as negative integer values Return value of 0 indicates CL_SUCCESS Negative values indicates an error cl.h defines meaning of each return value Note: Errors are sometimes reported asynchronously CL_DEVICE_NOT_FOUND -1 CL_DEVICE_NOT_AVAILABLE -2 CL_COMPILER_NOT_AVAILABLE -3 CL_MEM_OBJECT_ALLOCATION_FAILURE -4 CL_OUT_OF_RESOURCES -5 51 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 56. Big Picture 52 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 57. Programming Model Data parallel One-to-one mapping between work-items and elements in a memory object Work-groups can be defined explicitly (like CUDA) or implicitly (specify the number of work-items and OpenCL creates the work-groups) Task parallel Kernel is executed independent of an index space Other ways to express parallelism: enqueueing multiple tasks, using device-specific vector types, etc. Synchronization Possible between items in a work-group Possible between commands in a context command queue 53 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 58. Running the Example Code A simple vector addition OpenCL program that goes along with this lecture was provided Before running, the following should appear in your .bashrc file: export LD_LIBRARY_PATH=<path to stream sdk>/lib/x86_64 To compile: Make sure that vecadd.c and vecadd.cl are in the current working directory gcc -ovecaddvecadd.c -I<path to stream sdk>/include -L<path to stream sdk>/lib/x86_64 -lOpenCL 54 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011
  • 59. Summary OpenCL provides an interface for the interaction of hosts with accelerator devices A context is created that contains all of the information and data required to execute an OpenCL program Memory objects are created that can be moved on and off devices Command queues allow the host to request operations to be performed by the device Programs and kernels contain the code that devices need to execute 55 Perhaad Mistry & Dana Schaa, Northeastern Univ Computer Architecture Research Lab, with Ben Gaster, AMD © 2011

Hinweis der Redaktion

  1. Devices can be associated with multiple contexts if desired
  2. There is another function called clCreateContextFromType(), which will create a context using all the GPUs, CPUs, etc.
  3. Even though we show images here, the example will really be working with buffers
  4. If the device were a CPU, it could execute on the memory object in-place.
  5. Since kernels are executed asynchronously but return an error value immediately, runtime errors will likely be reported later.