Skip to content

Heuristic threshold to prevent OpenMP slow-down in simple spin_op *= - #1676

Merged
1tnguyen merged 4 commits into
NVIDIA:mainfrom
1tnguyen:tnguyen/spin-op-omp
May 14, 2024
Merged

1tnguyen merged 4 commits into
NVIDIA:mainfrom
1tnguyen:tnguyen/spin-op-omp

Conversation

@1tnguyen

Copy link
Copy Markdown
Collaborator

Description

Add a threshold for simple cases of spin_op::operator*= so that OpenMP doesn't start unnecessary multi-threading, causing a significant slow-down.

With some simple cases, like below (single term * single term)

const int n_spins = 50;
  const auto hamGen = [=]() -> cudaq::spin_op {
    cudaq::spin_op tdOp(n_spins);
    for (int i = 0; i < n_spins - 1; ++i) {
      tdOp += (cudaq::spin::x(i) * cudaq::spin::x(i + 1));
      tdOp += (cudaq::spin::y(i) * cudaq::spin::y(i + 1));
      tdOp += (cudaq::spin::z(i) * cudaq::spin::z(i + 1));
    }
    return tdOp;
  };

  constexpr int num_runs = 100;
  std::vector<double> runtimeUs;
  for (int run = 0; run < num_runs; ++run) {
    const auto start = std::chrono::high_resolution_clock::now();
    auto ham = hamGen();
    const auto stop = std::chrono::high_resolution_clock::now();
    auto duration =
        std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
    runtimeUs.emplace_back(duration.count());
  }

  const double avg = std::reduce(runtimeUs.begin(), runtimeUs.end()) / runtimeUs.size();
  std::cout << "Avg: " << avg << "[us]\n";

Without this threshold, unnecessary OpenMP multithreading made it really slow (up to ~100x).

@1tnguyen
1tnguyen requested review from amccaskey and bmhowe23 May 14, 2024 07:50

@amccaskey amccaskey left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@github-actions

Copy link
Copy Markdown

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions Bot pushed a commit that referenced this pull request May 14, 2024
@github-actions

Copy link
Copy Markdown

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions Bot pushed a commit that referenced this pull request May 14, 2024
@1tnguyen
1tnguyen merged commit 07f60fa into NVIDIA:main May 14, 2024
@github-actions github-actions Bot locked and limited conversation to collaborators May 14, 2024
@bettinaheim bettinaheim added release notes Changes need to be captured in the release notes performance labels Jul 29, 2024
@bettinaheim bettinaheim added this to the release 0.8.0 milestone Jul 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

performance release notes Changes need to be captured in the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants