-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexecute.py
More file actions
237 lines (197 loc) · 6.31 KB
/
execute.py
File metadata and controls
237 lines (197 loc) · 6.31 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
import logging
import os
import sys
import time
from csv import writer
import argparse
import lib.gprat as gprat
#import lib64.gprat as gprat # depending on system
#import gprat # if installed with pip
from config import get_config
from hpx_logger import setup_logging
logger = logging.getLogger()
log_filename = "./hpx_logs.log"
parser = argparse.ArgumentParser()
parser.add_argument(
"--use_gpu",
action="store_true",
help="Flag to use GPU (assuming available)",
)
args = parser.parse_args()
if args.use_gpu:
sys.argv.remove("--use_gpu")
use_gpu = gprat.compiled_with_cuda() and gprat.gpu_count() > 0 and args.use_gpu
def gprat_run(config, output_csv_obj, n_train, l, cores):
n_tile_size = gprat.compute_train_tile_size(n_train, config["N_TILES"])
m_tiles, m_tile_size = gprat.compute_test_tiles(
config["N_TEST"], config["N_TILES"], n_tile_size
)
hpar = gprat.AdamParams(learning_rate=0.1, opt_iter=config["OPT_ITER"])
train_in = gprat.GP_data(config["train_in_file"], n_train, config["N_REG"])
train_out = gprat.GP_data(
config["train_out_file"], n_train, config["N_REG"]
)
test_in = gprat.GP_data(
config["test_in_file"], config["N_TEST"], config["N_REG"]
)
total_t = time.time()
if not use_gpu:
target = "cpu"
###### GP object ######
init_t = time.time()
gp_cpu = gprat.GP(
train_in.data,
train_out.data,
config["N_TILES"],
n_tile_size,
kernel_params=[1.0, 1.0, 0.1],
n_reg=config["N_REG"],
trainable=[True, True, True],
)
init_t = time.time() - init_t
# Init hpx runtime but do not start it yet
gprat.start_hpx(sys.argv, cores)
# Perform optmization
opti_t = time.time()
losses = gp_cpu.optimize(hpar)
opti_t = time.time() - opti_t
logger.info("Finished optimization.")
# gprat.suspend_hpx()
# gprat.resume_hpx()
# Predict
pred_uncer_t = time.time()
pr, var = gp_cpu.predict_with_uncertainty(
test_in.data, m_tiles, m_tile_size
)
pred_uncer_t = time.time() - pred_uncer_t
logger.info("Finished predictions.")
# Predict
pred_full_t = time.time()
pr__, var__ = gp_cpu.predict_with_full_cov(
test_in.data, m_tiles, m_tile_size
)
pred_full_t = time.time() - pred_full_t
logger.info("Finished predictions with full cov.")
# Predict
pred_t = time.time()
pr_ = gp_cpu.predict(test_in.data, m_tiles, m_tile_size)
pred_t = time.time() - pred_t
logger.info("Finished predictions.")
else:
target = "gpu"
###### GP object ######
init_t = time.time()
gp_gpu = gprat.GP(
train_in.data,
train_out.data,
config["N_TILES"],
n_tile_size,
kernel_params=[1.0, 1.0, 0.1],
n_reg=config["N_REG"],
trainable=[True, True, True],
gpu_id=0,
n_streams=2,
)
init_t = time.time() - init_t
# Init hpx runtime but do not start it yet
gprat.start_hpx(sys.argv, cores)
# NOTE: optimization is not implemented for GPU
opti_t = -1
# gprat.suspend_hpx()
# gprat.resume_hpx()
# Predict
pred_uncer_t = time.time()
pr, var = gp_gpu.predict_with_uncertainty(
test_in.data, m_tiles, m_tile_size
)
pred_uncer_t = time.time() - pred_uncer_t
logger.info("Finished predictions.")
# Predict
pred_full_t = time.time()
pr__, var__ = gp_gpu.predict_with_full_cov(
test_in.data, m_tiles, m_tile_size
)
pred_full_t = time.time() - pred_full_t
logger.info("Finished predictions with full cov.")
# Predict
pred_t = time.time()
pr_ = gp_gpu.predict(test_in.data, m_tiles, m_tile_size)
pred_t = time.time() - pred_t
logger.info("Finished predictions.")
# Stop HPX runtime
gprat.stop_hpx()
total_t = time.time() - total_t
# config and measurements
row_data = [
target,
cores,
n_train,
config["N_TEST"],
config["N_TILES"],
config["N_REG"],
config["OPT_ITER"],
init_t,
-1, # NOTE: optimization is not implemented for GPU
total_t,
pred_uncer_t,
pred_full_t,
pred_t,
l,
]
output_csv_obj.writerow(row_data)
logger.info("Completed iteration.")
def execute():
"""
Execute the main process:
- Set up logging.
- Load configuration file.
- Initialize output CSV file.
- Write header to the output CSV file.
- Iterate through different training sizes and for each training size
"""
# setup logging
setup_logging(log_filename, True, logger)
# load config
logger.info("\n")
logger.info("-" * 40)
logger.info("Load config file.")
config = get_config()
# append log to ./output.csv
file_exists = os.path.isfile("./output.csv")
output_file = open("./output.csv", "a", newline="")
output_csv_obj = writer(output_file)
# write headers
if not file_exists:
logger.info("Write output file header")
header = [
"Target",
"Cores",
"N_train",
"N_test",
"N_TILES",
"N_regressor",
"Opt_iter",
"Init_time",
"Optimization_Time",
"Pred_Var_time",
"Pred_Full_time",
"Predict_time",
"N_loop",
]
output_csv_obj.writerow(header)
# runs tests on exponentially increasing number of cores and
# data size, for multiple loops (each loop starts with *s)
cores = 2
while cores <= config["N_CORES"]:
data_size = config["START"]
while data_size <= config["END"]:
for l in range(config["LOOP"]):
logger.info("*" * 40)
logger.info(
f"Core: {cores}, Train Size: {data_size}, Loop: {l}"
)
gprat_run(config, output_csv_obj, data_size, l, cores)
data_size = data_size * config["STEP"]
cores = cores * 2
if __name__ == "__main__":
execute()