| 123 | |
| 124 | |
| 125 | SuperOp allocSuperOp(int numQubits) { |
| 126 | |
| 127 | // this inner function will NOT validate, verify whether |
| 128 | // allocations succeeded, nor attempt to free memory if not |
| 129 | |
| 130 | // prior validation ensures these never overflow |
| 131 | qindex numRows = powerOf2(2 * numQubits); |
| 132 | qindex numElems = numRows * numRows; |
| 133 | |
| 134 | // attempt top allocate 1D memory |
| 135 | qcomp* cpuMem = cpu_allocArray(numElems); // nullptr if failed |
| 136 | qcomp* gpuMem = nullptr; |
| 137 | if (getQuESTEnv().isGpuAccelerated) |
| 138 | gpuMem = gpu_allocArray(numElems); // nullptr if failed |
| 139 | |
| 140 | // prepare output SuperOp (avoiding C++20 designated initialiser) |
| 141 | SuperOp out; |
| 142 | out.numQubits = numQubits; |
| 143 | out.numRows = numRows; |
| 144 | |
| 145 | // attemptedly allocate 2D alias for 1D CPU memory |
| 146 | out.cpuElems = cpu_allocAndInitMatrixWrapper(cpuMem, numRows); // nullptr if failed |
| 147 | out.cpuElemsFlat = cpuMem; |
| 148 | out.gpuElemsFlat = gpuMem; |
| 149 | |
| 150 | // attemptedly allocate (un-initialised) flags in the heap so that struct copies are mutable |
| 151 | out.wasGpuSynced = cpu_allocHeapFlag(); // nullptr if failed |
| 152 | |
| 153 | // if heap flag allocated, mark it as unsynced (caller will handle if allocation failed) |
| 154 | if (mem_isAllocated(out.wasGpuSynced)) |
| 155 | *(out.wasGpuSynced) = 0; |
| 156 | |
| 157 | // caller will handle if any above allocations failed |
| 158 | return out; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | extern "C" SuperOp createSuperOp(int numQubits) { |
no test coverage detected