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 auto-configuration of threads for qasm simulator #61

Merged
merged 29 commits into from
Apr 7, 2019

Conversation

hhorii
Copy link
Collaborator

@hhorii hhorii commented Feb 19, 2019

Summary

This PR changes qasm simulator to use an appropriate number of threads.

Details and comments

  • Added a new parameter max_statevector_memory_mb
  • Set a default value of max_statevector_memory_mb (largest power-of-two value in the system memory)
  • Ignore configured parallelization if memory is short
  • Avoid nested parallelization of OpenMP
  • Added test cases
  • Test on Linux
  • Test on Mac
  • Test on Windows
  • Add a CHANGELOG entry

@hhorii hhorii changed the title add auto-configuration of threads for qasm simulator [WIP] add auto-configuration of threads for qasm simulator Feb 19, 2019
@hhorii hhorii changed the title [WIP] add auto-configuration of threads for qasm simulator add auto-configuration of threads for qasm simulator Feb 20, 2019
@hhorii hhorii changed the title add auto-configuration of threads for qasm simulator [WIP] add auto-configuration of threads for qasm simulator Feb 22, 2019
@hhorii hhorii force-pushed the refactor_thread branch 2 times, most recently from 9addd65 to e37c7d0 Compare February 22, 2019 17:50
…ization. add a config parameter to standalone qasm simulator.
@hhorii hhorii force-pushed the refactor_thread branch from e37c7d0 to dd5fd52 Compare March 5, 2019 03:25
Copy link
Member

@atilag atilag left a comment

Choose a reason for hiding this comment

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

Thanks for the great job @hhorii
These are dense and complex changes, so I have many questions because I want to fully understand what's going on behind the scenes.
Once is perfectly clear, we may want to iterate again and try to make thing even simpler.

contrib/standalone/qasm_simulator.cpp Outdated Show resolved Hide resolved
contrib/standalone/qasm_simulator.cpp Outdated Show resolved Hide resolved
contrib/standalone/qasm_simulator.cpp Outdated Show resolved Hide resolved
src/base/controller.hpp Outdated Show resolved Hide resolved
src/base/controller.hpp Outdated Show resolved Hide resolved
// Set parallel_experiments_ to max_parallel_threads if necessary
parallel_experiments_ = 1; // default is 1
if (max_parallel_experiments_ > 1 // input exists
&& qobj.circuits.size() >= max_parallel_threads_ // sufficient parallelism
Copy link
Member

Choose a reason for hiding this comment

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

What's the problem of having less number of circuits than maximum number threads? I mean, if we have 6 circuits and 8 threads, we still want parallelism, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We should use threads to update states in most case for performance. But I'm now thinking that you are right.

I guess max_parallel_experiments_ will not be configured in most cases. If a user explicitly specifies it, I guess that the user wants to prioritize experiment-level parallelism.

parallel_experiments_ = 1; // default is 1
if (max_parallel_experiments_ > 1 // input exists
&& qobj.circuits.size() >= max_parallel_threads_ // sufficient parallelism
&& max_parallel_experiments_ >= max_parallel_threads_) { // sufficient parallelism
Copy link
Member

Choose a reason for hiding this comment

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

Same here, what if I have 8 parallel experiments but 16 threads. We still want parallelism.

int total_memory = 0;
for (Circuit &circ: qobj.circuits)
total_memory += state.required_memory_mb(circ.num_qubits, circ.ops);
if (total_memory <= max_statevector_memory_mb_) {
Copy link
Member

Choose a reason for hiding this comment

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

ok, so if we don't have enough memory for full parallelization, we just set parallelization at experiment level, isn't it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Right. But I'm thinking more precisely as follows.

    // if memory allows, execute experiments in parallel
    std::vector<int> required_memory_mb_list;
    for (const Circuit &circ: circuits)
      required_memory_mb_list.push_back(state.required_memory_mb(circ.num_qubits, circ.ops));
    std::sort(required_memory_mb_list.begin(), required_memory_mb_list.end(), std::greater<int>());

    int total_memory = 0;
    parallel_experiments_ = 0;
    for (int required_memory_mb: required_memory_mb_list) {
      total_memory += required_memory_mb;
      if (total_memory > max_statevector_memory_mb_)
        break;
      ++parallel_experiments_;
    }

    if (parallel_experiments_ == 0) {
      throw std::runtime_error("a circuit requires more memory than configured max_statevector_memory_mb.");
    } else if (parallel_experiments_ != 1) {
      if (parallel_experiments_ > max_parallel_experiments_)
        parallel_experiments_ = max_parallel_experiments_;
      parallel_shots_ = 1;
      parallel_state_update_ = 1;
      return;
    }

src/simulators/qasm/qasm_controller.hpp Outdated Show resolved Hide resolved
src/simulators/qasm/qasm_controller.hpp Outdated Show resolved Hide resolved
src/simulators/qasm/qasm_controller.hpp Outdated Show resolved Hide resolved
src/simulators/qasm/qasm_controller.hpp Outdated Show resolved Hide resolved
src/simulators/qasm/qasm_controller.hpp Outdated Show resolved Hide resolved
@hhorii hhorii changed the title [WIP] add auto-configuration of threads for qasm simulator add auto-configuration of threads for qasm simulator Mar 25, 2019
@hhorii hhorii force-pushed the refactor_thread branch 3 times, most recently from 513cf58 to 8fc8d3a Compare March 26, 2019 04:26
@hhorii hhorii force-pushed the refactor_thread branch from c748c76 to ebcfc36 Compare April 2, 2019 05:20
* Update QuantumError, ReadoutError to work with QuantumChannel

* update tests
@hhorii hhorii force-pushed the refactor_thread branch from bb335d4 to c52f8c5 Compare April 3, 2019 07:25
@chriseclectic chriseclectic added this to the 0.2 milestone Apr 3, 2019
Copy link
Member

@atilag atilag left a comment

Choose a reason for hiding this comment

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

Great job @hhorii
We are very close to merge this PR.
I haven't found anything stopping us from merging... just a few minor styling comments.

src/base/controller.hpp Outdated Show resolved Hide resolved
if (JSON::check_key("max_memory_mb", config)) {
JSON::get_value(max_memory_mb_, "max_memory_mb", config);
} else {
auto system_memory_mb = (int) get_system_memory_mb();
Copy link
Member

Choose a reason for hiding this comment

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

System memory can't be negative, why do you need a C-style cast to signed integer here?

static_cast<int>(circ.shots) });

if (max_parallel_shots_ < 1) {
// if max_parallel_shots is not configured, nested parallelization is avoided
Copy link
Member

Choose a reason for hiding this comment

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

The comment refers to the line above, could you move it right above the: if (max_parallel_shots_ 1) { ?

parallel_state_update_ = max_parallel_threads_;
}
} else {
// otherwise,
Copy link
Member

Choose a reason for hiding this comment

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

There's no need for this comment :)

@atilag
Copy link
Member

atilag commented Apr 3, 2019

@hhorii just one last thing before merging. Could you please add an entry in the CHANGELOG so we can keep track very easily of what have changed in this release??
I have added this as a new item in your first comment entry.
Thanks!

@atilag atilag merged commit e002af4 into Qiskit:master Apr 7, 2019
dcmckayibm pushed a commit to dcmckayibm/qiskit-aer that referenced this pull request Nov 3, 2019
add auto-configuration of threads for qasm simulator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants