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

Bug/init with sv #57

Open
wants to merge 6 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ docs/build
venv
docs/source/doxyoutput
docs/source/api
**/build/**
**/bin/**
23 changes: 17 additions & 6 deletions include/alignedallocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ namespace iqs {
template <typename T, unsigned int Alignment>
class AlignedAllocator
{
private:
T* memory_pointer = nullptr;
public:

typedef T* pointer;
Expand All @@ -65,11 +67,13 @@ class AlignedAllocator
};

AlignedAllocator() noexcept {}
AlignedAllocator(AlignedAllocator const&) noexcept {}
AlignedAllocator(T* memory_ptr) noexcept { memory_pointer = memory_ptr; }
AlignedAllocator(AlignedAllocator const& other) noexcept { memory_pointer = other.memory_pointer; }

template <typename U>
AlignedAllocator(AlignedAllocator<U, Alignment> const&) noexcept
AlignedAllocator(AlignedAllocator<U, Alignment> const& other) noexcept
{
memory_pointer = other.memory_pointer;
}

pointer allocate(size_type n)
Expand All @@ -78,22 +82,29 @@ class AlignedAllocator

static_assert(isPowerOf2(Alignment), "Alignment not a power of 2");

if ( memory_pointer != nullptr ) {
p = reinterpret_cast<pointer>(memory_pointer);
}
else {
#ifdef _WIN32
p = reinterpret_cast<pointer>(_aligned_malloc(n * sizeof(T), Alignment));
if (p == 0) throw std::bad_alloc();
p = reinterpret_cast<pointer>(_aligned_malloc(n * sizeof(T), Alignment));
if (p == 0) throw std::bad_alloc();
#else
if (posix_memalign(reinterpret_cast<void**>(&p), Alignment, n * sizeof(T)))
throw std::bad_alloc();
if (posix_memalign(reinterpret_cast<void**>(&p), Alignment, n * sizeof(T)))
throw std::bad_alloc();
#endif
}
return p;
}

void deallocate(pointer p, size_type) noexcept
{
#ifdef _WIN32
_aligned_free(p);
// std::cout << "_aligned_free: " << p << std::endl;
#else
std::free(p);
// std::cout << "std::free: " << p << std::endl;
#endif
}

Expand Down
20 changes: 10 additions & 10 deletions include/qureg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,22 +375,22 @@ class QubitRegister
{assert(rng_ptr_); rng_ptr_->SetSeedStreamPtrs(seed); }

// Members
std::size_t num_qubits;
std::size_t num_qubits = 0;
std::vector<Type, iqs::AlignedAllocator<Type, 256>> state_storage;
Type *state;
Permutation *qubit_permutation;
Timer *timer;
GateCounter *gate_counter; // Count how many gates acted on given program qubits.
std::size_t llc_watermarkbit;
bool imported_state;
bool specialize;
Type *state = nullptr;
Permutation *qubit_permutation = nullptr;
Timer *timer = nullptr;
GateCounter *gate_counter = nullptr; // Count how many gates acted on given program qubits.
std::size_t llc_watermarkbit = 0;
bool imported_state = false;
bool specialize = false;
bool specialize2 = false;
// Related to the simulation of quantum channels:
BaseType overall_sign_of_channels = 1;

// temporary buffer for fusion
bool fusion;
unsigned log2llc;
bool fusion = false;
unsigned log2llc = 0;
std::vector<std::tuple<std::string, TM2x2<Type>, unsigned, unsigned>> fwindow;

// set option of printing more info.
Expand Down
18 changes: 18 additions & 0 deletions src/qureg_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,27 @@ template <class Type>
QubitRegister<Type>::QubitRegister(std::size_t new_num_qubits, Type *state,
std::size_t tmp_spacesize_)
{
unsigned myrank=0, nprocs=1, num_ranks_per_node=1;
myrank = iqs::mpi::Environment::GetStateRank();
nprocs = iqs::mpi::Environment::GetStateSize();
num_ranks_per_node = iqs::mpi::Environment::GetNumRanksPerNode();

imported_state = true;
Initialize(new_num_qubits, tmp_spacesize_);

#ifdef USE_MM_MALLOC
this->state = state;
#else
// create a temporary vector to use AlignedAllocator with prepared memory block
std::vector<Type, iqs::AlignedAllocator<Type, 256>> tmp_storage(0, iqs::AlignedAllocator<Type, 256>(state));

std::size_t num_amplitudes = (nprocs == 1) ? LocalSize() : (LocalSize() + TmpSize());
tmp_storage.resize(num_amplitudes);

// move that memory block into our state_storage
state_storage = std::move(tmp_storage);
this->state = &state_storage[0];
#endif
}


Expand Down