OpenCL™规范 3.3.5.OpenCL 2.x的内存一致性模型

【OpenCL基础 · 二 】OpenCL架构 通过【OpenCL基础 · 一】因源,我们了解了OpenCL的起源和应用场景。在异构并行平台上,OpenCL可以协助开发者方便地使用计算资源,使得异构平台编程变得容易。那么如何使用这个便捷的开发工具就是开发人员需要学习的了,我们需要从宏观到细节去理解OpenCL是如何运作的,才能进行实际的代码开发工作。这篇博文将介绍OpenCL的整体框架,展示它是如何利用异构平台进行控制、资源调度及运算的。包括OpenCL平台模型、执行模型和存储器模型。 阅读详情
3.3.5. Memory Consistency Model for OpenCL 2.x
3.3.5.OpenCL 2.x的内存一致性模型

This memory consistency model is missing before version 2.0.

​此内存一致性模型在2.0版本之前丢失。

The OpenCL 2.x memory model tells programmers what they can expect from an OpenCL 2.x implementation; which memory operations are guaranteed to happen in which order and which memory values each read operation will return. The memory model tells compiler writers which restrictions they must follow when implementing compiler optimizations; which variables they can cache in registers and when they can move reads or writes around a barrier or atomic operation. The memory model also tells hardware designers about limitations on hardware optimizations; for example, when they must flush or invalidate hardware caches.

OpenCL2.x内存模型告诉程序员他们可以从OpenCL2.x实现中得到什么;保证哪些存储器操作以何种顺序发生,以及每个读取操作将返回哪些存储器值。内存模型告诉编译器编写器在实现编译器优化时必须遵循哪些限制;他们可以将哪些变量缓存在寄存器中,以及何时可以围绕栅栏或原子操作进行读取或写入。内存模型还告诉硬件设计者关于硬件优化的限制;例如,当它们必须刷新或使硬件缓存无效时。

The memory consistency model in OpenCL 2.x is based on the memory model from the ISO C11 programming language. To help make the presentation more precise and self-contained, we include modified paragraphs taken verbatim from the ISO C11 international standard. When a paragraph is taken or modified from the C11 standard, it is identified as such along with its original location in the C11 standard.

​OpenCL2.x中的内存一致性模型基于ISOC11编程语言中的内存模型。为了使演示更加精确和自成一体,我们包含了根据ISO C11国际标准逐字摘录的修改段落。当一个段落取自或修改自C11标准时,它与C11标准中的原始位置一起被识别为C11标准。

For programmers, the most intuitive model is the sequential consistency memory model. Sequential consistency interleaves the steps executed by each of the units of execution. Each access to a memory location sees the last assignment to that location in that interleaving. While sequential consistency is relatively straightforward for a programmer to reason about, implementing sequential consistency is expensive. Therefore, OpenCL 2.x implements a relaxed memory consistency model; i.e. it is possible to write programs where the loads from memory violate sequential consistency. Fortunately, if a program does not contain any races and if the program only uses atomic operations that utilize the sequentially consistent memory order (the default memory ordering for OpenCL 2.x), OpenCL programs appear to execute with sequential consistency.

对于程序员来说,最直观的模型是顺序一致性内存模型。顺序一致性交错由每个执行单元执行的步骤。对存储器位置的每次访问都会看到在该交织中对该位置的最后一次分配。虽然顺序一致性对于程序员来说是相对简单的,但实现顺序一致性是昂贵的。因此,OpenCL2.x实现了一个宽松的内存一致性模型;即,可以在从存储器加载违反顺序一致性的情况下写入程序。幸运的是,如果一个程序不包含任何种族,并且该程序只使用原子操作,这些原子操作使用顺序一致的内存顺序(OpenCL2.x的默认内存顺序),那么OpenCL程序的执行似乎具有顺序一致性。

