简体   繁体   English

大多维向量初始化 C++

[英]Big multidimensional vector initializaton C++

I am trying to load simulation results from a file into a vector.我正在尝试将模拟结果从文件加载到向量中。 The code works well for small and medium amount of data.该代码适用于中小数据量。 When I try do load big files I get an exception.当我尝试加载大文件时,出现异常。 The array that makes my program crash is about 17000*7*3000 elements.使我的程序崩溃的数组大约有 17000*7*3000 个元素。 I've tried splitting initialization into few steps but it has crashed as well.我试过将初始化分成几个步骤,但它也崩溃了。 Could you please tell me if I can do something to make it work?你能告诉我我是否可以做些什么来使它工作吗?

//////////////////////////////////////////////////////////
//Import of surface receivers file
    for(int freq=0;freq<7;freq++)
    {
        filePath=filePath_old;

        filePath.Replace(wxT("125 Hz"),wxString::Format(wxT("%i"), freqSTI[freq])+wxT(" Hz"));
        if(importer.ImportBIN(filePath,mainData))
        {
            if(timeTable.size()==0){
                for(int idstep=0;idstep<mainData.nbTimeStep;idstep++)
                {
                    timeTable.push_back(mainData.timeStep*(idstep+1)*1000);
                }
            }
            for(wxInt32 idrs=0;idrs<mainData.tabRsSize;idrs++)
            {
                for(wxInt32 idface=0;idface<mainData.tabRs[idrs].dataRec.quantFaces;idface++)
                {
                    if(tab_wj.size()<idrs+1){
                        tab_wj.push_back(std::vector<std::vector<std::vector<wxFloat32> > > (mainData.tabRs[idrs].dataRec.quantFaces,std::vector<std::vector<wxFloat32> >(7,std::vector<wxFloat32>(mainData.nbTimeStep,0.f))));
                    }
                    //Pour chaque enregistrement de cette face
                    int tmp=mainData.tabRs[idrs].dataFaces[idface].dataFace.nbRecords;
                    for(wxInt32 idenr=0;idenr<mainData.tabRs[idrs].dataFaces[idface].dataFace.nbRecords;idenr++)
                    {
                        t_faceValue* faceval=&mainData.tabRs[idrs].dataFaces[idface].tabTimeStep[idenr];
                        tab_wj[idrs][idface][freq][faceval->timeStep]=faceval->energy;
                    }
                }
            }
        }

    }

The place where an exception occures is tab_wj.push_back...发生异常的地方是tab_wj.push_back...

When I tried to split initialization into more than one step I wrote:当我试图将初始化分成多个步骤时,我写道:

std::vector<wxFloat32> t1(mainData.nbTimeStep,0.f);
std::vector<std::vector<wxFloat32> > t2(7,t1);
std::vector<std::vector<std::vector<wxFloat32> > > t3(mainData.tabRs[idrs].dataRec.quantFaces,t2);

Then it crashed at last one of these lines.然后它在这些线路中的最后一条崩溃了。

Thanks for any help!谢谢你的帮助!

Looks like it is getting an exception while allocating memory.看起来它在分配内存时出现异常。 std::vector<> must reallocate its storage as you add new items to it. std::vector<> 必须在向其添加新项目时重新分配其存储。 It doesn't do this for each push_back() because it allocates more than it needs.它不会为每个 push_back() 执行此操作,因为它分配的内容超出了它的需要。 A vector<> of size 5 might actually have space for 8 items (size() vs. capacity()).一个大小为 5 的 vector<> 实际上可能有 8 个项目的空间(size() vs. capacity())。 When you insert the 9th item it must reallocate, and typically it will double its capacity (from 8 to 16 (*)).当您插入第 9 个项目时,它必须重新分配,通常它的容量会翻倍(从 8 到 16 (*))。

This reallocation can fragment your heap.这种重新分配可能会使您的堆碎片化。 To improve you can preallocate the vector to the size it will eventually need, so it will never need to reallocate.为了改进,您可以将向量预先分配到它最终需要的大小,因此它永远不需要重新分配。 vector::reserve() So, if you know you will eventually need 17000*7*3000, reserve this up front. vector::reserve() 因此,如果您知道最终将需要 17000*7*3000,请预先保留它。

Also, if you have a lot of small vectors, allocating those at a small size can reduce your memory requirements.此外,如果您有很多小向量,则以较小的大小分配它们可以减少您的内存需求。 Usually vector<> has a default size which could be 8 or 16. If you only ever insert 3 items, then you are wasting a lot of storage.通常 vector<> 的默认大小可能是 8 或 16。如果您只插入 3 个项目,那么您将浪费大量存储空间。 During allocation of the vector I believe you can specify its initial capacity.在分配向量期间,我相信您可以指定其初始容量。

Since you are using nested vectors, each of these will in turn have their own allocations.由于您使用的是嵌套向量,因此它们中的每一个都将依次具有自己的分配。 Depending on your version of C++, pushing a vector onto a vector can cause a number of allocations.根据您的 C++ 版本,将向量推入向量可能会导致多次分配。

You might try restructuring the code to not require nested vectors.您可以尝试将代码重构为不需要嵌套向量。

(*): a naive vector implementation will double the allocation size when it needs more space, but a factor of 1.6 is better for a number of reasons (its just as big-O efficient and leads to much less memory fragmentation). (*):当需要更多空间时,一个简单的向量实现会将分配大小加倍,但出于多种原因,1.6 的因子更好(它与 big-O 效率一样高,并导致更少的内存碎片)。 See "The golden ratio": https://en.wikipedia.org/wiki/Golden_ratio参见“黄金比例”: https : //en.wikipedia.org/wiki/Golden_ratio

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM