CarpetLib dh:gfs needs to be traversed in order of variable index

Create issue
Issue #1449 closed
Roland Haas created an issue

Commit 9166952 "CarpetLib: Store registered gh, dh, th, gf, data etc. via sets, not via lists" replaced list<ggf> dh::gfs by set<ggf> dh::gfs. Unfortunately some code assumes that these containers are iterated over from varindex=0 to the last varindex. The set however is sorted by the address of the ggf structures it holds (the list was sorted by creation order which apparently happened to coincide with the variable index).

The bug manifests when storage is allocated for vectors of grid functions (eg vel[]) and recompose_allocate is called on vel[1] before vel[0]. Since only vel[0] actually allocactes memory vel[1] tries to refer to this memory and assert()s when trying to access not yet existing timelevels.

The attached patches (each!) fix the issues, once by using a vector<ggf*> indexed by the varindex which allows for simple iterators but comes at the price of having to deal with NULL pointers for not-yet registered ggfs.

The second uses a map<int,ggf*> which avoids this problem but comes at the price of having to deal with pairs for iterators.

Making the set<ggf> sorted by variable index is possible but awkward since C++ requires the sorting callback (type) to be part of the set type, ie one needs something like set<ggf,bool()(ggfconst,ggf*const)> as the type of the container.


Comments (4)

  1. Roland Haas reporter
    • changed status to open
    • removed comment

    Both patches make dh::gfs private to avoid anyone tampering with it.

  2. Erik Schnetter
    • changed status to open
    • removed comment

    The code should check that f->varindex is reasonable. For the vector version, you could directly resize to CCTK_NumVars(). Both patches are good.

  3. Roland Haas reporter
    • changed status to resolved
    • removed comment

    Applied the map patch as git hash 8237792 "CarpetLib: use map instead of set for dh::gfs" of CarpetLib. Added assert()s for varindex >=0, varindex <= CCTK_NumVars(), check that items that are deleted exist in the map, check that items that are added did not already exist in the map.

  4. Log in to comment