I have narrowed down the problem to this line:
indg = nets[i]->adjlist[i].size(); // indg is in a method of the Ensemble class
Where the above variables are
vector<DDNetwork*> nets; // this vector is in the Ensemble class
int indg;
class DDNetwork
{
friend class Ensemble;
...
public:
vector< vector<int> > adjlist; // the adjacency list of the network
...
};
I don't understand why indg = nets[i]->adjlist[i].size(); would cause a segfault, is there something I am missing? Also if you need more information I can add it.
EDIT: I just realized what was wrong, I am using the same index for adjlist that I am for nets, the line
indg = nets[i]->adjlist[i].size();
should be:
indg = nets[i]->adjlist[j].size();
EDIT: After stepping through the debugger, I noticed that in the constructor of Ensemble, nets.size() = 10 (expected), but when the method Ensemble::alloc_dev_memory is called, nets.size() = 803384 (unexpected), so I think that JaredPar's second suggestion might explain the problem. Here is the code that adds DDNetwork* instances into the nets variable:
Ensemble::Ensemble(int N, float K, int S, bool seedrand, int ltype, int numNets)
{
this->N = N;
this->K = K;
this->S = S;
this->ltype = ltype;
this->numNets = numNets;
if(seedrand)
srand(time(0));
nets.resize(numNets); // make a vector of pointers to DDNetwork
for(int i=0; i < numNets; ++i)
nets[i] = new DDNetwork(N,K,S,seedrand,ltype);
// pre-compute the S^k for k=0,1,...,Kmax
Spow[0]=1; // S^0 = 1
int k=1;
while(k <= Kmax*2) {
Spow[k] = S*Spow[k-1]; // S^k = S*(S^(k-1))
++k;
}
}
This constructor is called when I instantiate the ensemble variable in my main function:
// instantiate ensemble of networks
Ensemble ens(N, K, S, seed_rand, multiedge, numNets);
// run_the ensemble one time step
ens.run_gpu();
And after that, Ensemble::run_gpu calls Ensemble::alloc_dev_memory, then when nets[i]->adjlist[j].size() is called, that's when I receive the segmentation fault.
How would the nets reference get uninitialized?
The problem is likely one of the following
DDNetwork* reference in nets[i] is uninitialized causing a segfault when you access the members. nets and each instance of adjlist is not kept in sync causing one of the offsets to be invalidCould you post the code which adds DDNetwork* instances into the nets variable?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With