Make Kokkos init/finalize calls safer

This commit is contained in:
Stan Gerald Moore
2021-06-24 13:25:35 -06:00
parent ddac9c21dc
commit 2d5e8f050a
6 changed files with 39 additions and 15 deletions

View File

@ -189,7 +189,7 @@ KokkosLMP::KokkosLMP(LAMMPS *lmp, int narg, char **arg) : Pointers(lmp)
args.num_numa = numa; args.num_numa = numa;
args.device_id = device; args.device_id = device;
Kokkos::initialize(args); initialize(args);
// default settings for package kokkos command // default settings for package kokkos command
@ -302,6 +302,25 @@ KokkosLMP::~KokkosLMP()
} }
/* ---------------------------------------------------------------------- */
void KokkosLMP::initialize(Kokkos::InitArguments args)
{
if (!Kokkos::is_initialized())
Kokkos::initialize(args);
}
/* ---------------------------------------------------------------------- */
void KokkosLMP::finalize()
{
static int is_finalized = 0;
if (Kokkos::is_initialized() && !is_finalized)
Kokkos::finalize();
is_finalized = 1;
}
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
invoked by package kokkos command invoked by package kokkos command
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */

View File

@ -51,6 +51,8 @@ class KokkosLMP : protected Pointers {
KokkosLMP(class LAMMPS *, int, char **); KokkosLMP(class LAMMPS *, int, char **);
~KokkosLMP(); ~KokkosLMP();
static void initialize(Kokkos::InitArguments);
static void finalize();
void accelerator(int, char **); void accelerator(int, char **);
int neigh_count(int); int neigh_count(int);

View File

@ -43,6 +43,10 @@
#include "modify.h" #include "modify.h"
#include "neighbor.h" #include "neighbor.h"
namespace Kokkos {
typedef int InitArguements;
};
#define LAMMPS_INLINE inline #define LAMMPS_INLINE inline
namespace LAMMPS_NS { namespace LAMMPS_NS {
@ -56,17 +60,13 @@ class KokkosLMP {
KokkosLMP(class LAMMPS *, int, char **) { kokkos_exists = 0; } KokkosLMP(class LAMMPS *, int, char **) { kokkos_exists = 0; }
~KokkosLMP() {} ~KokkosLMP() {}
static void initialize(Kokkos::InitArguements args) {}
static void finalize() {}
void accelerator(int, char **) {} void accelerator(int, char **) {}
int neigh_list_kokkos(int) { return 0; } int neigh_list_kokkos(int) { return 0; }
int neigh_count(int) { return 0; } int neigh_count(int) { return 0; }
}; };
class Kokkos {
public:
static int is_initialized() {return false;}
static void finalize() {}
};
class AtomKokkos : public Atom { class AtomKokkos : public Atom {
public: public:
tagint **k_special; tagint **k_special;

View File

@ -81,7 +81,7 @@ void Error::universe_all(const std::string &file, int line, const std::string &s
throw LAMMPSException(mesg); throw LAMMPSException(mesg);
#else #else
if (Kokkos::is_initialized()) Kokkos::finalize(); KokkosLMP::finalize();
MPI_Finalize(); MPI_Finalize();
exit(1); exit(1);
#endif #endif
@ -107,6 +107,7 @@ void Error::universe_one(const std::string &file, int line, const std::string &s
throw LAMMPSAbortException(mesg, universe->uworld); throw LAMMPSAbortException(mesg, universe->uworld);
#else #else
KokkosLMP::finalize();
MPI_Abort(universe->uworld,1); MPI_Abort(universe->uworld,1);
exit(1); // to trick "smart" compilers into believing this does not return exit(1); // to trick "smart" compilers into believing this does not return
#endif #endif
@ -173,8 +174,8 @@ void Error::all(const std::string &file, int line, const std::string &str)
if (screen && screen != stdout) fclose(screen); if (screen && screen != stdout) fclose(screen);
if (logfile) fclose(logfile); if (logfile) fclose(logfile);
KokkosLMP::finalize();
if (universe->nworlds > 1) MPI_Abort(universe->uworld,1); if (universe->nworlds > 1) MPI_Abort(universe->uworld,1);
if (Kokkos::is_initialized()) Kokkos::finalize();
MPI_Finalize(); MPI_Finalize();
exit(1); exit(1);
#endif #endif
@ -213,7 +214,7 @@ void Error::one(const std::string &file, int line, const std::string &str)
#else #else
if (screen) fflush(screen); if (screen) fflush(screen);
if (logfile) fflush(logfile); if (logfile) fflush(logfile);
if (Kokkos::is_initialized()) Kokkos::finalize(); KokkosLMP::finalize();
MPI_Abort(world,1); MPI_Abort(world,1);
exit(1); // to trick "smart" compilers into believing this does not return exit(1); // to trick "smart" compilers into believing this does not return
#endif #endif
@ -316,7 +317,7 @@ void Error::done(int status)
if (screen && screen != stdout) fclose(screen); if (screen && screen != stdout) fclose(screen);
if (logfile) fclose(logfile); if (logfile) fclose(logfile);
if (Kokkos::is_initialized()) Kokkos::finalize(); KokkosLMP::finalize();
MPI_Finalize(); MPI_Finalize();
exit(status); exit(status);
} }

View File

@ -356,7 +356,7 @@ void lammps_mpi_finalize()
void lammps_kokkos_finalize() void lammps_kokkos_finalize()
{ {
if (Kokkos::is_initialized()) Kokkos::finalize(); KokkosLMP::finalize();
} }
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------

View File

@ -78,15 +78,16 @@ int main(int argc, char **argv)
lammps->input->file(); lammps->input->file();
delete lammps; delete lammps;
} catch (LAMMPSAbortException &ae) { } catch (LAMMPSAbortException &ae) {
if (Kokkos::is_initialized()) Kokkos::finalize(); KokkosLMP::finalize();
MPI_Abort(ae.universe, 1); MPI_Abort(ae.universe, 1);
} catch (LAMMPSException &e) { } catch (LAMMPSException &e) {
if (Kokkos::is_initialized()) Kokkos::finalize(); KokkosLMP::finalize();
MPI_Barrier(lammps_comm); MPI_Barrier(lammps_comm);
MPI_Finalize(); MPI_Finalize();
exit(1); exit(1);
} catch (fmt::format_error &fe) { } catch (fmt::format_error &fe) {
fprintf(stderr, "fmt::format_error: %s\n", fe.what()); fprintf(stderr, "fmt::format_error: %s\n", fe.what());
KokkosLMP::finalize();
MPI_Abort(MPI_COMM_WORLD, 1); MPI_Abort(MPI_COMM_WORLD, 1);
exit(1); exit(1);
} }
@ -97,11 +98,12 @@ int main(int argc, char **argv)
delete lammps; delete lammps;
} catch (fmt::format_error &fe) { } catch (fmt::format_error &fe) {
fprintf(stderr, "fmt::format_error: %s\n", fe.what()); fprintf(stderr, "fmt::format_error: %s\n", fe.what());
KokkosLMP::finalize();
MPI_Abort(MPI_COMM_WORLD, 1); MPI_Abort(MPI_COMM_WORLD, 1);
exit(1); exit(1);
} }
#endif #endif
if (Kokkos::is_initialized()) Kokkos::finalize(); KokkosLMP::finalize();
MPI_Barrier(lammps_comm); MPI_Barrier(lammps_comm);
MPI_Finalize(); MPI_Finalize();
} }