Programmers can to some degree control how the memory model is relaxed by choosing the memory order for synchronization operations. The precise semantics of synchronization and the memory orders are formally defined in Memory Ordering Rules. Here, we give a high level description of how these memory orders apply to atomic operations on atomic objects shared between units of execution. OpenCL 2.x memory_order choices are based on those from the ISO C11 standard memory model. They are specified in certain OpenCL functions through the following enumeration constants:

​程序员可以在一定程度上通过选择同步操作的内存顺序来控制内存模型是如何放松的。同步的精确语义和内存顺序是在内存排序规则中正式定义的。在这里,我们给出了这些内存顺序如何应用于执行单元之间共享的原子对象上的原子操作的高级描述。OpenCL 2.x memory_order的选择基于ISO C11标准内存模型中的选择。它们在某些OpenCL函数中通过以下枚举常量指定:

  • memory_order_relaxed: implies no order constraints. This memory order can be used safely to increment counters that are concurrently incremented, but it doesn’t guarantee anything about the ordering with respect to operations to other memory locations. It can also be used, for example, to do ticket allocation and by expert programmers implementing lock-free algorithms.

  • memory_order_relaxed:表示没有顺序约束。这个内存顺序可以安全地用于递增同时递增的计数器,但它不能保证对其他内存位置的操作的顺序。例如,它也可以用于进行票证分配,并由实现无锁算法的专业程序员使用。

  • memory_order_acquire: A synchronization operation (fence or atomic) that has acquire semantics "acquires" side-effects from a release operation that synchronises with it: if an acquire synchronises with a release, the acquiring unit of execution will see all side-effects preceding that release (and possibly subsequent side-effects.) As part of carefully-designed protocols, programmers can use an "acquire" to safely observe the work of another unit of execution.

  • memory_order_aquire:具有获取语义的同步操作(栅栏或原子操作)从与其同步的发布操作中“acquires”副作用:如果acquire与release同步,则执行的acquire单元将看到该release之前的所有副作用(以及可能的后续副作用)。作为精心设计的协议的一部分,程序员可以使用“acquire”来安全地观察另一个执行单元的工作。

  • memory_order_release: A synchronization operation (fence or atomic operation) that has release semantics "releases" side effects to an acquire operation that synchronises with it. All side effects that precede the release are included in the release. As part of carefully-designed protocols, programmers can use a "release" to make changes made in one unit of execution visible to other units of execution.

  • memory_order_release:具有release语义的同步操作(栅栏或原子操作)将副作用“releases”给与其同步的acquire操作。release之前的所有副作用都包含在release中。作为精心设计的协议的一部分,程序员可以使用“release”使一个执行单元中所做的更改对其他执行单元可见。

In general, no acquire must always synchronise with any particular release. However, synchronisation can be forced by certain executions. See the description of Fence Operations for detailed rules for when synchronisation must occur.

​
一般来说,任何acquire都必须始终与任何特定的release同步。但是,某些执行可能会强制同步。有关何时必须进行同步的详细规则,请参阅栅栏操作的说明。

  • memory_order_acq_rel: A synchronization operation with acquire-release semantics has the properties of both the acquire and release memory orders. It is typically used to order read-modify-write operations.

  • memory_order_aq_rel:具有acquire-release语义的同步操作同时具有acquire和release内存顺序的属性。它通常用于命令读取-修改-写入操作。

  • memory_order_seq_cst: The loads and stores of each unit of execution appear to execute in program (i.e., sequenced-before) order, and the loads and stores from different units of execution appear to be simply interleaved.

  • memory_order_seq_cst:每个执行单元的加载和存储似乎是按程序顺序执行的(即在之前排序),而来自不同执行单元的装载和存储似乎只是交错的。

Regardless of which memory_order is specified, resolving constraints on memory operations across a heterogeneous platform adds considerable overhead to the execution of a program. An OpenCL platform may be able to optimize certain operations that depend on the features of the memory consistency model by restricting the scope of the memory operations. Distinct memory scopes are defined by the values of the memory_scope enumeration constant:

