/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
 * This is part of the OpenMM molecular simulation toolkit.                   *
 * See https://openmm.org/development.                                        *
 *                                                                            *
 * Portions copyright (c) 2008-2026 Stanford University and the Authors.      *
 * Portions copyright (c) 2020 Advanced Micro Devices, Inc.                   *
 * Authors: Peter Eastman, Nicholas Curtis                                    *
 * Contributors:                                                              *
 *                                                                            *
 * This program 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 3 of the License, or       *
 * (at your option) any later version.                                        *
 *                                                                            *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.      *
 * -------------------------------------------------------------------------- */

#include "HipContext.h"
#include "HipExpressionUtilities.h"
#include "HipPlatform.h"
#include "HipKernelFactory.h"
#include "HipKernels.h"
#include "openmm/Context.h"
#include "openmm/System.h"
#include "openmm/internal/ContextImpl.h"
#include "openmm/internal/hardware.h"
#include <algorithm>
#include <cctype>
#include <sstream>
#include <cstdio>
#ifdef _MSC_VER
    #include <Windows.h>
#endif
using namespace OpenMM;
using namespace std;

#define CHECK_RESULT(result, prefix) \
    if (result != hipSuccess) { \
        std::stringstream m; \
        m<<prefix<<": "<<HipContext::getErrorString(result)<<" ("<<result<<")"<<" at "<<__FILE__<<":"<<__LINE__; \
        throw OpenMMException(m.str());\
    }


#ifdef OPENMM_COMMON_BUILDING_STATIC_LIBRARY
extern "C" void registerHipPlatform() {
    Platform::registerPlatform(new HipPlatform());
}
#else
extern "C" OPENMM_EXPORT_COMMON void registerPlatforms() {
    Platform::registerPlatform(new HipPlatform());
}
#endif

HipPlatform::HipPlatform() {
    deprecatedPropertyReplacements["HipDeviceIndex"] = HipDeviceIndex();
    deprecatedPropertyReplacements["HipDeviceName"] = HipDeviceName();
    deprecatedPropertyReplacements["HipUseBlockingSync"] = HipUseBlockingSync();
    deprecatedPropertyReplacements["HipPrecision"] = HipPrecision();
    deprecatedPropertyReplacements["HipUseCpuPme"] = HipUseCpuPme();
    deprecatedPropertyReplacements["HipTempDirectory"] = HipTempDirectory();
    deprecatedPropertyReplacements["HipDisablePmeStream"] = HipDisablePmeStream();
    deprecatedPropertyReplacements["HipDeterministicForces"] = HipDeterministicForces();
    deprecatedPropertyReplacements["HipFFTBackend"] = HipFFTBackend();
    HipKernelFactory* factory = new HipKernelFactory();
    registerKernelFactory(CalcForcesAndEnergyKernel::Name(), factory);
    registerKernelFactory(UpdateStateDataKernel::Name(), factory);
    registerKernelFactory(ApplyConstraintsKernel::Name(), factory);
    registerKernelFactory(VirtualSitesKernel::Name(), factory);
    registerKernelFactory(MinimizeKernel::Name(), factory);
    registerKernelFactory(CalcHarmonicBondForceKernel::Name(), factory);
    registerKernelFactory(CalcCustomBondForceKernel::Name(), factory);
    registerKernelFactory(CalcHarmonicAngleForceKernel::Name(), factory);
    registerKernelFactory(CalcCustomAngleForceKernel::Name(), factory);
    registerKernelFactory(CalcPeriodicTorsionForceKernel::Name(), factory);
    registerKernelFactory(CalcRBTorsionForceKernel::Name(), factory);
    registerKernelFactory(CalcCMAPTorsionForceKernel::Name(), factory);
    registerKernelFactory(CalcCustomTorsionForceKernel::Name(), factory);
    registerKernelFactory(CalcNonbondedForceKernel::Name(), factory);
    registerKernelFactory(CalcConstantPotentialForceKernel::Name(), factory);
    registerKernelFactory(CalcCustomNonbondedForceKernel::Name(), factory);
    registerKernelFactory(CalcGBSAOBCForceKernel::Name(), factory);
    registerKernelFactory(CalcCustomGBForceKernel::Name(), factory);
    registerKernelFactory(CalcCustomExternalForceKernel::Name(), factory);
    registerKernelFactory(CalcCustomHbondForceKernel::Name(), factory);
    registerKernelFactory(CalcCustomCentroidBondForceKernel::Name(), factory);
    registerKernelFactory(CalcCustomCompoundBondForceKernel::Name(), factory);
    registerKernelFactory(CalcCustomCPPForceKernel::Name(), factory);
    registerKernelFactory(CalcCustomCVForceKernel::Name(), factory);
    registerKernelFactory(CalcOrientationRestraintForceKernel::Name(), factory);
    registerKernelFactory(CalcPythonForceKernel::Name(), factory);
    registerKernelFactory(CalcRGForceKernel::Name(), factory);
    registerKernelFactory(CalcRMSDForceKernel::Name(), factory);
    registerKernelFactory(CalcCustomManyParticleForceKernel::Name(), factory);
    registerKernelFactory(CalcGayBerneForceKernel::Name(), factory);
    registerKernelFactory(CalcLCPOForceKernel::Name(), factory);
    registerKernelFactory(IntegrateVerletStepKernel::Name(), factory);
    registerKernelFactory(IntegrateNoseHooverStepKernel::Name(), factory);
    registerKernelFactory(IntegrateLangevinMiddleStepKernel::Name(), factory);
    registerKernelFactory(IntegrateBrownianStepKernel::Name(), factory);
    registerKernelFactory(IntegrateVariableVerletStepKernel::Name(), factory);
    registerKernelFactory(IntegrateVariableLangevinStepKernel::Name(), factory);
    registerKernelFactory(IntegrateCustomStepKernel::Name(), factory);
    registerKernelFactory(IntegrateDPDStepKernel::Name(), factory);
    registerKernelFactory(IntegrateQTBStepKernel::Name(), factory);
    registerKernelFactory(ApplyAndersenThermostatKernel::Name(), factory);
    registerKernelFactory(ApplyMonteCarloBarostatKernel::Name(), factory);
    registerKernelFactory(RemoveCMMotionKernel::Name(), factory);
    registerKernelFactory(CalcATMForceKernel::Name(), factory);
    platformProperties.push_back(HipDeviceIndex());
    platformProperties.push_back(HipDeviceName());
    platformProperties.push_back(HipUseBlockingSync());
    platformProperties.push_back(HipPrecision());
    platformProperties.push_back(HipUseCpuPme());
    platformProperties.push_back(HipTempDirectory());
    platformProperties.push_back(HipDisablePmeStream());
    platformProperties.push_back(HipDeterministicForces());
    platformProperties.push_back(HipFFTBackend());
    setPropertyDefaultValue(HipDeviceIndex(), "");
    setPropertyDefaultValue(HipDeviceName(), "");
    setPropertyDefaultValue(HipUseBlockingSync(), "true");
    setPropertyDefaultValue(HipPrecision(), "single");
    setPropertyDefaultValue(HipUseCpuPme(), "false");
    setPropertyDefaultValue(HipDisablePmeStream(), "false");
    setPropertyDefaultValue(HipDeterministicForces(), "false");
    setPropertyDefaultValue(HipFFTBackend(), "vkfft");
#ifdef _MSC_VER
    setPropertyDefaultValue(HipTempDirectory(), string(getenv("TEMP")));
#else
    char* tmpdir = getenv("TMPDIR");
    string tmp = (tmpdir == NULL ? string(P_tmpdir) : string(tmpdir));
    setPropertyDefaultValue(HipTempDirectory(), tmp);
#endif
}

double HipPlatform::getSpeed() const {
    // Reduce the speed of the HIP platform if there are no HIP devices in the system,
    // so the OpenCL plaform can be selected as default
    int numDevices;
    return hipGetDeviceCount(&numDevices) != hipErrorNoDevice ? 100 : 40;
}

bool HipPlatform::supportsDoublePrecision() const {
    return true;
}

const string& HipPlatform::getPropertyValue(const Context& context, const string& property) const {
    const ContextImpl& impl = getContextImpl(context);
    const PlatformData* data = reinterpret_cast<const PlatformData*>(impl.getPlatformData());
    string propertyName = property;
    if (deprecatedPropertyReplacements.find(property) != deprecatedPropertyReplacements.end())
        propertyName = deprecatedPropertyReplacements.find(property)->second;
    map<string, string>::const_iterator value = data->propertyValues.find(propertyName);
    if (value != data->propertyValues.end())
        return value->second;
    return Platform::getPropertyValue(context, property);
}

void HipPlatform::setPropertyValue(Context& context, const string& property, const string& value) const {
}

vector<map<string, string> > HipPlatform::getDevices(const map<string, string>& filters) const {
    // Check for properties that might act as filters.

    int deviceIndex = -1;
    if (filters.find(HipDeviceIndex()) != filters.end())
        stringstream(filters.at(HipDeviceIndex())) >> deviceIndex;
    string deviceName = (filters.find(HipDeviceName()) == filters.end() ? "" : filters.at(HipDeviceName()));

    // Loop over devices.

    vector<map<string, string> > results;
    int numDevices;
    if (hipGetDeviceCount(&numDevices) != hipSuccess)
        numDevices = 0;
    for (int i = 0; i < numDevices; i++) {
        if (deviceIndex != -1 && deviceIndex != i)
            continue;
        char name[1000];
        hipDevice_t device;
        CHECK_RESULT(hipDeviceGet(&device, i), "Error querying device");
        CHECK_RESULT(hipDeviceGetName(name, 1000, device), "Error querying device name");
        stringstream deviceNameStr;
        deviceNameStr << name;
        if (deviceName.size() > 0 && deviceName != deviceNameStr.str())
            continue;
        stringstream deviceIndexStr;
        deviceIndexStr << i;
        map<string, string> properties = {{HipDeviceIndex(), deviceIndexStr.str()},
                                          {HipDeviceName(), deviceNameStr.str()}};
        results.push_back(properties);
    }
    return results;
}

void HipPlatform::contextCreated(ContextImpl& context, const map<string, string>& properties) const {
    const string& devicePropValue = (properties.find(HipDeviceIndex()) == properties.end() ?
            getPropertyDefaultValue(HipDeviceIndex()) : properties.find(HipDeviceIndex())->second);
    string blockingPropValue = (properties.find(HipUseBlockingSync()) == properties.end() ?
            getPropertyDefaultValue(HipUseBlockingSync()) : properties.find(HipUseBlockingSync())->second);
    string precisionPropValue = (properties.find(HipPrecision()) == properties.end() ?
            getPropertyDefaultValue(HipPrecision()) : properties.find(HipPrecision())->second);
    string cpuPmePropValue = (properties.find(HipUseCpuPme()) == properties.end() ?
            getPropertyDefaultValue(HipUseCpuPme()) : properties.find(HipUseCpuPme())->second);
    const string& tempPropValue = (properties.find(HipTempDirectory()) == properties.end() ?
            getPropertyDefaultValue(HipTempDirectory()) : properties.find(HipTempDirectory())->second);
    string pmeStreamPropValue = (properties.find(HipDisablePmeStream()) == properties.end() ?
            getPropertyDefaultValue(HipDisablePmeStream()) : properties.find(HipDisablePmeStream())->second);
    string deterministicForcesValue = (properties.find(HipDeterministicForces()) == properties.end() ?
            getPropertyDefaultValue(HipDeterministicForces()) : properties.find(HipDeterministicForces())->second);
    string fftBackendValue = (properties.find(HipFFTBackend()) == properties.end() ?
            getPropertyDefaultValue(HipFFTBackend()) : properties.find(HipFFTBackend())->second);
    transform(blockingPropValue.begin(), blockingPropValue.end(), blockingPropValue.begin(), ::tolower);
    transform(precisionPropValue.begin(), precisionPropValue.end(), precisionPropValue.begin(), ::tolower);
    transform(cpuPmePropValue.begin(), cpuPmePropValue.end(), cpuPmePropValue.begin(), ::tolower);
    transform(pmeStreamPropValue.begin(), pmeStreamPropValue.end(), pmeStreamPropValue.begin(), ::tolower);
    transform(deterministicForcesValue.begin(), deterministicForcesValue.end(), deterministicForcesValue.begin(), ::tolower);
    transform(fftBackendValue.begin(), fftBackendValue.end(), fftBackendValue.begin(), ::tolower);
    vector<string> pmeKernelName;
    pmeKernelName.push_back(CalcPmeReciprocalForceKernel::Name());
    if (!supportsKernels(pmeKernelName))
        cpuPmePropValue = "false";
    int threads = getNumProcessors();
    char* threadsEnv = getenv("OPENMM_CPU_THREADS");
    if (threadsEnv != NULL)
        stringstream(threadsEnv) >> threads;
    context.setPlatformData(new PlatformData(&context, context.getSystem(), devicePropValue, blockingPropValue, precisionPropValue, cpuPmePropValue, tempPropValue,
            pmeStreamPropValue, deterministicForcesValue, fftBackendValue, threads, NULL));
}

void HipPlatform::linkedContextCreated(ContextImpl& context, ContextImpl& originalContext) const {
    Platform& platform = originalContext.getPlatform();
    string devicePropValue = platform.getPropertyValue(originalContext.getOwner(), HipDeviceIndex());
    string blockingPropValue = platform.getPropertyValue(originalContext.getOwner(), HipUseBlockingSync());
    string precisionPropValue = platform.getPropertyValue(originalContext.getOwner(), HipPrecision());
    string cpuPmePropValue = platform.getPropertyValue(originalContext.getOwner(), HipUseCpuPme());
    string tempPropValue = platform.getPropertyValue(originalContext.getOwner(), HipTempDirectory());
    string pmeStreamPropValue = platform.getPropertyValue(originalContext.getOwner(), HipDisablePmeStream());
    string deterministicForcesValue = platform.getPropertyValue(originalContext.getOwner(), HipDeterministicForces());
    string fftBackendValue = platform.getPropertyValue(originalContext.getOwner(), HipFFTBackend());
    int threads = reinterpret_cast<PlatformData*>(originalContext.getPlatformData())->threads.getNumThreads();
    context.setPlatformData(new PlatformData(&context, context.getSystem(), devicePropValue, blockingPropValue, precisionPropValue, cpuPmePropValue, tempPropValue,
            pmeStreamPropValue, deterministicForcesValue, fftBackendValue, threads, &originalContext));
}

void HipPlatform::contextDestroyed(ContextImpl& context) const {
    PlatformData* data = reinterpret_cast<PlatformData*>(context.getPlatformData());
    delete data;
}

HipPlatform::PlatformData::PlatformData(ContextImpl* context, const System& system, const string& deviceIndexProperty, const string& blockingProperty, const string& precisionProperty,
              const string& cpuPmeProperty, const string& tempProperty, const string& pmeStreamProperty,
              const string& deterministicForcesProperty, const string& fftBackendProperty, int numThreads, ContextImpl* originalContext) :
                context(context), removeCM(false), stepCount(0), computeForceCount(0), time(0.0), hasInitializedContexts(false),
                threads(numThreads) {
    bool blocking = (blockingProperty == "true");
    vector<string> devices;
    size_t searchPos = 0, nextPos;
    while ((nextPos = deviceIndexProperty.find_first_of(", ", searchPos)) != string::npos) {
        devices.push_back(deviceIndexProperty.substr(searchPos, nextPos-searchPos));
        searchPos = nextPos+1;
    }
    devices.push_back(deviceIndexProperty.substr(searchPos));
    PlatformData* originalData = NULL;
    if (originalContext != NULL)
        originalData = reinterpret_cast<PlatformData*>(originalContext->getPlatformData());
    try {
        for (int i = 0; i < (int) devices.size(); i++) {
            if (devices[i].length() > 0) {
                int deviceIndex;
                stringstream(devices[i]) >> deviceIndex;
                contexts.push_back(new HipContext(system, deviceIndex, blocking, precisionProperty, tempProperty, *this, (originalData == NULL ? NULL : originalData->contexts[i])));
            }
        }
        if (contexts.size() == 0)
            contexts.push_back(new HipContext(system, -1, blocking, precisionProperty, tempProperty, *this, (originalData == NULL ? NULL : originalData->contexts[0])));
    }
    catch (...) {
        // If an exception was thrown, do our best to clean up memory.

        for (int i = 0; i < (int) contexts.size(); i++)
            delete contexts[i];
        throw;
    }
    stringstream deviceIndex, deviceName;
    for (int i = 0; i < (int) contexts.size(); i++) {
        if (i > 0) {
            deviceIndex << ',';
            deviceName << ',';
        }
        deviceIndex << contexts[i]->getDeviceIndex();
        char name[1000];
        CHECK_RESULT(hipDeviceGetName(name, 1000, contexts[i]->getDevice()), "Error querying device name");
        deviceName << name;
    }

    useCpuPme = (cpuPmeProperty == "true" && !contexts[0]->getUseDoublePrecision());
    disablePmeStream = (pmeStreamProperty == "true");
    deterministicForces = (deterministicForcesProperty == "true");
    propertyValues[HipPlatform::HipDeviceIndex()] = deviceIndex.str();
    propertyValues[HipPlatform::HipDeviceName()] = deviceName.str();
    propertyValues[HipPlatform::HipUseBlockingSync()] = blocking ? "true" : "false";
    propertyValues[HipPlatform::HipPrecision()] = precisionProperty;
    propertyValues[HipPlatform::HipUseCpuPme()] = useCpuPme ? "true" : "false";
    propertyValues[HipPlatform::HipTempDirectory()] = tempProperty;
    propertyValues[HipPlatform::HipDisablePmeStream()] = disablePmeStream ? "true" : "false";
    propertyValues[HipPlatform::HipDeterministicForces()] = deterministicForces ? "true" : "false";
    propertyValues[HipPlatform::HipFFTBackend()] = fftBackendProperty;
    contextEnergy.resize(contexts.size());

    // Determine whether peer-to-peer copying is supported, and enable it if so.

    peerAccessSupported = true;
    for (int i = 1; i < contexts.size(); i++) {
        int canAccess;
        hipDeviceCanAccessPeer(&canAccess, contexts[i]->getDevice(), contexts[0]->getDevice());
        if (!canAccess) {
            peerAccessSupported = false;
            break;
        }
    }
}

HipPlatform::PlatformData::~PlatformData() {
    for (int i = 0; i < (int) contexts.size(); i++)
        delete contexts[i];
}

void HipPlatform::PlatformData::initializeContexts(const System& system) {
    if (hasInitializedContexts)
        return;
    for (int i = 0; i < (int) contexts.size(); i++)
        contexts[i]->initialize();
    hasInitializedContexts = true;
}

void HipPlatform::PlatformData::syncContexts() {
    for (int i = 0; i < (int) contexts.size(); i++)
        contexts[i]->getWorkThread().flush();
}
