From 4bcea545aef3bbf6117126d2378d8a5e241776e6 Mon Sep 17 00:00:00 2001 From: cangumeli Date: Wed, 9 Sep 2020 23:49:47 +0200 Subject: [PATCH 1/4] Add Barrier and timing to the Python interface --- include/mpi_env.hpp | 8 ++++++++ notebooks/python_mpi.py | 21 +++++++++++++++++---- pybind11/intelqs_py.cpp | 8 +++++++- src/mpi_env.cpp | 29 +++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 5 deletions(-) diff --git a/include/mpi_env.hpp b/include/mpi_env.hpp index 4631b75b..ede7d732 100644 --- a/include/mpi_env.hpp +++ b/include/mpi_env.hpp @@ -156,6 +156,14 @@ void PoolBarrier(); void StateBarrier(); void Barrier(); +/// @brief Find minimum wall clock time in all processes and return it at rank 0. +/// Returns local time for other processes. +double MinTime(); + +/// @brief Find maximum wall clock time in all processes and return it at rank 0. +/// Returns local time for other processes. +double MaxTime(); + ///////////////////////////////////////////////////////////////////////////////////////// /// @brief Print from all MPI processes diff --git a/notebooks/python_mpi.py b/notebooks/python_mpi.py index 3fcf12d6..1334d83e 100644 --- a/notebooks/python_mpi.py +++ b/notebooks/python_mpi.py @@ -1,11 +1,15 @@ import sys +import numpy as np +import time sys.path.insert(0, '../build/lib') import intelqs_py as iqs -import numpy as np +from intelqs_py import MPIEnvironment def run_circuit(num_qubits): reg = iqs.QubitRegister(num_qubits, 'base', 0, 0) + # Uncomment the following to enable specialization + reg.TurnOnSpecializeV2() for i in range(num_qubits): reg.ApplyHadamard(i) reg.ApplyRotationZ(i, np.pi/3) @@ -19,12 +23,21 @@ def run_circuit(num_qubits): num_qubits = int(sys.argv[1]) iqs.init() - + + start_time = MPIEnvironment.MinTime() reg = run_circuit(num_qubits) state_vector = np.array(reg, copy=False) + end_time = MPIEnvironment.MaxTime() + + rank = MPIEnvironment.GetRank() + print('\nFinal state at rank {}: {}'.format(rank, state_vector), + flush=True) - rank = iqs.MPIEnvironment.GetRank() - print('\nFinal state at rank {}: {}'.format(rank, state_vector)) + MPIEnvironment.Barrier() + if not rank: + time.sleep(1) + print('\nElapsed time : {} seconds'.format(end_time - start_time), + flush=True) iqs.finalize() diff --git a/pybind11/intelqs_py.cpp b/pybind11/intelqs_py.cpp index 64491dfa..fe3b9e2c 100644 --- a/pybind11/intelqs_py.cpp +++ b/pybind11/intelqs_py.cpp @@ -269,8 +269,14 @@ PYBIND11_MODULE(intelqs_py, m) .def_static("GetNumRanksPerNode", &Environment::GetNumRanksPerNode) .def_static("GetNumNodes", &Environment::GetNumNodes) .def_static("GetStateId", &Environment::GetStateId) - .def_static("GetNumStates", &Environment::GetNumStates); + .def_static("GetNumStates", &Environment::GetNumStates) + .def_static("Barrier", &qhipster::mpi::Barrier) + .def_static("PoolBarrier", &qhipster::mpi::PoolBarrier) + .def_static("StateBarrier", &qhipster::mpi::StateBarrier) + + .def_static("MinTime", &qhipster::mpi::MinTime) + .def_static("MaxTime", &qhipster::mpi::MaxTime); } ////////////////////////////////////////////////////////////////////////////// diff --git a/src/mpi_env.cpp b/src/mpi_env.cpp index 668340e6..7297d41b 100644 --- a/src/mpi_env.cpp +++ b/src/mpi_env.cpp @@ -6,6 +6,8 @@ #include #include #include +#else +#include #endif #include @@ -78,8 +80,17 @@ void StatePrint(std::string s, bool all) } } +double MaxTime() +{ + auto now = std::chrono::system_clock::now(); + return std::chrono::duration(now.time_since_epoch()).count(); +} + +double MinTime() { return MaxTime(); } + #endif + ///////////////////////////////////////////////////////////////////////////////////////// // A few methods have the same implementation with / without MPI. ///////////////////////////////////////////////////////////////////////////////////////// @@ -508,6 +519,24 @@ void StateBarrier() ///////////////////////////////////////////////////////////////////////////////////////// +double MinTime() +{ + double min_time; + double local_time = MPI_Wtime(); + MPI_Reduce(&local_time, &min_time, 1, MPI_DOUBLE, MPI_MIN, 0, MPI_COMM_WORLD); + return Environment::GetRank() ? local_time : min_time; +} + +double MaxTime() +{ + double max_time; + double local_time = MPI_Wtime(); + MPI_Reduce(&local_time, &max_time, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD); + return Environment::GetRank() ? local_time : max_time; +} + +///////////////////////////////////////////////////////////////////////////////////////// + void PoolPrint(std::string s, bool all) { Print(s, Environment::GetPoolComm(), all); } From 2b4e684be33676144b9eef5fb6270929ac090bd5 Mon Sep 17 00:00:00 2001 From: cangumeli Date: Wed, 9 Sep 2020 23:55:15 +0200 Subject: [PATCH 2/4] Comment out specialize by default --- notebooks/python_mpi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notebooks/python_mpi.py b/notebooks/python_mpi.py index 1334d83e..fdb2216e 100644 --- a/notebooks/python_mpi.py +++ b/notebooks/python_mpi.py @@ -9,7 +9,7 @@ def run_circuit(num_qubits): reg = iqs.QubitRegister(num_qubits, 'base', 0, 0) # Uncomment the following to enable specialization - reg.TurnOnSpecializeV2() + # reg.TurnOnSpecializeV2() for i in range(num_qubits): reg.ApplyHadamard(i) reg.ApplyRotationZ(i, np.pi/3) From d3cbabdb5de158f8f727b4981b7427697393a393 Mon Sep 17 00:00:00 2001 From: cangumeli Date: Wed, 9 Sep 2020 23:59:57 +0200 Subject: [PATCH 3/4] Update sample runs --- notebooks/python_mpi.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/notebooks/python_mpi.py b/notebooks/python_mpi.py index fdb2216e..80e9035f 100644 --- a/notebooks/python_mpi.py +++ b/notebooks/python_mpi.py @@ -52,6 +52,8 @@ def run_circuit(num_qubits): 0.21650635-0.125j 0.21650635-0.125j 0.21650635-0.125j 0.21650635-0.125j 0.21650635+0.125j 0.21650635+0.125j 0.21650635+0.125j 0.21650635+0.125j 0.21650635+0.125j 0.21650635+0.125j 0.21650635+0.125j 0.21650635+0.125j] + +Elapsed time : 0.00019025802612304688 seconds ''' ## Multi-Process: @@ -65,4 +67,6 @@ def run_circuit(num_qubits): Final state at rank 1: [0.21650635+0.125j 0.21650635+0.125j 0.21650635+0.125j 0.21650635+0.125j 0.21650635+0.125j 0.21650635+0.125j 0.21650635+0.125j 0.21650635+0.125j] + +Elapsed time : 0.002885580062866211 seconds ''' From 6e783b5bd19fefb59310c20f471457c5fc0f29eb Mon Sep 17 00:00:00 2001 From: cangumeli Date: Sun, 13 Sep 2020 20:36:29 +0200 Subject: [PATCH 4/4] Add barrier example --- notebooks/mpi_barrier.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 notebooks/mpi_barrier.py diff --git a/notebooks/mpi_barrier.py b/notebooks/mpi_barrier.py new file mode 100644 index 00000000..f4d62f33 --- /dev/null +++ b/notebooks/mpi_barrier.py @@ -0,0 +1,19 @@ +import sys +import time +sys.path.insert(0, '../build/lib') +import intelqs_py as iqs +from intelqs_py import MPIEnvironment + + +if __name__ == '__main__': + iqs.init() + rank = MPIEnvironment.GetRank() + if not rank: + print(flush=True) + print('Process {} before the barrier'.format(rank), flush=True) + if not rank: + time.sleep(1) + print(flush=True) + MPIEnvironment.Barrier() + print('Process {} after the barrier'.format(rank), flush=True) + iqs.finalize()