Skip to content

Commit

Permalink
New CUDA simulator.
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeisakov committed Aug 10, 2022
1 parent a1e53ff commit 2f475ae
Show file tree
Hide file tree
Showing 16 changed files with 1,292 additions and 11,456 deletions.
24 changes: 10 additions & 14 deletions apps/qsim_base_cuda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -112,46 +112,42 @@ int main(int argc, char* argv[]) {
return 1;
}

Circuit<GateQSim<float>> circuit;
using fp_type = float;

Circuit<GateQSim<fp_type>> circuit;
if (!CircuitQsimParser<IOFile>::FromFile(opt.maxtime, opt.circuit_file,
circuit)) {
return 1;
}

struct Factory {
using Simulator = qsim::SimulatorCUDA<float>;
using Simulator = qsim::SimulatorCUDA<fp_type>;
using StateSpace = Simulator::StateSpace;

Factory(const StateSpace::Parameter& param1,
const Simulator::Parameter& param2)
: param1(param1), param2(param2) {}
Factory(const StateSpace::Parameter& param) : param(param) {}

StateSpace CreateStateSpace() const {
return StateSpace(param1);
return StateSpace(param);
}

Simulator CreateSimulator() const {
return Simulator(param2);
return Simulator();
}

const StateSpace::Parameter& param1;
const Simulator::Parameter& param2;
const StateSpace::Parameter& param;
};

using Simulator = Factory::Simulator;
using StateSpace = Simulator::StateSpace;
using State = StateSpace::State;
using Fuser = MultiQubitGateFuser<IO, GateQSim<float>>;
using Fuser = MultiQubitGateFuser<IO, GateQSim<fp_type>>;
using Runner = QSimRunner<IO, Fuser, Factory>;

StateSpace::Parameter param1;
param1.num_threads = opt.num_threads;
param1.num_dblocks = opt.num_dblocks;

Simulator::Parameter param2;
param2.num_threads = opt.num_threads;

Factory factory(param1, param2);
Factory factory(param1);

StateSpace state_space = factory.CreateStateSpace();
State state = state_space.Create(circuit.num_qubits);
Expand Down
16 changes: 6 additions & 10 deletions apps/qsim_qtrajectory_cuda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -190,23 +190,20 @@ int main(int argc, char* argv[]) {
using fp_type = float;

struct Factory {
using Simulator = qsim::SimulatorCUDA<fp_type>;
using Simulator = qsim::SimulatorCUDA<float>;
using StateSpace = Simulator::StateSpace;

Factory(const StateSpace::Parameter& param1,
const Simulator::Parameter& param2)
: param1(param1), param2(param2) {}
Factory(const StateSpace::Parameter& param) : param(param) {}

StateSpace CreateStateSpace() const {
return StateSpace(param1);
return StateSpace(param);
}

Simulator CreateSimulator() const {
return Simulator(param2);
return Simulator();
}

const StateSpace::Parameter& param1;
const Simulator::Parameter& param2;
const StateSpace::Parameter& param;
};

using Simulator = Factory::Simulator;
Expand Down Expand Up @@ -235,8 +232,7 @@ int main(int argc, char* argv[]) {
}

StateSpace::Parameter param1;
Simulator::Parameter param2;
Factory factory(param1, param2);
Factory factory(param1);

Simulator simulator = factory.CreateSimulator();
StateSpace state_space = factory.CreateStateSpace();
Expand Down
8 changes: 3 additions & 5 deletions docs/cirq_interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,9 @@ is required to enable GPU execution:
library if set to any other value.

If `use_gpu` is set and `gpu_mode` is set to 0, the remaining parameters can
optionally be set to fine-tune perfomance for a specific device or circuit.
optionally be set to fine-tune StateSpace perfomance for a specific device.
In most cases, the default values provide good performance.
* `gpu_sim_threads`: number of threads per CUDA block to use for the GPU
Simulator. This must be a power of 2 in the range [32, 256].
* `gpu_state_threads`: number of threads per CUDA block to use for the GPU
StateSpace. This must be a power of 2 in the range [32, 1024].
* `gpu_data_blocks`: number of data blocks to use on GPU. Below 16 data blocks,
performance is noticeably reduced.
* `gpu_data_blocks`: number of data blocks to use for the GPU StateSpace.
Below 16 data blocks, performance is noticeably reduced.
4 changes: 4 additions & 0 deletions lib/run_qsim.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ struct QSimRunner final {
}

if (param.verbosity > 3) {
state_space.DeviceSync();
double t2 = GetTime();
IO::messagef("gate %lu done in %g seconds.\n", i, t2 - t1);
}
Expand All @@ -147,6 +148,7 @@ struct QSimRunner final {
}

if (param.verbosity > 0) {
state_space.DeviceSync();
double t2 = GetTime();
IO::messagef("time is %g seconds.\n", t2 - t0);
}
Expand Down Expand Up @@ -221,12 +223,14 @@ struct QSimRunner final {
}

if (param.verbosity > 3) {
state_space.DeviceSync();
double t2 = GetTime();
IO::messagef("gate %lu done in %g seconds.\n", i, t2 - t1);
}
}

if (param.verbosity > 0) {
state_space.DeviceSync();
double t2 = GetTime();
IO::messagef("simu time is %g seconds.\n", t2 - t0);
}
Expand Down
Loading

0 comments on commit 2f475ae

Please sign in to comment.