forked from NOAA-EMC/MOM6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
758 lines (627 loc) · 24.9 KB
/
Makefile
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
# MOM6 Test suite Makefile
#
# Usage:
# make -j
# Build the FMS library and test executables
#
# make -j test
# Run the test suite, defined in the `tc` directores.
#
# make clean
# Wipe the MOM6 test executables
# (NOTE: This does not delete FMS in the `deps`)
#
#
# Configuration:
# These settings can be provided as either command-line flags, or saved in a
# `config.mk` file.
#
# Experiment Configuration:
# BUILDS Executables to be built by `make` or `make all`
# CONFIGS Model configurations to test (default: `tc*`)
# TESTS Tests to run
# DIMS Dimensional scaling tests
# (NOTE: Each test will build its required executables, regardless of BUILDS)
#
# General test configuration:
# FRAMEWORK Model framework (fms1 or fms2)
# MPIRUN MPI job launcher (mpirun, srun, etc)
# DO_REPRO_TESTS Enable production ("repro") testing equivalence
# DO_REGRESSION_TESTS Enable regression tests (usually dev/gfdl)
# REPORT_COVERAGE Enable code coverage and report to codecov
#
# Compiler configuration:
# CC C compiler
# MPICC MPI C compiler
# FC Fortran compiler
# MPIFC MPI Fortran compiler
# (NOTE: These are environment variables and may be inherited from a shell.)
#
# Build configuration:
# FCFLAGS_DEBUG Testing ("debug") compiler flags
# FCFLAGS_REPRO Production ("repro") compiler flags
# FCFLAGS_OPT Aggressive optimization compiler flags
# FCFLAGS_INIT Variable initialization flags
# FCFLAGS_COVERAGE Code coverage flags
#
# Regression repository ("target") configuration:
# MOM_TARGET_SLUG URL slug (minus domain) of the target repo
# MOM_TARGET_URL Full URL of the target repo
# MOM_TARGET_LOCAL_BRANCH Target branch name
# (NOTE: These would typically be configured by a CI.)
#
#----
# TODO: POSIX shell compatibility
SHELL = bash
# No implicit rules
.SUFFIXES:
# No implicit variables
MAKEFLAGS += -R
# User-defined configuration
-include config.mk
# Set the infra framework
FRAMEWORK ?= fms1
# Set the MPI launcher here
# TODO: This needs more automated configuration
MPIRUN ?= mpirun
# Generic compiler variables are pass through to the builds
export CC
export MPICC
export FC
export MPIFC
# Builds are distinguished by FCFLAGS
# NOTE: FMS will be built using FCFLAGS_DEBUG
FCFLAGS_DEBUG ?= -g -O0
FCFLAGS_REPRO ?= -g -O2
FCFLAGS_OPT ?= -g -O3 -mavx -fno-omit-frame-pointer
FCFLAGS_INIT ?=
FCFLAGS_COVERAGE ?=
# Additional notes:
# - These default values are simple, minimalist flags, supported by nearly all
# compilers which are comparable to GFDL's canonical DEBUG and REPRO builds.
#
# - These flags should be configured outside of the Makefile, either with
# config.mk or as environment variables.
#
# - FMS cannot be built with the same aggressive initialization flags as MOM6,
# so FCFLAGS_INIT is used to provide additional MOM6 configuration.
# User-defined LDFLAGS (applied to all builds and FMS)
LDFLAGS_USER ?=
# Set to `true` to require identical results from DEBUG and REPRO builds
# NOTE: Many compilers (Intel, GCC on ARM64) do not yet produce identical
# results across DEBUG and REPRO builds (as defined below), so we disable on
# default.
DO_REPRO_TESTS ?=
# Time measurement (configurable by the CI)
TIME ?= time
# Experiment configuration
BUILDS ?= symmetric asymmetric openmp
CONFIGS ?= $(wildcard tc*)
TESTS ?= grid layout rotate restart openmp nan $(foreach d,$(DIMS),dim.$(d))
DIMS ?= t l h z q r
#---
# Dependencies
DEPS = deps
# mkmf, list_paths (GFDL build toolchain)
LIST_PATHS := $(DEPS)/bin/list_paths
MKMF := $(DEPS)/bin/mkmf
#---
# Test configuration
# REPRO tests enable reproducibility with optimization, and often do not match
# the DEBUG results in older GCCs and vendor compilers, so we can optionally
# disable them.
ifeq ($(DO_REPRO_TESTS), true)
BUILDS += repro
TESTS += repro
endif
# Profiling
ifeq ($(DO_PROFILE), false)
BUILDS += opt opt_target
endif
# The following variables are configured by Travis:
# DO_REGRESSION_TESTS: true if $(TRAVIS_PULL_REQUEST) is a PR number
# MOM_TARGET_SLUG: TRAVIS_REPO_SLUG
# MOM_TARGET_LOCAL_BRANCH: TRAVIS_BRANCH
# These are set to true by our Travis configuration if testing a pull request
DO_REGRESSION_TESTS ?=
REPORT_COVERAGE ?=
CODECOV_UPLOADER_URL ?= https://uploader.codecov.io/latest/linux/codecov
ifeq ($(DO_REGRESSION_TESTS), true)
BUILDS += target
TESTS += regression
MOM_TARGET_SLUG ?= NOAA-GFDL/MOM6
MOM_TARGET_URL ?= https://github.com/$(MOM_TARGET_SLUG)
MOM_TARGET_LOCAL_BRANCH ?= dev/gfdl
MOM_TARGET_BRANCH := origin/$(MOM_TARGET_LOCAL_BRANCH)
TARGET_CODEBASE = build/target_codebase
else
MOM_TARGET_URL =
MOM_TARGET_BRANCH =
TARGET_CODEBASE =
endif
# List of source files to link this Makefile's dependencies to model Makefiles
# Assumes a depth of two, and the following extensions: F90 inc c h
# (1): Root directory
# NOTE: extensions could be a second variable
SOURCE = \
$(foreach ext,F90 inc c h,$(wildcard $(1)/*/*.$(ext) $(1)/*/*/*.$(ext)))
MOM_SOURCE = $(call SOURCE,../src) \
$(wildcard ../config_src/infra/FMS1/*.F90) \
$(wildcard ../config_src/drivers/solo_driver/*.F90) \
$(wildcard ../config_src/ext*/*/*.F90)
TARGET_SOURCE = $(call SOURCE,build/target_codebase/src) \
$(wildcard build/target_codebase/config_src/infra/FMS1/*.F90) \
$(wildcard build/target_codebase/config_src/drivers/solo_driver/*.F90) \
$(wildcard build/target_codebase/config_src/ext*/*.F90)
FMS_SOURCE = $(call SOURCE,$(DEPS)/fms/src)
#---
# Python preprocessing environment configuration
HAS_NUMPY = $(shell python -c "import numpy" 2> /dev/null && echo "yes")
HAS_NETCDF4 = $(shell python -c "import netCDF4" 2> /dev/null && echo "yes")
USE_VENV =
ifneq ($(HAS_NUMPY), yes)
USE_VENV = yes
endif
ifneq ($(HAS_NETCDF4), yes)
USE_VENV = yes
endif
# When disabled, activation is a null operation (`true`)
VENV_PATH =
VENV_ACTIVATE = true
ifeq ($(USE_VENV), yes)
VENV_PATH = work/local-env
VENV_ACTIVATE = . $(VENV_PATH)/bin/activate
endif
#---
# Rules
.PHONY: all build.regressions build.prof
all: $(foreach b,$(BUILDS),build/$(b)/MOM6) $(VENV_PATH)
build.regressions: $(foreach b,symmetric target,build/$(b)/MOM6)
build.prof: $(foreach b,opt opt_target,build/$(b)/MOM6)
# Executable
BUILD_TARGETS = MOM6 Makefile path_names
.PRECIOUS: $(foreach b,$(BUILDS),$(foreach f,$(BUILD_TARGETS),build/$(b)/$(f)))
# Compiler flags
# Conditionally build symmetric with coverage support
COVERAGE=$(if $(REPORT_COVERAGE),$(FCFLAGS_COVERAGE),)
# .testing dependencies
# TODO: We should probably build TARGET with the FMS that it was configured
# to use. But for now we use the same FMS over all builds.
FCFLAGS_FMS = -I../../$(DEPS)/include
LDFLAGS_FMS = -L../../$(DEPS)/lib
PATH_FMS = PATH="${PATH}:../../$(DEPS)/bin"
# Define the build targets in terms of the traditional DEBUG/REPRO/etc labels
SYMMETRIC_FCFLAGS := FCFLAGS="$(FCFLAGS_DEBUG) $(FCFLAGS_INIT) $(COVERAGE) $(FCFLAGS_FMS)"
ASYMMETRIC_FCFLAGS := FCFLAGS="$(FCFLAGS_DEBUG) $(FCFLAGS_INIT) $(FCFLAGS_FMS)"
REPRO_FCFLAGS := FCFLAGS="$(FCFLAGS_REPRO) $(FCFLAGS_FMS)"
OPT_FCFLAGS := FCFLAGS="$(FCFLAGS_OPT) $(FCFLAGS_FMS)"
OPENMP_FCFLAGS := FCFLAGS="$(FCFLAGS_DEBUG) $(FCFLAGS_INIT) $(FCFLAGS_FMS)"
TARGET_FCFLAGS := FCFLAGS="$(FCFLAGS_DEBUG) $(FCFLAGS_INIT) $(FCFLAGS_FMS)"
MOM_LDFLAGS := LDFLAGS="$(LDFLAGS_FMS) $(LDFLAGS_USER)"
SYMMETRIC_LDFLAGS := LDFLAGS="$(COVERAGE) $(LDFLAGS_FMS) $(LDFLAGS_USER)"
# Environment variable configuration
build/symmetric/Makefile: MOM_ENV=$(PATH_FMS) $(SYMMETRIC_FCFLAGS) $(SYMMETRIC_LDFLAGS)
build/asymmetric/Makefile: MOM_ENV=$(PATH_FMS) $(ASYMMETRIC_FCFLAGS) $(MOM_LDFLAGS)
build/repro/Makefile: MOM_ENV=$(PATH_FMS) $(REPRO_FCFLAGS) $(MOM_LDFLAGS)
build/openmp/Makefile: MOM_ENV=$(PATH_FMS) $(OPENMP_FCFLAGS) $(MOM_LDFLAGS)
build/target/Makefile: MOM_ENV=$(PATH_FMS) $(TARGET_FCFLAGS) $(MOM_LDFLAGS)
build/opt/Makefile: MOM_ENV=$(PATH_FMS) $(OPT_FCFLAGS) $(MOM_LDFLAGS)
build/opt_target/Makefile: MOM_ENV=$(PATH_FMS) $(OPT_FCFLAGS) $(MOM_LDFLAGS)
build/coupled/Makefile: MOM_ENV=$(PATH_FMS) $(SYMMETRIC_FCFLAGS) $(SYMMETRIC_LDFLAGS)
build/nuopc/Makefile: MOM_ENV=$(PATH_FMS) $(SYMMETRIC_FCFLAGS) $(SYMMETRIC_LDFLAGS)
build/mct/Makefile: MOM_ENV=$(PATH_FMS) $(SYMMETRIC_FCFLAGS) $(SYMMETRIC_LDFLAGS)
# Configure script flags
build/symmetric/Makefile: MOM_ACFLAGS=
build/asymmetric/Makefile: MOM_ACFLAGS=--enable-asymmetric
build/repro/Makefile: MOM_ACFLAGS=
build/openmp/Makefile: MOM_ACFLAGS=--enable-openmp
build/target/Makefile: MOM_ACFLAGS=
build/opt/Makefile: MOM_ACFLAGS=
build/opt_target/Makefile: MOM_ACFLAGS=
build/coupled/Makefile: MOM_ACFLAGS=--with-driver=coupled_driver
build/nuopc/Makefile: MOM_ACFLAGS=--with-driver=nuopc_driver
build/mct/Makefile: MOM_ACFLAGS=--with-driver=mct_driver
# Fetch regression target source code
build/target/Makefile: | $(TARGET_CODEBASE)
build/opt_target/Makefile: | $(TARGET_CODEBASE)
# Define source code dependencies
# NOTE: ./configure is too much, but Makefile is not enough!
# Ideally we would want to re-run both Makefile and mkmf, but our mkmf call
# is inside ./configure, so we must re-run ./configure as well.
$(foreach b,$(filter-out target,$(BUILDS)),build/$(b)/Makefile): $(MOM_SOURCE)
build/target_codebase/configure: $(TARGET_SOURCE)
# Build MOM6
.PRECIOUS: $(foreach b,$(BUILDS),build/$(b)/MOM6)
build/%/MOM6: build/%/Makefile
cd $(@D) && $(TIME) $(MAKE) -j
# Use autoconf to construct the Makefile for each target
.PRECIOUS: $(foreach b,$(BUILDS),build/$(b)/Makefile)
build/%/Makefile: ../ac/configure ../ac/Makefile.in $(DEPS)/lib/libFMS.a $(MKMF) $(LIST_PATHS)
mkdir -p $(@D)
cd $(@D) \
&& $(MOM_ENV) ../../../ac/configure $(MOM_ACFLAGS) --with-framework=$(FRAMEWORK) \
|| (cat config.log && false)
../ac/configure: ../ac/configure.ac ../ac/m4
autoreconf -i $<
# Fetch the regression target codebase
build/target/Makefile build/opt_target/Makefile: \
$(TARGET_CODEBASE)/ac/configure $(DEPS)/lib/libFMS.a $(MKMF) $(LIST_PATHS)
mkdir -p $(@D)
cd $(@D) \
&& $(MOM_ENV) ../../$(TARGET_CODEBASE)/ac/configure $(MOM_ACFLAGS) \
|| (cat config.log && false)
$(TARGET_CODEBASE)/ac/configure: $(TARGET_CODEBASE)
autoreconf -i $</ac
$(TARGET_CODEBASE):
git clone --recursive $(MOM_TARGET_URL) $@
cd $@ && git checkout --recurse-submodules $(MOM_TARGET_BRANCH)
#---
# FMS
# Set up the FMS build environment variables
FMS_ENV = PATH="${PATH}:../../bin" FCFLAGS="$(FCFLAGS_DEBUG)"
# TODO: *.mod dependencies?
$(DEPS)/lib/libFMS.a: $(DEPS)/fms/build/libFMS.a
$(MAKE) -C $(DEPS) lib/libFMS.a
$(DEPS)/fms/build/libFMS.a: $(DEPS)/fms/build/Makefile
$(MAKE) -C $(DEPS) fms/build/libFMS.a
$(DEPS)/fms/build/Makefile: $(DEPS)/fms/src/configure $(DEPS)/Makefile.fms.in $(MKMF) $(LIST_PATHS)
$(FMS_ENV) $(MAKE) -C $(DEPS) fms/build/Makefile
$(MAKE) -C $(DEPS) fms/build/Makefile
$(DEPS)/Makefile.fms.in: ../ac/deps/Makefile.fms.in $(DEPS)/Makefile
cp $< $(DEPS)
# TODO: m4 dependencies?
$(DEPS)/fms/src/configure: ../ac/deps/configure.fms.ac $(DEPS)/Makefile $(FMS_SOURCE) | $(DEPS)/fms/src
cp ../ac/deps/configure.fms.ac $(DEPS)
cp -r ../ac/deps/m4 $(DEPS)
$(MAKE) -C $(DEPS) fms/src/configure
$(DEPS)/fms/src: $(DEPS)/Makefile
make -C $(DEPS) fms/src
# mkmf
$(MKMF) $(LIST_PATHS): $(DEPS)/mkmf
$(MAKE) -C $(DEPS) bin/$(@F)
$(DEPS)/mkmf: $(DEPS)/Makefile
$(MAKE) -C $(DEPS) mkmf
# Dependency init
$(DEPS)/Makefile: ../ac/deps/Makefile
mkdir -p $(@D)
cp $< $@
#---
# The following block does a non-library build of a coupled driver interface to MOM, along with everything below it.
# This simply checks that we have not broken the ability to compile. This is not a means to build a complete coupled executable.
# Todo:
# - avoid re-building FMS and MOM6 src by re-using existing object/mod files
# - use autoconf rather than mkmf templates
MK_TEMPLATE ?= ../../$(DEPS)/mkmf/templates/ncrc-gnu.mk
# NUOPC driver
build/nuopc/mom_ocean_model_nuopc.o: build/nuopc/Makefile
cd $(@D) && make $(@F)
check_mom6_api_nuopc: build/nuopc/mom_ocean_model_nuopc.o
# GFDL coupled driver
build/coupled/ocean_model_MOM.o: build/coupled/Makefile
cd $(@D) && make $(@F)
check_mom6_api_coupled: build/coupled/ocean_model_MOM.o
# MCT driver
build/mct/mom_ocean_model_mct.o: build/mct/Makefile
cd $(@D) && make $(@F)
check_mom6_api_mct: build/mct/mom_ocean_model_mct.o
#---
# Python preprocessing
# NOTE: Some less mature environments (e.g. Arm64 Ubuntu) require explicit
# installation of numpy before netCDF4, as well as wheel and cython support.
work/local-env:
python3 -m venv $@
. $@/bin/activate \
&& python3 -m pip install --upgrade pip \
&& pip3 install wheel \
&& pip3 install cython \
&& pip3 install numpy \
&& pip3 install netCDF4
#---
# Testing
.PHONY: test
test: $(foreach t,$(TESTS),test.$(t))
# NOTE: We remove tc3 (OBC) from grid test since it cannot run asymmetric grids
# NOTE: rotation diag chksum disabled since we cannot yet compare rotationally
# equivalent diagnostics
# TODO: restart checksum comparison is not yet implemented
.PHONY: $(foreach t,$(TESTS),test.$(t))
test.grid: $(foreach c,$(filter-out tc3,$(CONFIGS)),$(c).grid $(c).grid.diag)
test.layout: $(foreach c,$(CONFIGS),$(c).layout $(c).layout.diag)
test.rotate: $(foreach c,$(CONFIGS),$(c).rotate)
test.restart: $(foreach c,$(CONFIGS),$(c).restart)
test.repro: $(foreach c,$(CONFIGS),$(c).repro $(c).repro.diag)
test.openmp: $(foreach c,$(CONFIGS),$(c).openmp $(c).openmp.diag)
test.nan: $(foreach c,$(CONFIGS),$(c).nan $(c).nan.diag)
test.regression: $(foreach c,$(CONFIGS),$(c).regression $(c).regression.diag)
test.dim: $(foreach d,$(DIMS),test.dim.$(d))
define TEST_DIM_RULE
test.dim.$(1): $(foreach c,$(CONFIGS),$(c).dim.$(1) $(c).dim.$(1).diag)
endef
$(foreach d,$(DIMS),$(eval $(call TEST_DIM_RULE,$(d))))
.PHONY: run.symmetric run.asymmetric run.nans run.openmp
run.symmetric: $(foreach c,$(CONFIGS),work/$(c)/symmetric/ocean.stats)
run.asymmetric: $(foreach c,$(filter-out tc3,$(CONFIGS)),$(CONFIGS),work/$(c)/asymmetric/ocean.stats)
run.nan: $(foreach c,$(CONFIGS),work/$(c)/nan/ocean.stats)
run.openmp: $(foreach c,$(CONFIGS),work/$(c)/openmp/ocean.stats)
# Configuration test rules
# $(1): Configuration name (tc1, tc2, &c.)
# $(2): Excluded tests
.PRECIOUS: $(foreach c,$(CONFIGS),$(c))
define CONFIG_RULE
$(1): \
$(foreach t,$(filter-out $(2),$(TESTS)),$(1).$(t)) \
$(foreach t,$(filter-out $(2) rotate restart,$(TESTS)),$(1).$(t).diag)
endef
$(foreach c,$(filter-out tc3,$(CONFIGS)),$(eval $(call CONFIG_RULE,$(c),)))
# NOTE: tc3 uses OBCs and does not support asymmetric grid
$(eval $(call CONFIG_RULE,tc3,grid))
# Color highlights for test results
RED = \033[0;31m
YELLOW = \033[0;33m
GREEN = \033[0;32m
MAGENTA = \033[0;35m
RESET = \033[0m
DONE = ${GREEN}DONE${RESET}
PASS = ${GREEN}PASS${RESET}
WARN = ${YELLOW}WARN${RESET}
FAIL = ${RED}FAIL${RESET}
# Comparison rules
# $(1): Configuration (tc1, tc2, &c.)
# $(2): Test type (grid, layout, &c.)
# $(3): Comparison targets (symmetric asymmetric, symmetric layout, &c.)
define CMP_RULE
.PRECIOUS: $(foreach b,$(3),work/$(1)/$(b)/ocean.stats)
$(1).$(2): $(foreach b,$(3),work/$(1)/$(b)/ocean.stats)
@test "$$(shell ls -A results/$(1) 2>/dev/null)" || rm -rf results/$(1)
@cmp $$^ || !( \
mkdir -p results/$(1); \
(diff $$^ | tee results/$(1)/ocean.stats.$(2).diff | head -n 20) ; \
echo -e "$(FAIL): Solutions $(1).$(2) have changed." \
)
@echo -e "$(PASS): Solutions $(1).$(2) agree."
.PRECIOUS: $(foreach b,$(3),work/$(1)/$(b)/chksum_diag)
$(1).$(2).diag: $(foreach b,$(3),work/$(1)/$(b)/chksum_diag)
@cmp $$^ || !( \
mkdir -p results/$(1); \
(diff $$^ | tee results/$(1)/chksum_diag.$(2).diff | head -n 20) ; \
echo -e "$(FAIL): Diagnostics $(1).$(2).diag have changed." \
)
@echo -e "$(PASS): Diagnostics $(1).$(2).diag agree."
endef
$(foreach c,$(CONFIGS),$(eval $(call CMP_RULE,$(c),grid,symmetric asymmetric)))
$(foreach c,$(CONFIGS),$(eval $(call CMP_RULE,$(c),layout,symmetric layout)))
$(foreach c,$(CONFIGS),$(eval $(call CMP_RULE,$(c),rotate,symmetric rotate)))
$(foreach c,$(CONFIGS),$(eval $(call CMP_RULE,$(c),repro,symmetric repro)))
$(foreach c,$(CONFIGS),$(eval $(call CMP_RULE,$(c),openmp,symmetric openmp)))
$(foreach c,$(CONFIGS),$(eval $(call CMP_RULE,$(c),nan,symmetric nan)))
define CONFIG_DIM_RULE
$(1).dim: $(foreach d,$(DIMS),$(1).dim.$(d))
$(foreach d,$(DIMS),$(eval $(call CMP_RULE,$(1),dim.$(d),symmetric dim.$(d))))
endef
$(foreach c,$(CONFIGS),$(eval $(call CONFIG_DIM_RULE,$(c))))
# Custom comparison rules
# Restart tests only compare the final stat record
.PRECIOUS: $(foreach b,symmetric restart target,work/%/$(b)/ocean.stats)
%.restart: $(foreach b,symmetric restart,work/%/$(b)/ocean.stats)
@test "$(shell ls -A results/$* 2>/dev/null)" || rm -rf results/$*
@cmp $(foreach f,$^,<(tr -s ' ' < $(f) | cut -d ' ' -f3- | tail -n 1)) \
|| !( \
mkdir -p results/$*; \
(diff $^ | tee results/$*/chksum_diag.restart.diff | head -n 20) ; \
echo -e "$(FAIL): Solutions $*.restart have changed." \
)
@echo -e "$(PASS): Solutions $*.restart agree."
# TODO: chksum_diag parsing of restart files
# stats rule is unchanged, but we cannot use CMP_RULE to generate it.
%.regression: $(foreach b,symmetric target,work/%/$(b)/ocean.stats)
@test "$(shell ls -A results/$* 2>/dev/null)" || rm -rf results/$*
@cmp $^ || !( \
mkdir -p results/$*; \
(diff $^ | tee results/$*/ocean.stats.regression.diff | head -n 20) ; \
echo -e "$(FAIL): Solutions $*.regression have changed." \
)
@echo -e "$(PASS): Solutions $*.regression agree."
# Regression testing only checks for changes in existing diagnostics
%.regression.diag: $(foreach b,symmetric target,work/%/$(b)/chksum_diag)
@! diff $^ | grep "^[<>]" | grep "^>" > /dev/null \
|| ! (\
mkdir -p results/$*; \
(diff $^ | tee results/$*/chksum_diag.regression.diff | head -n 20) ; \
echo -e "$(FAIL): Diagnostics $*.regression.diag have changed." \
)
@cmp $^ || ( \
diff $^ | head -n 20; \
echo -e "$(WARN): New diagnostics in $<" \
)
@echo -e "$(PASS): Diagnostics $*.regression.diag agree."
#---
# Test run output files
# Rule to build work/<tc>/{ocean.stats,chksum_diag}.<tag>
# $(1): Test configuration name <tag>
# $(2): Executable type
# $(3): Enable coverage flag
# $(4): MOM_override configuration
# $(5): Environment variables
# $(6): Number of MPI ranks
define STAT_RULE
work/%/$(1)/ocean.stats work/%/$(1)/chksum_diag: build/$(2)/MOM6 $(VENV_PATH)
@echo "Running test $$*.$(1)..."
if [ $(3) ]; then find build/$(2) -name *.gcda -exec rm -f '{}' \; ; fi
mkdir -p $$(@D)
cp -RL $$*/* $$(@D)
if [ -f $$(@D)/Makefile ]; then \
$$(VENV_ACTIVATE) \
&& cd $$(@D) \
&& $(MAKE); \
else \
cd $$(@D); \
fi
mkdir -p $$(@D)/RESTART
echo -e "$(4)" > $$(@D)/MOM_override
rm -f results/$$*/std.$(1).{out,err}
cd $$(@D) \
&& $(TIME) $(5) $(MPIRUN) -n $(6) ../../../$$< 2> std.err > std.out \
|| !( \
mkdir -p ../../../results/$$*/ ; \
cat std.out | tee ../../../results/$$*/std.$(1).out | tail -n 20 ; \
cat std.err | tee ../../../results/$$*/std.$(1).err | tail -n 20 ; \
rm ocean.stats chksum_diag ; \
echo -e "$(FAIL): $$*.$(1) failed at runtime." \
)
@echo -e "$(DONE): $$*.$(1); no runtime errors."
if [ $(3) ]; then \
mkdir -p results/$$* ; \
cd build/symmetric ; \
gcov *.gcda > gcov.$$*.$(1).out ; \
curl -s $(CODECOV_UPLOADER_URL) -o codecov ; \
chmod +x codecov ; \
./codecov -Z -f "*.gcov" -n $$@ \
> codecov.$$*.$(1).out \
2> codecov.$$*.$(1).err \
&& echo -e "${MAGENTA}Report uploaded to codecov.${RESET}"; \
fi
endef
# Define $(,) as comma escape character
, := ,
$(eval $(call STAT_RULE,symmetric,symmetric,$(REPORT_COVERAGE),,,1))
$(eval $(call STAT_RULE,asymmetric,asymmetric,,,,1))
$(eval $(call STAT_RULE,target,target,,,,1))
$(eval $(call STAT_RULE,repro,repro,,,,1))
$(eval $(call STAT_RULE,openmp,openmp,,,GOMP_CPU_AFFINITY=0,1))
$(eval $(call STAT_RULE,layout,symmetric,,LAYOUT=2$(,)1,,2))
$(eval $(call STAT_RULE,rotate,symmetric,,ROTATE_INDEX=True\nINDEX_TURNS=1,,1))
$(eval $(call STAT_RULE,nan,symmetric,,,MALLOC_PERTURB_=1,1))
$(eval $(call STAT_RULE,dim.t,symmetric,,T_RESCALE_POWER=11,,1))
$(eval $(call STAT_RULE,dim.l,symmetric,,L_RESCALE_POWER=11,,1))
$(eval $(call STAT_RULE,dim.h,symmetric,,H_RESCALE_POWER=11,,1))
$(eval $(call STAT_RULE,dim.z,symmetric,,Z_RESCALE_POWER=11,,1))
$(eval $(call STAT_RULE,dim.q,symmetric,,Q_RESCALE_POWER=11,,1))
$(eval $(call STAT_RULE,dim.r,symmetric,,R_RESCALE_POWER=11,,1))
# Generate the half-period input namelist as follows:
# 1. Fetch DAYMAX and TIMEUNIT from MOM_input
# 2. Convert DAYMAX from TIMEUNIT to seconds
# 3. Apply seconds to `ocean_solo_nml` inside input.nml.
# NOTE: Assumes that runtime set by DAYMAX, will fail if set by input.nml
work/%/restart/ocean.stats: build/symmetric/MOM6 $(VENV_PATH)
rm -rf $(@D)
mkdir -p $(@D)
cp -RL $*/* $(@D)
if [ -f $(@D)/Makefile ]; then \
$(VENV_ACTIVATE) \
&& cd work/$*/restart \
&& $(MAKE); \
else \
cd work/$*/restart; \
fi
mkdir -p $(@D)/RESTART
# Set the half-period
cd $(@D) \
&& daymax=$$(grep DAYMAX MOM_input | cut -d '!' -f 1 | cut -d '=' -f 2 | xargs) \
&& timeunit=$$(grep TIMEUNIT MOM_input | cut -d '!' -f 1 | cut -d '=' -f 2 | xargs) \
&& if [ -z "$${timeunit}" ]; then timeunit="8.64e4"; fi \
&& printf -v timeunit_int "%.f" "$${timeunit}" \
&& halfperiod=$$(awk -v t=$${daymax} -v dt=$${timeunit} 'BEGIN {printf "%.f", 0.5*t*dt}') \
&& printf "\n&ocean_solo_nml\n seconds = $${halfperiod}\n/\n" >> input.nml
# Remove any previous archived output
rm -f results/$*/std.restart{1,2}.{out,err}
# Run the first half-period
cd $(@D) && $(TIME) $(MPIRUN) -n 1 ../../../$< 2> std1.err > std1.out \
|| !( \
cat std1.out | tee ../../../results/$*/std.restart1.out | tail -n 20 ; \
cat std1.err | tee ../../../results/$*/std.restart1.err | tail -n 20 ; \
echo -e "$(FAIL): $*.restart failed at runtime." \
)
# Setup the next inputs
cd $(@D) && rm -rf INPUT && mv RESTART INPUT
mkdir $(@D)/RESTART
cd $(@D) && sed -i -e "s/input_filename *= *'n'/input_filename = 'r'/g" input.nml
# Run the second half-period
cd $(@D) && $(TIME) $(MPIRUN) -n 1 ../../../$< 2> std2.err > std2.out \
|| !( \
cat std2.out | tee ../../../results/$*/std.restart2.out | tail -n 20 ; \
cat std2.err | tee ../../../results/$*/std.restart2.err | tail -n 20 ; \
echo -e "$(FAIL): $*.restart failed at runtime." \
)
# TODO: Restart checksum diagnostics
#---
# Not a true rule; only call this after `make test` to summarize test results.
.PHONY: test.summary
test.summary:
@if ls results/*/* &> /dev/null; then \
if ls results/*/std.*.err &> /dev/null; then \
echo "The following tests failed to complete:" ; \
ls results/*/std.*.out \
| awk '{split($$0,a,"/"); split(a[3],t,"."); v=t[2]; if(length(t)>3) v=v"."t[3]; print a[2],":",v}'; \
fi; \
if ls results/*/ocean.stats.*.diff &> /dev/null; then \
echo "The following tests report solution regressions:" ; \
ls results/*/ocean.stats.*.diff \
| awk '{split($$0,a,"/"); split(a[3],t,"."); v=t[3]; if(length(t)>4) v=v"."t[4]; print a[2],":",v}'; \
fi; \
if ls results/*/chksum_diag.*.diff &> /dev/null; then \
echo "The following tests report diagnostic regressions:" ; \
ls results/*/chksum_diag.*.diff \
| awk '{split($$0,a,"/"); split(a[3],t,"."); v=t[2]; if(length(t)>3) v=v"."t[3]; print a[2],":",v}'; \
fi; \
false ; \
else \
echo -e "$(PASS): All tests passed!"; \
fi
#---
# Profiling
# XXX: This is experimental work to track, log, and report changes in runtime
PCONFIGS = p0
.PHONY: profile
profile: $(foreach p,$(PCONFIGS), prof.$(p))
.PHONY: prof.p0
prof.p0: work/p0/opt/clocks.json work/p0/opt_target/clocks.json
python tools/compare_clocks.py $^
work/p0/%/clocks.json: work/p0/%/std.out
python tools/parse_fms_clocks.py -d $(@D) $^ > $@
work/p0/opt/std.out: build/opt/MOM6
work/p0/opt_target/std.out: build/opt_target/MOM6
work/p0/%/std.out:
mkdir -p $(@D)
cp -RL p0/* $(@D)
mkdir -p $(@D)/RESTART
echo -e "" > $(@D)/MOM_override
cd $(@D) \
&& $(MPIRUN) -n 1 ../../../$< 2> std.err > std.out
#---
# Same but with perf
# TODO: This expects the -e flag, can I handle it in the command?
PERF_EVENTS ?=
.PHONY: perf
perf: $(foreach p,$(PCONFIGS), perf.$(p))
.PHONY: prof.p0
perf.p0: work/p0/opt/profile.json work/p0/opt_target/profile.json
python tools/compare_perf.py $^
work/p0/%/profile.json: work/p0/%/perf.data
python tools/parse_perf.py -f $< > $@
work/p0/opt/perf.data: build/opt/MOM6
work/p0/opt_target/perf.data: build/opt_target/MOM6
work/p0/%/perf.data:
mkdir -p $(@D)
cp -RL p0/* $(@D)
mkdir -p $(@D)/RESTART
echo -e "" > $(@D)/MOM_override
cd $(@D) \
&& perf record \
-F 3999 \
${PERF_EVENTS} \
../../../$< 2> std.perf.err > std.perf.out \
|| cat std.perf.err
#----
# NOTE: These tests assert that we are in the .testing directory.
.PHONY: clean
clean: clean.build clean.stats
@[ $$(basename $$(pwd)) = .testing ]
rm -rf deps
.PHONY: clean.build
clean.build:
@[ $$(basename $$(pwd)) = .testing ]
rm -rf build
.PHONY: clean.stats
clean.stats:
@[ $$(basename $$(pwd)) = .testing ]
rm -rf work results