Unverified Commit f2a97788 authored by Axel Kohlmeyer's avatar Axel Kohlmeyer Committed by GitHub
Browse files

Merge pull request #1290 from akohlmey/python-script-portability

Use argparse module in for Install.py scripts in lib and further portability and consistency enhancements. Some lint removal as well.
parents a9f8b17c 8ee1fefe
Loading
Loading
Loading
Loading
+10 −5
Original line number Diff line number Diff line
@@ -559,10 +559,15 @@ if(PKG_USER-PLUMED)
    message(STATUS "PLUMED download requested - we will build our own")
    include(ExternalProject)
    ExternalProject_Add(plumed_build
      URL https://github.com/plumed/plumed2/releases/download/v2.4.3/plumed-src-2.4.3.tgz
      URL_MD5 b1be7c48971627febc11c61b70767fc5
      URL https://github.com/plumed/plumed2/releases/download/v2.4.4/plumed-src-2.4.4.tgz
      URL_MD5 71ed465bdc7c2059e282dbda8d564e71
      BUILD_IN_SOURCE 1
      CONFIGURE_COMMAND <SOURCE_DIR>/configure --prefix=<INSTALL_DIR> ${CONFIGURE_REQUEST_PIC})
      CONFIGURE_COMMAND <SOURCE_DIR>/configure --prefix=<INSTALL_DIR>
                                               ${CONFIGURE_REQUEST_PIC}
                                                --enable-modules=all
                                               CXX=${CMAKE_MPI_CXX_COMPILER}
                                               CC=${CMAKE_MPI_C_COMPILER}
    )
    ExternalProject_get_property(plumed_build INSTALL_DIR)
    set(PLUMED_INSTALL_DIR ${INSTALL_DIR})
    list(APPEND LAMMPS_DEPS plumed_build)
@@ -614,8 +619,8 @@ if(PKG_USER-SMD)
    message(STATUS "Eigen3 download requested - we will build our own")
    include(ExternalProject)
    ExternalProject_Add(Eigen3_build
      URL http://bitbucket.org/eigen/eigen/get/3.3.4.tar.gz
      URL_MD5 1a47e78efe365a97de0c022d127607c3
      URL http://bitbucket.org/eigen/eigen/get/3.3.7.tar.gz
      URL_MD5 f2a417d083fe8ca4b8ed2bc613d20f07
      CONFIGURE_COMMAND "" BUILD_COMMAND "" INSTALL_COMMAND ""
    )
    ExternalProject_get_property(Eigen3_build SOURCE_DIR)
+2 −1
Original line number Diff line number Diff line
@@ -95,6 +95,7 @@ which GPU hardware to build for.

GPU_ARCH settings for different GPU hardware is as follows:

sm_12 or sm_13 for GT200 (supported by CUDA 3.2 until CUDA 6.5)
sm_20 or sm_21 for Fermi (supported by CUDA 3.2 until CUDA 7.5)
sm_30 or sm_35 or sm_37 for Kepler (supported since CUDA 5)
sm_50 or sm_52 for Maxwell (supported since CUDA 6)
@@ -135,7 +136,7 @@ specified by the "-m" switch. For your convenience, machine makefiles
for "mpi" and "serial" are provided, which have the same settings as
the corresponding machine makefiles in the main LAMMPS source
folder. In addition you can alter 4 important settings in the
Makefile.machine you start from via the corresponding -h, -a, -p, -e
Makefile.machine you start from via the corresponding -c, -a, -p, -e
switches (as in the examples above), and also save a copy of the new
Makefile if desired:

+58 −38
Original line number Diff line number Diff line
#!/usr/bin/env python

# install.py tool to do a generic build of a library
# soft linked to by many of the lib/Install.py files
# used to automate the steps described in the corresponding lib/README
"""
Install.py tool to do a generic build of a library
soft linked to by many of the lib/Install.py files
used to automate the steps described in the corresponding lib/README
"""

from __future__ import print_function
import sys, os, subprocess
from argparse import ArgumentParser

sys.path.append('..')
from install_helpers import error,get_cpus

# parse args

args = sys.argv[1:]
nargs = len(args)
if nargs == 0: error()

machine = None
extraflag = 0

iarg = 0
while iarg < nargs:
  if args[iarg] == "-m":
    if iarg+2 > nargs: error()
    machine = args[iarg+1]
    iarg += 2
  elif args[iarg] == "-e":
    if iarg+2 > nargs: error()
    extraflag = 1
    suffix = args[iarg+1]
    iarg += 2
  else: error()
from install_helpers import get_cpus, fullpath

parser = ArgumentParser(prog='Install.py',
                        description="LAMMPS library build wrapper script")

HELP = """
Syntax from src dir: make lib-libname args="-m machine -e suffix"
Syntax from lib dir: python Install.py -m machine -e suffix

libname = name of lib dir (e.g. atc, h5md, meam, poems, etc)
specify -m and optionally -e, order does not matter

Examples:

make lib-poems args="-m serial" # build POEMS lib with same settings as in the serial Makefile in src
make lib-colvars args="-m mpi"  # build USER-COLVARS lib with same settings as in the mpi Makefile in src
make lib-meam args="-m ifort"   # build MEAM lib with custom Makefile.ifort (using Intel Fortran)
"""

# parse and process arguments

parser.add_argument("-m", "--machine",
                    help="suffix of a <libname>/Makefile.* file used for compiling this library")
parser.add_argument("-e", "--extramake",
                    help="set EXTRAMAKE variable in <libname>/Makefile.<machine> to Makefile.lammps.<extramake>")

args = parser.parse_args()

# print help message and exit, if neither build nor path options are given
if not args.machine and not args.extramake:
  parser.print_help()
  sys.exit(HELP)

machine = args.machine
extraflag = not args.extramake
suffix = args.extramake

# set lib from working dir

cwd = os.getcwd()
cwd = fullpath('.')
lib = os.path.basename(cwd)

# create Makefile.auto as copy of Makefile.machine
# reset EXTRAMAKE if requested

if not os.path.exists("Makefile.%s" % machine):
  error("lib/%s/Makefile.%s does not exist" % (lib,machine))
  sys.exit("lib/%s/Makefile.%s does not exist" % (lib, machine))

lines = open("Makefile.%s" % machine, 'r').readlines()
fp = open("Makefile.auto", 'w')
@@ -68,7 +85,10 @@ except subprocess.CalledProcessError as e:
  print("Make failed with:\n %s" % e.output.decode('UTF-8'))
  sys.exit(1)

if os.path.exists("lib%s.a" % lib): print("Build was successful")
else: error("Build of lib/%s/lib%s.a was NOT successful" % (lib,lib))
if os.path.exists("lib%s.a" % lib):
  print("Build was successful")
else:
  sys.exit("Build of lib/%s/lib%s.a was NOT successful" % (lib, lib))

if has_extramake and not os.path.exists("Makefile.lammps"):
  print("lib/%s/Makefile.lammps was NOT created" % lib)
  print("WARNING: lib/%s/Makefile.lammps was NOT created" % lib)
+26 −25
Original line number Diff line number Diff line
@@ -5,7 +5,12 @@
from __future__ import print_function
import sys,os,subprocess
sys.path.append('..')
from install_helpers import error,get_cpus
from install_helpers import get_cpus

from argparse import ArgumentParser

parser = ArgumentParser(prog='Install.py',
                        description="LAMMPS library build wrapper script")

# help message

