【问题标题】:Generate a quick checksum for a large array of floats, without using any libraries?为大量浮点数生成快速校验和,而不使用任何库?
【发布时间】:2011-05-15 15:19:07
【问题描述】:

在 C 中(更具体地说,C 代表 CUDA),计算大量浮点数(比如两万个值)的校验和的最佳方法是什么,使用 printf 很容易打印,而不使用任何库?

我可以将所有值以浮点精度相加,但我担心舍入误差或饱和度或 nan/inf 值会使某些更改无法检测到。

这用于比较在相同 gpu 硬件上运行相同二进制文件之间的变量值,这仅用于调试,而不是用于安全性。

更清楚地说,如果数组中的任何浮点值发生变化时校验和的所有数字都发生变化(很有可能),那就太好了,这样校验和就可以很容易地在视觉上进行比较。

【问题讨论】:

    标签: c++ c debugging cuda checksum


    【解决方案1】:

    这正是循环冗余检查的用途。 Boost有a CRC library,网上有几十个源码实现。可能 16 位 CRC 最适合您,因为目测结果很容易。但如果您对误报有疑虑,您可能需要 32 位 CRC。

    【讨论】:

    • 如果 CRC 的代码已经可用于目标架构(在我的例子中是 CUDA Fermi),我认为这可能是正确的方法。我找到了 pycrc,它能够为 CRC 变体生成 C 代码。只需要一些实验就能找到一个可以在单个 cuda 线程中工作的。谢谢!
    • 我接受这个答案,因为我是第一个提到 CRC 的人。我在另一个答案中发布了我的实现。谢谢!
    【解决方案2】:

    如果您使用的是 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);
    }
    

    编辑:正如 cmets 中所述,这不会以任何方式创建 secure 校验和......这是一种非常简单的校验和形式,但希望它可以用于您的调试目的.

    【讨论】:

    • 此校验和不会检测到数组中任何两个值的交换(或者实际上任何值的排列)。这就是 CRC 更好的原因之一。
    • 非常正确......无论如何它都不是一个强大的校验和方法,但 OP 确实提到它是为了简单的调试而不是为了安全,并且想要做一个简单的校验和但是担心浮点舍入错误。所以这是解决这些问题的方法。
    • 你可能是对的。基本上这取决于 OP 的希望和恐惧。
    • 这与我刚刚尝试的非常接近,只是我使用的是“^=”而不是“+=”。至少可以捕获一个错误,但我仍在寻找更能激发信心的东西。谢谢!
    【解决方案3】:

    也许这对于 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();
    }
    
    #endif
    

    【讨论】:

      【解决方案4】:

      一个合适的 CRC 库可能是您最好的选择,但只是为了潜在的兴趣:您可以使用每个字节(即 *(uint8_t*)&amp; 重新解释)来索引一个表,比如 32-,而不是对值中的位进行异或运算位随机数,然后将这些表条目异或在一起。这意味着值的单个位变化会随机翻转输出中的位。如果不希望有尽可能多的查找表,则可以对已使用表中的位进行循环移位,从而获得合理的结果。它在概念上比数学哈希算法简单得多......

      【讨论】:

        【解决方案5】:

        已编辑:
        我相信最好的答案是 TonyK 和 Jason 的答案的结合。在将float* 转换为uint32_t* 之后,在缓冲区上使用32 位CRC。如果您的编译器提供了 uint32 定义,或者根据您的平台自行对其进行 typedef 定义(通常在 32 位机器上为 unsigned long,在 64 位机器上为 unsigned int)。这是good CRC explanation and implementation

        【讨论】:

        • 现在这完全是错误的。如果将 3.3 和 3.7 转换为 unsigned long,您会得到相同的结果: 3. 我认为您的意思是浮点值的位表示应该被视为 unsigned long。但这是多余的——只需计算数组中 80,000 个字节的 CRC。
        • 顺便说一句,我不是反对者......但你不能只将浮点数转换为长整数,这将导致浮点数到非浮点数的转换,即float 将被截断并四舍五入到最接近的 int 值。您必须获得一个指向存储浮点数的内存的指针,然后将该 pointer 转换为无符号 int 指针,此时取消引用指针将为您提供表示浮点值而不是浮点值本身。然后对位运行校验和。
        • 也远离指向 unsigned long 的指针,因为在 64 位平台上,根据操作系统,可能会将指针视为指向 64 位值而不是 32 位值, IEEE 浮点数是 32 位值。
        • 这就是为什么我不应该输入快速答案 - 我没有说出我的意思。我的意思是@Jason 在 cmets 中建议的内容:将指向浮点缓冲区的指针转换为 32 位无符号整数的指针。 (当我指的是 Uint32 时,我应该知道不要说“无符号长整数”。)答案已编辑。
        【解决方案6】:

        如果要在 GPU 上计算校验和,请使用 __int_as_float() 和 __float_as_int() 内部函数将浮点数视为整数。 CUDA 4.0 中包含的 Thrust 库使计算这个校验和变得容易 - 这是 minmax Thrust 示例,移植到您正在寻找的东西。

        #include <thrust/device_vector.h>
        #include <thrust/host_vector.h>
        #include <thrust/transform_reduce.h>
        #include <thrust/functional.h>
        #include <thrust/extrema.h>
        
        
        // sumAsInt contains a float, but implements a binary
        // operator that adds them as if they were ints.
        template <typename T>
        struct sumAsInt
        {
            T val;
        };
        
        // sumAsInt_unary_op is a functor that initializes a sumAsInt
        // with a given value T.
        template <typename T>
        struct sumAsInt_unary_op : public thrust::unary_function<T,T>
        {
            __host__ __device__
                sumAsInt<T> operator()(const T& x) const {
                    sumAsInt<T> result;
                    result.val = x;
                    return result;
                }
        };
        
        // sumAsInt_binary_op is a functor that accepts two sumAsInt 
        // structs and returns a new sumAsInt that contains the
        // sum of the two floats, as if they were integers.
        template <typename T>
        struct sumAsInt_binary_op : public thrust::binary_function<T,T,T>
        {
            __host__ __device__
                sumAsInt<T> operator()(const sumAsInt<T>& x, const sumAsInt<T>& y) const {
                    sumAsInt<T> result;
                    result.val = __int_as_float(__float_as_int(x.val)+__float_as_int(y.val));
                    return result;
                }
        };
        
        
        int main(void)
        {
            // initialize host array
            float x[7] = {-1, 2, 7, -3, -4, 5};
        
            int sum = 0;
            for ( int i = 0; i < sizeof(x)/sizeof(x[0]); i++ ) {
                sum += *((int *) (&x[i]));
            }
            printf( "CPU sum: %d\n", sum );
        
            // transfer to device
            thrust::device_vector<float> d_x(x, x + 7);
        
            // setup arguments
            sumAsInt_unary_op<float>  unary_op;
            sumAsInt_binary_op<float> binary_op;
            sumAsInt<float> init = unary_op(0.0f/*d_x[0]*/);  // initialize with first element
        
            // compute sum-as-int
            sumAsInt<float> result = thrust::transform_reduce(d_x.begin(), d_x.end(), unary_op, init, binary_op);
        
            printf( "GPU sum: %d\n", *((int *) (&result.val)) );
        
        //    std::cout << result.val << std::endl;
        
            return 0;
        }
        

        【讨论】:

        • 我无法让 CUDA 4 干净地安装在 debian 上,更不用说编译我们的代码了。我相信 CUDA 3 中与模板相关的编译器错误是导致我尝试调试的问题的原因;感谢您的代码,但在这一点上,我不会相信任何模板化的东西。
        • 从 SDK 移植归约样本并用 __int_as_float(__float_as_int(x)+__float_as_int(y)) 构造替换求和将非常简单。
        猜你喜欢
        • 2010-09-12
        • 2020-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-13
        相关资源
        最近更新 更多