【问题标题】:Hamming weight based indexing基于汉明权重的索引
【发布时间】:2012-11-12 15:04:17
【问题描述】:

假设我们有一个整数bitsize n=4;
我正在描述的问题是如何根据汉明权重和知道bitsize 的值将数字索引到数组位置。例如。 对于位大小为 4 的具有 16 个元素的数组将/可能如下所示:

|0|1|2|4|8|3|5|6|9|10|12|7|11|13|14|15|

元素按其汉明权重分组(必要)并根据大小排序(非必要)。 只要你可以采取例如排序是不必要的3(0011) 执行一些操作并返回索引 5、5(0101) -> 6 等。

n 位的所有组合都将存在并且不会有重复。例如。 3 的位大小将具有数组:

|0|1|2|4|3|5|6|7|

我最好有一个没有循环的解决方案。 或任何讨论类似解决方案的论文。 或者最后只是抛出任何关于如何去做的想法。

【问题讨论】:

  • 我不确定我是否理解您要完成的工作。在 CUDA 中,您可以使用 __popc() 内在函数确定整数的汉明权重,该内在函数在较新的 GPU 上作为快速硬件指令实现,否则映射到高效的软件仿真。然后,您可以使用 __popc() 的输出来索引表示您的查找表的数组。
  • 事情就是现在,我正在到处拉内存,也就是随机访问。我想按他们的 popcount 对内存进行分组,因为我只为每个内核和以下内核的某个 popcount (_popc(x) <= z) 请求内存。因此,通过拥有一个从 popcount 转换而来的函数 f,并且 value 为我提供了数组中的索引,我不会受到随机访问的惩罚。您描述的查找表还需要一个函数来获取值并返回索引,这是同样的问题。我正在使用大型数组,认为 2^n 其中 n > 20,因此空间受到限制。
  • 转换n <-> index是否需要快速?
  • 越快越好,但“慢”也可以,它应该避免任何内存操作并以算术方式进行。我想我有办法,但需要先吃饭。
  • 嗯,我的想法是 1. 给定宽度的整数数量,设置的位数更少。 2. 计算有多少个具有相同位数的较小数字存在, 3. 相加。 a 1 或 2 的 O((log width)^a) 不记得 OTTOMH,可能对您的目的来说太慢了。

标签: c sorting cuda indexing hammingweight


【解决方案1】:

请注意,您可以使用以下函数枚举具有相同汉明权重的数字(按计数顺序):

int next(int n) { // get the next one with same # of bits set
  int lo = n & -n;       // lowest one bit
  int lz = (n + lo) & ~n;      // lowest zero bit above lo
  n |= lz;                     // add lz to the set
  n &= ~(lz - 1);              // reset bits below lz
  n |= (lz / lo / 2) - 1;      // put back right number of bits at end
  return n;
}

int prev(int n) { // get the prev one with same # of bits set
   int y = ~n;
   y &= -y; // lowest zero bit
   n &= ~(y-1); // reset all bits below y
   int z = n & -n; // lowest set bit
   n &= ~z;        // clear z bit
   n |= (z - z / (2*y)); // add requried number of bits below z
   return n;
 }

例如,在 x = 5678 上重复应用 prev():

0: 00000001011000101110 (5678)
1: 00000001011000101101 (5677)
2: 00000001011000101011 (5675)
3: 00000001011000100111 (5671)
4: 00000001011000011110 (5662)
5: 00000001011000011101 (5661)
6: 00000001011000011011 (5659)
.....

因此理论上您可以通过重复应用 这。但是,这可能需要很长时间。更好的方法是“跳过”某些组合。

有两条规则:

 1. if the number starts with: ..XXX10..01..1 we can replace it by ..XXX0..01..1
adding corresponding number of combinations
 2. if the number starts with: ..XXX1..10..0 again replace it by XXX0..01..1 with corresponding number of combinations 

以下算法计算具有相同汉明权重的数字中的数字的索引(我不关心二项式的快速实现):

#define LOG2(x) (__builtin_ffs(x)-1)

int C(int n, int k) { // simple implementation of binomial
 int c = n - k; 
 if(k < c) 
   std::swap(k,c);
 if(c == 0)
  return 1;
 if(k == n-1) 
  return n;
 int b = k+1;
 for(int i = k+2; i <= n; i++) 
    b = b*i;
 for(int i = 2; i <= c; i++)
   b = b / i;
 return b;
}
int position_jumping(unsigned x) {
   int index = 0;
  while(1) {

    if(x & 1) { // rule 1: x is of the form: ..XXX10..01..1
        unsigned y = ~x;
        unsigned lo = y & -y; // lowest zero bit
        unsigned xz = x & ~(lo-1); // reset all bits below lo
        unsigned lz = xz & -xz; // lowest one bit after lo
        if(lz == 0) // we are in the first position!
           return index;

        int nn = LOG2(lz), kk = LOG2(lo)+1;       
        index += C(nn, kk); //   C(n-1,k) where n = log lz and k = log lo + 1

        x &= ~lz; //! clear lz bit
        x |= lo; //! add lo

    } else { // rule 2: x is of the form: ..XXX1..10..0
        int lo = x & -x; // lowest set bit
        int lz = (x + lo) & ~x;  // lowest zero bit above lo  
        x &= ~(lz-1); // clear all bits below lz
        int sh = lz / lo;

        if(lz == 0) // special case meaning that lo is in the last position
            sh=((1<<31) / lo)*2;
        x |= sh-1;

        int nn = LOG2(lz), kk = LOG2(sh);
        if(nn == 0)
           nn = 32;
        index += C(nn, kk);
    }
    std::cout << "x: " << std::bitset<20>(x).to_string() << "; pos: " << index << "\n";
  }
 }

例如,给定数字 x=5678 该算法将在 4 次迭代中计算其索引:

  x: 00000001011000100111; pos: 4
  x: 00000001011000001111; pos: 9
  x: 00000001010000011111; pos: 135
  x: 00000001000000111111; pos: 345
  x: 00000000000001111111; pos: 1137

请注意,1137 是 5678 在具有相同汉明权重的数字组中的位置。因此,您必须相应地移动该指数以考虑所有具有较小汉明权重的数字

【讨论】:

  • 有趣。稍后将不得不查看它,但看起来很有希望。
  • @ks6g10:对于二项式的计算,我建议您使用帕斯卡三角形。您可以在共享内存中的 gpu 上快速计算它。例如。对于 24 位数字,您需要 24*24 字的共享空间,这是负担得起的。或者,您可以只在主机上准备表并将其加载到常量内存中。请注意,@cctan 的解决方案很好,但它使用了大量的先验数学,这在 gpu 上可能不是很快
  • @asm 你的答案很好,但没有多少人支持,这真的很奇怪。我希望我能对此 +2。
  • 感谢@cctan,感谢。人们通常不会投票给他们不理解或不受欢迎的东西,即与 iphone 或 C# 或类似的东西无关;)
  • @asm 最近真的很忙。但是目前我的应用程序受到带宽的限制,所以我可以负担得起大量使用指令,只要我不超过用于 100% 利用率的寄存器数量。提到的表格,你的意思是像stackoverflow.com/questions/3959211/…这里的答案。还是更多“从 12 个 (P[2][12]) 中选择 2 个”表?
【解决方案2】:

这是一个概念作品,只是为了开始讨论。
第一步是最难的 - 使用近似值来计算阶乘。
还有什么好主意吗?

Ideone link

#include <stdio.h>
#include <math.h>

//gamma function using Lanczos approximation formula
//output result in log base e
//use exp() to convert back
//has a nice side effect: can store large values in small [power of e] form
double logGamma(double x)
{
    double tmp = (x-0.5) * log(x+4.5) - (x+4.5);
    double ser = 1.0 + 76.18009173     / (x+0) - 86.50532033    / (x+1)
                     + 24.01409822     / (x+2) -  1.231739516   / (x+3)
                     +  0.00120858003  / (x+4) -  0.00000536382 / (x+5);
    return tmp + log(ser * sqrt(2*M_PI) );  
}

//result from logGamma() are actually (n-1)!
double combination(int n, int r)
{
    return exp(logGamma(n+1)-( logGamma(r+1) + logGamma(n-r+1) ));
}

//primitive hamming weight counter
int hWeight(int x)
{
    int count, y;
    for (count=0, y=x; y; count++)
        y &= y-1; 
    return count;
}

//-------------------------------------------------------------------------------------
//recursively find the previous group's "hamming weight member count" and sum them
int rCummGroupCount(int bitsize, int hw)
{
    if (hw <= 0 || hw == bitsize) 
        return 1;
    else
        return round(combination(bitsize, hw)) + rCummGroupCount(bitsize,hw-1);
}
//-------------------------------------------------------------------------------------

int main(int argc, char* argv[])
{
    int bitsize = 4, integer = 14;
    int hw = hWeight(integer);
    int groupStartIndex = rCummGroupCount(bitsize,hw-1);
    printf("bitsize: %d\n", bitsize);
    printf("integer: %d  hamming weight: %d\n", integer, hw);
    printf("group start index: %d\n", groupStartIndex);
}

输出:

位大小:4
整数:14 汉明权重:3
组开始索引:11

【讨论】:

  • 看起来不错,但有没有办法进行 rCummGroupCount 迭代? Lanczos 近似公式有多精确?
  • @ks6g10 需要一些时间来研究精度问题和递归rCummGroupCount。不过,使用阶乘查找表要简单得多,您可以研究一下。
猜你喜欢
  • 1970-01-01
  • 2015-01-30
  • 2011-10-30
  • 1970-01-01
  • 2012-07-16
  • 1970-01-01
  • 2016-10-31
  • 2012-03-12
  • 1970-01-01
相关资源
最近更新 更多