aboutsummaryrefslogtreecommitdiff
path: root/tester/threading.c
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2026-09-20 18:37:48 +0200
committerhachem <im@hachem.wtf>2026-09-20 18:37:48 +0200
commita136c80d978841d317a6ad3930b9e608fdba1586 (patch)
tree7dde0e0372f60125bd7cb5f851f22efb01be703b /tester/threading.c
parente1f5c41c35fe26fddf5163ec06f78efd61ad35f4 (diff)
feat: add pthread parallel executionHEADmain
Diffstat (limited to 'tester/threading.c')
-rw-r--r--tester/threading.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/tester/threading.c b/tester/threading.c
new file mode 100644
index 0000000..78f788a
--- /dev/null
+++ b/tester/threading.c
@@ -0,0 +1,53 @@
+#include <stdio.h>
+
+#include "tests.h"
+
+static void build_wide(struct PsiQuantumCircuit* c)
+{
+ for (size_t i = 0; i < 8; i++)
+ psi_apply_h(c, i);
+ for (size_t i = 0; i + 1 < 8; i++)
+ psi_apply_cnot(c, i, i + 1);
+ psi_apply_rx(c, 3, 0.7);
+ psi_apply_t(c, 5);
+ psi_apply_cz(c, 2, 6);
+}
+
+void run_threading_tests(void)
+{
+ psi_test_section("Threading (parallel runtimes)");
+
+ printf(" threads: %zu\n", psi_thread_count());
+
+ struct PsiQuantumCircuit base = psi_new_quantum_circuit(8);
+ build_wide(&base);
+ struct PsiVector reference =
+ psi_clone_vector(*psi_compute_circuit_with(&base, PSI_RUNTIME_BASIC));
+
+ enum PsiRuntime runtimes[] = {
+ PSI_RUNTIME_BASIC_MT,
+ PSI_RUNTIME_BATCHED_MT,
+ PSI_RUNTIME_SIMD_MT,
+ PSI_RUNTIME_STRUCTURE_AWARE_MT,
+ };
+ const char* names[] = {
+ "BASIC_MT matches BASIC (8 qubits)",
+ "BATCHED_MT matches BASIC (8 qubits)",
+ "SIMD_MT matches BASIC (8 qubits)",
+ "STRUCTURE_AWARE_MT matches BASIC (8 qubits)",
+ };
+
+ for (size_t i = 0; i < sizeof runtimes / sizeof runtimes[0]; i++)
+ {
+ struct PsiQuantumCircuit c = psi_new_quantum_circuit(8);
+ build_wide(&c);
+ const struct PsiVector* state = psi_compute_circuit_with(&c, runtimes[i]);
+ bool ok = state->size == reference.size &&
+ psi_amps_match(state->data, reference.data, reference.size);
+ psi_test_check(ok, names[i]);
+ psi_free_quantum_circuit(&c);
+ }
+
+ psi_free_vector(&reference);
+ psi_free_quantum_circuit(&base);
+}