The Quantum Exact Simulation Toolkit v4.3.0
Loading...
Searching...
No Matches
config.cpp
1/** @file
2 * Testing utilities for loading environment variables
3 * which configure the unit tests, independent of QuEST's
4 * internal environment variable facilities
5 *
6 * @author Tyson Jones
7 */
8
9#include <string>
10#include <cstdlib>
11#include <stdexcept>
12
13using std::string;
14
15
16/*
17 * PRIVATE
18 */
19
20string getEnvVarValue(string name) {
21
22 // unspecified var returns empty string
23 const char* ptr = std::getenv(name.c_str());
24 return (ptr == nullptr)? "" : std::string(ptr);
25}
26
27int getIntEnvVarValueOrDefault(string name, int defaultValue) {
28
29 string strValue = getEnvVarValue(name);
30 int intValue = defaultValue;
31
32 // overwrite default only when passed variable is interpretable
33 try {
34 intValue = std::stoi(strValue);
35 }
36 catch (const std::out_of_range&) { }
37 catch (const std::invalid_argument&) { }
38 return intValue;
39}
40
41
42/*
43 * PUBLIC TEST ENV VARS
44 */
45
46int getNumQubitsInUnitTestedQuregs() {
47
48 static int value = getIntEnvVarValueOrDefault("QUEST_TEST_NUM_QUBITS_IN_QUREG", 6);
49 return value;
50}
51
52int getMaxNumTestedQubitPermutations() {
53
54 static int value = getIntEnvVarValueOrDefault("QUEST_TEST_MAX_NUM_QUBIT_PERMUTATIONS", 0);
55 return value;
56}
57
58int getMaxNumTestedSuperoperatorTargets() {
59
60 static int value = getIntEnvVarValueOrDefault("QUEST_TEST_MAX_NUM_SUPEROP_TARGETS", 4);
61 return value;
62}
63
64int getNumTestedMixedDeploymentRepetitions() {
65
66 static int value = getIntEnvVarValueOrDefault("QUEST_TEST_NUM_MIXED_DEPLOYMENT_REPETITIONS", 10);
67 return value;
68}
69
70bool getWhetherToTestAllDeployments() {
71
72 static bool value = getIntEnvVarValueOrDefault("QUEST_TEST_TRY_ALL_DEPLOYMENTS", 1);
73 return value;
74}
75
76
77
78/*
79 * PUBLIC QUEST ENV VARS
80 */
81
82int getDefaultNumGpuThreadsPerBlock() {
83
84 // when the env-var is not present, we MUST return the default assumed by the QuEST src code,
85 // which at the time of writing, is a fixed 128 (rather than hardware-specific value)
86 const int compileTimeDefaultTPB = 128;
87
88 // when the env-var is present, we consult that, just like QuEST
89 static int value = getIntEnvVarValueOrDefault("QUEST_NUM_GPU_THREADS_PER_BLOCK", compileTimeDefaultTPB);
90 return value;
91}