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

Fix nested parallel performance #1949

Merged
merged 1 commit into from
Sep 28, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fixes:
- |
OpenMP nested parallel simulation for parallel experiments + parallel state
update was very slow because gate fusion uses unitary simulator inside
and it used omp parallel region. This fix remove parallel region in
gate fusion and improve performance of nested parallel simulations
2 changes: 1 addition & 1 deletion src/controllers/aer_controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ Result Controller::execute(std::vector<std::shared_ptr<Circuit>> &circuits,

// nested should be set to zero if num_threads clause will be used
#if _OPENMP >= 200805
omp_set_max_active_levels(2);
omp_set_max_active_levels(1);
#else
omp_set_nested(1);
#endif
Expand Down
9 changes: 4 additions & 5 deletions src/simulators/statevector/qubitvector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -890,11 +890,10 @@ template <typename data_t>
void QubitVector<data_t>::zero() {
const int_t END = data_size_; // end for k loop

#pragma omp parallel for if (num_qubits_ > omp_threshold_ && omp_threads_ > 1) \
num_threads(omp_threads_)
for (int_t k = 0; k < END; ++k) {
data_[k] = 0.0;
}
auto zero_proc = [this](int_t i) { data_[i] = 0.0; };
Utils::apply_omp_parallel_for(
(num_qubits_ > omp_threshold_ && omp_threads_ > 1), 0, END, zero_proc,
omp_threads_);
}

template <typename data_t>
Expand Down
30 changes: 15 additions & 15 deletions src/simulators/unitary/unitarymatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,13 @@ void UnitaryMatrix<data_t>::initialize() {
BaseVector::zero();
// Set to be identity matrix
const int_t nrows = rows_; // end for k loop
#pragma omp parallel if (BaseVector::num_qubits_ > \
BaseVector::omp_threshold_ && \
BaseVector::omp_threads_ > 1) \
num_threads(BaseVector::omp_threads_)
for (int_t k = 0; k < nrows; ++k) {
BaseVector::data_[k * (nrows + 1)] = 1.0;
}
auto initialize_proc = [this](int_t i) {
BaseVector::data_[i * (rows_ + 1)] = 1.0;
};
Utils::apply_omp_parallel_for(
(BaseVector::num_qubits_ > BaseVector::omp_threshold_ &&
BaseVector::omp_threads_ > 1),
0, rows_, initialize_proc, BaseVector::omp_threads_);
}

template <class data_t>
Expand All @@ -260,15 +260,15 @@ void UnitaryMatrix<data_t>::initialize_from_matrix(
std::to_string(mat.GetRows()) + "," + std::to_string(mat.GetColumns()) +
").");
}

#pragma omp parallel if (BaseVector::num_qubits_ > \
BaseVector::omp_threshold_ && \
BaseVector::omp_threads_ > 1) \
num_threads(BaseVector::omp_threads_)
for (int_t row = 0; row < nrows; ++row)
for (int_t col = 0; col < nrows; ++col) {
BaseVector::data_[row + nrows * col] = mat(row, col);
auto initialize_proc = [this, &mat](int_t row) {
for (int_t col = 0; col < rows_; ++col) {
BaseVector::data_[row + rows_ * col] = mat(row, col);
}
};
Utils::apply_omp_parallel_for(
(BaseVector::num_qubits_ > BaseVector::omp_threshold_ &&
BaseVector::omp_threads_ > 1),
0, rows_, initialize_proc, BaseVector::omp_threads_);
}

template <class data_t>
Expand Down
20 changes: 15 additions & 5 deletions src/transpile/fusion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -851,11 +851,21 @@ void Fusion::optimize_circuit(Circuit &circ, Noise::NoiseModel &noise,
if (circ.ops.size() % parallelization_)
++unit;

#pragma omp parallel for if (parallelization_ > 1) num_threads(parallelization_)
for (int_t i = 0; i < parallelization_; i++) {
int_t start = unit * i;
int_t end = std::min(start + unit, (int_t)circ.ops.size());
optimize_circuit(circ, noise, allowed_opset, start, end, fuser, method);
if (parallelization_ > 1) {
#pragma omp parallel for num_threads(parallelization_)
for (int_t i = 0; i < parallelization_; i++) {
int_t start = unit * i;
int_t end = std::min(start + unit, (int_t)circ.ops.size());
optimize_circuit(circ, noise, allowed_opset, start, end, fuser,
method);
}
} else {
for (int_t i = 0; i < parallelization_; i++) {
int_t start = unit * i;
int_t end = std::min(start + unit, (int_t)circ.ops.size());
optimize_circuit(circ, noise, allowed_opset, start, end, fuser,
method);
}
}
result.metadata.add(parallelization_, "fusion", "parallelization");
}
Expand Down
Loading