Skip to content

Commit

Permalink
Fix segfault when 0 OpenCL devices
Browse files Browse the repository at this point in the history
  • Loading branch information
WrathfulSpatula committed Sep 26, 2024
1 parent 6d1f6f5 commit 4e4c3a4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
6 changes: 4 additions & 2 deletions include/common/big_integer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,12 +485,14 @@ inline bool operator<=(const BigInteger& left, const BigInteger& right) { return
inline bool operator>(const BigInteger& left, const BigInteger& right) { return bi_compare(left, right) > 0; }
inline bool operator>=(const BigInteger& left, const BigInteger& right) { return bi_compare(left, right) >= 0; }

inline BigInteger operator++(BigInteger& a) {
inline BigInteger operator++(BigInteger& a)
{
bi_increment(&a, 1U);
return a;
}

inline BigInteger operator--(BigInteger& a) {
inline BigInteger operator--(BigInteger& a)
{
bi_decrement(&a, 1U);
return a;
}
Expand Down
3 changes: 3 additions & 0 deletions include/qtensornetwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class QTensorNetwork : public QInterface {
bool isReactiveSeparate;
bool useTGadget;
bool isNearClifford;
#if ENABLE_OPENCL || ENABLE_CUDA
bool isCpu;
#endif
int64_t devID;
real1_f separabilityThreshold;
complex globalPhase;
Expand Down
18 changes: 18 additions & 0 deletions src/qtensornetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ QTensorNetwork::QTensorNetwork(std::vector<QInterfaceEngine> eng, bitLenInt qBit
, isSparse(useSparseStateVec)
, useTGadget(true)
, isNearClifford(true)
#if ENABLE_OPENCL || ENABLE_CUDA
, isCpu(false)
#endif
, devID(deviceId)
, separabilityThreshold(sep_thresh)
, globalPhase(phaseFac)
Expand Down Expand Up @@ -76,6 +79,18 @@ QTensorNetwork::QTensorNetwork(std::vector<QInterfaceEngine> eng, bitLenInt qBit
}
}

#if ENABLE_OPENCL || ENABLE_CUDA
for (const QInterfaceEngine& et : engines) {
if ((et == QINTERFACE_HYBRID) || (et == QINTERFACE_OPENCL) || (et == QINTERFACE_CUDA)) {
break;
}
if (et == QINTERFACE_CPU) {
isCpu = true;
break;
}
}
#endif

SetPermutation(initState, globalPhase);
}

Expand All @@ -92,6 +107,9 @@ bitLenInt QTensorNetwork::GetThresholdQb()
return (bitLenInt)std::stoi(std::string(getenv("QRACK_MAX_PAGING_QB")));
}
#endif
if (isCpu) {
return 32U;
}
const size_t devCount = QRACK_GPU_SINGLETON.GetDeviceCount();
if (!devCount) {
return 32U;
Expand Down
3 changes: 2 additions & 1 deletion src/wasm_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,8 @@ quid init_qbdd_count(bitLenInt q)
}
}

const std::vector<QInterfaceEngine> simulatorType{ QINTERFACE_TENSOR_NETWORK, QINTERFACE_QUNIT, QINTERFACE_STABILIZER_HYBRID, QINTERFACE_BDT };
const std::vector<QInterfaceEngine> simulatorType{ QINTERFACE_TENSOR_NETWORK, QINTERFACE_QUNIT,
QINTERFACE_STABILIZER_HYBRID, QINTERFACE_BDT };

QInterfacePtr simulator = NULL;
if (q) {
Expand Down

0 comments on commit 4e4c3a4

Please sign in to comment.