Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hierarchical asTuple #141

Merged
merged 2 commits into from
Dec 21, 2020

Conversation

bernhardmgruber
Copy link
Member

Rename asTuple into asFlatTuple and add a new function asTuple to VirtualDatum which reflects the datum domains hierarchy.

@bernhardmgruber bernhardmgruber merged commit adaff4c into alpaka-group:develop Dec 21, 2020
@bernhardmgruber bernhardmgruber deleted the rectuple branch December 21, 2020 23:55
@bernhardmgruber
Copy link
Member Author

bernhardmgruber commented Dec 22, 2020

This PR just allowed us to write e.g. the nbody kernel as:

template <typename VirtualParticleI, typename VirtualParticleJ>
LLAMA_FN_HOST_ACC_INLINE void pPInteraction(VirtualParticleI&& pi, VirtualParticleJ pj)
{
#ifdef USE_STRUCTURED_BINDING
    auto [piPos, piVel, _] = pi.asTuple();
    auto [piPosX, piPosY, piPosZ] = piPos;
    auto [piVelX, piVelY, piVelZ] = piVel;

    auto [pjPos, _2, pjMass] = pj.asTuple();
    auto [pjPosX, pjPosY, pjPosZ] = pjPos;

    auto distX = piPosX - pjPosX;
    auto distY = piPosY - pjPosY;
    auto distZ = piPosZ - pjPosZ;
    distX *= distX;
    distY *= distY;
    distZ *= distZ;
    const FP distSqr = EPS2 + distX + distY + distZ;
    const FP distSixth = distSqr * distSqr * distSqr;
    const FP invDistCube = 1.0f / std::sqrt(distSixth);
    const FP sts = pjMass * invDistCube * TIMESTEP;
    piVelX += distX * sts;
    piVelY += distY * sts;
    piVelZ += distZ * sts;
#else
    auto dist = pi(tag::Pos{}) - pj(tag::Pos{});
    dist *= dist;
    const FP distSqr = EPS2 + dist(tag::X{}) + dist(tag::Y{}) + dist(tag::Z{});
    const FP distSixth = distSqr * distSqr * distSqr;
    const FP invDistCube = 1.0f / std::sqrt(distSixth);
    const FP sts = pj(tag::Mass{}) * invDistCube * TIMESTEP;
    pi(tag::Vel{}) += dist * sts;
#endif
}

Both versions compile down to exactly the same assembly: https://godbolt.org/z/r7K1hM

This alone might not look like much, but it proofs that the VirtualDatum could be replaced entirely by a std::tuple of memory locations. And this opens up some possibly interesting API designs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant