Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Barrier and Timing to the Python Interface #31

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/mpi_env.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions notebooks/mpi_barrier.py
Original file line number Diff line number Diff line change
@@ -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()
25 changes: 21 additions & 4 deletions notebooks/python_mpi.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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()

Expand All @@ -39,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:
Expand All @@ -52,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
'''
8 changes: 7 additions & 1 deletion pybind11/intelqs_py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

//////////////////////////////////////////////////////////////////////////////
29 changes: 29 additions & 0 deletions src/mpi_env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <mpi.h>
#include <stdexcept>
#include <vector>
#else
#include <chrono>
#endif

#include <iomanip>
Expand Down Expand Up @@ -78,8 +80,17 @@ void StatePrint(std::string s, bool all)
}
}

double MaxTime()
{
auto now = std::chrono::system_clock::now();
return std::chrono::duration<double>(now.time_since_epoch()).count();
}

double MinTime() { return MaxTime(); }

#endif


/////////////////////////////////////////////////////////////////////////////////////////
// A few methods have the same implementation with / without MPI.
/////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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); }

Expand Down