c - 数百万 UINT64 RGBZ 图形像素的最快排序算法

标签 c algorithm performance sorting

我正在使用 .RAW 文件中的 RGB 数据对 10+ 百万个 uint64_t 进行排序,我 79% 的 C 程序时间花在了 qsort 上。我正在为这种特定数据类型寻找更快的排序。

作为 RAW 图形数据,数字非常随机并且大约 80% 是唯一的。预计不会出现部分排序或已排序数据的运行。 uint64_t 中的 4 个 uint16_t 是 R、G、B 和零(可能是一个小计数 <= ~20)。

我有我能想到的最简单的比较函数,使用 unsigned long long(你不能只减去它们):

qsort(hpidx, num_pix, sizeof(uint64_t), comp_uint64); 
...
int comp_uint64(const void *a, const void *b)  {
    if(*((uint64_t *)a) > *((uint64_t *)b))  return(+1);
    if(*((uint64_t *)a) < *((uint64_t *)b))  return(-1);
    return(0);
}  // End Comp_uint64().

StackExchange 上有一个非常有趣的“Programming Puzzles & Code Golf”,但是他们使用了float。然后就是QSort、RecQuick、heap、stooge、tree、radix...

swenson/sort 看起来很有趣,但没有(明显)支持我的数据类型 uint64_t。而“快速排序”时间是最好的。一些消息来源说系统 qsort 可以是任何东西,不一定是“快速排序”。

C++ 排序绕过了 void 指针的泛型转换,并实现了 C 的性能上的巨大改进。必须有一种优化的方法来以惊人的速度通过 64 位处理器冲击 U8。


系统/编译器信息:

我目前正在使用 GCC 和 Strawberry Perl

gcc version 4.9.2 (x86_64-posix-sjlj, built by strawberryperl.com
Intel 2700K Sandy Bridge CPU, 32GB DDR3
windows 7/64 pro

gcc -D__USE_MINGW_ANSI_STDIO -O4 -ffast-math -m64 -Ofast -march=corei7-avx -mtune=corei7 -Ic:/bin/xxHash-master -Lc:/bin/xxHash-master c:/bin/stddev.c -o c:/bin/stddev.g6.exe 

首次尝试更好的qsortQSORT()!

尝试使用 Michael Tokarev 的内联 qsort

“准备好使用”?来自 qsort.h 文档

-----------------------------
* Several ready-to-use examples:
 *
 * Sorting array of integers:
 * void int_qsort(int *arr, unsigned n) {
 * #define int_lt(a,b) ((*a)<(*b))
 *   QSORT(int, arr, n, int_lt);
--------------------------------

Change from type "int" to "uint64_t"
compile error on TYPE???
    
    c:/bin/bpbfct.c:586:8: error: expected expression before 'uint64_t'
      QSORT(uint64_t, hpidx, num_pix, islt);

我找不到一个真正的、编译的、工作的示例程序,只是用“一般概念”评论

#define QSORT_TYPE uint64_t 
#define islt(a,b) ((*a)<(*b))

uint64_t *QSORT_BASE; 
int QSORT_NELT;

hpidx=(uint64_t *) calloc(num_pix+2, sizeof(uint64_t));  // Hash . PIDX
QSORT_BASE = hpidx;
QSORT_NELT = num_pix;  // QSORT_LT is function QSORT_LT()
QSORT(uint64_t, hpidx, num_pix, islt);  
//QSORT(uint64_t *, hpidx, num_pix, QSORT_LT);  // QSORT_LT mal-defined?
//qsort(hpidx, num_pix, sizeof(uint64_t), comp_uint64); // << WORKS

“即用型”示例使用intchar *struct elt 类型。 uint64_t 不是一个类型吗?试试 long long

QSORT(long long, hpidx, num_pix, islt); 
c:/bin/bpbfct.c:586:8: error: expected expression before 'long'
 QSORT(long long, hpidx, num_pix, islt);

下一次尝试:RADIXSORT:

结果:RADIX_SORT 是 RADICAL!

  I:\br3\pf.249465>grep "Event" bb12.log | grep -i Sort       
 << 1.40 sec average
4) Time=1.411 sec    = 49.61%, Event RADIX_SORT        , hits=1
4) Time=1.396 sec    = 49.13%, Event RADIX_SORT        , hits=1
4) Time=1.392 sec    = 49.15%, Event RADIX_SORT        , hits=1
16) Time=1.414 sec    = 49.12%, Event RADIX_SORT        , hits=1

I:\br3\pf.249465>grep "Event" bb11.log | grep -i Sort 
 << 5.525 sec average  = 3.95 time slower
4) Time=5.538 sec    = 86.34%, Event QSort             , hits=1
4) Time=5.519 sec    = 79.41%, Event QSort             , hits=1
4) Time=5.519 sec    = 79.02%, Event QSort             , hits=1
4) Time=5.563 sec    = 79.49%, Event QSort             , hits=1
4) Time=5.684 sec    = 79.83%, Event QSort             , hits=1
4) Time=5.509 sec    = 79.30%, Event QSort             , hits=1

比开箱即用的任何排序 qsort 快 3.94 倍!

而且,更重要的是,有实际的、有效的代码,而不仅仅是你需要的 80% 的东西,一些专家认为你知道他们所知道的一切,并且可以填写另外 20%。

