Skip to content

Commit

Permalink
immintrin.h is not included in CUDA code.
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeisakov committed Jan 5, 2022
1 parent 931865c commit 91bc014
Show file tree
Hide file tree
Showing 17 changed files with 69 additions and 26 deletions.
1 change: 1 addition & 0 deletions apps/qsim_amplitudes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "../lib/run_qsim.h"
#include "../lib/simmux.h"
#include "../lib/util.h"
#include "../lib/util_cpu.h"

constexpr char usage[] = "usage:\n ./qsim_amplitudes -c circuit_file "
"-d times_to_save_results -i input_files "
Expand Down
2 changes: 1 addition & 1 deletion apps/qsim_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "../lib/io_file.h"
#include "../lib/run_qsim.h"
#include "../lib/simmux.h"
#include "../lib/util.h"
#include "../lib/util_cpu.h"

constexpr char usage[] = "usage:\n ./qsim_base -c circuit -d maxtime "
"-s seed -t threads -f max_fused_size "
Expand Down
2 changes: 1 addition & 1 deletion apps/qsim_von_neumann.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "../lib/io_file.h"
#include "../lib/run_qsim.h"
#include "../lib/simmux.h"
#include "../lib/util.h"
#include "../lib/util_cpu.h"

constexpr char usage[] = "usage:\n ./qsim_von_neumann -c circuit -d maxtime "
"-s seed -t threads -f max_fused_size "
Expand Down
1 change: 1 addition & 0 deletions apps/qsimh_amplitudes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "../lib/run_qsimh.h"
#include "../lib/simmux.h"
#include "../lib/util.h"
#include "../lib/util_cpu.h"

constexpr char usage[] = "usage:\n ./qsimh_amplitudes -c circuit_file "
"-d maxtime -k part1_qubits "
Expand Down
1 change: 1 addition & 0 deletions apps/qsimh_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "../lib/run_qsimh.h"
#include "../lib/simmux.h"
#include "../lib/util.h"
#include "../lib/util_cpu.h"

constexpr char usage[] = "usage:\n ./qsimh_base -c circuit_file "
"-d maximum_time -k part1_qubits "
Expand Down
9 changes: 9 additions & 0 deletions lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ cc_library(
"unitary_calculator_basic.h",
"unitary_calculator_sse.h",
"util.h",
"util_cpu.h",
"vectorspace.h",
],
)
Expand Down Expand Up @@ -123,6 +124,7 @@ cc_library(
"unitary_calculator_basic.h",
"unitary_calculator_sse.h",
"util.h",
"util_cpu.h",
"util_cuda.h",
"vectorspace.h",
"vectorspace_cuda.h",
Expand Down Expand Up @@ -170,6 +172,7 @@ cc_library(
"unitary_calculator_basic.h",
"unitary_calculator_sse.h",
"util.h",
"util_cpu.h",
"vectorspace.h",
],
)
Expand Down Expand Up @@ -208,6 +211,7 @@ cc_library(
"statespace_basic.h",
"statespace_sse.h",
"util.h",
"util_cpu.h",
"vectorspace.h",
],
)
Expand Down Expand Up @@ -238,6 +242,11 @@ cc_library(
hdrs = ["util.h"],
)

cc_library(
name = "util_cpu",
hdrs = ["util_cpu.h"],
)

# cuda_library
cc_library(
name = "util_cuda",
Expand Down
21 changes: 0 additions & 21 deletions lib/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
#ifndef UTIL_H_
#define UTIL_H_

#ifdef __SSE__
# include <immintrin.h>
#endif

#include <algorithm>
#include <chrono>
#include <random>
Expand Down Expand Up @@ -88,23 +84,6 @@ inline std::vector<DistrRealType> GenerateRandomValues(
return rs;
}

// This function sets flush-to-zero and denormals-are-zeros MXCSR control
// flags. This prevents rare cases of performance slowdown potentially at
// the cost of a tiny precision loss.
inline void SetFlushToZeroAndDenormalsAreZeros() {
#ifdef __SSE2__
_mm_setcsr(_mm_getcsr() | 0x8040);
#endif
}

// This function clears flush-to-zero and denormals-are-zeros MXCSR control
// flags.
inline void ClearFlushToZeroAndDenormalsAreZeros() {
#ifdef __SSE2__
_mm_setcsr(_mm_getcsr() & ~unsigned{0x8040});
#endif
}

} // namespace qsim