无论指定了哪个memory_order,在异构平台上解决内存操作的约束都会给程序的执行增加相当大的开销。OpenCL平台可能能够通过限制存储器操作的范围来优化取决于存储器一致性模型的特征的某些操作。不同的内存作用域由memory_scope枚举常量的值定义:

  • memory_scope_work_item: memory-ordering constraints only apply within the work-item [1].

  • ​memory_scope_work_item:内存排序约束仅适用于工作项[1]。

  • memory_scope_sub_group: memory-ordering constraints only apply within the sub-group.

  • memory_scope_sub_group:内存排序约束仅适用于子组内。

  • memory_scope_work_group: memory-ordering constraints only apply to work-items executing within a single work-group.

  • memory_scope_work_group:内存排序约束仅适用于在单个工作组中执行的工作项。

  • memory_scope_device: memory-ordering constraints only apply to work-items executing on a single device

  • memory_scope_device:内存排序约束仅适用于在单个设备上执行的工作项

  • memory_scope_all_svm_devices: memory-ordering constraints apply to work-items executing across multiple devices and (when using SVM) the host. A release performed with memory_scope_all_svm_devices to a buffer that does not have the CL_MEM_SVM_ATOMICS flag set will commit to at least memory_scope_device visibility, with full synchronization of the buffer at a queue synchronization point (e.g. an OpenCL event).

  • ​memory_scope_all_svm_devices:内存排序约束适用于在多个设备和(当使用svm时)主机上执行的工作项。使用memory_scope_all_svm_devices对未设置CL_MEM_SVM_ATOMICS标志的缓冲区执行的释放将至少提交到memory_scope_device可见性,并在队列同步点对缓冲区进行完全同步(例如OpenCL事件)。

  • memory_scope_all_devices: an alias for memory_scope_all_svm_devices.

  • memory_scope_all_devices:memory_scope_all_svm_devices的别名。

These memory scopes define a hierarchy of visibilities when analyzing the ordering constraints of memory operations. For example if a programmer knows that a sequence of memory operations will only be associated with a collection of work-items from a single work-group (and hence will run on a single device), the implementation is spared the overhead of managing the memory orders across other devices within the same context. This can substantially reduce overhead in a program. All memory scopes are valid when used on global memory or local memory. For local memory, all visibility is constrained to within a given work-group and scopes wider than memory_scope_work_group carry no additional meaning.

在分析内存操作的排序约束时,这些内存作用域定义了可见性的层次结构。例如,如果程序员知道存储器操作序列将仅与来自单个工作组的工作项集合相关联(因此将在单个设备上运行),则该实现省去了在同一上下文中跨其他设备管理存储器顺序的开销。这可以大大减少程序中的开销。在全局内存或本地内存上使用时,所有内存作用域都是有效的。对于本地内存,所有可见性都被限制在给定的工作组内,并且比memory_scope_work_group更宽的范围没有其他意义。

In the following subsections (leading up to OpenCL Framework), we will explain the synchronization constructs and detailed rules needed to use OpenCL’s 2.x relaxed memory models. It is important to appreciate, however, that many programs do not benefit from relaxed memory models. Even expert programmers have a difficult time using atomics and fences to write correct programs with relaxed memory models. A large number of OpenCL programs can be written using a simplified memory model. This is accomplished by following these guidelines.

​在下面的小节(通向OpenCL框架)中,我们将解释使用OpenCL的2.x宽松内存模型所需的同步结构和详细规则。然而,重要的是要认识到,许多程序并没有从松弛的内存模型中受益。即使是专业的程序员也很难使用原子和栅栏来编写具有宽松内存模型的正确程序。可以使用简化的内存模型编写大量的OpenCL程序。这是通过遵循这些准则来实现的。

  • Write programs that manage safe sharing of global memory objects through the synchronization points defined by the command-queues.

  • 编写程序,通过命令队列定义的同步点管理全局内存对象的安全共享。

  • Restrict low level synchronization inside work-groups to the work-group functions such as barrier.

  • 将工作组内部的低级别同步限制为工作组功能(如栅栏)。

  • If you want sequential consistency behavior with system allocations or fine-grain SVM buffers with atomics support, use only memory_order_seq_cst operations with the scope memory_scope_all_svm_devices.

  • 如果想要具有系统分配的顺序一致性行为或具有原子支持的细粒度SVM缓冲区,请仅对作用域memory_scope_all_SVM_devices使用memory_order_seq_cst操作。

  • If you want sequential consistency behavior when not using system allocations or fine-grain SVM buffers with atomics support, use only memory_order_seq_cst operations with the scope memory_scope_device or memory_scope_all_svm_devices.

  • 如果在不使用系统分配或支持原子的细粒度SVM缓冲区时想要顺序一致性行为,请仅对作用域memory_rope_device或memory_rope_all_SVM_devices使用memory_order_seq_cst操作。

  • Ensure your program has no races.

  • 确保项目没有比赛。

