/*
 * This file is part of the GROMACS molecular simulation package.
 *
 * Copyright 2020- The GROMACS Authors
 * and the project initiators Erik Lindahl, Berk Hess and David van der Spoel.
 * Consult the AUTHORS/COPYING files and https://www.gromacs.org for details.
 *
 * GROMACS is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * GROMACS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with GROMACS; if not, see
 * https://www.gnu.org/licenses, or write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
 *
 * If you want to redistribute modifications to GROMACS, please
 * consider that scientific software is very special. Version
 * control is crucial - bugs must be traceable. We will be happy to
 * consider code for inclusion in the official distribution, but
 * derived work must not be called official GROMACS. Details are found
 * in the README & COPYING files - if they are missing, get the
 * official version at https://www.gromacs.org.
 *
 * To help us fund GROMACS development, we humbly ask that you cite
 * the research papers on the package. Check out https://www.gromacs.org.
 */
/*! \internal \file
 * \brief Utilities to setup GROMACS data structures for non-bonded force calculations.
 *
 * \author Victor Holanda <victor.holanda@cscs.ch>
 * \author Joe Jordan <ejjordan@kth.se>
 * \author Prashanth Kanduri <kanduri@cscs.ch>
 * \author Sebastian Keller <keller@cscs.ch>
 */
#include "nblib/nbnxmsetuphelpers.h"

#include <cassert>

#include <algorithm>
#include <iterator>
#include <optional>
#include <string>
#include <utility>

#include "gromacs/ewald/ewald_utils.h"
#include "gromacs/gmxlib/nrnb.h"
#include "gromacs/gpu_utils/device_stream_manager.h"
#include "gromacs/gpu_utils/hostallocator.h"
#include "gromacs/math/functions.h"
#include "gromacs/mdlib/forcerec.h"
#include "gromacs/mdlib/gmx_omp_nthreads.h"
#include "gromacs/mdlib/rf_util.h"
#include "gromacs/mdtypes/atominfo.h"
#include "gromacs/mdtypes/forcerec.h"
#include "gromacs/mdtypes/interaction_const.h"
#include "gromacs/mdtypes/md_enums.h"
#include "gromacs/mdtypes/simulation_workload.h"
#include "gromacs/nbnxm/atomdata.h"
#include "gromacs/nbnxm/gpu_data_mgmt.h"
#include "gromacs/nbnxm/nbnxm.h"
#include "gromacs/nbnxm/nbnxm_enums.h"
#include "gromacs/nbnxm/nbnxm_gpu.h"
#include "gromacs/nbnxm/nbnxm_simd.h"
#include "gromacs/nbnxm/pairlistparams.h"
#include "gromacs/nbnxm/pairlistset.h"
#include "gromacs/nbnxm/pairlistsets.h"
#include "gromacs/nbnxm/pairsearch.h"
#include "gromacs/pbcutil/pbc.h"
#include "gromacs/utility/arrayref.h"
#include "gromacs/utility/logger.h"
#include "gromacs/utility/smalloc.h"

#include "nblib/exception.h"
#include "nblib/kerneloptions.h"
#include "nblib/particletype.h"

struct NbnxmGpu;