#endif // UTIL_H_
43 changes: 43 additions & 0 deletions lib/util_cpu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2019 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef UTIL_CPU_H_
#define UTIL_CPU_H_

#ifdef __SSE2__
# include <immintrin.h>
#endif

namespace qsim {

// This function sets flush-to-zero and denormals-are-zeros MXCSR control
// flags. This prevents rare cases of performance slowdown potentially at
// the cost of a tiny precision loss.
inline void SetFlushToZeroAndDenormalsAreZeros() {
#ifdef __SSE2__
_mm_setcsr(_mm_getcsr() | 0x8040);
#endif
}

// This function clears flush-to-zero and denormals-are-zeros MXCSR control
// flags.
inline void ClearFlushToZeroAndDenormalsAreZeros() {
#ifdef __SSE2__
_mm_setcsr(_mm_getcsr() & ~unsigned{0x8040});
#endif
}

} // namespace qsim

#endif // UTIL_CPU_H_
1 change: 1 addition & 0 deletions pybind_interface/avx2/pybind_main_avx2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "../../lib/formux.h"
#include "../../lib/simulator_avx.h"
#include "../../lib/util_cpu.h"

namespace qsim {
template <typename For>
Expand Down
1 change: 1 addition & 0 deletions pybind_interface/avx512/pybind_main_avx512.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "../../lib/formux.h"
#include "../../lib/simulator_avx512.h"
#include "../../lib/util_cpu.h"

namespace qsim {
template <typename For>
Expand Down
1 change: 1 addition & 0 deletions pybind_interface/basic/pybind_main_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "../../lib/formux.h"
#include "../../lib/simulator_basic.h"
#include "../../lib/util_cpu.h"

namespace qsim {
template <typename For>
Expand Down
3 changes: 3 additions & 0 deletions pybind_interface/cuda/pybind_main_cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ namespace qsim {
StateSpace::Parameter ss_params;
Simulator::Parameter sim_params;
};

inline void SetFlushToZeroAndDenormalsAreZeros() {}
inline void ClearFlushToZeroAndDenormalsAreZeros() {}
}

#include "../pybind_main.cpp"
3 changes: 3 additions & 0 deletions pybind_interface/custatevec/pybind_main_custatevec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ struct Factory {
custatevecHandle_t custatevec_handle;
};

inline void SetFlushToZeroAndDenormalsAreZeros() {}
inline void ClearFlushToZeroAndDenormalsAreZeros() {}

}

#include "../pybind_main.cpp"
1 change: 0 additions & 1 deletion pybind_interface/pybind_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include "../lib/qtrajectory.h"
#include "../lib/run_qsim.h"
#include "../lib/run_qsimh.h"
#include "../lib/util.h"

using namespace qsim;

Expand Down
1 change: 1 addition & 0 deletions pybind_interface/sse/pybind_main_sse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "../../lib/formux.h"
#include "../../lib/simulator_sse.h"
#include "../../lib/util_cpu.h"

namespace qsim {
template <typename For>
Expand Down
2 changes: 1 addition & 1 deletion tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ cc_library(
"//lib:gate_appl",
"//lib:gates_qsim",
"//lib:io",
"//lib:util",
"//lib:util_cpu",
],
testonly = 1,
)
Expand Down
2 changes: 1 addition & 1 deletion tests/simulator_testfixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "../lib/gate_appl.h"
#include "../lib/gates_qsim.h"
#include "../lib/io.h"
#include "../lib/util.h"
#include "../lib/util_cpu.h"

namespace qsim {

Expand Down

0 comments on commit 91bc014

Please sign in to comment.