OpenCL中的half与float的转换
在kernel中使用half类型可以在牺牲一定精度的代价下来提升运算速度. 在kernel中, 可以比较方便的对half数据进行计算, 但在host上的, 对half的使用就没那么方便了. 查看cl_float的定义:typedef uint16_t cl_half __attribute__((aligned(2)));可知其本质是一个uint16_t. 所以, 如果直接拿cl_float的内存的值来使用的话, 系统会把它当做一个uint16_t来解析. 一般来说, 我们遇到最多的情况可能是在kernel中保为half, 然后把该内存数据复制到host, 然后在host中使用. 关于half和float的转换, 主要有如下几个方面值得注意.
使用vstore_half和vload_half
在OpenCL 1.1文档中是这么说的:
Loads from a pointer to a half and stores to a pointer to a half can be performed using the **vload_half, vload_halfn, vloada_halfn and vstore_half, vstore_halfn, vstorea_halfn **functions
respectively as described in section 6.11.7. The load functions read scalar or vector half values
from memory and convert them to a scalar or vector float value. The store functions take a
scalar or vector float value as input, convert it to a half scalar or vector value (with appropriate
rounding mode) and write the half scalar or vector value to memory
函数申明如下: vector类型类似
float vload_half (size_t offset, const __global half *p);
void vstore_half (float data, size_t offset, __global half *p);
可以知道, load时接受的half的内存数据, 然后vload_half会自动把他们变成float. store时接手的float数据, 然后vstore_half会自动把float数据变成half类型数据写入内存.
使用read_imageh和write_imageh
来看定义:
half4 read_imageh (image2d_t image, sampler_t sampler, int2 coord);
half4 read_imageh (image2d_t image, sampler_t sampler, float2 coord);
void write_imageh (image2d_t image, int2 coord, half4 color);
其中, 对于read_iamgeh, 其返回值与image2d_t的image_channel_data_type类型有关:
| image_channel_data_type | 返回值 |
|---|---|
| CL_UNORM_INT8,or CL_UNORM_INT16 | [0.0 , 1.0] |
| CL_SNORM_INT8, or CL_SNORM_INT16 | [-1.0, 1.0] |
| CL_HALF_FLOAT | half精度的值 |
如果image2d_t的类型定义不是表格中所表示的类型, 那么read的返回值将是undefined. 同理, write写入的iamge对象也只能是定义为表格中类型.
在host中进行float和half的转换
我们前面说到在host中, half实际是按照一个unit16_t来存储, 所以我们肯定需要一个算法或者规则来解析其内存数据, 得到我们想要的half-float值. 幸好, 我在高通的opencl sdk中找到了转换方法, 大家可去下载, 贴出代码如下:
//--------------------------------------------------------------------------------------
// File: half_float.cpp
// Desc:
//
// Author: QUALCOMM
//
// Copyright (c) 2018 QUALCOMM Technologies, Inc.
// All Rights Reserved.
// QUALCOMM Proprietary/GTDR
//--------------------------------------------------------------------------------------
#include "half_float.h"
#include <cmath>
#include <limits>
cl_half to_half(float f)
{
static const struct
{
unsigned int bit_size = 16; // total number of bits in the representation
unsigned int num_frac_bits = 10; // number of fractional (mantissa) bits
unsigned int num_exp_bits = 5; // number of (biased) exponent bits
unsigned int sign_bit = 15; // position of the sign bit
unsigned int sign_mask = 1 << 15; // mask to extract sign bit
unsigned int frac_mask = (1 << 10) - 1; // mask to extract the fractional (mantissa) bits
unsigned int exp_mask = ((1 << 5) - 1) << 10; // mask to extract the exponent bits
unsigned int e_max = (1 << (5 - 1)) - 1; // max value for the exponent
int e_min = -((1 << (5 - 1)) - 1) + 1; // min value for the exponent
unsigned int max_normal = ((((1 << (5 - 1)) - 1) + 127) << 23) | 0x7FE000; // max value that can be represented by the 16 bit float
unsigned int min_normal = ((-((1 << (5 - 1)) - 1) + 1) + 127) << 23; // min value that can be represented by the 16 bit float
unsigned int bias_diff = ((unsigned int)(((1 << (5 - 1)) - 1) - 127) << 23); // difference in bias between the float16 and float32 exponent
unsigned int frac_bits_diff = 23 - 10; // difference in number of fractional bits between float16/float32
} float16_params;
static const struct
{
unsigned int abs_value_mask = 0x7FFFFFFF; // ANDing with this value gives the abs value
unsigned int sign_bit_mask = 0x80000000; // ANDing with this value gives the sign
unsigned int e_max = 127; // max value for the exponent
unsigned int num_mantissa_bits = 23; // 23 bit mantissa on single precision floats
unsigned int mantissa_mask = 0x007FFFFF; // 23 bit mantissa on single precision floats
} float32_params;
const union
{
float f;
unsigned int bits;
} value = {f};
const unsigned int f_abs_bits = value.bits & float32_params.abs_value_mask;
const bool is_neg = value.bits & float32_params.sign_bit_mask;
const unsigned int sign = (value.bits & float32_params.sign_bit_mask) >> (float16_params.num_frac_bits + float16_params.num_exp_bits + 1);
cl_half half = 0;
if (std::isnan(value.f))
{
half = float16_params.exp_mask | float16_params.frac_mask;
}
else if (std::isinf(value.f))
{
half = is_neg ? float16_params.sign_mask | float16_params.exp_mask : float16_params.exp_mask;
}
else if (f_abs_bits > float16_params.max_normal)
{
// Clamp to max float 16 value
half = sign | (((1 << float16_params.num_exp_bits) - 1) << float16_params.num_frac_bits) | float16_params.frac_mask;
}
else if (f_abs_bits < float16_params.min_normal)
{
const unsigned int frac_bits = (f_abs_bits & float32_params.mantissa_mask) | (1 << float32_params.num_mantissa_bits);
const int nshift = float16_params.e_min + float32_params.e_max - (f_abs_bits >> float32_params.num_mantissa_bits);
const unsigned int shifted_bits = nshift < 24 ? frac_bits >> nshift : 0;
half = sign | (shifted_bits >> float16_params.frac_bits_diff);
}
else
{
half = sign | ((f_abs_bits + float16_params.bias_diff) >> float16_params.frac_bits_diff);
}
return half;
}
cl_float to_float(cl_half f)
{
static const struct {
uint16_t sign_mask = 0x8000;
uint16_t exp_mask = 0x7C00;
int exp_bias = 15;
int exp_offset = 10;
uint16_t biased_exp_max = (1 << 5) - 1;
uint16_t frac_mask = 0x03FF;
float smallest_subnormal_as_float = 5.96046448e-8f;
} float16_params;
static const struct {
int sign_offset = 31;
int exp_bias = 127;
int exp_offset = 23;
} float32_params;
const bool is_pos = (f & float16_params.sign_mask) == 0;
const uint32_t biased_exponent = (f & float16_params.exp_mask) >> float16_params.exp_offset;
const uint32_t frac = (f & float16_params.frac_mask);
const bool is_inf = biased_exponent == float16_params.biased_exp_max
&& (frac == 0);
if (is_inf)
{
return is_pos ? std::numeric_limits<float>::infinity() : -std::numeric_limits<float>::infinity();
}
const bool is_nan = biased_exponent == float16_params.biased_exp_max
&& (frac != 0);
if (is_nan)
{
return std::numeric_limits<float>::quiet_NaN();
}
const bool is_subnormal = biased_exponent == 0;
if (is_subnormal)
{
return static_cast<float>(frac) * float16_params.smallest_subnormal_as_float * (is_pos ? 1.f : -1.f);
}
const int unbiased_exp = static_cast<int>(biased_exponent) - float16_params.exp_bias;
const uint32_t biased_f32_exponent = static_cast<uint32_t>(unbiased_exp + float32_params.exp_bias);
union
{
cl_float f;
uint32_t ui;
} res = {0};
res.ui = (is_pos ? 0 : 1 << float32_params.sign_offset)
| (biased_f32_exponent << float32_params.exp_offset)
| (frac << (float32_params.exp_offset - float16_params.exp_offset));
return res.f;
}
关于转换精度
贴出 一组数据给大家感受下:
//原始float数据
11.15780, -128.9570, 6.154780, 0.9487320, -1327.1247, 256.0, 0.0, -127.597, 917.0, -1.0047
//to_half然后在to_float的数据
11.156250 -128.875000 6.152344 0.948730 -1327.000000 256.000000 0.000000 -127.562500 917.000000 -1.003906
根据文档, 在0~2048范围内的整数是可准确表示的. 然后对于浮点数, 精度大概可以形容为百分比的形式, 即如果数本身绝对值大, 那么相差的绝对值也大, 如果本身小, 相差的绝对值也小. 对于常用的图像处理来说, 精度一般是够了.
OpenCL中的half与float的转换的更多相关文章
- Java中几种常用数据类型之间转换的方法
Java中几种常用的数据类型之间转换方法: 1. short-->int 转换 exp: short shortvar=0; int intvar=0; shortvar= (short) in ...
- [ios][swift]swift中如果做基本类型的转换
在swift中如果做基本类型的转换的?比如Int -> Float(Double)Double -> 保留两位小数String -> IntDouble -> String 有 ...
- C++中数字与字符串之间的转换 scanf string总结(复习必读)
1 string的scanf读入操作 C++里面控制台输入直接使用cin操作就可以了:或者getline(istringstream,string); 字符和数字加减就是字符的ASCII码和数字直接加 ...
- 对CSS中的Position、Float属性的一些深入探讨
对CSS中的Position.Float属性的一些深入探讨 对于Position.Float我们在平时使用上可以说是使用频率非常高的两个CSS属性,对于这两个属性的使用上面可能大多数人存在一些模糊与不 ...
- SQL中CONVERT日期不同格式的转换用法
SQL中CONVERT日期不同格式的转换用法 格式: CONVERT(data_type,expression[,style]) 说明:此样式一般在时间类型(datetime,smalldatetim ...
- [转]JS中对象与字符串的互相转换
原文地址:http://www.cnblogs.com/luminji/p/3617160.html 在使用 JSON2.JS 文件的 JSON.parse(data) 方法时候,碰到了问题: thr ...
- JS中对象与字符串的互相转换
在使用 JSON2.JS 文件的 JSON.parse(data) 方法时候,碰到了问题: throw new SyntaxError('JSON.parse'); 查询资料,大概意思如下: JSON ...
- 装载:对CSS中的Position、Float属性的一些深入探讨
对CSS中的Position.Float属性的一些深入探讨 对CSS中的Position.Float属性的一些深入探讨 对于Position.Float我们在平时使用上可以说是使用频率非常高的两个 ...
- ACCESS中类型操作(限制、转换)
ACCESS如何保留两位小数 1.可以通过修改表结构中字段的“小数位数”即可. 2.可以通过“更新查询”,将所有该字段的值更新为round(字段名,2) ACCESS如何转换类型 每个函数都可以强制将 ...
随机推荐
- 整理4种Vue组件通信方式
整理4种Vue组件通信方式 重点是梳理了前两个,父子组件通信和eventBus通信,我觉得Vue文档里的说明还是有一些简易,我自己第一遍是没看明白. 父子组件的通信 非父子组件的eventBus通信 ...
- [Swift]LeetCode5. 最长回文子串 | Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- [Swift]LeetCode68. 文本左右对齐 | Text Justification
Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...
- 分布式事务之如何基于RocketMQ的事务消息特性实现分布式系统的最终一致性?
导读 在之前的文章中我们介绍了如何基于RocketMQ搭建生产级消息集群,以及2PC.3PC和TCC等与分布式事务相关的基本概念(没有读过的读者详见
- MySQL 的性能(上篇)—— SQL 执行分析
简介 文中内容均为阅读前辈的文章所整理而来,参考文章已在最后全指明 本文分为上下两篇: 上篇:MySQL 的 SQL 执行分析 下篇:MySQL 性能优化 后端开发必然会接触到数据库,数据层的优劣会影 ...
- 《HelloGitHub月刊》第 05 期
<HelloGitHub>第 05 期 兴趣是最好的老师,<HelloGitHub>就是帮你找到兴趣! 欢迎各路人士加入本项目,丰富月刊的内容,也可以直接在Issue(需要登录 ...
- leetcode — convert-sorted-list-to-binary-search-tree
import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...
- 深度学习入门实战(一):像Prisma一样算法生成梵高风格画像
本文由云+社区发表 作者:董超 导语:现在人工智能是个大热点,而人工智能离不开机器学习,机器学习中深度学习又是比较热门的方向,本系列文章就从实战出发,介绍下如何使用MXnet进行深度学习~ 既然是实战 ...
- 【MongoDB】MongoDB环境配置
软件下载与安装 1.mongDB下载,可到官网下载,我用的是3.4.6版本.可以放到任意目录下,我的MongDB安装目录为 D:\software\small_softeware\MongoDB 2. ...
- Java基础之 数组详解
前言:Java内功心法之数组详解,看完这篇你向Java大神的路上又迈出了一步(有什么问题或者需要资料可以联系我的扣扣:734999078) 数组概念 同一种类型数据的集合.其实数组就是一个容器. 数组 ...