-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from EcohydrologyTeam/performance-pre-init
Pre-Initialize model xarray with timesteps
- Loading branch information
Showing
11 changed files
with
4,705 additions
and
349 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1,169 changes: 1,169 additions & 0 deletions
1,169
examples/dev_sandbox/performance_profiling_tsm.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import clearwater_modules | ||
import time | ||
import sys | ||
import logging | ||
import numpy as np | ||
import xarray as xr | ||
import cProfile | ||
|
||
from typing import ( | ||
List | ||
) | ||
|
||
def create_data_array( | ||
gridsize: int, | ||
value: int | float | ||
): | ||
data = np.full(gridsize, value) | ||
data_array = xr.DataArray(data) | ||
return data_array | ||
|
||
def run_performance_test( | ||
iterations_list: List[int], | ||
gridsize_list: List[int], | ||
log_file: str, | ||
detailed_profile: bool | ||
): | ||
"""Log the performance for different numbers of iterations.""" | ||
# set up logger | ||
log_format = '%(asctime)s,%(levelname)s,%(message)s' | ||
logging.basicConfig( | ||
filename=log_file, | ||
level=logging.INFO, | ||
format=log_format, | ||
) | ||
if detailed_profile: | ||
logging.info(f"timestep,MB,time_per_iter") | ||
else: | ||
logging.info(f"iters,gridsize,avg_increment_time,total_increment_time") | ||
|
||
for iters in iterations_list: | ||
for gridsize in gridsize_list: | ||
# define starting state values | ||
if gridsize == 1: | ||
state_i = { | ||
'water_temp_c': 40.0, | ||
'surface_area': 1.0, | ||
'volume': 1.0, | ||
} | ||
meteo_parameters = {'wind_c': 1.0} | ||
|
||
else: | ||
state_i = { | ||
'water_temp_c': create_data_array(gridsize, 40.0), | ||
'surface_area': create_data_array(gridsize, 1.0), | ||
'volume': create_data_array(gridsize, 1.0), | ||
} | ||
meteo_parameters = { | ||
'wind_c': 1.0 | ||
} | ||
|
||
ti = time.time() | ||
|
||
# instantiate the TSM module | ||
tsm = clearwater_modules.tsm.EnergyBudget( | ||
time_steps=iters, | ||
initial_state_values=state_i, | ||
meteo_parameters=meteo_parameters, | ||
track_dynamic_variables=False, | ||
) | ||
|
||
t2 = time.time() | ||
curr_time = t2 | ||
for _ in range(iters): | ||
tsm.increment_timestep() | ||
|
||
# detailed profiling | ||
if detailed_profile: | ||
if (_ % 100 == 0) and (_ != 0): | ||
current_time = (time.time() - curr_time) / 100 | ||
logging.info(f"{_},{tsm.dataset.nbytes * 0.000001}, {current_time}") | ||
curr_time = time.time() | ||
|
||
# wrap up | ||
avg_increment_time = (time.time() - t2) / iters | ||
total_run_time = time.time() - ti | ||
if not detailed_profile: | ||
logging.info(f"{iters},{gridsize},{avg_increment_time},{total_run_time}") | ||
|
||
if __name__ == '__main__': | ||
if len(sys.argv) < 2: | ||
print("Usage: python performance_profiling_tsm.py <log_file>") | ||
sys.exit(1) | ||
|
||
log_file = f"{sys.argv[1]}.log" | ||
cprofile_file = f"{sys.argv[1]}.prof" | ||
iterations_list = [1, 10, 100, 1000, 10000, 100000] | ||
gridsize_list = [1, 1000, 10000] | ||
detailed_profile = False | ||
# iterations_list = [1000] | ||
# gridsize_list = [1] | ||
# detailed_profile = True | ||
# profiler = cProfile.Profile() | ||
# profiler.enable() | ||
run_performance_test(iterations_list, gridsize_list, log_file, detailed_profile) | ||
# profiler.disable() | ||
# profiler.dump_stats(cprofile_file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file added
BIN
+742 KB
examples/temp/performance_profiling/pre_init_1000_1_detailed_profile.prof
Binary file not shown.
Oops, something went wrong.