绝妙的解决方案!谢谢 Louis Ricci!

最佳答案

我会使用带有 8 位基数的基数排序。对于 64 位值,经过良好优化的基数排序将必须遍历列表 9 次(一次用于预先计算计数和偏移量,8 次用于 64 位/8 位)。 9*N时间2*N空间(使用影子数组)。

这是经过优化的基数排序的样子。

typedef union {
    struct {
        uint32_t c8[256];
        uint32_t c7[256];
        uint32_t c6[256];
        uint32_t c5[256];
        uint32_t c4[256];
        uint32_t c3[256];
        uint32_t c2[256];
        uint32_t c1[256];
    };
    uint32_t counts[256 * 8];
} rscounts_t;

uint64_t * radixSort(uint64_t * array, uint32_t size) {
    rscounts_t counts;
    memset(&counts, 0, 256 * 8 * sizeof(uint32_t));
    uint64_t * cpy = (uint64_t *)malloc(size * sizeof(uint64_t));
    uint32_t o8=0, o7=0, o6=0, o5=0, o4=0, o3=0, o2=0, o1=0;
    uint32_t t8, t7, t6, t5, t4, t3, t2, t1;
    uint32_t x;
    // calculate counts
    for(x = 0; x < size; x++) {
        t8 = array[x] & 0xff;
        t7 = (array[x] >> 8) & 0xff;
        t6 = (array[x] >> 16) & 0xff;
        t5 = (array[x] >> 24) & 0xff;
        t4 = (array[x] >> 32) & 0xff;
        t3 = (array[x] >> 40) & 0xff;
        t2 = (array[x] >> 48) & 0xff;
        t1 = (array[x] >> 56) & 0xff;
        counts.c8[t8]++;
        counts.c7[t7]++;
        counts.c6[t6]++;
        counts.c5[t5]++;
        counts.c4[t4]++;
        counts.c3[t3]++;
        counts.c2[t2]++;
        counts.c1[t1]++;
    }
    // convert counts to offsets
    for(x = 0; x < 256; x++) {
        t8 = o8 + counts.c8[x];
        t7 = o7 + counts.c7[x];
        t6 = o6 + counts.c6[x];
        t5 = o5 + counts.c5[x];
        t4 = o4 + counts.c4[x];
        t3 = o3 + counts.c3[x];
        t2 = o2 + counts.c2[x];
        t1 = o1 + counts.c1[x];
        counts.c8[x] = o8;
        counts.c7[x] = o7;
        counts.c6[x] = o6;
        counts.c5[x] = o5;
        counts.c4[x] = o4;
        counts.c3[x] = o3;
        counts.c2[x] = o2;
        counts.c1[x] = o1;
        o8 = t8; 
        o7 = t7; 
        o6 = t6; 
        o5 = t5; 
        o4 = t4; 
        o3 = t3; 
        o2 = t2; 
        o1 = t1;
    }
    // radix
    for(x = 0; x < size; x++) {
        t8 = array[x] & 0xff;
        cpy[counts.c8[t8]] = array[x];
        counts.c8[t8]++;
    }
    for(x = 0; x < size; x++) {
        t7 = (cpy[x] >> 8) & 0xff;
        array[counts.c7[t7]] = cpy[x];
        counts.c7[t7]++;
    }
    for(x = 0; x < size; x++) {
        t6 = (array[x] >> 16) & 0xff;
        cpy[counts.c6[t6]] = array[x];
        counts.c6[t6]++;
    }
    for(x = 0; x < size; x++) {
        t5 = (cpy[x] >> 24) & 0xff;
        array[counts.c5[t5]] = cpy[x];
        counts.c5[t5]++;
    }
    for(x = 0; x < size; x++) {
        t4 = (array[x] >> 32) & 0xff;
        cpy[counts.c4[t4]] = array[x];
        counts.c4[t4]++;
    }
    for(x = 0; x < size; x++) {
        t3 = (cpy[x] >> 40) & 0xff;
        array[counts.c3[t3]] = cpy[x];
        counts.c3[t3]++;
    }
    for(x = 0; x < size; x++) {
        t2 = (array[x] >> 48) & 0xff;
        cpy[counts.c2[t2]] = array[x];
        counts.c2[t2]++;
    }
    for(x = 0; x < size; x++) {
        t1 = (cpy[x] >> 56) & 0xff;
        array[counts.c1[t1]] = cpy[x];
        counts.c1[t1]++;
    }
    free(cpy);
    return array;
}

编辑 此实现基于 JavaScript 版本 Fastest way to sort 32bit signed integer arrays in JavaScript?

这是 C 基数排序的 IDEONE http://ideone.com/JHI0d9

关于c - 数百万 UINT64 RGBZ 图形像素的最快排序算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32188370/

相关文章:

asp.net - 数百个 ASP.NET MVC 路由

解析章节时的 Coredump

c - 将Node插入第一名C编程

c++ - 如何计算结构的偏移量

ruby-on-rails - 匹配具有最大相似性的 2 个哈希的有效方法

javascript - 如何格式化具有空行的 excel 文件以具有嵌套数组并匹配我需要呈现的 JSON 对象?

php - 用 PHP 向大量用户发送电子邮件的最有效方法是什么?

c - Flex 和 Bison,检测宏语句(新手)

php - 这个 PHP 广告 yield 分享逻辑有效吗?

R:加速多个 lm()