Skip to content

[BUG] Hashing function causing slow insert for SPH grid #656

@dsfenn

Description

@dsfenn

Summary

The provided CoordHash for hashing ChVector3i arrays causes large slowdowns when dealing with a large number of particles. Improving the hashing function reduces execution time by over a factor of 1000x on my system.

Chrono Version and Platform

RHEL 9
Chrono hash: 6cdcdbc
CPU: i9-14900K
CPU cores: 32
RAM: 92 GB

Expected Behavior

The SPH particle domain should be constructed with reasonable overhead.

Actual Behavior

As the number of particles grows, the overhead for insertion into the grid becomes very high. On my system, a domain of 75 million SPH particles takes ~7 hours just to initialize.

I was able to ameliorate this by redefining the hashing function like this:

static inline std::uint64_t mix64(std::uint64_t x) {
        x += 0x9e3779b97f4a7c15ULL;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ULL;
        x = (x ^ (x >> 27)) * 0x94d049bb133111ebULL;
        return x ^ (x >> 31);
    }

    struct CoordHash {
        std::size_t operator()(const ChVector3i& p) const noexcept {
            // Pack the three ints into 64-bit lanes; mix each, then combine.
            std::uint64_t x = static_cast<std::uint32_t>(p.x());
            std::uint64_t y = static_cast<std::uint32_t>(p.y());
            std::uint64_t z = static_cast<std::uint32_t>(p.z());
            std::uint64_t h = mix64(x);
            h ^= mix64(y) + 0x9e3779b97f4a7c15ULL + (h << 6) + (h >> 2);
            h ^= mix64(z) + 0x9e3779b97f4a7c15ULL + (h << 6) + (h >> 2);
            return static_cast<std::size_t>(h);
        }
    };

I additionally modified ChFsiProblemCartesian::Construct() with the following:

m_sph.max_load_factor(0.5f);
m_sph.reserve(num_sph);

I'm not sure if it's good to use a max load factor of 0.5 in general, but that's what I tested with. With these changes, initialization time was reduced to less than 20 s for the same problem.

Steps to Reproduce

Set up an SPH simulation with a large number of particles. I specifically used the test rig, with a particle spacing of 0.003 m.

Further Information, Files, and Links

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugconcerns bugs or unusual behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions