// clang-format off /* -*- c++ -*- ---------------------------------------------------------- LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator https://www.lammps.org/, Sandia National Laboratories LAMMPS development team: developers@lammps.org Copyright (2003) Sandia Corporation. Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains certain rights in this software. This software is distributed under the GNU General Public License. See the README file in the top-level LAMMPS directory. ------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- Contributing author: Matt Bettencourt (NVIDIA) ------------------------------------------------------------------------- */ #ifdef MLIAP_PYTHON #include "mliap_model_python_kokkos.h" #include "mliap_data_kokkos.h" #include "comm.h" #include "error.h" #include "utils.h" #include "mliap_model_python_couple_kokkos.h" #include "lmppython.h" #include "python_compat.h" #include using namespace LAMMPS_NS; template MLIAPModelPythonKokkos::~MLIAPModelPythonKokkos() { auto nontemplated_this = static_cast((void*)this); if (model_loaded) MLIAPPYKokkos_unload_model(nontemplated_this); model_loaded=false; } template MLIAPModelPythonKokkos::MLIAPModelPythonKokkos(LAMMPS *lmp, char *coefffilename) : MLIAPModelPython(lmp,coefffilename,true), MLIAPModelKokkos(lmp, this) { if (!std::is_same_v ) MLIAPModelKokkos::error->all(FLERR, "MLIAP Kokkos version of the python interface is ONLY available on device"); model_loaded = 0; MLIAPModelKokkos::python->init(); PyGILState_STATE gstate = PyGILState_Ensure(); PyObject *pyMain = PyImport_AddModule("__main__"); if (!pyMain) { PyGILState_Release(gstate); MLIAPModelKokkos::error->all(FLERR, "Could not initialize embedded Python"); } PyObject *coupling_module = PyImport_ImportModule("mliap_model_python_couple_kokkos"); if (!coupling_module) { PyErr_Print(); PyErr_Clear(); PyGILState_Release(gstate); MLIAPModelKokkos::error->all(FLERR, "Loading MLIAPPYKokkos coupling module failure."); } // Recipe from lammps/src/pair_python.cpp : // add current directory to PYTHONPATH PyObject *py_path = PySys_GetObject((char *) "path"); PyList_Append(py_path, PY_STRING_FROM_STRING(".")); // if LAMMPS_POTENTIALS environment variable is set, add it to PYTHONPATH as well const char *potentials_path = getenv("LAMMPS_POTENTIALS"); if (potentials_path != nullptr) { PyList_Append(py_path, PY_STRING_FROM_STRING(potentials_path)); } PyGILState_Release(gstate); if (coefffilename) read_coeffs(coefffilename); if (coefffilename) MLIAPModelKokkos::set_k_coeffelem(); nonlinearflag = 1; } /* ---------------------------------------------------------------------- */ template void MLIAPModelPythonKokkos::read_coeffs(char *fname) { PyGILState_STATE gstate = PyGILState_Ensure(); auto nontemplated_this = static_cast((void*)this); model_loaded = MLIAPPYKokkos_load_model(nontemplated_this, fname); if (PyErr_Occurred()) { PyErr_Print(); PyErr_Clear(); PyGILState_Release(gstate); MLIAPModelKokkos::error->all(FLERR, "Loading python model failure."); } PyGILState_Release(gstate); if (model_loaded) { this->connect_param_counts(); } else { if (MLIAPModelKokkos::comm->me == 0) utils::logmesg(MLIAPModelKokkos::lmp, "Loading python model deferred.\n"); } } /* ---------------------------------------------------------------------- */ // Finalize loading of the model. template void MLIAPModelPythonKokkos::connect_param_counts() { PyGILState_STATE gstate = PyGILState_Ensure(); auto nontemplated_this = static_cast((void*)this); nelements = MLIAPPYKokkos_nelements(nontemplated_this); nparams = MLIAPPYKokkos_nparams(nontemplated_this); ndescriptors = MLIAPPYKokkos_ndescriptors(nontemplated_this); if (PyErr_Occurred()) { PyErr_Print(); PyErr_Clear(); PyGILState_Release(gstate); MLIAPModelKokkos::error->all(FLERR, "Loading python model failure."); } PyGILState_Release(gstate); model_loaded = 1; utils::logmesg(MLIAPModelKokkos::lmp, "Loading python model complete.\n"); } /* ---------------------------------------------------------------------- */ template void MLIAPModelPythonKokkos::compute_gradients(class MLIAPData *data) { if (!model_loaded) { MLIAPModelKokkos::error->all(FLERR, "Model not loaded."); } PyGILState_STATE gstate = PyGILState_Ensure(); auto nontemplated_this = static_cast((void*)this); auto *kokkos_data = dynamic_cast*>(data); MLIAPDataKokkosDevice raw_data(*kokkos_data); MLIAPPYKokkos_compute_gradients(nontemplated_this, &raw_data); if (PyErr_Occurred()) { PyErr_Print(); PyErr_Clear(); PyGILState_Release(gstate); MLIAPModelKokkos::error->all(FLERR, "Running python model failure."); } PyGILState_Release(gstate); } /* ---------------------------------------------------------------------- */ template void MLIAPModelPythonKokkos::compute_gradgrads(class MLIAPData *data) { MLIAPModelPython::compute_gradgrads(data); } /* ---------------------------------------------------------------------- */ template void MLIAPModelPythonKokkos::compute_force_gradients(class MLIAPData *data) { MLIAPModelPython::compute_force_gradients(data); } /* ---------------------------------------------------------------------- */ namespace LAMMPS_NS { template class MLIAPModelPythonKokkos; #ifdef LMP_KOKKOS_GPU template class MLIAPModelPythonKokkos; #endif } #endif