Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build miniconda and conda environments. #277

Merged
merged 6 commits into from
Jul 15, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add conda ability in the stack
aerorahul committed Jul 12, 2021

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
commit d0428a5fae7ee8a756066784bcdfb8db04ff8330
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -33,6 +33,10 @@ WCOSS-Cray | Hang-Lei
Cheyenne | Dom Heinzeller
Gaea | Dom Heinzeller

## Contributors

Mark Potts, Steve Lawrence, Ed Hartnett, Guoqing Ge, Raffaele Montuoro, David Huber

## Prerequisites:

The prerequisites of building hpc-stack are:
@@ -143,6 +147,9 @@ script.
- [JSON Schema Validator for C++](https://github.com/pboettch/json-schema-validator)
- [pybind11](https://github.com/pybind/pybind11)
- [MADIS](https://madis-data.ncep.noaa.gov)
- [SQLite](https://www.sqlite.org)
- [PROJ](https://proj.org)
- [GEOS](https://www.osgeo.org/projects/geos)

* UFS Dependencies
- [ESMF](https://www.earthsystemcog.org/projects/esmf/)
@@ -179,7 +186,8 @@ script.
- [fckit](https://github.com/ecmwf/fckit.git)
- [atlas](https://github.com/ecmwf/atlas.git)

* Python Virtual Environments
* Python and Virtual Environments
- [Miniconda3](https://docs.conda.io/en/latest/)
- [r2d2](https://github.com/jcsda-internal/r2d2.git)

**IMPORTANT: Steps 1, 2, and 3 need to be repeated for each
@@ -313,6 +321,8 @@ config directory
5. Create a new module template at the appropriate place in the
modulefiles directory, using exising files as a template

6. Update `README.md` to include the name of the new library or package

### Configuring for a new HPC

If you want to port this to a new HPC, you need to follow these steps:
3 changes: 2 additions & 1 deletion build_stack.sh
Original file line number Diff line number Diff line change
@@ -185,8 +185,9 @@ build_lib ncio

build_lib madis

# Python virtual environments
# Python and associate virtual environments

build_lib miniconda3
build_lib r2d2

# JEDI 3rd party dependencies
3 changes: 2 additions & 1 deletion config/config_mac.sh
Original file line number Diff line number Diff line change
@@ -3,7 +3,8 @@
# Compiler/MPI combination
export HPC_COMPILER="clang/12.0.5"
export HPC_MPI="mpich/3.3.2"
export HPC_PYTHON="python/3.9.6"
#export HPC_PYTHON="python/3.9.6"
export HPC_PYTHON="miniconda3/4.7.12"

# Build options
export USE_SUDO=N
58 changes: 58 additions & 0 deletions libs/build_condaenv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash

set -eux

name=$1

var="STACK_${name}_version"
set +u
stack_version=${!var}
set -u
version=${2:-$stack_version}

var="STACK_${name}_environment"
set +u
stack_rqmts=${!var}
set -u
rqmts=${stack_rqmts:-"environments.yml"}

python=$(echo $HPC_PYTHON | sed 's/\//-/g')

if $MODULES; then
set +x
source $MODULESHOME/init/bash
module load hpc-$HPC_PYTHON
module list
set -x
prefix="${PREFIX:-"/opt/modules"}/$python/$name/$version"
if [[ -d $prefix ]]; then
[[ $OVERWRITE =~ [yYtT] ]] && ( echo "WARNING: $prefix EXISTS: OVERWRITING!"; $SUDO rm -rf $prefix ) \
|| ( echo "WARNING: $prefix EXISTS, SKIPPING"; exit 1 )
fi
else
nameUpper=$(echo $name | tr [a-z] [A-Z])
eval prefix="\${${nameUpper}_ROOT:-'/usr/local'}"
fi

cd ${HPC_STACK_ROOT}/${PKGDIR:-"pkg"}

# Check for conda environment file
rqmts_file=${HPC_STACK_ROOT}/pyvenv/$rqmts
[[ ! -f $rqmts_file ]] && ( echo "Unable to find enviroment file: $rqmts \nABORT!"; exit 1 )

# Determine python version; 3.x
python_version=$(python3 -c 'import sys;print(sys.version_info[0],".",sys.version_info[1],sep="")')

# Support python version >= 3.6
min_python_version=3.6
if (( $(echo "$min_python_version >= $python_version" | bc -l) )); then
echo "Must have python version ($python_version) >= ${min_python_version}. ABORT!"
exit 1
fi

# Create the conda environment
conda env create --file $rqmts_file -p $prefix

# generate modulefile from template
$MODULES && update_modules python $name $version $python_version
echo $name $version $rqmts_file >> ${HPC_STACK_ROOT}/hpc-stack-contents.log
45 changes: 45 additions & 0 deletions libs/build_miniconda3.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

# Download and install Miniconda3
# https://docs.conda.io/projects/conda/en/latest/user-guide/getting-started.html

set -eux

name="miniconda3"
version=${2:-${STACK_miniconda3_version:-"latest"}}

if $MODULES; then
prefix="${PREFIX:-"/opt/modules"}/core/$name/$version"
if [[ -d $prefix ]]; then
[[ $OVERWRITE =~ [yYtT] ]] && ( echo "WARNING: $prefix EXISTS: OVERWRITING!"; $SUDO rm -rf $prefix; $SUDO mkdir $prefix ) \
|| ( echo "WARNING: $prefix EXISTS, SKIPPING"; exit 1 )
fi
else
prefix=${MINICONDA3_ROOT:-"/usr/local"}
fi

cd ${HPC_STACK_ROOT}/${PKGDIR:-"pkg"}

host=$(uname -s)

if [[ "$host" == "Darwin" ]]; then
os="MacOSX"
elif [[ "$host" == "Linux" ]]; then
os="Linux"
else
echo "Unsupported host $host for Miniconda3; ABORT!"
exit 1
fi

software=$name-$version
installer="Miniconda3-${version}-${os}-x86_64.sh"
URL="https://repo.anaconda.com/miniconda/$installer"

[[ -d $software ]] || ( mkdir -p $software; $WGET $URL -O $software/$installer )
[[ ${DOWNLOAD_ONLY} =~ [yYtT] ]] && exit 0

$SUDO bash $software/$installer -b -p $prefix

# generate modulefile from template
$MODULES && update_modules core $name $version
echo $name $version $URL >> ${HPC_STACK_ROOT}/hpc-stack-contents.log
41 changes: 36 additions & 5 deletions modulefiles/core/miniconda3/miniconda3.lua
Original file line number Diff line number Diff line change
@@ -13,13 +13,44 @@ conflict("intelpython")

local opt = os.getenv("HPC_OPT") or os.getenv("OPT") or "/opt/modules"

local mpath = pathJoin(opt,"modulefiles/core",pkgName,pkgVersion)
local mpath = pathJoin(opt,"modulefiles/python",pkgName,pkgVersion)
prepend_path("MODULEPATH", mpath)

local base = pathJoin(opt,pkgName,pkgVersion)

prepend_path("PATH", pathJoin(base,"bin"))
prepend_path("PYTHONPATH", pathJoin(base,"lib/python@PYTHON_VERSION@/site-packages"))
local base = pathJoin(opt,"core",pkgName,pkgVersion)

pushenv("CONDA_ENVS_PATH", pathJoin(base,"envs"))
pushenv("CONDA_PKGS_PATH", pathJoin(base,"pkgs"))

-- These are conda functions defined in conda.sh
local funcs = "conda __conda_activate __conda_hashr __conda_reactivate __add_sys_prefix_to_path"

-- Line #: What does it do?
-- 1: source conda.sh from the installation path
-- 2: export conda functions
local load_cmd = "source " .. pathJoin(base, "etc/profile.d/conda.sh") .. "; \
export -f " .. funcs

-- Line #: What does it do?
-- 1: deactivate all conda envs
-- 2: unset the conda funcs
-- 3: define local variable prefix as path to Miniconda installation
-- 4: remove from PATH all paths to $prefix
-- 5: unset CONDA_ env. variables that are set via sourcing conda.sh
-- 6: unset previously set variable $prefix
local unload_cmd="for i in $(seq ${CONDA_SHLVL:=0}); do conda deactivate; done; \
unset -f " .. funcs .. "; \
prefix=" .. base .. "; \
export PATH=$(echo ${PATH} | tr ':' '\n' | grep . | grep -v $prefix | tr '\n' ':' | sed 's/:$//'); \
unset $(env | grep CONDA | grep -ev 'CONDA_ENVS_PATH|CONDA_PKGS_PATH' | cut -d= -f1)"

-- source conda on load, deactivate on unload
if (mode() == "load") then
execute{cmd=load_cmd, modeA={"load"}}
else
if (mode() == "unload") then
execute{cmd=unload_cmd, modeA={"unload"}}
end
end

whatis("Name: ".. pkgName)
whatis("Version: " .. pkgVersion)
2 changes: 1 addition & 1 deletion modulefiles/core/python/python.lua
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ conflict("intelpython")

local opt = os.getenv("HPC_OPT") or os.getenv("OPT") or "/opt/modules"

local mpath = pathJoin(opt,"modulefiles/core",pkgName,pkgVersion)
local mpath = pathJoin(opt,"modulefiles/python",pkgName,pkgVersion)
prepend_path("MODULEPATH", mpath)

local base = pathJoin(opt,pkgName,pkgVersion)
32 changes: 32 additions & 0 deletions modulefiles/python/pythonName/pythonVersion/condaenv/condaenv.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
help([[
]])

local pkgName = myModuleName()
local pkgVersion = myModuleVersion()
local pkgNameVer = myModuleFullName()

local hierA = hierarchyA(pkgNameVer,1)
local pythonNameVer = hierA[1]
local pythonNameVerD = pythonNameVer:gsub("/","-")

conflict(pkgName)

local opt = os.getenv("HPC_OPT") or os.getenv("OPT") or "/opt/modules"

local base = pathJoin(opt,pythonNameVerD,pkgName,pkgVersion)

-- activate on load, deactivate on unload
if (mode() == "load") then
local load_cmd = "conda activate " .. pkgName
execute{cmd=load_cmd, modeA={"load"}}
else
if (mode() == "unload") then
local unload_cmd = "conda deactivate " .. pkgName
execute{cmd=unload_cmd, modeA={"unload"}}
end
end

whatis("Name: " .. pkgName)
whatis("Version: " .. pkgVersion)
whatis("Category: Software")
whatis("Description: " .. pkgName .. " Conda Environment")
4 changes: 4 additions & 0 deletions stack/stack_mac.yaml
Original file line number Diff line number Diff line change
@@ -339,6 +339,10 @@ proj:
version: 7.1.0
cmake_opts: "-DENABLE_CURL=OFF -DBUILD_PROJSYNC=OFF"

miniconda3:
build: YES
version: 4.7.12

r2d2:
build: NO
version: 1.0.0