chr1shr / voro

Voro++: a three-dimensional Voronoi cell library in C++
Other
164 stars 47 forks source link

Number of particles limited by size of int? #32

Closed lukascodispoti closed 1 year ago

lukascodispoti commented 1 year ago

Hello,

First, many thanks to all contributors!

I am using a lot of particles. I'm running into integer overflows when putting particles into my container, since the pid field is of type int.

Now, is there a workaround for this problem that does not involve a great deal of modification of the code base?

Cheers, Lukas

lukascodispoti commented 1 year ago

Closed this. The version on the dev branch has what I need.

lukascodispoti commented 1 year ago

I mixed something up here. Dev branch implements parallelization, but the integer overflow issue persists. I'm trying to refactor the code base to use uint64_t for the particle id.

chr1shr commented 1 year ago

Currently there is no standard support for this, although it's fairly high on the to-do list as we are trying to move toward larger systems, such as with the recent multithreaded version. Currently the container... classes have a 32-bit integer array for particle IDs, and a separate array for 64-bit double precision particle coordinates. I am hoping in the future to combine these into a single data structure with 256-bit entries, using three double-precision numbers and a 64-bit integer ID. But this will require a substantial overhaul, particularly since the ID numbers enter into other parts of the code, such as the voronoicell_neighbor class.

Since the particles are internally sorted into a grid data structure, there should be no problem already with calculating more than 2^{32} particles (unless the system was highly degenerate and there were so many particles in a single grid block that the 32-bit counter overflowed). But you can currently only have 2^{32} unique particle IDs and thus some IDs would have to be reused.

Another practical strategy would be to decompose your domain into chunks, each of which have less than 2^{32} particles. If they are fairly densely packed, so that the Voronoi cells don't experience the effects of far-away particles, then you should be able to get valid computations in each subdomain that you can merge later.

lukascodispoti commented 1 year ago

Hi @chr1shr

Thank you for your comments.

I noticed that even when using a large number of particles that does not overflow the pid datatype, say e.g. 5.4e8, I get errors like

terminate called after throwing an instance of 'std::bad_alloc'  what():  std::bad_alloc

Can you point me out to why this might happen and how I could debug it?

lukascodispoti commented 1 year ago

This specific error was associated with integer overflows in the particle_order class. With some changes here and in other instances I was able to push the number of particles put into a container_3d to 2^32 and beyond. Some bugs remain when actually computing the cells for numbers of particles larger than 2.7e9, but since a general overhaul to the codebase is planned, I will close this issue for now until further questions come up.

chr1shr commented 1 year ago

I'm glad you were able to figure this out. Sorry I wasn't able to look into this myself, although I plan to address this issue more generally during the next code overhaul, at which point this should be moot.