在C中(更具体地说,C代表CUDA),计算大量浮点数组(比方说两万个值)的校验和的最好方法是什么,这很容易用printf打印,而不需要使用任何库?
我可以用浮点精度对所有的值求和,但我担心舍入误差或饱和度,或者nan/inf值,会使一些变化无法检测。
它用于比较同一gpu硬件上相同二进制文件的运行之间变量的值,并且仅用于调试,而不用于安全性。
更清楚地说,当数组中的任何浮点值改变时,如果校验和的所有数字都改变(概率很高),那就更好了,这样校验和就很容易进行直观的比较。
发布于 2011-05-15 23:44:56
这正是循环冗余校验的目的所在。Boost有a CRC library,在web上有几十个源代码实现。可能16位CRC最适合您,因为查看结果很容易。但是,如果您对误报有疑虑,则可能需要32位CRC。
发布于 2011-05-16 01:27:55
如果您使用的是IEEE-754浮点数,您可以将浮点数转换为指针,然后将该指针重新解释为无符号整型指针,并以这种方式对它们求和,以避免任何浮点舍入问题。在这一点上,您基本上是在表示浮点数的实际位上创建校验和,而不是浮点值本身。
举个例子:
float array[20] = { /* initialized to some values */ };
unsigned int total = 0;
for (int i=0; i < 20; i++)
{
float* temp_float_ptr = &array[i];
unsigned int* temp_uint_ptr = (unsigned int*)temp_float_ptr;
total += (*temp_uint_ptr);
}编辑:正如评论中提到的,这不会以任何方式创建安全的校验和...这是一种非常简单的校验和形式,但希望它能用于您的调试目的。
发布于 2011-05-21 02:30:40
对于stackoverflow响应来说,这可能太大了,但这是我从pycrc的输出中拼凑而成的crc.cu文件。它包括一些在其他回复中已经提到的技术。我最信任crc版本,但是当数组应该全是零的时候,add和xor版本很方便。
/* The MIT License
Copyright (c) <year> <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */
/*
* (formerly) file crc.h
* Functions and types for CRC checks.
*
* Generated on Sun May 15 16:28:36 2011,
* by pycrc v0.7.7, http://www.tty1.net/pycrc/
* using the configuration:
* Width = 32
* Poly = 0x04c11db7
* XorIn = 0xffffffff
* ReflectIn = True
* XorOut = 0xffffffff
* ReflectOut = True
* Algorithm = table-driven
*
* , and then hacked by Drew Wagner to work in CUDA.
* NOTE: Note, most of this code was generated by the MIT license
* version of the pycrc. Accordingly, this derivative work is also
* licensed under the MIT license. This license applies ONLY to this file!
*
*****************************************************************************/
#ifndef __CRC_CU__
#define __CRC_CU__
/**
* The definition of the used algorithm.
*****************************************************************************/
#define CRC_ALGO_TABLE_DRIVEN 1
/**
* The type of the CRC values.
*
* This type must be big enough to contain at least 32 bits.
*****************************************************************************/
typedef uint32_t crc_t;
/**
* Calculate the initial crc value.
*
* \return The initial crc value.
*****************************************************************************/
__device__ crc_t crc_init(void)
{
return 0xffffffff;
}
/**
* Calculate the final crc value.
*
* \param crc The current crc value.
* \return The final crc value.
*****************************************************************************/
__device__ crc_t crc_finalize(crc_t crc)
{
return crc ^ 0xffffffff;
}
/**
* (formally) file crc.c
* Functions and types for CRC checks.
*
* Generated on Sun May 15 16:28:42 2011,
* by pycrc v0.7.7, http://www.tty1.net/pycrc/
* using the configuration:
* Width = 32
* Poly = 0x04c11db7
* XorIn = 0xffffffff
* ReflectIn = True
* XorOut = 0xffffffff
* ReflectOut = True
* Algorithm = table-driven
*****************************************************************************/
/**
* Static table used for the table_driven implementation.
*****************************************************************************/
__device__ static const crc_t crc_table[16] = {
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
};
/**
* Reflect all bits of a \a data word of \a data_len bytes.
*
* \param data The data word to be reflected.
* \param data_len The width of \a data expressed in number of bits.
* \return The reflected data.
*****************************************************************************/
__device__ crc_t crc_reflect(crc_t data, size_t data_len)
{
unsigned int i;
crc_t ret;
ret = data & 0x01;
for (i = 1; i < data_len; i++) {
data >>= 1;
ret = (ret << 1) | (data & 0x01);
}
return ret;
}
/**
* Update the crc value with new data.
*
* \param crc The current crc value.
* \param data Pointer to a buffer of \a data_len bytes.
* \param data_len Number of bytes in the \a data buffer.
* \return The updated crc value.
*****************************************************************************/
__device__ crc_t crc_update(crc_t crc, const unsigned char *data, size_t data_len)
{
unsigned int tbl_idx;
while (data_len--) {
tbl_idx = crc ^ (*data >> (0 * 4));
crc = crc_table[tbl_idx & 0x0f] ^ (crc >> 4);
tbl_idx = crc ^ (*data >> (1 * 4));
crc = crc_table[tbl_idx & 0x0f] ^ (crc >> 4);
data++;
}
return crc & 0xffffffff;
}
// Note 1: The xor and add versions below will return 0x00000000 if the vector, or array,
// is all zeros. This can be convenient, but they will NOT detect if zero values move
// around. This invariance to changes in order is especially true for the add version.
// Note 2: Calling these introduces thread synchronization! Be wary of heisenbugs!
// Note 3: The CRC version is the most principled, but is also slowest, and makes zeros arrays less obvious.
__device__ uint32_t vector_checksum_xor(const float* array, int m, uint32_t prevValue=0x00000000)
{
__syncthreads();
if(threadIdx.x==0 && blockIdx.x==0)
{
uint32_t sum = prevValue;
uint32_t * array_ptr = (uint32_t*) array;
for(int i=0; i<m; i++)
if(array_ptr[i]!=0x00000000)
sum ^= array_ptr[i];
return sum;
} else { return 0xffffffff;}
__syncthreads();
}
// Coded for m x n column major arrays with column stride lda
__device__ uint32_t array_checksum_xor(const float* A, int m, int n, int lda, uint32_t prevValue=0x00000000)
{
uint32_t sum = prevValue;
__syncthreads();
if(threadIdx.x==0 && blockIdx.x==0)
{
for(int i=0; i<n; i++)
sum = vector_checksum_xor(&A[i*lda], m, sum);
return sum;
} else { return 0xffffffff;}
__syncthreads();
}
__device__ uint32_t vector_checksum_sum(const float* array, int m, uint32_t prevValue=0x00000000)
{
__syncthreads();
if(threadIdx.x==0 && blockIdx.x==0)
{
uint32_t sum = prevValue;
uint32_t * array_ptr = (uint32_t*) array;
for(int i=0; i<m; i++)
if(array_ptr[i]!=0x00000000)
sum += array_ptr[i];
return sum;
} else { return 0xffffffff;}
__syncthreads();
}
// Coded for m x n column major arrays with column stride lda
__device__ uint32_t array_checksum_sum(const float* A, int m, int n, int lda, uint32_t prevValue=0x00000000)
{
uint32_t sum = prevValue;
__syncthreads();
if(threadIdx.x==0 && blockIdx.x==0)
{
for(int i=0; i<n; i++)
{
sum = vector_checksum_sum(&A[i*lda], m, sum);
}
return sum;
} else { return 0xffffffff;}
__syncthreads();
}
__device__ uint32_t vector_checksum_crc(const float* array, int m, uint32_t sum=0xffffffff)
{
__syncthreads();
if(threadIdx.x==0 && blockIdx.x==0)
{
const unsigned char * array_ptr = (const unsigned char*) array;
sum = crc_update(sum, array_ptr, m*sizeof(float));
sum = crc_finalize(sum);
return sum;
} else { return 0xffffffff;}
__syncthreads();
}
// Coded for m x n column major arrays with column stride lda
__device__ uint32_t array_checksum_crc(const float* A, int m, int n, int lda, uint32_t sum=0xffffffff)
{
__syncthreads();
if(threadIdx.x==0 && blockIdx.x==0)
{
for(int i=0; i<n; i++)
{
const unsigned char * array_ptr = (const unsigned char*) A;
sum = crc_update(sum, array_ptr, m*sizeof(float));
}
sum = crc_finalize(sum);
return sum;
} else { return 0xffffffff;}
__syncthreads();
}
#endifhttps://stackoverflow.com/questions/6009268
复制相似问题