namespace nblib
{

int32_t findNumEnergyGroups(gmx::ArrayRef<int32_t> particleInteractionFlags)
{
    auto groupId = [](int code1, int code2)
    {
        return (code1 & gmx::sc_atomInfo_EnergyGroupIdMask) < (code2 & gmx::sc_atomInfo_EnergyGroupIdMask);
    };

    int maxElement = *std::max_element(
            std::begin(particleInteractionFlags), std::end(particleInteractionFlags), groupId);
    return ((maxElement + 1) & gmx::sc_atomInfo_EnergyGroupIdMask);
}

gmx::NbnxmKernelType translateBenchmarkEnum(const SimdKernels& kernel)
{
    int kernelInt = static_cast<int>(kernel);
    return static_cast<gmx::NbnxmKernelType>(kernelInt);
}

void checkKernelSetupSimd(const SimdKernels nbnxmSimd)
{
    if (nbnxmSimd >= SimdKernels::Count || nbnxmSimd == SimdKernels::SimdAuto)
    {
        throw InputException("Need a valid kernel SIMD type");
    }
    // Check SIMD support
    if ((nbnxmSimd != SimdKernels::SimdNo && !GMX_SIMD)
#ifndef GMX_NBNXN_SIMD_4XN
        || nbnxmSimd == SimdKernels::Simd4XM
#endif
#ifndef GMX_NBNXN_SIMD_2XNN
        || nbnxmSimd == SimdKernels::Simd2XMM
#endif
    )
    {
        throw InputException("The requested SIMD kernel was not set up at configuration time");
    }
}

gmx::NbnxmKernelSetup createKernelSetupCPU(const SimdKernels nbnxmSimd, const bool useTabulatedEwaldCorr)
{
    checkKernelSetupSimd(nbnxmSimd);

    gmx::NbnxmKernelSetup kernelSetup;

    // The int enum options.nbnxnSimd is set up to match Nbnxm::KernelType + 1
    kernelSetup.kernelType = translateBenchmarkEnum(nbnxmSimd);

    // The plain-C kernel does not support analytical ewald correction
    if (kernelSetup.kernelType == gmx::NbnxmKernelType::Cpu4x4_PlainC
        || kernelSetup.kernelType == gmx::NbnxmKernelType::Cpu1x1_PlainC)
    {
        kernelSetup.ewaldExclusionType = gmx::EwaldExclusionType::Table;
    }
    else
    {
        kernelSetup.ewaldExclusionType = useTabulatedEwaldCorr ? gmx::EwaldExclusionType::Table
                                                               : gmx::EwaldExclusionType::Analytical;
    }

    return kernelSetup;
}

gmx::NbnxmKernelSetup createKernelSetupGPU(const bool useTabulatedEwaldCorr)
{
    gmx::NbnxmKernelSetup kernelSetup;
    kernelSetup.kernelType         = gmx::NbnxmKernelType::Gpu8x8x8;
    kernelSetup.ewaldExclusionType = useTabulatedEwaldCorr ? gmx::EwaldExclusionType::Table
                                                           : gmx::EwaldExclusionType::Analytical;

    return kernelSetup;
}

std::vector<int32_t> createParticleInfoAllVdw(const size_t numParticles)
{
    std::vector<int32_t> particleInfoAllVdw(numParticles);
    for (size_t particleI = 0; particleI < numParticles; particleI++)
    {
        particleInfoAllVdw[particleI] |= gmx::sc_atomInfo_HasVdw;
        particleInfoAllVdw[particleI] |= gmx::sc_atomInfo_HasCharge;
    }
    return particleInfoAllVdw;
}

std::vector<real> createNonBondedParameters(const std::vector<ParticleType>& particleTypes,
                                            const NonBondedInteractionMap& nonBondedInteractionMap)
{
    /* Todo: Refactor nbnxm to take nonbondedParameters_ directly
     *
     * initial self-handling of combination rules
     * size: 2*(numParticleTypes^2)
     */
    std::vector<real> nonbondedParameters;
    nonbondedParameters.reserve(2 * particleTypes.size() * particleTypes.size());

    constexpr real c6factor  = 6.0;
    constexpr real c12factor = 12.0;

    for (const ParticleType& particleType1 : particleTypes)
    {
        for (const ParticleType& particleType2 : particleTypes)
        {
            nonbondedParameters.push_back(
                    nonBondedInteractionMap.getC6(particleType1.name(), particleType2.name()) * c6factor);
            nonbondedParameters.push_back(
                    nonBondedInteractionMap.getC12(particleType1.name(), particleType2.name()) * c12factor);
        }
    }
    return nonbondedParameters;
}

gmx::StepWorkload createStepWorkload()
{
    gmx::StepWorkload stepWorkload;
    stepWorkload.computeForces          = true;
    stepWorkload.computeNonbondedForces = true;
    stepWorkload.useGpuFBufferOps       = false;
    stepWorkload.useGpuXBufferOps       = false;

    return stepWorkload;
}

static gmx::SimulationWorkload createSimulationWorkload()
{
    gmx::SimulationWorkload simulationWork;
    simulationWork.computeNonbonded = true;
    return simulationWork;
}

gmx::SimulationWorkload createSimulationWorkloadGpu()
{
    gmx::SimulationWorkload simulationWork = createSimulationWorkload();

    simulationWork.useGpuNonbonded = true;
    simulationWork.useGpuUpdate    = false;

    return simulationWork;
}

std::shared_ptr<gmx::DeviceStreamManager> createDeviceStreamManager(const DeviceInformation& deviceInfo,
                                                                    const gmx::SimulationWorkload& simulationWorkload)
{
    return std::make_shared<gmx::DeviceStreamManager>(deviceInfo, simulationWorkload, false);
}

real ewaldCoeff(const real ewald_rtol, const real pairlistCutoff)
{
    return calc_ewaldcoeff_q(pairlistCutoff, ewald_rtol);
}

interaction_const_t createInteractionConst(const NBKernelOptions& options)
{
    interaction_const_t interactionConst;
    interactionConst.vdw.type     = VanDerWaalsType::Cut;
    interactionConst.vdw.modifier = InteractionModifiers::PotShift;
    interactionConst.vdw.cutoff   = options.pairlistCutoff;

    switch (options.coulombType)
    {
        case CoulombType::Pme: interactionConst.coulomb.type = CoulombInteractionType::Pme; break;
        case CoulombType::Cutoff:
            interactionConst.coulomb.type = CoulombInteractionType::Cut;
            break;
        case CoulombType::ReactionField:
            interactionConst.coulomb.type = CoulombInteractionType::RF;
            break;
        case CoulombType::Count: throw InputException("Unsupported electrostatic interaction");
    }
    interactionConst.coulomb.modifier = InteractionModifiers::PotShift;
    interactionConst.coulomb.cutoff   = options.pairlistCutoff;
    // Note: values correspond to ic->coulomb_modifier = eintmodPOTSHIFT
    interactionConst.vdw.dispersionShift.cpot = -1.0 / gmx::power6(interactionConst.vdw.cutoff);
    interactionConst.vdw.repulsionShift.cpot  = -1.0 / gmx::power12(interactionConst.vdw.cutoff);

    // These are the initialized values but we leave them here so that later
    // these can become options.
    interactionConst.coulomb.epsilon_r   = 1.0;
    const real reactionFieldPermittivity = 1.0;

    /* Set the Coulomb energy conversion factor */
    if (interactionConst.coulomb.epsilon_r != 0)
    {
        interactionConst.coulomb.epsfac = ONE_4PI_EPS0 / interactionConst.coulomb.epsilon_r;
    }
    else
    {
        /* eps = 0 is infinite dieletric: no Coulomb interactions */
        interactionConst.coulomb.epsfac = 0;
    }

    calc_rffac(nullptr,
               interactionConst.coulomb.epsilon_r,
               reactionFieldPermittivity,
               interactionConst.coulomb.cutoff,
               &interactionConst.coulomb.reactionFieldCoefficient,
               &interactionConst.coulomb.reactionFieldShift);


    if (usingPmeOrEwald(interactionConst.coulomb.type))
    {
        // Ewald coefficients, we ignore the potential shift
        interactionConst.coulomb.ewaldCoeff = ewaldCoeff(1e-5, options.pairlistCutoff);
        if (interactionConst.coulomb.ewaldCoeff <= 0)
        {
            throw InputException("Ewald coefficient should be > 0");
        }
        interactionConst.coulombEwaldTables = std::make_unique<EwaldCorrectionTables>();
        init_interaction_const_tables(nullptr, &interactionConst, 0, 0);
    }
    return interactionConst;
}

std::unique_ptr<gmx::nonbonded_verlet_t> createNbnxmCPU(const size_t           numParticleTypes,
                                                        const NBKernelOptions& options,
                                                        int                    numEnergyGroups,
                                                        gmx::ArrayRef<const real> nonbondedParameters)
{
    if (nonbondedParameters.size() != numParticleTypes * numParticleTypes * 2)
    {
        throw InputException(
                "The size of the non-bonded parameter matrix does not match numParticleTypes");
    }

    const auto pinPolicy  = gmx::PinningPolicy::CannotBePinned;
    const int  numThreads = options.numOpenMPThreads;

    gmx::NbnxmKernelSetup kernelSetup =
            createKernelSetupCPU(options.nbnxmSimd, options.useTabulatedEwaldCorr);

    gmx::PairlistParams pairlistParams(kernelSetup.kernelType, {}, false, options.pairlistCutoff, false);

    auto pairlistSets = std::make_unique<gmx::PairlistSets>(pairlistParams, false, 0, pinPolicy);
    auto pairSearch   = std::make_unique<gmx::PairSearch>(
            PbcType::Xyz, false, nullptr, nullptr, pairlistParams.pairlistType, false, false, numThreads, pinPolicy);

    // Needs to be called with the number of unique ParticleTypes
    auto atomData = std::make_unique<gmx::nbnxn_atomdata_t>(pinPolicy,
                                                            gmx::MDLogger(),
                                                            kernelSetup.kernelType,
                                                            std::nullopt,
                                                            gmx::LJCombinationRule::None,
                                                            nonbondedParameters,
                                                            true,
                                                            numEnergyGroups,
                                                            numThreads);

    // Put everything together
    auto nbv = std::make_unique<gmx::nonbonded_verlet_t>(
            std::move(pairlistSets), std::move(pairSearch), std::move(atomData), kernelSetup, nullptr);

    return nbv;
}

std::unique_ptr<gmx::nonbonded_verlet_t> createNbnxmGPU(const size_t           numParticleTypes,
                                                        const NBKernelOptions& options,
                                                        const std::vector<real>& nonbondedParameters,
                                                        const interaction_const_t& interactionConst,
                                                        const gmx::DeviceStreamManager& deviceStreamManager)
{
    if (nonbondedParameters.size() != numParticleTypes * numParticleTypes * 2)
    {
        throw InputException(
                "The size of the non-bonded parameter matrix does not match numParticleTypes");
    }

    const auto pinPolicy = gmx::PinningPolicy::PinnedIfSupported;

    gmx::NbnxmKernelSetup kernelSetup = createKernelSetupGPU(options.useTabulatedEwaldCorr);

    gmx::PairlistParams pairlistParams(
            kernelSetup.kernelType, gmx::PairlistType::Hierarchical8x8x8, false, options.pairlistCutoff, false);


    // nbnxn_atomdata is always initialized with 1 thread if the GPU is used
    constexpr int numThreadsInit = 1;
    // multiple energy groups are not supported on the GPU
    constexpr int numEnergyGroups = 1;
    auto          atomData        = std::make_unique<gmx::nbnxn_atomdata_t>(pinPolicy,
                                                            gmx::MDLogger(),
                                                            kernelSetup.kernelType,
                                                            std::nullopt,
                                                            gmx::LJCombinationRule::None,
                                                            nonbondedParameters,
                                                            true,
                                                            numEnergyGroups,
                                                            numThreadsInit);

    gmx::NbnxmGpu* nbnxmGpu = gmx::gpu_init(
            deviceStreamManager, &interactionConst, pairlistParams, atomData.get(), false, std::nullopt);

    // minimum iList count for GPU balancing
    int iListCount = gmx::gpu_min_ci_balanced(nbnxmGpu);

    auto pairlistSets = std::make_unique<gmx::PairlistSets>(pairlistParams, false, iListCount, pinPolicy);
    auto pairSearch = std::make_unique<gmx::PairSearch>(
            PbcType::Xyz, false, nullptr, nullptr, pairlistParams.pairlistType, false, false, options.numOpenMPThreads, pinPolicy);

    // Put everything together
    auto nbv = std::make_unique<gmx::nonbonded_verlet_t>(
            std::move(pairlistSets), std::move(pairSearch), std::move(atomData), kernelSetup, nbnxmGpu);

    // Some parameters must be copied to NbnxmGpu to have a fully constructed nonbonded_verlet_t
    gmx::gpu_init_atomdata(nbv->gpuNbv(), &nbv->nbat());

    return nbv;
}

void setGmxNonBondedNThreads(int numThreads)
{
    gmx_omp_nthreads_set(ModuleMultiThread::Pairsearch, numThreads);
    gmx_omp_nthreads_set(ModuleMultiThread::Nonbonded, numThreads);
}

void updateForcerec(t_forcerec* forcerec, const matrix& box)
{
    assert(forcerec != nullptr && "Forcerec not initialized");
    forcerec->shift_vec.resize(numShiftVectors);
    calc_shifts(box, forcerec->shift_vec);
}


} // namespace nblib