@@ -26,37 +31,33 @@ Examples:
make lib-colvars args="-m mpi"     # build COLVARS lib with default mpi compiler wrapper
"""

# parse args
# set lib from working dir

args = sys.argv[1:]
nargs = len(args)
if nargs == 0: error(help=help)
cwd = os.getcwd()
lib = os.path.basename(cwd)

machine = None
extraflag = False
# parse and process arguments

iarg = 0
while iarg < nargs:
  if args[iarg] == "-m":
    if iarg+2 > len(args): error(help=help)
    machine = args[iarg+1]
    iarg += 2
  elif args[iarg] == "-e":
    if iarg+2 > len(args): error(help=help)
    extraflag = True
    suffix = args[iarg+1]
    iarg += 2
  else: error(help=help)
parser.add_argument("-m", "--machine",
                    help="suffix of a <libname>/Makefile.* or of a src/MAKE/MACHINES/Makefile.* file used for compiling this library")
parser.add_argument("-e", "--extramake",
                    help="set EXTRAMAKE variable in <libname>/Makefile.<machine> to Makefile.lammps.<extramake>")

# set lib from working dir
args = parser.parse_args()

cwd = os.getcwd()
lib = os.path.basename(cwd)
# print help message and exit, if neither build nor path options are given
if not args.machine and not args.extramake:
  parser.print_help()
  sys.exit(help)

machine = args.machine
extraflag = args.extramake != None
suffix = args.extramake

def get_lammps_machine_flags(machine):
  """Parse Makefile.machine from LAMMPS, return dictionary of compiler flags"""
  if not os.path.exists("../../src/MAKE/MACHINES/Makefile.%s" % machine):
    error("Cannot locate src/MAKE/MACHINES/Makefile.%s" % machine)
    sys.exit("Cannot locate src/MAKE/MACHINES/Makefile.%s" % machine)
  lines = open("../../src/MAKE/MACHINES/Makefile.%s" % machine,
               'r').readlines()
  machine_flags = {}
@@ -102,7 +103,7 @@ if not os.path.exists("Makefile.%s" % machine):
  machine_flags = get_lammps_machine_flags(machine)
  gen_colvars_makefile_machine(machine, machine_flags)
if not os.path.exists("Makefile.%s" % machine):
  error("lib/%s/Makefile.%s does not exist" % (lib,machine))
  sys.exit("lib/%s/Makefile.%s does not exist" % (lib,machine))

# create Makefile.auto as copy of Makefile.machine
# reset EXTRAMAKE if requested
@@ -131,6 +132,6 @@ except subprocess.CalledProcessError as e:
  sys.exit(1)

if os.path.exists("lib%s.a" % lib): print("Build was successful")
else: error("Build of lib/%s/lib%s.a was NOT successful" % (lib,lib))
else: sys.exit("Build of lib/%s/lib%s.a was NOT successful" % (lib,lib))
if not os.path.exists("Makefile.lammps"):
  print("lib/%s/Makefile.lammps was NOT created" % lib)
+77 −90
Original line number Diff line number Diff line
#!/usr/bin/env python

# Install.py tool to build the GPU library
# used to automate the steps described in the README file in this dir
"""
Install.py tool to build the GPU library
used to automate the steps described in the README file in this dir
"""

from __future__ import print_function
import sys,os,subprocess
import sys, os, subprocess, shutil
from argparse import ArgumentParser

sys.path.append('..')
from install_helpers import error,get_cpus
from install_helpers import get_cpus

parser = ArgumentParser(prog='Install.py',
                        description="LAMMPS library build wrapper script")

# help message

help = """
HELP = """
Syntax from src dir: make lib-gpu args="-m machine -h hdir -a arch -p precision -e esuffix -b -o osuffix"
Syntax from lib dir: python Install.py -m machine -h hdir -a arch -p precision -e esuffix -b -o osuffix

@@ -23,28 +30,8 @@ optionally uses Makefile.auto to build the GPU library -> libgpu.a
  and to copy a Makefile.lammps.esuffix -> Makefile.lammps
optionally copies Makefile.auto to a new Makefile.osuffix

  -m = use Makefile.machine as starting point, copy to Makefile.auto
       default machine = linux
       default for -h, -a, -p, -e settings are those in -m Makefile
  -h = set CUDA_HOME variable in Makefile.auto to hdir
       hdir = path to NVIDIA Cuda software, e.g. /usr/local/cuda
  -a = set CUDA_ARCH variable in Makefile.auto to arch
       use arch = sm_20 for Fermi (C2050/C2070, deprecated as of CUDA 8.0)
                        or GeForce GTX 580 or similar
       use arch = sm_30 for Kepler (K10)
       use arch = sm_35 for Kepler (K40) or GeForce GTX Titan or similar
       use arch = sm_37 for Kepler (dual K80)
       use arch = sm_60 for Pascal (P100)
       use arch = sm_70 for Volta
  -p = set CUDA_PRECISION variable in Makefile.auto to precision
       use precision = double or mixed or single
  -e = set EXTRAMAKE variable in Makefile.auto to Makefile.lammps.esuffix
  -b = make the GPU library using Makefile.auto
       first performs a "make clean"
       then produces libgpu.a if successful
       also copies EXTRAMAKE file -> Makefile.lammps
         -e can set which Makefile.lammps.esuffix file is copied
  -o = copy final Makefile.auto to Makefile.osuffix
See lib/gpu/README and the LAMMPS manual for more information
on which settings to use and how to build.

Examples:

@@ -53,64 +40,65 @@ make lib-gpu args="-m xk7 -p single -o xk7.single" # create new Makefile.xk
make lib-gpu args="-m mpi -a sm_35 -p single -o mpi.mixed -b" # create new Makefile.mpi.mixed, also build GPU lib with these settings
"""

# parse args

args = sys.argv[1:]
nargs = len(args)
if nargs == 0: error(help=help)

isuffix = "linux"
hflag = aflag = pflag = eflag = 0
# parse and process arguments

parser.add_argument("-b", "--build", action="store_true",
                    help="build the GPU library from scratch from a customized Makefile.auto")
parser.add_argument("-m", "--machine", default='linux',
                    help="suffix of Makefile.machine used as base for customizing Makefile.auto")
parser.add_argument("-a", "--arch", default='sm_30',
                    choices=['sm_12', 'sm_13', 'sm_20', 'sm_21', 'sm_30', 'sm_35', 'sm_37',
                             'sm_50', 'sm_52', 'sm_60', 'sm_61', 'sm_70', 'sm_75'],
                    help="set GPU architecture and instruction set (default: 'sm_30')")