If these guidelines are followed in your OpenCL programs, you can skip the detailed rules behind the relaxed memory models and go directly to OpenCL Framework.

​如果在OpenCL程序中遵循这些准则,可以跳过宽松内存模型背后的详细规则,直接转到OpenCL框架。

4、OpenCL:内存模型、内核编写与硬件架构解析 本文详细解析了 OpenCL 的内存模型、内核编写方法以及硬件架构的权衡与应用场景。内容涵盖全局内存、本地内存等内存空间的使用,OpenCL 内核实现技巧,并通过向量加法示例展示了 C 和 C++ 实现方式。同时,文章分析了多核 CPU、SIMD/VLIW 架构和多线程架构的特点,以及异构计算的优势与应用,为开发者在不同硬件平台上实现高性能并行程序提供了指导。 阅读详情

相关推荐

1、OpenCL编程入门与实践

本博客详细介绍了OpenCL编程的基础与实践,涵盖了并行编程的发展历程、OpenCL的架构模型、编程基础以及优化技术。通过具体案例,如SAXPY、矩阵乘法、直方图计算和图像滤波等,展示了如何利用OpenCL进行高效并行计算。此外,还比较了OpenCL与其他并行编程技术(如CUDA)的优劣,以及其在不同硬件平台上的应用支持。

ol78901234的博客 135

OpenCL™规范 3.3.4.OpenCL 1.x的内存一致性模型

工作项内存具有加载/存储一致性。在工作组栅栏处,单个工作组中的工作项之间的本地内存是一致的。全局内存在工作组栅栏上的单个工作组中的工作项之间是一致的,但不能保证执行内核的不同工作组之间的内存一致性。OpenCL1.x使用了一个宽松的一致性内存模型;即,不能保证工作项可见的存储器状态在工作项的集合中始终一致。排队命令之间共享的内存对象的内存一致性在同步点强制执行。​2.0版本不赞成使用此内存一致性模型。

aggs1990的专栏 207

Python-uiautomator2保姆级安装指南:从环境配置到设备连接全流程

本文提供Python-uiautomator2的保姆级安装指南,涵盖从环境配置到设备连接的全流程。详细介绍了Python 3.6+、ADB工具等核心组件的准备,以及uiautomator2的安装和设备端服务初始化步骤,帮助开发者快速搭建Android自动化测试环境,避免常见问题。

nnn11的博客 942

OpenCL中的内存对象

为了更高效地完成计算,OpenCL内存模型中划分了具有不同特性的内存,本章节对此做简要介绍并强调使用相关函数时的注意事项。

qq_43116001的博客 1494

EXCEL实现分层自定义比例随机抽样(图+文教程)

EXCEL实现分层自定义比例随机抽样(图+文教程) 单一字段分层自定义比例随机抽样 多字段分层自定义比例随机抽样 单一字段分层自定义比例随机抽样 首先数据源如下,我们想随机抽张三的金额数据10%、李四金额20%、王五5%、哈哈15%、李六10% 首先第一步,添加辅助列,随机数 =RAND() 对数据进行高级排序 升序、降序都可以 添加辅助列...

可乐联盟 2万+

【电驱动】永磁同步电机工作原理及分类

永磁同步电机工作原理及分类

