3.3.1. Fundamental Memory Regions
3.3.1.基本内存区域
Memory in OpenCL is divided into two parts.
OpenCL中的内存分为两部分。
-
Host Memory: The memory directly available to the host. The detailed behavior of host memory is defined outside of OpenCL. Memory objects move between the Host and the devices through functions within the OpenCL API or through a shared virtual memory interface.
-
主机内存:主机直接可用的内存。主机内存的详细行为是在OpenCL之外定义的。内存对象通过OpenCL API中的功能或通过共享虚拟内存接口在主机和设备之间移动。
-
Device Memory: Memory directly available to kernels executing on OpenCL devices.
-
设备内存:可直接用于在OpenCL设备上执行的内核的内存。
Device memory consists of four named address spaces or memory regions:
设备内存由四个命名地址空间或内存区域组成:
-
Global Memory: This memory region permits read/write access to all work-items in all work-groups running on any device within a context. Work-items can read from or write to any element of a memory object. Reads and writes to global memory may be cached depending on the capabilities of the device.
-
全局内存:此内存区域允许对上下文中任何设备上运行的所有工作组中的所有工作项进行读/写访问。工作项可以读取或写入内存对象的任何元素。根据设备的功能,可以缓存对全局内存的读取和写入。
-
Constant Memory: A region of global memory that remains constant during the execution of a kernel-instance. The host allocates and initializes memory objects placed into constant memory.
-
常量内存:在内核实例执行期间保持恒定的全局内存区域。主机分配并初始化放置在恒定内存中的内存对象。
-
Local Memory: A memory region local to a work-group. This memory region can be used to allocate variables that are shared by all work-items in that work-group.
-
本地内存:工作组的本地内存区域。此内存区域可用于分配由该工作组中的所有工作项共享的变量。
-
Private Memory: A region of memory private to a work-item. Variables defined in one work-items private memory are not visible to another work-item.
-
专用内存:工作项专用的内存区域。在一个工作项专用内存中定义的变量对另一个工作项目不可见。
The memory regions and their relationship to the OpenCL Platform model are summarized below. Local and private memories are always associated with a particular device. The global and constant memories, however, are shared between all devices within a given context. An OpenCL device may include a cache to support efficient access to these shared memories.
内存区域及其与OpenCL平台模型的关系总结如下。本地存储器和专用存储器总是与特定设备相关联。然而,全局和常量存储器在给定上下文中的所有设备之间共享。OpenCL设备可以包括高速缓存以支持对这些共享存储器的有效访问。
To understand memory in OpenCL, it is important to appreciate the relationships between these named address spaces. The four named address spaces available to a device are disjoint meaning they do not overlap. This is a logical relationship, however, and an implementation may choose to let these disjoint named address spaces share physical memory.
要理解OpenCL中的内存,了解这些命名地址空间之间的关系是很重要的。设备可用的四个命名地址空间是不相交的,这意味着它们不重叠。然而,这是一种逻辑关系,实现可以选择让这些不相交的命名地址空间共享物理内存。
Programmers often need functions callable from kernels where the pointers manipulated by those functions can point to multiple named address spaces. This saves a programmer from the error-prone and wasteful practice of creating multiple copies of functions; one for each named address space. Therefore the global, local and private address spaces belong to a single generic address space. This is closely modeled after the concept of a generic address space used in the embedded C standard (ISO/IEC 9899:1999). Since they all belong to a single generic address space, the following properties are supported for pointers to named address spaces in device memory:
程序员通常需要从内核调用的函数,这些函数所操作的指针可以指向多个命名地址空间。这使程序员避免了创建多个函数副本的容易出错和浪费的做法;每个命名地址空间一个。因此,全局、本地和专用地址空间属于单个通用地址空间。这是根据嵌入式C标准(ISO/IEC 9899:1999)中使用的通用地址空间的概念紧密建模的。由于它们都属于一个通用地址空间,因此设备内存中指向命名地址空间的指针支持以下属性:
-
A pointer to the generic address space can be cast to a pointer to a global, local or private address space
-
指向通用地址空间的指针可以强制转换为指向全局、本地或专用地址空间的指针
-
A pointer to a global, local or private address space can be cast to a pointer to the generic address space.
-
指向全局、本地或专用地址空间的指针可以强制转换为指向通用地址空间的指针。
-
A pointer to a global, local or private address space can be implicitly converted to a pointer to the generic address space, but the converse is not allowed.
-
指向全局、本地或专用地址空间的指针可以隐式转换为指向通用地址空间的指针,但不允许相反。
The constant address space is disjoint from the generic address space.
常量地址空间与通用地址空间是不相交的。
The generic address space is missing before version 2.0. 2.0版本之前缺少通用地址空间。 |
The addresses of memory associated with memory objects in Global memory are not preserved between kernel instances, between a device and the host, and between devices. In this regard global memory acts as a global pool of memory objects rather than an address space. This restriction is relaxed when shared virtual memory (SVM) is used.
全局内存中与内存对象相关联的内存地址不会在内核实例之间、设备与主机之间以及设备之间保留。在这方面,全局存储器充当存储器对象的全局池,而不是地址空间。当使用共享虚拟存储器(SVM)时,这种限制被放宽。
Shared virtual memory is missing before version 2.0. 2.0版本之前缺少共享虚拟内存。 |
SVM causes addresses to be meaningful between the host and all of the devices within a context hence supporting the use of pointer based data structures in OpenCL kernels. It logically extends a portion of the global memory into the host address space giving work-items access to the host address space. On platforms with hardware support for a shared address space between the host and one or more devices, SVM may also provide a more efficient way to share data between devices and the host. Details about SVM are presented in Shared Virtual Memory.
SVM使地址在主机和上下文中的所有设备之间具有意义,从而支持在OpenCL内核中使用基于指针的数据结构。它在逻辑上将全局内存的一部分扩展到主机地址空间,使工作项能够访问主机地址空间。在硬件支持主机和一个或多个设备之间共享地址空间的平台上,SVM还可以提供在设备和主机之间共享数据的更有效的方式。有关SVM的详细信息在共享虚拟内存中介绍。
Figure 4. The named address spaces exposed in an OpenCL Platform. Global and Constant memories are shared between the one or more devices within a context, while local and private memories are associated with a single device. Each device may include an optional cache to support efficient access to their view of the global and constant address spaces.
图4。OpenCL平台中公开的命名地址空间。全局存储器和常量存储器在上下文中的一个或多个设备之间共享,而本地存储器和专用存储器与单个设备相关联。每个设备可以包括可选的高速缓存,以支持对其全局和常量地址空间的视图的有效访问。
A programmer may use the features of the memory consistency model to manage safe access to global memory from multiple work-items potentially running on one or more devices. In addition, when using shared virtual memory (SVM), the memory consistency model may also be used to ensure that host threads safely access memory locations in the shared memory region.
程序员可以使用存储器一致性模型的特征来管理从可能在一个或多个设备上运行的多个工作项对全局存储器的安全访问。此外,当使用共享虚拟存储器(SVM)时,还可以使用存储器一致性模型来确保主机线程安全地访问共享存储器区域中的存储器位置。