forked from cp2k/cp2k
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_cp2k.sh
More file actions
executable file
·1436 lines (1335 loc) · 52.6 KB
/
make_cp2k.sh
File metadata and controls
executable file
·1436 lines (1335 loc) · 52.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# Purpose: Build CP2K using Spack and CMake locally within the folder CP2K_ROOT
# which defaults to the current working directory. This should be a
# "cp2k/" folder containing the CP2K source tree.
#
# The script can either be sourced with
#
# > source ./make_cp2k.sh
#
# or run in a subshell with
#
# > ./make_cp2k.sh
#
# The latter, running in a subshell, is recommended.
#
# The flags -h or --help print the available options.
#
# The first run will take longer as it will build all CP2K dependencies
# with Spack. The Spack installation is kept fully local in the subfolder
# cp2k/spack which corresponds to the tools/toolchain/install folder
# created by the CP2K toolchain.
#
# Subsequent runs of the script will use the software stack from that
# cp2k/spack/ folder.
#
# A rebuild of all CP2K dependencies can be enforced simply by removing
# or renaming the folder cp2k/spack. The latter allows for keeping different
# software stacks (see also -bd and -bd_only flags).
#
# It is recommended to install podman to take advantage of a local cache.
# This will accelerate the (re)build of the CP2K dependencies with Spack
# significantly.
#
# After the CP2K dependencies are built with Spack, CP2K itself is built
# and installed using CMake in the subfolders cp2k/build and cp2k/install,
# respectively.
#
# Subsequent runs of the script will use the CMake configuration in the
# subfolder cp2k/build. A rebuild of CP2K from scratch can be enforced
# by removing or renaming that subfolder.
#
# A CP2K regression run can be launched automatically by adding the flag
# -t "" (or --test ""). This flag expects a string with the TESTOPTS, e.g.
#
# > ./make_cp2k.sh -t "--maxtasks 8 --restrictdir QS/regtest-gpw-1"
#
# Alternatively, the script cp2k/install/run_tests can be launched after
# a successful CP2K build.
# Authors: Matthias Krack (MK)
# Version: 1.6
# History: - Creation (19.12.2025, MK)
# - Version 0.1: First working version (09.01.2026, MK)
# - Version 0.2: Add more flags and checks (19.01.2026, MK)
# - Version 0.3: Add no_externals flag and perform more checks (21.01.2026, MK)
# - Version 0.4: Improve error handling and provide more hints (22.01.2026, MK)
# - Version 0.5: Adapt script for use within a container (24.01.2026, MK)
# - Version 0.6: Add MPI flag and revise flag parsing (27.01.2026, MK)
# - Version 0.7: Fix container detection (28.01.2026, MK)
# - Version 0.8: Add --build_deps_only flag (29.01.2026, MK)
# - Version 0.9: Add --disable_local_cache flag (30.01.2026, MK)
# - Version 1.0: Add Cray specific configuration (01.02.2026, MK)
# - Version 1.1: Allow for selecting the GCC version (02.02.2026, MK)
# - Version 1.2: Add option for static build (05.02.2026, MK)
# - Version 1.3: Add CUDA GPU support (10.02.2026, MK)
# - Version 1.4: Drop download of spack-packages (12.02.2026, MK)
# - Version 1.5: Add flags to enable/disable features selectively (15.02.2026, MK)
# - Version 1.6: Enable dbg and smp builds (01.03.2026, MK)
# Facilitate the deugging of this script
set -uo pipefail
# Retrieve script name
SCRIPT_NAME="$(basename "${BASH_SOURCE[0]}")"
# Check if the script is sourced or run in a subshell
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
echo "${SCRIPT_NAME}: Running script in sourcing mode"
echo "${SCRIPT_NAME}: All changes from this script will become effective within the current shell"
EXIT_CMD="return"
else
echo "${SCRIPT_NAME}: Running script in subshell mode"
echo "${SCRIPT_NAME}: Changes from this script in the shell environment are lost after completion"
EXIT_CMD="exit"
fi
# Help finding ldconfig
if ! command -v ldconfig &> /dev/null; then
PATH="${PATH}:/sbin"
fi
# Check if all mandatory packages are installed in the environment
for package in awk bzip2 find g++ gcc gfortran git gzip ldconfig make patch python3 tar wget xz; do
if ! command -v "${package}" &> /dev/null; then
echo "ERROR: The package \"${package}\" is mandatory to build CP2K with Spack/CMake"
echo " Install the missing package and re-run the script"
${EXIT_CMD} 1
fi
done
# Check platform
if [[ "$(uname -s)" == "Darwin" ]]; then
echo "ERROR: A build for Apple macOS (Darwin, Homebrew) is not supported yet,"
echo " but CP2K can be built within a container, e.g. using Ubuntu"
${EXIT_CMD} 1
fi
# Check bash version
if ((BASH_VERSINFO < 4)); then
echo "ERROR: The employed bash version ${BASH_VERSION} is too old (from 2004)"
echo " Install a newer bash version"
if [[ "$(uname -s)" == "Darwin" ]]; then
echo "HINT: Use the bash command provided by Homebrew with Apple"
echo " /opt/homebrew/bin should precede /bin/bash in the PATH"
echo " In sourcing mode, run /opt/homebrew/bin/bash first before sourcing this script"
fi
${EXIT_CMD} 1
elif ((BASH_VERSINFO < 5)); then
echo "WARNING: The employed bash version ${BASH_VERSION} is quite old (from 2009)"
else
echo "INFO: Using bash version ${BASH_VERSION}"
fi
# Check if the python3 version is new enough for spack
if ! python3 -c 'import sys; sys.exit(not(sys.version_info >= (3, 10)))'; then
echo "ERROR: Python version is NOT >= 3.10 (needed for Spack)"
echo " Found only $(python3 -V)"
${EXIT_CMD} 1
else
echo "INFO: Found $(python3 -V)"
fi
# Default values
BUILD_DEPS="if_needed"
BUILD_DEPS_ONLY="no"
CP2K_BUILD_TYPE="${CP2K_BUILD_TYPE:-Release}"
DEPS_BUILD_TYPE="${DEPS_BUILD_TYPE:-Release}"
CMAKE_FEATURE_FLAG_ALL="-DCP2K_USE_EVERYTHING=ON" # all features are activated by default
CMAKE_FEATURE_FLAGS="-DCP2K_BLAS_VENDOR=OpenBLAS" # LAPACK/BLAS from OpenBLAS by default
CMAKE_FEATURE_FLAGS+=" -DCP2K_USE_FFTW3=ON" # FFTW3 is always activated unless explicitly disabled
CMAKE_FEATURE_FLAGS+=" -DCP2K_USE_DLAF=OFF" # DLAF is deactivated by default
CMAKE_FEATURE_FLAG_MPI="-DCP2K_USE_MPI=ON" # MPI is switched on by default
CMAKE_FEATURE_FLAGS_GPU="-DCP2K_USE_SPLA_GEMM_OFFLOADING=ON"
CRAY="no"
CUDA_SM_CODE=0
DISABLE_LOCAL_CACHE="no"
GCC_VERSION="auto"
GPU_MODEL="none"
HAS_PODMAN="no"
HELP="no"
INSTALL_MESSAGE="NEVER"
MPI_MODE="mpich"
if command -v nproc &> /dev/null; then
MAX_PROCS=$(nproc)
NUM_PROCS=${NUM_PROCS:-${MAX_PROCS}}
else
MAX_PROCS=-1
NUM_PROCS=${NUM_PROCS:-8}
fi
NVCC_VERSION=0
REBUILD_CP2K="no"
RUN_TEST="no"
SED_PATTERN_LIST=""
TESTOPTS=""
USE_EXTERNALS="no"
VERBOSE=0
VERBOSE_FLAG="--quiet"
VERBOSE_MAKEFILE="OFF"
export CP2K_ENV="cp2k_env"
export CP2K_ROOT=${CP2K_ROOT:-${PWD}}
export CP2K_VERSION="${CP2K_VERSION:-psmp}"
export INSTALL_PREFIX="${INSTALL_PREFIX:-${CP2K_ROOT}/install}"
# Parse flags
while [[ $# -gt 0 ]]; do
case "$1" in
-bd | --build_deps | --build_dependencies)
BUILD_DEPS="always"
shift 1
;;
-bd_only | --build_deps_only | --build_dependencies_only)
BUILD_DEPS="always"
BUILD_DEPS_ONLY="yes"
shift 1
;;
-bt | --build_type)
CP2K_BUILD_TYPE="${2}"
case "${CP2K_BUILD_TYPE}" in
Debug)
CP2K_VERSION="${CP2K_VERSION/smp/dbg}"
;;
Release | RelWithDebInfo)
CP2K_VERSION="${CP2K_VERSION/dbg/smp}"
;;
esac
shift 2
;;
-cray)
CRAY="yes"
shift 1
;;
-cv | --cp2k_version)
if (($# > 1)); then
case "${2,,}" in
pdbg | psmp | sdbg | ssmp | ssmp-static)
CP2K_VERSION="${2,,}"
# Set build type
case "${CP2K_VERSION}" in
pdbg | sdbg)
CP2K_BUILD_TYPE="Debug"
DEPS_BUILD_TYPE="RelWithDebInfo"
;;
psmp | ssmp | ssmp-static)
CP2K_BUILD_TYPE="Release"
DEPS_BUILD_TYPE="Release"
;;
esac
# Disable MPI for a serial CP2K binary
case "${CP2K_VERSION}" in
sdbg | ssmp | ssmp-static)
MPI_MODE="no"
CMAKE_FEATURE_FLAG_MPI="-DCP2K_USE_MPI=OFF"
;;
esac
# Apply specific packages selection for statically linked binaries
case "${CP2K_VERSION}" in
ssmp-static)
CMAKE_FEATURE_FLAG_ALL="-DCP2K_USE_EVERYTHING=ON"
for package in dftd4 libint2 libxc libxsmm spglib vori tblite; do
CMAKE_FEATURE_FLAGS+=" -DCP2K_USE_${package^^}=ON"
done
for package in ace deepmd greenx hdf5 libtorch pexsi trexio; do
CMAKE_FEATURE_FLAGS+=" -DCP2K_USE_${package^^}=OFF"
done
;;
esac
;;
*)
echo "ERROR: Invalid CP2K version \"${2}\" specified (choose pdbg, psmp, sdbg, ssmp, or ssmp-static)"
${EXIT_CMD} 1
;;
esac
else
echo "ERROR: No argument found for flag \"${1}\" (choose psmp, ssmp, or ssmp-static)"
${EXIT_CMD} 1
fi
shift 2
;;
-df | --disable | --disable_feature | -ef | --enable | --enable_feature)
if (($# > 1)); then
case "${1}" in
-df | --disable | --disable_feature)
ON_OFF="OFF"
SUBST="s/^ /#/'"
;;
-ef | --enable | --enable_feature)
ON_OFF="ON"
SUBST="s/#/ /'"
;;
esac
case "${2,,}" in
all)
CMAKE_FEATURE_FLAG_ALL="-DCP2K_USE_EVERYTHING=${ON_OFF}"
for package in adios2 cosma deepmdkit dftd4 dla-future dla-future-fortran \
elpa greenx hdf5 libfabric libint libvdwxc libsmeagol libvori libxc \
libxsmm mimic-mcl openpmd-api pace pexsi plumed py-torch sirius spfft \
spglib spla tblite trexio; do
SED_PATTERN_LIST+=" -e '/\s*-\s+\"${package}@/ ${SUBST}"
done
if [[ "${ON_OFF}" == "OFF" ]]; then
SED_PATTERN_LIST+=" -e '/\s*-\s+\"smm=libxsmm\"/ s/libxsmm/blas/'"
fi
;;
ace | cosma | deepmd | dftd4 | dlaf | elpa | fftw3 | greenx | hdf5 | libint2 | \
libsmeagol | libtorch | libxc | libxsmm | mimic | openpmd | pexsi | plumed | \
spglib | tblite | trexio | vori)
CMAKE_FEATURE_FLAGS+=" -DCP2K_USE_${2^^}=${ON_OFF}"
# Translate package selection to sed pattern
case "${2,,}" in
ace)
SED_PATTERN_LIST+=" -e '/\s*-\s+\"p${2,,}@/ ${SUBST}"
;;
cosma | dftd4 | elpa | greenx | hdf5 | libsmeagol | libxc | pexsi | plumed | spglib | tblite | trexio)
SED_PATTERN_LIST+=" -e '/\s*-\s+\"${2,,}@/ ${SUBST}"
;;
deepmd | libtorch)
CMAKE_FEATURE_FLAGS+=" -DCP2K_USE_DEEPMD=${ON_OFF}"
CMAKE_FEATURE_FLAGS+=" -DCP2K_USE_LIBTORCH=${ON_OFF}"
SED_PATTERN_LIST+=" -e '/\s*-\s+\"${2,,}kit@/ ${SUBST}"
SED_PATTERN_LIST+=" -e '/\s*-\s+\"py-torch@/ ${SUBST}"
;;
dlaf)
SED_PATTERN_LIST+=" -e '/\s*-\s+\"dla-future.*@/ ${SUBST}"
;;
fftw3)
SED_PATTERN_LIST+=" -e '/\s*-\s+\"fftw@/ ${SUBST}"
;;
libint2)
SED_PATTERN_LIST+=" -e '/\s*-\s+\"libint@/ ${SUBST}"
;;
libxsmm)
SED_PATTERN_LIST+=" -e '/\s*-\s+\"${2,,}@/ ${SUBST}"
if [[ "${ON_OFF}" == "OFF" ]]; then
SED_PATTERN_LIST+=" -e '/\s*-\s+\"smm=${2,,}\"/ s/${2,,}/blas/'"
fi
;;
mimic)
SED_PATTERN_LIST+=" -e '/\s*-\s+\"mimic-mcl@/ ${SUBST}"
;;
openpmd | adios2)
SED_PATTERN_LIST+=" -e '/\s*-\s+\"adios2@/ ${SUBST}"
SED_PATTERN_LIST+=" -e '/\s*-\s+\"openpmd-api@/ ${SUBST}"
;;
vori)
SED_PATTERN_LIST+=" -e '/\s*-\s+\"lib${2,,}@/ ${SUBST}"
;;
esac
;;
libvdwxc | spfft | spla | sirius)
echo "WARNING: You have enabled or disabled one of the packages libvdwxc, spfft, spla, sirius"
echo " which means that the other packages will also be enabled or disabled"
CMAKE_FEATURE_FLAGS+=" -DCP2K_USE_LIBVDWXC=${ON_OFF} -DCP2K_USE_SpFFT=${ON_OFF}"
CMAKE_FEATURE_FLAGS+=" -DCP2K_USE_SPLA=${ON_OFF} -DCP2K_USE_SIRIUS=${ON_OFF}"
SED_PATTERN_LIST+=" -e '/\s*-\s+\"libvdwxc@/ ${SUBST}"
SED_PATTERN_LIST+=" -e '/\s*-\s+\"spfft@/ ${SUBST}"
SED_PATTERN_LIST+=" -e '/\s*-\s+\"spla@/ ${SUBST}"
SED_PATTERN_LIST+=" -e '/\s*-\s+\"sirius@/ ${SUBST}"
;;
cray_pm_accel_energy | cusolver_mp | spla_gemm_offloading | unified_memory)
CMAKE_FEATURE_FLAGS_GPU+=" -DCP2K_USE_${2^^}=${ON_OFF}"
;;
dbm_gpu | elpa_gpu | grid_gpu | pw_gpu)
CMAKE_FEATURE_FLAGS_GPU+=" -DCP2K_ENABLE_${2^^}=${ON_OFF}"
;;
none)
# do nothing
;;
*)
echo "ERROR: Unknown CP2K feature \"${2}\" specified"
${EXIT_CMD} 1
;;
esac
else
echo "ERROR: No feature found for flag \"${1}\""
${EXIT_CMD} 1
fi
shift 2
;;
-dlc | --disable_local_cache)
DISABLE_LOCAL_CACHE="yes"
shift 1
;;
-gv | --gcc_version)
if (($# > 1)); then
case "${2}" in
1[0-6])
GCC_VERSION="${2}"
;;
*)
echo "ERROR: Invalid GCC version \"${2}\" specified (choose from 10 to 16)"
${EXIT_CMD} 1
;;
esac
else
echo "ERROR: No argument found for flag \"${1}\" (choose a GCC version)"
${EXIT_CMD} 1
fi
shift 2
;;
-gm | -gpu | --gpu_model)
if (($# > 1)); then
case "${2^^}" in
P100 | V100 | T400 | A100 | A40 | H100 | H200 | GH200)
GPU_MODEL="${2^^}"
case "${GPU_MODEL}" in
P100)
CUDA_SM_CODE=60
;;
V100)
CUDA_SM_CODE=70
;;
T400)
CUDA_SM_CODE=75
;;
A100)
CUDA_SM_CODE=80
;;
A40)
CUDA_SM_CODE=86
;;
H100 | H200 | GH200)
CUDA_SM_CODE=90
;;
esac
;;
60 | 70 | 75 | 80 | 86 | 87 | 89 | 90 | 120 | 121)
CUDA_SM_CODE=${2}
;;
NONE)
GPU_MODEL="${2,,}"
;;
*)
echo -e "\nERROR: Unknown GPU model \"${2}\" specified (choose <CUDA SM code>, P100, V100, T400, A100, A40, H100, H200, GH200 or none)\n"
${EXIT_CMD} 1
;;
esac
if ((CUDA_SM_CODE > 0)); then
# Currently needed
USE_EXTERNALS="yes"
echo "INFO: The use of externals (-ue flag) is currently enforced with CUDA"
fi
else
echo -e "\nERROR: No argument found for flag \"${1}\" (choose <CUDA SM code>, P100, V100, T400, A100, A40, H100, H200, GH200 or none)\n"
${EXIT_CMD} 1
fi
shift 2
;;
-h | --help)
HELP="yes"
shift 1
;;
-ip | --install_path | --install_prefix)
if (($# > 1)); then
INSTALL_PREFIX="${2}"
else
echo "ERROR: No install path argument found for flag \"${1}\""
${EXIT_CMD} 1
fi
shift 2
;;
-j)
if (($# > 1)); then
case "${2}" in
-*)
shift 1
;;
[0-9]*)
NUM_PROCS="${2}"
shift 2
;;
*)
echo "ERROR: The -j flag can only be followed by an integer number, found \"${2}\""
${EXIT_CMD} 1
;;
esac
else
shift 1
fi
;;
-j[0-9]*)
NUM_PROCS="${1#-j}"
shift 1
;;
-mpi | --mpi_mode)
if (($# > 1)); then
case "${2,,}" in
mpich | openmpi)
CMAKE_FEATURE_FLAG_MPI="-DCP2K_USE_MPI=ON"
MPI_MODE="${2,,}"
;;
no | none | off)
CMAKE_FEATURE_FLAG_MPI="-DCP2K_USE_MPI=OFF"
MPI_MODE="no"
;;
*)
echo "ERROR: Unknown MPI mode \"${2}\" specified (choose mpich, openmpi, or no)"
${EXIT_CMD} 1
;;
esac
else
echo "ERROR: No argument found for flag \"${1}\""
echo " The MPI mode is required (choose mpich, openmpi, or no)"
${EXIT_CMD} 1
fi
shift 2
;;
-rc | --rebuild_cp2k)
REBUILD_CP2K="yes"
shift 1
;;
-t | --test)
RUN_TEST="yes"
if (($# > 1)); then
TESTOPTS="${2}"
else
echo "ERROR: No argument found for flag \"${1}\""
echo " A string argument with the TESTOPTS (even an empty one \"\") is required"
${EXIT_CMD} 1
fi
shift 2
;;
-ue | --use_externals)
USE_EXTERNALS="yes"
shift 1
;;
-v | --verbose)
VERBOSE=1
INSTALL_MESSAGE="LAZY"
VERBOSE_FLAG="--verbose"
VERBOSE_MAKEFILE="ON"
shift 1
;;
--)
shift 1
break
;;
-*)
echo "ERROR: Unknown option \"${1}\" specified"
${EXIT_CMD} 1
;;
*)
break
;;
esac
done
# Remove leading zeros from NUM_PROCS
NUM_PROCS=$(awk '{print $1+0}' <<< "${NUM_PROCS}")
# Check if we are working within a docker or podman container
[[ -f /.dockerenv || -f /run/.containerenv ]] && IN_CONTAINER="yes" || IN_CONTAINER="no"
# Assemble CMake feature flag list
CMAKE_FEATURE_FLAGS="${CMAKE_FEATURE_FLAG_ALL} ${CMAKE_FEATURE_FLAG_MPI} ${CMAKE_FEATURE_FLAGS}"
# Clean CMake feature flag list from repeated entries
declare -A seen=()
out=()
for flag in ${CMAKE_FEATURE_FLAGS}; do
[[ ${seen[${flag}]+_} ]] || {
seen[${flag}]=1
out+=("${flag}")
}
done
CMAKE_FEATURE_FLAGS="$(printf '%s\n' "${out[*]}")"
export BUILD_DEPS BUILD_DEPS_ONLY CMAKE_FEATURE_FLAGS CMAKE_FEATURE_FLAGS_GPU CP2K_BUILD_TYPE CRAY CUDA_SM_CODE \
DEPS_BUILD_TYPE DISABLE_LOCAL_CACHE GCC_VERSION GPU_MODEL HAS_PODMAN IN_CONTAINER INSTALL_MESSAGE MPI_MODE \
NUM_PROCS REBUILD_CP2K RUN_TEST TESTOPTS VERBOSE VERBOSE_FLAG VERBOSE_MAKEFILE
# Show help if requested
if [[ "${HELP}" == "yes" ]]; then
echo ""
echo "Usage: ${SCRIPT_NAME} [-bd | --build_deps]"
echo " [-bd_only | --build_deps_only]"
echo " [-bt | --build_type (Debug | Release | RelWithDebInfo)]"
echo " [-cray]"
echo " [-cv | --cp2k_version (psmp | ssmp | ssmp-static)]"
echo " [-df | --disable | --disable_feature (all | FEATURE | PACKAGE | none)"
echo " [-dlc | --disable_local_cache]"
echo " [-ef | --enable | --enable_feature (all | FEATURE | PACKAGE | none)"
echo " [-gm | -gpu | --gpu_model (<CUDA SM code> | P100 | V100 | T400 | A100 | H100 | H200 | GH200 | none)]"
echo " [-gv | --gcc_version (10 | 11 | 12 | 13 | 14 | 15 | 16)]"
echo " [-h | --help]"
echo " [-ip | --install_path PATH]"
echo " [-j #PROCESSES]"
echo " [-mpi | --mpi_mode (mpich | no | openmpi)]"
echo " [-rc | --rebuild_cp2k]"
echo " [-t | -test \"TESTOPTS\"]"
echo " [-ue | --use_externals]"
echo " [-v | --verbose]"
echo ""
echo "Flags:"
echo " --build_deps : Force a rebuild of all CP2K dependencies from scratch (removes the spack folder)"
echo " --build_deps_only : Rebuild ONLY the CP2K dependencies from scratch (removes the spack folder)"
echo " --build_type : Set preferred CMake build type for CP2K (default: \"Release\")"
echo " --cp2k_version : CP2K version to be built (default: \"psmp\")"
echo " -cray : Use Cray specific spack configuration"
echo " --disable_local_cache: Don't add local Spack cache"
echo " --enable_feature : Enable feature or package (default: all)"
echo " --disable_feature : Disable feature or package"
echo " --help : Print this help information"
echo " --gcc_version : Use the specified GCC version (default: automatically decided by spack)"
echo " --gpu_model : Select GPU model (default: none)"
echo " --install_path : Define the CP2K installation path (default: ./install)"
echo " -j : Number of processes used in parallel"
echo " --mpi_mode : Set preferred MPI mode (default: \"mpich\")"
echo " --rebuild_cp2k : Rebuild CP2K: removes the build folder (default: no)"
echo " --test : Perform a regression test run after a successful build"
echo " --use_externals : Use external packages installed on the host system. This results in much"
echo " faster build times, but it can also cause conflicts with outdated packages"
echo " pulled in from the host system, e.g. old python or gcc versions"
echo " --verbose : Write verbose output"
echo ""
echo "Hints:"
echo " - Remove the folder ${CP2K_ROOT}/build to (re)build CP2K from scratch"
echo " (see also --rebuild_cp2k flag)"
echo " - Remove the folder ${CP2K_ROOT}/spack to (re)build CP2K and all its dependencies from scratch"
echo " (see also --build_deps flag)"
echo " - The folder ${CP2K_ROOT}/install is updated after each successful run"
echo ""
echo "Packages: all | ace | cosma | deepmd | dftd4 | dlaf | elpa | fftw3 | greenx | hdf5 | libint2 |"
echo " libsmeagol | libtorch | libvdwxc | libxsmm | mimic | openpmd | pexsi | plumed | sirius |"
echo " spfft | spglib | spla | tblite | trexio | vori "
echo ""
echo "Features: cray_pm_accel_energy | cusolver_mp | dbm_gpu | elpa_gpu | grid_gpu | pw_gpu |"
echo " spla_gemm_offloading | unified_memory"
echo ""
${EXIT_CMD}
fi
echo ""
echo "BUILD_DEPS = ${BUILD_DEPS}"
echo "BUILD_DEPS_ONLY = ${BUILD_DEPS_ONLY}"
echo "CP2K_BUILD_TYPE = ${CP2K_BUILD_TYPE}"
echo "CP2K_VERSION = ${CP2K_VERSION}"
echo "CRAY = ${CRAY}"
echo "DEPS_BUILD_TYPE = ${DEPS_BUILD_TYPE}"
echo "DISABLE_LOCAL_CACHE = ${DISABLE_LOCAL_CACHE}"
echo "GCC_VERSION = ${GCC_VERSION}"
if ((CUDA_SM_CODE > 0)); then
echo "GPU = ${GPU_MODEL} (CUDA SM code: ${CUDA_SM_CODE})"
else
echo "GPU = ${GPU_MODEL}"
fi
echo "INSTALL_PREFIX = ${INSTALL_PREFIX}"
echo "INSTALL_MESSAGE = ${INSTALL_MESSAGE}"
echo "IN_CONTAINER = ${IN_CONTAINER}"
echo "MPI_MODE = ${MPI_MODE}"
echo "NUM_PROCS = ${NUM_PROCS} (processes)"
echo "Physical cores = $(lscpu -p=Core,Socket | grep -v '#' | sort -u | wc -l) (host view)"
echo "REBUILD_CP2K = ${REBUILD_CP2K}"
echo "RUN_TEST = ${RUN_TEST}"
if [[ "${RUN_TEST}" == "yes" ]]; then
echo "TESTOPTS = \"${TESTOPTS}\""
fi
echo "USE_EXTERNALS = ${USE_EXTERNALS}"
echo "VERBOSE_FLAG = ${VERBOSE_FLAG}"
echo "VERBOSE_MAKEFILE = ${VERBOSE_MAKEFILE}"
echo "VERBOSE = ${VERBOSE}"
if (($# > 0)); then
echo "Remaining args =" "$@" "(not used)"
fi
echo ""
echo "LD_LIBRARY_PATH = ${LD_LIBRARY_PATH:-}"
echo ""
echo "PATH = ${PATH:-}"
echo ""
echo "CMAKE_FEATURE_FLAGS = ${CMAKE_FEATURE_FLAGS}"
echo ""
((VERBOSE > 0)) && echo "SED_PATTERN_LIST = ${SED_PATTERN_LIST}"
# Check if a valid number of processes is requested
if ((NUM_PROCS < 1)); then
echo "ERROR: The requested number of processes should be larger than 0, found \"${NUM_PROCS}\""
${EXIT_CMD} 1
elif ((MAX_PROCS > 0)) && ((NUM_PROCS > MAX_PROCS)); then
echo "WARNING: The requested number of processes (${NUM_PROCS}) is larger than the detected number of CPU cores (${MAX_PROCS})"
fi
# Check if a valid CMake build type is selected for the dependencies
case "${DEPS_BUILD_TYPE^}" in
Release | RelWithDebInfo)
true
;;
Debug)
echo "ERROR: The CMake build type \"${DEPS_BUILD_TYPE}\" is not supported for building the dependencies"
${EXIT_CMD} 1
;;
*)
echo "ERROR: Invalid CMake build type \"${DEPS_BUILD_TYPE}\" selected for building the dependencies"
${EXIT_CMD} 1
;;
esac
# Check if a valid CMake build type is selected for CP2K
case "${CP2K_BUILD_TYPE^}" in
Debug | Release | RelWithDebInfo)
true
;;
*)
echo "ERROR: Invalid CMake build type \"${CP2K_BUILD_TYPE}\" selected for building CP2K"
${EXIT_CMD} 1
;;
esac
# Check if a valid MPI type is selected
case "${MPI_MODE}" in
mpich | openmpi)
if [[ "${CP2K_VERSION}" == "ssmp"* ]]; then
echo "ERROR: MPI type \"${MPI_MODE}\" specified for building a serial CP2K binary"
${EXIT_CMD} 1
fi
;;
no)
if [[ "${CP2K_VERSION}" == "psmp" ]]; then
echo "ERROR: MPI type \"${MPI_MODE}\" specified for building an MPI-parallel CP2K binary"
${EXIT_CMD} 1
fi
;;
*)
echo "ERROR: Invalid MPI type \"${MPI_MODE}\" selected"
echo " Choose from (mpich | no | openmpi)"
${EXIT_CMD} 1
;;
esac
# Check if CP2K_VERSION and the selected features are compatible
case "${CP2K_VERSION}" in
ssmp | ssmp-static)
for package in cosma dlaf elpa libfabric libsmeagol mimic openpmd pexsi plumed sirius spla; do
if [[ "${CMAKE_FEATURE_FLAGS}" == *" -DCP2K_USE_${package^^}=ON"* ]]; then
echo -e "ERROR: The feature ${package^^} is not available for building serial CP2K binaries (${CP2K_VERSION})\n"
${EXIT_CMD} 1
fi
done
# Further exclusions are needed for statically linked serial CP2K binaries
if [[ "${CP2K_VERSION}" == "ssmp-static" ]]; then
for package in ace deepmd greenx hdf5 libtorch trexio; do
if [[ "${CMAKE_FEATURE_FLAGS}" == *" -DCP2K_USE_${package^^}=ON"* ]]; then
echo -e "ERROR: The feature ${package^^} is not available for building statically linked serial CP2K binaries (${CP2K_VERSION})\n"
${EXIT_CMD} 1
fi
done
fi
;;
esac
# Perform CUDA GPU related settings
if ((CUDA_SM_CODE > 0)); then
if command -v nvcc &> /dev/null; then
CUDA_VERSION=$(nvcc -V | sed -n 's/.*release \([0-9.]*\).*/\1/p')
NVCC_VERSION=$(awk -v x="${CUDA_VERSION}" 'BEGIN{print 10*x}')
else
echo -e "\nERROR: No CUDA toolkit installation found (nvcc compiler not found)\n"
${EXIT_CMD} 1
fi
# Check if the selected CUDA SM code is valid when the nvidia-smi command is available
if command -v nvidia-smi &> /dev/null; then
echo -e "NVIDIA driver installation found:\n"
nvidia-smi
HOST_CUDA_SM_CODE=$(nvidia-smi --query-gpu=compute_cap --format=csv,noheader | tail -n 1 | awk '{print 10*$1}')
if ((CUDA_SM_CODE > HOST_CUDA_SM_CODE)); then
echo ""
echo "ERROR: The requested CUDA SM code (${CUDA_SM_CODE}) is larger than the maximum"
echo " CUDA SM code (${HOST_CUDA_SM_CODE}) supported by the host system"
echo ""
${EXIT_CMD} 1
fi
else
echo "INFO: No NVIDIA driver installation found (nvidia-smi not found)"
fi
# Check GPU support by the CUDA SDK
if ((NVCC_VERSION < 128)) && ((CUDA_SM_CODE > 90)); then
echo ""
echo "ERROR: The CUDA SDK version ${NVCC_VERSION} does not support GPUs with a"
echo " CUDA SM code larger than 90 (found ${CUDA_SM_CODE})"
echo ""
${EXIT_CMD} 1
fi
if ((NVCC_VERSION > 129)) && ((CUDA_SM_CODE < 75)); then
echo ""
echo "ERROR: The CUDA SDK version ${NVCC_VERSION} does not support GPUs with a"
echo " CUDA SM code smaller than 75 (found ${CUDA_SM_CODE})"
echo ""
${EXIT_CMD} 1
fi
CMAKE_CUDA_FLAGS="-DCP2K_USE_ACCEL=CUDA"
CMAKE_CUDA_FLAGS+=" -DCP2K_WITH_GPU=${GPU_MODEL}"
CMAKE_CUDA_FLAGS+=" -DCMAKE_CUDA_ARCHITECTURES=${CUDA_SM_CODE}"
CMAKE_CUDA_FLAGS+=" ${CMAKE_FEATURE_FLAGS_GPU}"
out=()
for flag in ${CMAKE_CUDA_FLAGS}; do
[[ ${seen[${flag}]+_} ]] || {
seen[${flag}]=1
out+=("${flag}")
}
done
CMAKE_CUDA_FLAGS="$(printf '%s\n' "${out[*]}")"
echo -e "\nCMAKE_CUDA_FLAGS = ${CMAKE_CUDA_FLAGS}"
[[ -n "${CUDA_VERSION:-}" ]] && echo -e "\nCUDA_VERSION = ${CUDA_VERSION} (nvcc compiler)"
[[ -n "${CUDA_HOME:-}" ]] && echo -e "\nCUDA_HOME = ${CUDA_HOME}"
echo ""
else
CMAKE_CUDA_FLAGS="-DCP2K_USE_ACCEL=OFF"
fi
export CMAKE_CUDA_FLAGS
((VERBOSE > 0)) && ulimit -a
### Build CP2K dependencies with Spack if needed or requested ###
# Spack version
export SPACK_VERSION="${SPACK_VERSION:-1.1.1}"
export SPACK_BUILD_PATH="${CP2K_ROOT}/spack"
export SPACK_ROOT="${SPACK_BUILD_PATH}/spack"
# Define the CP2K spack configuration file
export CP2K_CONFIG_FILE="${SPACK_BUILD_PATH}/cp2k_deps_${CP2K_VERSION:0:1}${CP2K_VERSION:4}.yaml"
# If requested, remove the spack folder for (re)building all CP2K dependencies
if [[ "${BUILD_DEPS}" == "always" ]]; then
for folder in ${SPACK_BUILD_PATH} ${CP2K_ROOT}/build ${CP2K_ROOT}/install; do
if [[ -d "${folder}" ]]; then
echo "Removing folder \"${folder}\""
rm -rf "${folder}"
fi
done
fi
# If requested, remove the build folder for (re)building CP2K
if [[ "${REBUILD_CP2K}" == "yes" ]]; then
for folder in ${CP2K_ROOT}/build ${CP2K_ROOT}/install; do
if [[ -d "${folder}" ]]; then
echo "Removing folder \"${folder}\""
rm -rf "${folder}"
fi
done
fi
if [[ ! -d "${SPACK_BUILD_PATH}" ]]; then
# Create a new local spack folder
mkdir -p "${SPACK_BUILD_PATH}"
cd "${SPACK_BUILD_PATH}" || ${EXIT_CMD} 1
# Reset the spack environment
unset SPACK_ENV
# Install Spack
if [[ ! -d "${SPACK_ROOT}" ]]; then
echo "Installing Spack ${SPACK_VERSION}"
wget -q "https://github.com/spack/spack/archive/v${SPACK_VERSION}.tar.gz"
tar -xzf "v${SPACK_VERSION}.tar.gz" && rm -f "v${SPACK_VERSION}.tar.gz"
mv -f "${SPACK_BUILD_PATH}/spack-${SPACK_VERSION}" "${SPACK_ROOT}"
fi
export PATH="${SPACK_ROOT}/bin:${PATH}"
# Isolate user configuration for spack
export SPACK_DISABLE_LOCAL_CONFIG=true
export SPACK_USER_CONFIG_PATH="${SPACK_BUILD_PATH}"
export SPACK_USER_CACHE_PATH="${SPACK_BUILD_PATH}/cache"
mkdir -p "${SPACK_USER_CACHE_PATH}"
if [[ "${DISABLE_LOCAL_CACHE}" == "no" ]]; then
# Create and activate a virtual environment (venv) for Python packages
if command -v python3 -m venv --help &> /dev/null; then
echo "Installing virtual environment for Python packages"
if ! python3 -m venv "${SPACK_BUILD_PATH}/venv"; then
echo "ERROR: The creation of a virtual environment (venv) for Python packages failed"
${EXIT_CMD} 1
fi
export PATH="${SPACK_BUILD_PATH}/venv/bin:${PATH}"
else
echo "ERROR: python3 -m venv was not found"
${EXIT_CMD} 1
fi
# Upgrade pip and install boto3
if command -v python3 -m pip --version &> /dev/null; then
python3 -m pip install "${VERBOSE_FLAG}" --upgrade pip
echo "Installing boto3 module"
python3 -m pip install "${VERBOSE_FLAG}" boto3==1.38.11 google-cloud-storage==3.1.0
else
echo "ERROR: python3 -m pip was not found"
${EXIT_CMD} 1
fi
fi
# Initialize Spack shell hooks
# shellcheck source=/dev/null
source "${SPACK_ROOT}/share/spack/setup-env.sh"
if [[ "${IN_CONTAINER}" == "no" ]] && [[ "${DISABLE_LOCAL_CACHE}" == "no" ]]; then
# The package podman is required for using a MinIO cache
if command -v podman &> /dev/null; then
# Check podman version
((VERBOSE > 0)) && podman version
PODMAN_VERSION=$(podman version --format '{{.Client.Version}}' | awk -F'[.-]' '{print $1}')
if ((PODMAN_VERSION < 4)); then
echo "WARNING: Outdated podman version $(podman version --format '{{.Client.Version}}') found"
fi
HAS_PODMAN="yes"
else
echo "INFO: podman was not found"
echo " Install the package podman to take advantage of a local spack cache"
echo " This accelerates a rebuild of the CP2K dependencies, significantly"
fi
fi
# Start Spack cache if we are not within a container and have podman available
if [[ "${IN_CONTAINER}" == "no" ]] && [[ "${DISABLE_LOCAL_CACHE}" == "no" ]]; then
if [[ "${HAS_PODMAN}" == "yes" ]]; then
if ! "${CP2K_ROOT}"/tools/docker/spack_cache_start.sh; then
echo "ERROR: Could not start (new) spack cache"
echo ""
echo "An error message starting with \"Error: initial journal cursor: ...\" indicates that the"
echo "journald logging does not work. Try to switch podman (3.x) to file-based logs by creating"
echo "the file \"~/.config/containers/containers.conf\" with the following two lines:"
echo "[containers]"
echo "log_driver = \"k8s-file\""
${EXIT_CMD} 1
fi
fi
fi
# Add local spack cache if possible
if [[ "${DISABLE_LOCAL_CACHE}" == "no" ]]; then
export SPACK_CACHE=${SPACK_CACHE:-"s3://spack-cache --s3-endpoint-url=http://localhost:9000"}
echo "SPACK_CACHE = \"${SPACK_CACHE}\""
spack mirror list
if ! spack mirror list | grep -q "local-cache"; then
echo "Setting up local spack cache"
if ((VERBOSE > 0)); then
"${CP2K_ROOT}"/tools/docker/scripts/setup_spack_cache.sh
else
"${CP2K_ROOT}"/tools/docker/scripts/setup_spack_cache.sh &> /dev/null
fi
fi
else
echo "INFO: A local Spack cache is NOT used"
fi
# Prepare the CP2K spack configuration file
CP2K_DEPS_FILE="${CP2K_ROOT}/tools/spack/cp2k_deps_${CP2K_VERSION:0:1}${CP2K_VERSION:4}.yaml"
if [[ -f "${CP2K_DEPS_FILE}" ]]; then
sed -E \
-e "s|root: /opt/spack|root: ${SPACK_ROOT}/opt/spack|" \
-e "/\"build_type=/s|build_type=[^\"]*|build_type=${DEPS_BUILD_TYPE}|" \
"${CP2K_DEPS_FILE}" > "${CP2K_CONFIG_FILE}"
else
echo -e "\nERROR: The spack configuration file ${CP2K_DEPS_FILE} was not found\n"
${EXIT_CMD} 1
fi
# Apply selected MPI type if needed
# MPICH is selected by default for psmp and no change needed for ssmp
if [[ "${MPI_MODE}" == "openmpi" ]]; then
sed -E \
-e '/\s*-\s+"mpich@/ s/^ /#/' \
-e '/\s*#\s*-\s+"openmpi@/ s/#/ /' \
-e '/\s*-\s+mpich/ s/mpich$/openmpi/' \
-i "${CP2K_CONFIG_FILE}"
fi
# Activate CUDA in the spack configuration file if requested
if ((CUDA_SM_CODE > 0)); then
sed -E \
-e "0,/~cuda/s//+cuda cuda_arch=${CUDA_SM_CODE}/" \
-e 's/"~cuda\s+~gpu_direct"/"\+cuda \+gpu_direct"/' \
-e '/\s*#\s*-\s+"fabrics=efa,ucx"/ s/#/ /' \
-i "${CP2K_CONFIG_FILE}"
# Building libfabric with CUDA causes problems
# sed -E -e 's/"~cuda\s+~gdrcopy"/"\+cuda \+gdrcopy"/' -i "${CP2K_CONFIG_FILE}"
sed -E -e 's/"~cuda\s+~gdrcopy"/"\~cuda"/' -i "${CP2K_CONFIG_FILE}"
if [[ -n "${CUDA_VERSION:-}" ]]; then
# Set CUDA SM code
sed -E -e "s/spec:\s+cuda@[.0-9]*/spec: cuda@${CUDA_VERSION}/" -i "${CP2K_CONFIG_FILE}"
fi
if [[ -n "${CUDA_HOME:-}" ]]; then
sed -E -e "s|prefix: /usr/local/cuda|prefix: ${CUDA_HOME}|" -i "${CP2K_CONFIG_FILE}"
fi
else
sed -E -e 's/"~cuda\s+~gdrcopy"/"\~cuda"/' -i "${CP2K_CONFIG_FILE}"
fi
# Apply Cray specific adaptation of the spack configuration if requested (CSCS)
if [[ "${CRAY}" == "yes" ]]; then
sed -E \
-e '/\s*#\s*-\s+"netmod=ofi"/ s/#/ /' \
-e 's/~xpmem/+xpmem/' \
-e 's/"libfabric@[.0-9]*"/"libfabric@1.22.0"/' \
-i "${CP2K_CONFIG_FILE}"
fi
# Apply feature selection to spack configuration file
if [[ -n "${SED_PATTERN_LIST}" ]]; then
eval sed -E "${SED_PATTERN_LIST}" -i "${CP2K_CONFIG_FILE}"
fi
# Find all compilers
echo "Searching for GCC compilers"
if ! spack compiler find &> /dev/null; then
echo "ERROR: The compiler detection of spack failed"
${EXIT_CMD} 1
fi
spack compiler list
# Retrieve the newest compiler version found by spack
GCC_VERSION_NEWEST="$(spack compilers | awk '/gcc/ {print $2}' | sort -V | tail -n 1)"
echo "The newest GCC compiler version found by spack is ${GCC_VERSION_NEWEST}"
GCC_VERSION_NEWEST="$(echo "${GCC_VERSION_NEWEST}" | sed -E -e 's/.*@([0-9]+).*/\1/' | cut -d. -f1)"
# Check if the newest compiler version found on the host system is new enough
GCC_VERSION_MINIMUM="10"
if ((GCC_VERSION_NEWEST < GCC_VERSION_MINIMUM)); then
echo "INFO: The newest GCC compiler version ${GCC_VERSION_NEWEST} is too old,"
echo " because at least version ${GCC_VERSION_MINIMUM} is required"
GCC_VERSION="14"
echo "INFO: The lastest stable GCC version ${GCC_VERSION} will be built and used by spack"
fi
# Add a specific GCC version to the spack configuration if requested
if [[ "${GCC_VERSION}" == "auto" ]]; then
echo "Spack will automatically select the GCC version which is not necessarily the newest one found"
else