星Yeah 的博客 7361

Opencl四大模型

opencl四大模型 一、平台模型 描述了协同执行的单个处理器(宿主机)及其一个或者多个能执行Opencl代码的处理器(设备)。它定义了一个抽象的硬件模型,供编程人员用于编写能够在设备上执行Opencl C函数(kernel)。 Opencl使用一种InstallableClient Drive 模型,这样不同厂商的平台就能够在系统中共存,现在的Opencl drive模型不允许不同厂商的GPU同...

qq_41391174的博客 1034

【一】PCIe基础知识

Linux PCIe基础知识

他山之石 可以攻玉 4万+

OpenCL™规范官方指南文档目录

OpenCL™规范-CSDN博客

aggs1990的专栏 1669

U8 API接口开发踩过的坑

U8 API接口开发踩过的坑

haiyang5233233的专栏 1662

基于机器学习的网络入侵检测系统设计与实现

本文设计并实现了一个基于机器学习的网络入侵检测系统,采用随机森林模型结合KMM算法优化,使用CICIDS2017数据集进行训练。系统包含完整的数据预处理流程、特征工程、模型调优和远程部署方案。实验结果表明,该系统能有效识别多种网络攻击类型,在检测准确率和误报率方面表现优异。研究为网络安全防护提供了新的技术思路和实用工具,具有重要的应用价值。

huanghm88的专栏 323

OpenCL™规范 5.6.2. SVM分配的内存一致性

​当主机和设备能够使用SVM原子操作时(即,在CL_DEVICE_SVM_CAPABILITIES中设置CL_DEVICE_SVM_ATOMICS),这些原子操作可以用于在共享存储器区域中提供细粒度的存储器一致性。这些指针(sb1_ptr和sb2_ptr)可以传递给在这两个设备上执行的内核。clEnqueueMapBuffer和clEnqueueUnmapMemObject以及现有的内存对象访问规则确保了这些内核读取和写入的缓冲区(sb1_ptr和sb2_ptr)的一致性。

aggs1990的专栏 225

SGP4/SDP4详解

**SGP4** (Simplified General Perturbations Version 4) 和 **SDP4** (Simplified Deep-space Perturbations Version 4) 是一组**解析轨道预报模型**,用于预测地球轨道卫星的位置和速度。它们是北美防空司令部(NORAD)维护的**TLE**数据格式的标准配套算法。 简单来说:**TLE是卫星轨道的"身份证",而SGP4/SDP4是解读这张身份证的"说明书"**。

ScilogyHunter的博客 598

OpenCL™规范 3.3.7.1. 原子操作

