简体   繁体   English

使用矩阵进行计算时擦除C ++ 2d向量行

[英]Erase C++ 2d vector row while doing calculations with the matrix

I'm trying to simulate the mean behaviour of an ensemble of neurons. 我正在尝试模拟一组神经元的平均行为。 This means I need to do calculations with a matrix of a couple of billions of elements ( steps ~10 6 , neurons ~10 4 ). 这意味着我需要使用数十亿个元素的矩阵进行计算( steps 〜10 6neurons 〜10 4 )。

To avoid eating my whole RAM (and dying trying) I decided to delete rows from the matrix as soon as I'm done doing calculations with it. 为了避免吃光我的整个RAM(并竭尽全力),我决定在完成矩阵计算后立即从矩阵中删除行。 I don't have too much experience with C++, but my understanding is that v.erase( v.begin()-i+1); 我没有太多的C ++经验,但是我的理解是v.erase( v.begin()-i+1); should allow me to do so. 应该允许我这样做。

// Membrane potential matrix using st::vector
vector<vector<double>> v;
v.resize(steps + 1, vector<double>(neurons));
// Initialise v
for (size_t n = 0; n < neurons; n++) {
    v[0][n] = v0;
}

double v_avg[steps + 1] = {v0};

// Loop
for (size_t i = 1; i < steps + 1; i++) {
    for (size_t n = 0; n < neurons; n++) {
        if(v[i-1][n] >= vp) {
            v[i][n] = -vp;
        }
        else {
            v[i][n] = v[i-1][n] + h * ( pow(v[i-1][n], 2) + I[i] + eta[n] );
        }
        v_avg[i] += v[i][n]; // Sum of membrane potentials
    }
    cout << "step " << i << "/" << steps << " done\n";
    v.erase( v.begin()-i+1); // Erase row v[i-1]
    v_avg[i] = v_avg[i]/neurons; // Mean membrane potential
}
v.erase( v.begin()+steps+1 ); // Erase last row

I'm not sure why I'm getting segmentation fault after the steps /2 step (I'm doing tests with a small steps value): 我不确定为什么在steps / 2步骤之后出现细分错误(我正在使用较小的steps值进行测试):

...    
step 10/20 done
[1]    1791 segmentation fault (core dumped)  ./qif_solve_vect

Update: 更新:

Thanks to @1201ProgramAlarm I see what's my problem. 感谢@ 1201ProgramAlarm,我看到了我的问题。 My question would be: 我的问题是:

  1. How can I work with the matrix in a way it isn't allocated from the very beginning. 如何以一种从一开始就不分配的方式处理矩阵。
  2. How can I deallocate/free rows whilst keeping the indices (unlike v.erase( v.begin()) ). 我如何在保留索引的同时释放/释放行(与v.erase( v.begin()) )。 This is essential, as I will later implement different refractory times for each neuron when they produce a spike ( v[i][n] = -vp; ). 这是必不可少的,因为我稍后将在每个神经元产生尖峰( v[i][n] = -vp; )时执行不同的不应时间。

In your erase statement, you're subtracting from v.begin() , which will result in an invalid iterator since it will point before the start of the vector. 在您的erase语句中,您要减去v.begin() ,这将导致无效的迭代器,因为它将指向向量的开始之前。 You probably meant v.erase( v.begin() + i - 1); 您可能是说v.erase( v.begin() + i - 1); .

However, erasing like this isn't saving you any space since you already have the full matrix allocated. 但是,由于已经分配了完整的矩阵,因此进行这种擦除操作不会节省任何空间。 The erase will move all the remaining elements down one element, and your indexing for the next loop will be wrong (since you'd want to use v[0] all the time). 擦除会将所有其余元素向下移动一个元素,并且下一个循环的索引将是错误的(因为您一直希望使用v[0] )。

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

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