Skip to content

Commit

Permalink
Merge branch 'update.interface.return_value' into update
Browse files Browse the repository at this point in the history
  • Loading branch information
aoymt committed Aug 8, 2024
2 parents 593419b + 0f25d18 commit 2839deb
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 13 deletions.
5 changes: 4 additions & 1 deletion sample/user_function/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import py2dmat
import py2dmat.util.toml
import py2dmat.algorithm.mapper_mpi as pm_alg
#import py2dmat.algorithm.min_search as pm_alg
import py2dmat.solver.function


Expand All @@ -20,4 +21,6 @@ def my_objective_fn(x: np.ndarray) -> float:
runner = py2dmat.Runner(solver, info)

alg = pm_alg.Algorithm(info, runner)
alg.main()
retv = alg.main()

print(retv)
22 changes: 14 additions & 8 deletions src/py2dmat/algorithm/_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,18 @@ def run(self) -> None:
def _run(self) -> None:
pass

def post(self) -> None:
def post(self) -> Dict:
if self.status < AlgorithmStatus.RUN:
msg = "algorithm has not run yet"
raise RuntimeError(msg)
original_dir = os.getcwd()
os.chdir(self.output_dir)
self._post()
result = self._post()
os.chdir(original_dir)
return result

@abstractmethod
def _post(self) -> None:
def _post(self) -> Dict:
pass

def main(self):
Expand All @@ -172,17 +173,22 @@ def main(self):
self.mpicomm.Barrier()

time_sta = time.perf_counter()
self.post()
result = self.post()
time_end = time.perf_counter()
self.timer["post"]["total"] = time_end - time_sta

with open(self.proc_dir / "time.log", "w") as fw:
self.write_timer(self.proc_dir / "time.log")

return result

def write_timer(self, filename: Path):
with open(filename, "w") as fw:
fw.write("#in units of seconds\n")

def output_file(type):
tmp_dict = self.timer[type]
fw.write("#{}\n total = {}\n".format(type, tmp_dict["total"]))
for key, t in tmp_dict.items():
d = self.timer[type]
fw.write("#{}\n total = {}\n".format(type, d["total"]))
for key, t in d.items():
if key == "total":
continue
fw.write(" - {} = {}\n".format(key, t))
Expand Down
1 change: 1 addition & 0 deletions src/py2dmat/algorithm/bayes.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,4 @@ def _post(self) -> None:
print("Best Solution:")
for x, y in zip(label_list, self.xopt):
print(x, "=", y)
return {"x": self.xopt, "fx": self.best_fx}
13 changes: 12 additions & 1 deletion src/py2dmat/algorithm/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,10 @@ def _post(self) -> None:
best_x = [self.best_x]
best_istep = [self.best_istep]
best_iwalker = [self.best_iwalker]

best_rank = np.argmin(best_fx)

if self.mpirank == 0:
best_rank = np.argmin(best_fx)
with open("best_result.txt", "w") as f:
f.write(f"nprocs = {self.nreplica}\n")
f.write(f"rank = {best_rank}\n")
Expand All @@ -293,3 +295,12 @@ def _post(self) -> None:
print(f" fx = {best_fx[best_rank]}")
for label, x in zip(self.label_list, best_x[best_rank]):
print(f" {label} = {x}")

return {
"x": best_x[best_rank],
"fx": best_fx[best_rank],
"nprocs": self.nreplica,
"rank": best_rank,
"step": best_istep[best_rank],
"walker": best_iwalker[best_rank],
}
5 changes: 3 additions & 2 deletions src/py2dmat/algorithm/mapper_mpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.

from typing import List, Union
from typing import List, Union, Dict

from pathlib import Path
from io import open
Expand Down Expand Up @@ -119,7 +119,7 @@ def _prepare(self) -> None:
# do nothing
pass

def _post(self) -> None:
def _post(self) -> Dict:
if self.mpirank == 0:
with open("ColorMap.txt", "w") as file_output:
for i in range(self.mpisize):
Expand All @@ -128,3 +128,4 @@ def _post(self) -> None:
line = line.lstrip()
if not line.startswith("#"):
file_output.write(line)
return {}
1 change: 1 addition & 0 deletions src/py2dmat/algorithm/min_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,4 @@ def _post(self):
f.write(f"fx = {self.fopt}\n")
for x, y in zip(label_list, self.xopt):
f.write(f"{x} = {y}\n")
return {"x": self.xopt, "fx": self.fopt}
10 changes: 9 additions & 1 deletion src/py2dmat/algorithm/pamc.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,8 @@ def _post(self) -> None:
best_x = [self.best_x]
best_istep = [self.best_istep]
best_iwalker = [self.best_iwalker]
best_rank = np.argmin(best_fx)
if self.mpirank == 0:
best_rank = np.argmin(best_fx)
with open("best_result.txt", "w") as f:
f.write(f"nprocs = {self.mpisize}\n")
f.write(f"rank = {best_rank}\n")
Expand Down Expand Up @@ -500,3 +500,11 @@ def _post(self) -> None:
f.write(f" {self.logZs[i]}")
f.write(f" {self.acceptance_ratio[i]}")
f.write("\n")
return {
"x": best_x[best_rank],
"fx": best_fx[best_rank],
"nprocs": self.mpisize,
"rank": best_rank,
"step": best_istep[best_rank],
"walker": best_iwalker[best_rank],
}

0 comments on commit 2839deb

Please sign in to comment.