Skip to content

Commit 7fc1228

Browse files
committed
Add instance parameter to data generation and update output files
1 parent 15fbdaf commit 7fc1228

File tree

2 files changed

+53
-39
lines changed

2 files changed

+53
-39
lines changed

examples-proposed/024-aggregated-compute-ensemble/gen_data.py

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,28 @@
55
import argparse
66
from typing import Any
77
import json
8+
import csv
89
import numpy as np
910
import matplotlib.pyplot as plt
1011

12+
from ipsframework.resourceHelper import get_platform_info
1113

12-
def main(alpha: float, L:float, T_final:float, Nx:int, Nt:int) -> dict[str, Any]:
14+
def main(instance: str,
15+
alpha: float, L:float, T_final:float, Nx:int, Nt:int) -> dict[str, Any]:
1316
""" Generate synthetic data to emulate an actual simulation or complex
1417
calculation.
1518
1619
As a side-effect it will save a plot to the current working directory with
1720
the name `solution.png`.
1821
22+
:param instance: instance name
1923
:param alpha: thermal diffusivity
2024
:param L: domain length
2125
:param T_final: final time
2226
:param Nx: number of spatial grid points
2327
:param Nt: number of time steps
2428
:returns: x, y, where x is the steps and u the corresponding values
25-
"""
29+
"""
2630
# Discretization
2731
dx = L / (Nx - 1)
2832
dt = T_final / Nt
@@ -33,9 +37,6 @@ def main(alpha: float, L:float, T_final:float, Nx:int, Nt:int) -> dict[str, Any]
3337
print("Warning: Stability condition r <= 0.5 is not met. "
3438
"Results may be inaccurate.")
3539

36-
# Initialize solution array
37-
u = np.zeros(Nx)
38-
3940
# Initial condition (e.g., a sine wave)
4041
x = np.linspace(0, L, Nx)
4142
u = np.sin(np.pi * x)
@@ -60,13 +61,36 @@ def main(alpha: float, L:float, T_final:float, Nx:int, Nt:int) -> dict[str, Any]
6061
plt.grid(True)
6162
plt.savefig("solution.png")
6263

64+
# Save some per-component stats
65+
stats_fname = f'{instance}_stats_{timestamp}.csv'
66+
run_env = get_platform_info()
67+
68+
with open(stats_fname, 'w') as f:
69+
# Write run-time stats to a CSV as well as the runtime parameters
70+
# specific to this instance.
71+
writer = csv.writer(f)
72+
writer.writerow(
73+
['instance', 'hostname', 'pid', 'core',
74+
'affinity',
75+
'alpha', 'L', 'T_final', 'Nx', 'Nt',
76+
'start', 'end'])
77+
78+
writer.writerow([instance_id, run_env['hostname'],
79+
run_env['pid'], run_env['core_id'],
80+
run_env['affinity'],
81+
alpha, L, T_final, Nx, Nt,
82+
start, time()])
83+
6384
return {'x': x.tolist(), 'u': u.tolist()}
6485

6586

6687

6788
if __name__ == '__main__':
6889
parser = argparse.ArgumentParser(description='Generate synthetic data to '
69-
'emulate an actual simulation or complex')
90+
'emulate an actual simulation '
91+
'or complex')
92+
parser.add_argument('--instance', type=str,
93+
help='instance name')
7094
parser.add_argument('--alpha', type=float, default=1.0,)
7195
parser.add_argument('--L', type=float, default=1.0,)
7296
parser.add_argument('--T_final', type=float, default=1.0,)
@@ -75,7 +99,12 @@ def main(alpha: float, L:float, T_final:float, Nx:int, Nt:int) -> dict[str, Any]
7599

76100
args = parser.parse_args()
77101

78-
data = main(args.alpha, args.L, args.T_final, args.Nx, args.Nt)
102+
data = main(args.instance,
103+
args.alpha, args.L, args.T_final, args.Nx, args.Nt)
104+
105+
file_name = f'{args.instance}_solution.json'
106+
107+
print(f'Writing to {file_name}')
79108

80-
with open('solution.json', 'w') as f:
109+
with open(file_name, 'w') as f:
81110
json.dump(data, f)

examples-proposed/024-aggregated-compute-ensemble/instance_component.py

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
#!/usr/bin/env python3
22
"""
3-
Component to be stepped in instance
4-
"""
3+
Component to be stepped in instance.
54
6-
import csv
7-
import sys
5+
This should generate a PNG image, a JSON file, and a CSV file. The first
6+
two are from synthetic data generated from `gen_data.py`. The latter is
7+
also generated from provenance data captured in `gen_data.py`, too.
8+
"""
9+
from pathlib import Path
810
from time import time
911
from typing import Any
10-
from pathlib import Path
1112

1213
from ipsframework import Component
13-
from ipsframework.resourceHelper import get_platform_info
1414

1515

16-
def create_cmd(alpha: float, L:float, T_final:float, Nx:int, Nt:int) -> list[Any]:
16+
def create_cmd(instance: str, alpha: float, L:float, T_final:float,
17+
Nx:int, Nt:int) -> list[Any]:
1718
""" create the command to run the external data generator
1819
20+
:parma instance: instance name
1921
:param alpha: thermal diffusivity
2022
:param L: domain length
2123
:param T_final: final time
2224
:param Nx: number of spatial grid points
2325
:param Nt: number of time steps
2426
:returns: list of command line arguments to be executed in step()
2527
"""
26-
cmd = ['python3', 'gen_data.py', '--alpha', alpha, '--L', L, '--T_final', T_final,
28+
cmd = ['python3', 'gen_data.py', '--instance', instance,
29+
'--alpha', alpha, '--L', L, '--T_final', T_final,
2730
'--Nx', Nx, '--Nt', Nt]
2831
return cmd
2932

@@ -45,7 +48,8 @@ def step(self, timestamp: float = 0.0, **keywords):
4548
f'T_final={self.T_final}, Nx={self.Nx}, '
4649
f'Nt={self.Nt}')
4750

48-
cmd = create_cmd(self.alpha, self.L, self.T_final, self.Nx, self.Nt)
51+
cmd = create_cmd(instance_id,
52+
self.alpha, self.L, self.T_final, self.Nx, self.Nt)
4953

5054
working_dir = str(Path('.').absolute())
5155
self.services.info(f'{instance_id}: Launching executable '
@@ -60,29 +64,10 @@ def step(self, timestamp: float = 0.0, **keywords):
6064
self.services.critical(f'{instance_id}: Unable to launch '
6165
f'executable in {working_dir}')
6266

63-
self.services.wait_task(run_id) # block until done
64-
65-
self.services.info(f'{instance_id}: Completed MPI executable.')
66-
67-
68-
# Save some per-component stats
69-
stats_fname = f'stats_{timestamp}.csv'
70-
run_env = get_platform_info()
67+
return_value = self.services.wait_task(run_id) # block until done
7168

72-
with open(stats_fname, 'w') as f:
73-
# Write run-time stats to a CSV as well as the runtime parameters
74-
# specific to this instance.
75-
writer = csv.writer(f)
76-
writer.writerow(
77-
['instance', 'executable', 'hostname', 'pid', 'core',
78-
'affinity',
79-
'alpha', 'L', 'T_final', 'Nx', 'Nt',
80-
'start', 'end'])
81-
writer.writerow([instance_id, sys.argv[0], run_env['hostname'],
82-
run_env['pid'], run_env['core_id'],
83-
run_env['affinity'],
84-
self.alpha, self.L, self.T_final, self.Nx, self.Nt,
85-
start, time()])
69+
self.services.info(f'{instance_id}: Completed MPI executable with '
70+
f'return value: {return_value}.')
8671

8772
# TODO temporarily commenting this out until the actual
8873
# example is ready to consider adding data files to the portal. This

0 commit comments

Comments
 (0)