parser.add_argument("-p", "--precision", default='mixed', choices=['single', 'mixed', 'double'],
                    help="set GPU kernel precision mode (default: mixed)")
parser.add_argument("-e", "--extramake", default='standard',
                    help="set EXTRAMAKE variable in Makefile.auto to Makefile.lammps.<extramake>")
parser.add_argument("-c", "--cuda",
                    help="set CUDA_HOME variable in Makefile.auto. Will be used if $CUDA_HOME environment variable is not set")
parser.add_argument("-o", "--output",
                    help="if set, copy final Makefile.auto to Makefile.<output> for later re-use")

args = parser.parse_args()

# print help message and exit, if neither build nor output options are given
if not args.build and not args.output:
  parser.print_help()
  sys.exit(HELP)

hflag = 0
eflag = 0
makeflag = 0
outflag = 0

iarg = 0
while iarg < nargs:
  if args[iarg] == "-m":
    if iarg+2 > nargs: error(help=help)
    isuffix = args[iarg+1]
    iarg += 2
  elif args[iarg] == "-h":
    if iarg+2 > nargs: error(help=help)
    hflag = 1
    hdir = args[iarg+1]
    iarg += 2
  elif args[iarg] == "-a":
    if iarg+2 > nargs: error(help=help)
    aflag = 1
    arch = args[iarg+1]
    iarg += 2
  elif args[iarg] == "-p":
    if iarg+2 > nargs: error(help=help)
    pflag = 1
    precision = args[iarg+1]
    iarg += 2
  elif args[iarg] == "-e":
    if iarg+2 > nargs: error(help=help)
    eflag = 1
    lmpsuffix = args[iarg+1]
    iarg += 2
  elif args[iarg] == "-b":
if args.build:
  makeflag = 1
    iarg += 1
  elif args[iarg] == "-o":
    if iarg+2 > nargs: error(help=help)
    outflag = 1
    osuffix = args[iarg+1]
    iarg += 2
  else: error(help=help)

if pflag:
  if precision == "double": precstr = "-D_DOUBLE_DOUBLE"
  elif precision == "mixed": precstr = "-D_SINGLE_DOUBLE"
  elif precision == "single": precstr = "-D_SINGLE_SINGLE"
  else: error("Invalid precision setting")
isuffix = args.machine
arch = args.arch

if args.precision == "double":
  precstr = "-D_DOUBLE_DOUBLE"
elif args.precision == "mixed":
  precstr = "-D_SINGLE_DOUBLE"
else:
  precstr = "-D_SINGLE_SINGLE"

lmpsuffix = args.extramake

if args.cuda:
  hflag = 1
  hdir = args.cuda

if args.output:
  outflag = 1
  osuffix = args.output

# create Makefile.auto
# reset EXTRAMAKE, CUDA_HOME, CUDA_ARCH, CUDA_PRECISION if requested

if not os.path.exists("Makefile.%s" % isuffix):
  error("lib/gpu/Makefile.%s does not exist" % isuffix)
  sys.exit("lib/gpu/Makefile.%s does not exist" % isuffix)

lines = open("Makefile.%s" % isuffix, 'r').readlines()
fp = open("Makefile.auto", 'w')
@@ -123,9 +111,9 @@ for line in lines:

  if hflag and words[0] == "CUDA_HOME" and words[1] == '=':
    line = line.replace(words[2], hdir)
  if aflag and words[0] == "CUDA_ARCH" and words[1] == '=':
  if words[0] == "CUDA_ARCH" and words[1] == '=':
    line = line.replace(words[2], "-arch=%s" % arch)
  if pflag and words[0] == "CUDA_PRECISION" and words[1] == '=':
  if words[0] == "CUDA_PRECISION" and words[1] == '=':
    line = line.replace(words[2], precstr)
  if eflag and words[0] == "EXTRAMAKE" and words[1] == '=':
    line = line.replace(words[2], "Makefile.lammps.%s" % lmpsuffix)
@@ -150,13 +138,12 @@ if makeflag:
    sys.exit(1)

  if not os.path.exists("libgpu.a"):
    error("Build of lib/gpu/libgpu.a was NOT successful")
    sys.exit("Build of lib/gpu/libgpu.a was NOT successful")
  if not os.path.exists("Makefile.lammps"):
    error("lib/gpu/Makefile.lammps was NOT created")
    sys.exit("lib/gpu/Makefile.lammps was NOT created")

# copy new Makefile.auto to Makefile.osuffix

if outflag:
  print("Creating new Makefile.%s" % osuffix)
  cmd = "cp Makefile.auto Makefile.%s" % osuffix
  subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)
  shutil.copyfile("Makefile.auto", "Makefile.%s" % osuffix)
Loading