Skip to content

Commit 3d31b49

Browse files
authored
Merge pull request #496 from quantumlib/no-immintrin-in-cuda-code
immintrin.h is not included in CUDA code.
2 parents 931865c + 91bc014 commit 3d31b49

17 files changed

+69
-26
lines changed

apps/qsim_amplitudes.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "../lib/run_qsim.h"
3131
#include "../lib/simmux.h"
3232
#include "../lib/util.h"
33+
#include "../lib/util_cpu.h"
3334

3435
constexpr char usage[] = "usage:\n ./qsim_amplitudes -c circuit_file "
3536
"-d times_to_save_results -i input_files "

apps/qsim_base.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include "../lib/io_file.h"
2727
#include "../lib/run_qsim.h"
2828
#include "../lib/simmux.h"
29-
#include "../lib/util.h"
29+
#include "../lib/util_cpu.h"
3030

3131
constexpr char usage[] = "usage:\n ./qsim_base -c circuit -d maxtime "
3232
"-s seed -t threads -f max_fused_size "

apps/qsim_von_neumann.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include "../lib/io_file.h"
2929
#include "../lib/run_qsim.h"
3030
#include "../lib/simmux.h"
31-
#include "../lib/util.h"
31+
#include "../lib/util_cpu.h"
3232

3333
constexpr char usage[] = "usage:\n ./qsim_von_neumann -c circuit -d maxtime "
3434
"-s seed -t threads -f max_fused_size "

apps/qsimh_amplitudes.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "../lib/run_qsimh.h"
3131
#include "../lib/simmux.h"
3232
#include "../lib/util.h"
33+
#include "../lib/util_cpu.h"
3334

3435
constexpr char usage[] = "usage:\n ./qsimh_amplitudes -c circuit_file "
3536
"-d maxtime -k part1_qubits "

apps/qsimh_base.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "../lib/run_qsimh.h"
3131
#include "../lib/simmux.h"
3232
#include "../lib/util.h"
33+
#include "../lib/util_cpu.h"
3334

3435
constexpr char usage[] = "usage:\n ./qsimh_base -c circuit_file "
3536
"-d maximum_time -k part1_qubits "

lib/BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ cc_library(
6161
"unitary_calculator_basic.h",
6262
"unitary_calculator_sse.h",
6363
"util.h",
64+
"util_cpu.h",
6465
"vectorspace.h",
6566
],
6667
)
@@ -123,6 +124,7 @@ cc_library(
123124
"unitary_calculator_basic.h",
124125
"unitary_calculator_sse.h",
125126
"util.h",
127+
"util_cpu.h",
126128
"util_cuda.h",
127129
"vectorspace.h",
128130
"vectorspace_cuda.h",
@@ -170,6 +172,7 @@ cc_library(
170172
"unitary_calculator_basic.h",
171173
"unitary_calculator_sse.h",
172174
"util.h",
175+
"util_cpu.h",
173176
"vectorspace.h",
174177
],
175178
)
@@ -208,6 +211,7 @@ cc_library(
208211
"statespace_basic.h",
209212
"statespace_sse.h",
210213
"util.h",
214+
"util_cpu.h",
211215
"vectorspace.h",
212216
],
213217
)
@@ -238,6 +242,11 @@ cc_library(
238242
hdrs = ["util.h"],
239243
)
240244

245+
cc_library(
246+
name = "util_cpu",
247+
hdrs = ["util_cpu.h"],
248+
)
249+
241250
# cuda_library
242251
cc_library(
243252
name = "util_cuda",

lib/util.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
#ifndef UTIL_H_
1616
#define UTIL_H_
1717

18-
#ifdef __SSE__
19-
# include <immintrin.h>
20-
#endif
21-
2218
#include <algorithm>
2319
#include <chrono>
2420
#include <random>
@@ -88,23 +84,6 @@ inline std::vector<DistrRealType> GenerateRandomValues(
8884
return rs;
8985
}
9086

91-
// This function sets flush-to-zero and denormals-are-zeros MXCSR control
92-
// flags. This prevents rare cases of performance slowdown potentially at
93-
// the cost of a tiny precision loss.
94-
inline void SetFlushToZeroAndDenormalsAreZeros() {
95-
#ifdef __SSE2__
96-
_mm_setcsr(_mm_getcsr() | 0x8040);
97-
#endif
98-
}
99-
100-
// This function clears flush-to-zero and denormals-are-zeros MXCSR control
101-
// flags.
102-
inline void ClearFlushToZeroAndDenormalsAreZeros() {
103-
#ifdef __SSE2__
104-
_mm_setcsr(_mm_getcsr() & ~unsigned{0x8040});
105-
#endif
106-
}
107-
10887
} // namespace qsim
10988

11089
#endif // UTIL_H_

lib/util_cpu.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2019 Google LLC. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef UTIL_CPU_H_
16+
#define UTIL_CPU_H_
17+
18+
#ifdef __SSE2__
19+
# include <immintrin.h>
20+
#endif
21+
22+
namespace qsim {
23+
24+
// This function sets flush-to-zero and denormals-are-zeros MXCSR control
25+
// flags. This prevents rare cases of performance slowdown potentially at
26+
// the cost of a tiny precision loss.
27+
inline void SetFlushToZeroAndDenormalsAreZeros() {
28+
#ifdef __SSE2__
29+
_mm_setcsr(_mm_getcsr() | 0x8040);
30+
#endif
31+
}
32+
33+
// This function clears flush-to-zero and denormals-are-zeros MXCSR control
34+
// flags.
35+
inline void ClearFlushToZeroAndDenormalsAreZeros() {
36+
#ifdef __SSE2__
37+
_mm_setcsr(_mm_getcsr() & ~unsigned{0x8040});
38+
#endif
39+
}
40+
41+
} // namespace qsim
42+
43+
#endif // UTIL_CPU_H_

pybind_interface/avx2/pybind_main_avx2.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "../../lib/formux.h"
1818
#include "../../lib/simulator_avx.h"
19+
#include "../../lib/util_cpu.h"
1920

2021
namespace qsim {
2122
template <typename For>

pybind_interface/avx512/pybind_main_avx512.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "../../lib/formux.h"
1818
#include "../../lib/simulator_avx512.h"
19+
#include "../../lib/util_cpu.h"
1920

2021
namespace qsim {
2122
template <typename For>

0 commit comments

Comments
 (0)