计算大数据流中每个元素的出现次数

Count the occurrence of each element in large data stream

本文关键字:元素 数据流 计算      更新时间:2023-10-16

我有一个模拟,有N个粒子,在T个时间步长上运行。在每个时间步,每个粒子计算一些关于自己和附近(半径内)其他粒子的数据,这些数据被打包成一个4-22字节长的c字符串(取决于附近有多少粒子)。我称之为状态字符串。

我需要计算每个状态字符串出现的次数,以形成直方图。我试过使用Google的Sparse Hash Map,但是内存开销太大了。

我一直在运行一些简化的测试(附)超过100,000个时间步,500个粒子。这导致在50mil个可能的状态字符串中只有超过18mil个唯一的状态字符串,这与需要完成的实际工作是一致的。

它最终为每个唯一条目的char*和int以及实际状态字符串本身使用了323 MB的空间。然而,任务管理器报告使用了870M。这是547M的开销,或约251.87比特/条,远远超过谷歌广告的4-5比特。

所以我想我一定是做错了什么。但后来我找到了这个网站,它显示了类似的结果,然而,我不确定他的图表是否只显示了哈希表的大小,还是也包括了实际数据的大小。此外,他的代码没有释放任何插入到已经存在的hashmap中的字符串(这意味着如果他的图表包含实际数据的大小,它将结束)。

以下是显示输出问题的一些代码:
#include <google/sparse_hash_map>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
//String equality
struct eqstrc
{
    bool operator()(const char* s1, const char* s2) const
    {
        return (s1 == s2) || (s1 && s2 && !strcmp(s1,s2));
    }   
};
//Hashing function
template <class T>
class fnv1Hash
{
public:
    size_t operator()(const T& c) const {
            unsigned int hash = 2166136261;
            const unsigned char *key = (const unsigned char*)(c);
            size_t L = strlen((const char*)c);
            size_t i = 0;
            for(const unsigned char *s = key; i < L; ++s, ++i)
                hash = (16777619 * hash) ^ (*s);
            return (size_t)hash;
    }
};
//Function to form new string
char * new_string_from_integer(int num)
{
    int ndigits = num == 0 ? 1 : (int)log10((float)num) + 1;
    char * str = (char *)malloc(ndigits + 1);
    sprintf(str, "%d", num);
    return str;
}
typedef google::sparse_hash_map<const char*, int, fnv1Hash<const char*>, eqstrc> HashCharMap;

int main()
{
    HashCharMap hashMapChar;
    int N = 500;
    int T = 100000;
    
    //Fill hash table with strings
    for(int k = 0; k < T; ++k)
    {
        for(int i = 0; i < N; ++i)
        {
            char * newString = new_string_from_integer(i*k);
            std::pair<HashCharMap::iterator, bool> res =  hashMapChar.insert(HashCharMap::value_type(newString, HashCharMap::data_type()));
            (res.first)->second++;
            if(res.second == false) //If the string already in hash map, don't need this memory
                free(newString);
        }
    }
    //Count memory used by key 
    size_t dataCount = 0;
    for(HashCharMap::iterator hashCharItr = hashMapChar.begin(); hashCharItr != hashMapChar.end(); ++hashCharItr)
    {
        dataCount += sizeof(char*) + sizeof(unsigned int); //Size of data to store entries
        dataCount += (((strlen(hashCharItr->first) + 1) + 3) & ~0x03); //Size of entries, padded to 4 byte boundaries
    }
    printf("Hash Map Size: %lun", (unsigned long)hashMapChar.size());
    printf("Bytes written: %lun", (unsigned long)dataCount);
    system("pause");
}

输出
Hash Map Size: 18218975
Bytes written: 339018772
Peak Working Set (Reported by TaskManager): 891,228 K
Overhead: 560,155 K, or 251.87 bits/entry

我已经尝试了谷歌稀疏散列图v1.10和v2.0.2。

我在使用哈希映射时做错了什么吗?或者有更好的方法来解决这个问题,因为对于这些字符串,我几乎可以只存储字符串列表,排序,然后计算连续条目。

谢谢你的帮助

<标题>编辑

因为我被问到,这里是实际数据的格式:每个组件是2字节,并分为两个子部分。12位和4位

  • 前两个字节(短):[当前粒子id(12位)|角度当前粒子(4位)]
  • 秒短:[交互次数]粒子(12比特)(N) |当前粒子的前一个角度(4比特)]
  • 对于下一个N短线:[粒子i的id(12比特)|粒子i的前一个角度(4比特)]

角度被近似(除以16),以4位存储。

这有点啰嗦,所以我写一个例子:

0x120A 0x001B 0x136F =粒子288 (0x120),夹角为10 (0xA)。前一时间步长角为11 (0xB)。与1 (0x001)其他粒子相互作用。另一个粒子是粒子310 (0x136),在上一个时间步长角为15 ((0xF)。

粒子与0到9个其他粒子相互作用,因此我上面提到的4-22字节(尽管很少,可以与多达12个或更多的其他粒子相互作用)。没有限制。如果所有500个粒子都在半径内,那么字符串将是1004字节长)

附加信息:在我的实际代码中,哈希函数和比较函数使用存储在第二短的最高12位中的大小来进行处理,因为非终端0x000s可能出现在我的状态字符串中。

这些图来自Linux上的gcc实验。分配4-22字节的短块,长度为1 - 12需要16字节,长度为13 - 20需要24字节,其余部分需要32字节。

这意味着你对18218975个字符串("0".."50000000")的实验在堆上需要291503600字节,它们的长度之和(加上末尾的0)为156681483。

因此,由于malloc,您有135MB的开销。

(这个峰值工作集大小是一个可靠的数字吗?)