​对于原子对象M上的原子操作A和B,其中A修改M,B取其值,如果存在memory_order_seq_cst栅栏X和Y,使得A在X之前排序,Y在B之前排序,并且X在S中在Y之前,则B观察到A的影响或M在其修改顺序中的后续修改。​对于读取原子对象M的值的原子操作B,如果存在在B之前排序的memory_order_seq_cst栅栏X,则B观察到M在总顺序S中在X之前的最后一次memory_order_seq_cest修改或M在其修改顺序中的后续修改。[C11标准,第7.17.3节,第6段,修改。

aggs1990的专栏 530

使用 MATLAB/Simulink 来构建一个基于多能互补的微电网储能系统模型

【摘要】本文详细介绍了基于MATLAB/Simulink的多能互补微电网储能系统建模方法。教程涵盖光伏发电、风力发电、电池储能和负荷模块的搭建,重点讲解了能量管理系统(EMS)的调度策略实现。通过设置传感器监测关键参数,并采用固定步长离散求解器进行仿真,分析了系统在动态工况下的响应特性,包括发电-负荷平衡、储能状态变化及系统稳定性等指标。该建模方法为多能互补微电网的设计与优化提供了有效工具,对提升可再生能源利用效率具有实践指导意义。

MHD0815的博客 191

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

OpenCL™规范 3.3.6.原子和栅栏操作概述

andfencesOpenCL2.x有许多同步操作,用于定义程序中的内存顺序约束。它们在控制一个执行单元中的内存操作(如工作项或使用SVM时的主机线程)如何对另一个单元可见方面发挥着特殊作用。OpenCL中有两种类型的同步操作;原子操作和栅栏。原子操作是不可分割的。它们要么完全发生,要么根本不发生。这些操作用于对执行单元之间的内存操作进行排序,因此使用OpenCL内存一致性模型定义的memory_order和memory_scope参数对它们进行参数化。

aggs1990的专栏 490

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

OpenCL™规范 3.3.7.5. 主机端和设备端命令

7.如果主机API调用将命令队列屏障命令C与C应该等待的事件的空列表一起排队,则在命令队列全局同步-与C一起的C之前排队的所有命令的事件。当使用来自从主机排队的不同内核实例的原子操作时,访问单个cl_mem对象中的共享位置或不相交位置,使得原子操作中的一个或多个是写入实现定义的,并且除了在同步点之外,不能保证正确的行为。当使用来自由父内核和该内核排队的任何数量的子内核组成的不同内核实例的原子操作时,根据本节前面描述的内存排序规则,可以保证对单个cl_mem对象中的共享位置或不相交位置的访问。

aggs1990的专栏 265

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain&#39;s apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath(&#39;DeepLearnToolbox&#39;));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand(&#39;state&#39;,0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W&#39;); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand(&#39;state&#39;,0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = &#39;sigm&#39;;%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, &#39;Too big error&#39;);```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand(&#39;state&#39;,0)sae = saesetup([784 100]);sae.ae{1}.activation_function = &#39;sigm&#39;;sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)&#39;)% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;;nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, &#39;Too big error&#39;);```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x&#39;,28,28,60000))/255;test_x = double(reshape(test_x&#39;,28,28,10000))/255;train_y = double(train_y&#39;);test_y = double(test_y&#39;);%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you&#39;ll get around 1.2% errorrand(&#39;state&#39;,0)cnn.layers = { struct(&#39;type&#39;, &#39;i&#39;) %input layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 6, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %sub sampling layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 12, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, &#39;Too big error&#39;);```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, &#39;Too big error&#39;);%% ex2 neural net with L2 weight decayrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex3 neural net with dropoutrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex4 neural net with sigmoid activation functionrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex5 plotting functionalityrand(&#39;state&#39;,0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = &#39;softmax&#39;; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand(&#39;state&#39;,0)nn = nnsetup([784 20 10]); nn.output = &#39;softmax&#39;; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

部署到 Adreno™ GPU

​Adreno™ 是由高通开发并用于许多 SoC 的图形处理单元(GPU)半导体 IP 核系列。Adreno™ GPU 可以加速复杂几何图形的渲染,在提供高性能图形和丰富的用户体验的同时拥有很低的功耗。TVM 使用 TVM 的原生 OpenCL 后端 和 OpenCLML 后端以支持加速 Adreno™ GPU 上的深度学习。TVM 的原生 OpenCL 后端通过结合纹理内存使用和 Adreno™ 友好布局来改进 Adreno™。

HyperAI超神经 1356

OpenCL™规范 5.3.5. 填充图像对象

如果event_wait_list为NULL,则num_events_in_wait_list必须为0。如果event_wait_list不为NULL,则event_wail_list指向的事件列表必须有效,并且num_events_in_wait_list必须大于0。如果与command_queue和image相关联的上下文不相同,或者与command_ queue和event_wait_list中的事件相关联的语境不相同,则CL_INVALID_CONTEXT。[2]描述2D图像阵列中的图像索引。

aggs1990的专栏 167

OpenCL™规范 5.3.14. 从OpenGL纹理和渲染缓冲区创建OpenCL图像对象

如果支持支持多样本二维纹理的OpenGL实现,并且支持cl_khr_gl_msaa_sharing扩展,则可以指定GL_TEXTURE_2D_MULTISAMPLE和GL_TEXTURE_2D_MULTISAMPLE_ARRAY。CL_INVALID_GL_OBJECT如果纹理不是类型与texture_target匹配的OpenGL纹理对象,如果未定义指定的纹理mipvlevel,或者如果指定的mipvlevel的宽度或高度为零,或者OpenGL纹理对象不完整。有关标志的说明,请参阅内存标志表。

aggs1990的专栏 360

OpenCL™规范 5.3.4. 读、写和复制图像对象

CL_INVALID_VALUE如果src_origin和src_origin+区域指定的2D或3D矩形区域指的是src_image之外的区域,或者如果dst_origin和dst_origin+区域规定的2D或三维矩形区域指代的是dst_image之外的区域。CL_INVALID_MIP_LEVEL如果支持CL_khr_mipmap_image扩展,并且在src_origin或dst_origin中指定的MIP级别不是对应src_image或dst_image的有效级别,

aggs1990的专栏 399

OpenCL™规范 5.3.7. 映射图像对象

函数返回后,可以重用或释放与event_wait_list关联的内存。CL_INVALID_OPERATION如果图像是用CL_MEM_HOST_WRITE_ONLY或CL_MEM_MOST_NO_ACCESS创建的,并且CL_MAP_READ设置在MAP_flags中,或者如果图像是使用CL_MEM_HOST_READ_ONLY、CL_MEM_HOST_NO_ACCESS创建并且CL_MAP_WRITE或CL_MAP_WRITE_INVALIDATE_REGION设置在map_flags中。

aggs1990的专栏 268

OpenCL™规范 5.8.6.2. 数学内部函数选项

如果指定了-cl-fp32-correct-rounded-divide-sqrt选项,并且未为设备设置CL_FP_CORRECTLY_ROUNDED_DIVIDE_SQRT,则clBuildProgram或clCompileProgram将无法为设备编译程序。如果支持双精度的可选扩展,则双精度非规范化数也可能被刷新为零。​如果设备不支持双精度,或者如果设备支持双精度但不支持非标准化双精度数字,即CL_FP_DENORM位未在CLD_double_FP_CONFIG中设置,则双精度数字将忽略此选项。

aggs1990的专栏 231

OpenCL™规范 5.3.6. 在图像和缓冲区对象之间复制

CL_INVALID_VALUE如果由dst_origin和dst_origin+区域指定的1D、2D或3D矩形区域指的是dst_image之外的区域,或者如果由src_offset和src_offset+src_cb指定的区域是指src_buffer之外的区域。如果dst_image是2D图像对象,则。CL_INVALID_MEM_OBJECT如果src_image不是有效的图像对象或dst_buffer不是有效的缓冲对象,或者如果src_image是从dst_bufper创建的1D图像缓冲对象。

aggs1990的专栏 268

OpenCL™规范 5.8.7.3. SPIR编译选项

必须指定以指示二进制文件为SPIR格式,并指定compile选项。必须用于指定描述二进制文件格式和含义的SPIR规范的版本。未能指定这些编译选项可能会导致实现定义的行为。例如,如果二进制文件如SPIR 1.2版本中所述,则。​如果支持cl_khr_spir扩展,则编译选项。

aggs1990的专栏 165

OpenCL™ C 6.7. 地址空间限定符

如果对象的类型由地址空间名称限定,则该对象将分配到指定的地址空间中。同样,对于指针,指向的类型可以通过地址空间来限定,该地址空间用信号通知指向的对象所在的地址空间。​嵌入式C规范第5.1.2节和第5.3节的大部分限制适用于OpenCL C,例如地址空间不能与返回类型、函数参数或函数类型一起使用,也不允许使用多个地址空间限定符。__global、__constant、__local、__private、global、constant、local和private名称保留用作地址空间限定符,不得用于其他用途。

aggs1990的专栏 119
上一篇: OpenCL™规范 3.3.4.OpenCL 1.x的内存一致性模型
下一篇: OpenCL™规范 3.3.6.原子和栅栏操作概述
꧁白杨树下꧂
博客等级 码龄15年 1019粉丝 209原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值