Skip to content

Commit efbde66

Browse files
Merge pull request #378 from laurynasas/separate-simd-compilation-units
Separate simd compilation units
2 parents 786cd6e + 6a2e84a commit efbde66

24 files changed

+582
-189
lines changed

CMakeLists.txt

+5-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
cmake_minimum_required(VERSION 3.11)
22
project(qsim)
33

4-
set(CMAKE_CXX_FLAGS "-O3 -march=native -fopenmp")
5-
6-
include(FetchContent)
7-
8-
FetchContent_Declare(
9-
pybind11
10-
GIT_REPOSITORY https://github.com/pybind/pybind11
11-
GIT_TAG v2.2.4
12-
)
13-
FetchContent_GetProperties(pybind11)
14-
if(NOT pybind11_POPULATED)
15-
FetchContent_Populate(pybind11)
16-
add_subdirectory(${pybind11_SOURCE_DIR} ${pybind11_BINARY_DIR})
17-
endif()
18-
pybind11_add_module(qsim pybind_interface/pybind_main.cpp)
4+
ADD_SUBDIRECTORY(pybind_interface/sse)
5+
ADD_SUBDIRECTORY(pybind_interface/avx512)
6+
ADD_SUBDIRECTORY(pybind_interface/avx2)
7+
ADD_SUBDIRECTORY(pybind_interface/basic)
8+
ADD_SUBDIRECTORY(pybind_interface/decide)

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ TARGETS = qsim
55
TESTS = run-cxx-tests
66

77
CXX=g++
8-
CXXFLAGS = -O3 -march=native -fopenmp
8+
CXXFLAGS = -O3 -fopenmp
99
PYBIND11 = true
1010

1111
export CXX

pybind_interface/Makefile

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
1-
# The name of the shared library that results after compiling qsim for Pybind11
2-
QSIMLIB = ../qsimcirq/qsim`python3-config --extension-suffix`
1+
# The names of the shared libraries that result after compiling qsim for Pybind11
2+
QSIMLIB_BASIC = ../qsimcirq/qsim_basic`python3-config --extension-suffix`
3+
QSIMLIB_SSE = ../qsimcirq/qsim_sse`python3-config --extension-suffix`
4+
QSIMLIB_AVX2 = ../qsimcirq/qsim_avx2`python3-config --extension-suffix`
5+
QSIMLIB_AVX512 = ../qsimcirq/qsim_avx512`python3-config --extension-suffix`
6+
QSIMLIB_DECIDE = ../qsimcirq/qsim_decide`python3-config --extension-suffix`
37

4-
# The flags for the compilation of the Pybind11 interface
5-
PYBINDFLAGS = -Wall -shared -std=c++17 -fPIC `python3 -m pybind11 --includes`
8+
9+
# The flags for the compilation of the simd-specific Pybind11 interfaces
10+
PYBINDFLAGS_BASIC = -Wall -shared -std=c++17 -fPIC `python3 -m pybind11 --includes`
11+
PYBINDFLAGS_SSE = -msse4.1 -Wall -shared -std=c++17 -fPIC `python3 -m pybind11 --includes`
12+
PYBINDFLAGS_AVX2 = -mavx2 -mfma -Wall -shared -std=c++17 -fPIC `python3 -m pybind11 --includes`
13+
PYBINDFLAGS_AVX512 = -mavx512f -Wall -shared -std=c++17 -fPIC `python3 -m pybind11 --includes`
614

715
.PHONY: pybind
816
pybind:
9-
$(CXX) pybind_main.cpp -o $(QSIMLIB) $(CXXFLAGS) $(PYBINDFLAGS)
17+
$(CXX) basic/pybind_main_basic.cpp -o $(QSIMLIB_BASIC) $(CXXFLAGS) $(PYBINDFLAGS_BASIC)
18+
$(CXX) sse/pybind_main_sse.cpp -o $(QSIMLIB_SSE) $(CXXFLAGS) $(PYBINDFLAGS_SSE)
19+
$(CXX) avx2/pybind_main_avx2.cpp -o $(QSIMLIB_AVX2) $(CXXFLAGS) $(PYBINDFLAGS_AVX2)
20+
$(CXX) avx512/pybind_main_avx512.cpp -o $(QSIMLIB_AVX512) $(CXXFLAGS) $(PYBINDFLAGS_AVX512)
21+
$(CXX) decide/decide.cpp -o $(QSIMLIB_DECIDE) $(CXXFLAGS) $(PYBINDFLAGS_BASIC)
1022

1123
.PHONY: clean
1224
clean:
13-
-rm -f ./*.x ./*.a ./*.so ./*.mod $(QSIMLIB)
25+
-rm -f ./basic/*.x ./basic/*.a ./basic/*.so ./basic/*.mod $(QSIMLIB_BASIC)
26+
-rm -f ./sse/*.x ./sse/*.a ./sse/*.so ./sse/*.mod $(QSIMLIB_SSE)
27+
-rm -f ./avx2/*.x ./avx2/*.a ./avx2/*.so ./avx2/*.mod $(QSIMLIB_AVX2)
28+
-rm -f ./avx512/*.x ./avx512/*.a ./avx512/*.so ./avx512/*.mod $(QSIMLIB_AVX512)
29+
-rm -f ./decide/*.x ./decide/*.a ./decide/*.so ./decide/*.mod $(QSIMLIB_DECIDE)

pybind_interface/avx2/CMakeLists.txt

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cmake_minimum_required(VERSION 3.11)
2+
project(qsim)
3+
4+
IF (WIN32)
5+
set(CMAKE_CXX_FLAGS "/arch:AVX2 /O2 /openmp")
6+
ELSE()
7+
set(CMAKE_CXX_FLAGS "-mavx2 -mfma -O3 -fopenmp")
8+
ENDIF()
9+
10+
if(APPLE)
11+
set(CMAKE_CXX_STANDARD 14)
12+
include_directories("/usr/local/include" "/usr/local/opt/llvm/include")
13+
link_directories("/usr/local/lib" "/usr/local/opt/llvm/lib")
14+
endif()
15+
16+
include(FetchContent)
17+
18+
FetchContent_Declare(
19+
pybind11
20+
GIT_REPOSITORY https://github.com/pybind/pybind11
21+
GIT_TAG v2.2.4
22+
)
23+
FetchContent_GetProperties(pybind11)
24+
if(NOT pybind11_POPULATED)
25+
FetchContent_Populate(pybind11)
26+
add_subdirectory(${pybind11_SOURCE_DIR} ${pybind11_BINARY_DIR})
27+
endif()
28+
pybind11_add_module(qsim_avx2 pybind_main_avx2.cpp)
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
#include "pybind_main_avx2.h"
16+
17+
#include "../../lib/simulator_avx.h"
18+
19+
namespace qsim {
20+
template <typename For>
21+
using Simulator = SimulatorAVX<For>;
22+
}
23+
24+
#include "../pybind_main.cpp"
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
#include "../pybind_main.h"
16+
17+
PYBIND11_MODULE(qsim_avx2, m) { MODULE_BINDINGS }
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
cmake_minimum_required(VERSION 3.11)
2+
project(qsim)
3+
4+
5+
IF (WIN32)
6+
set(CMAKE_CXX_FLAGS "/arch:AVX512 /O2 /openmp")
7+
ELSE()
8+
set(CMAKE_CXX_FLAGS "-mavx512f -O3 -fopenmp")
9+
ENDIF()
10+
11+
if(APPLE)
12+
set(CMAKE_CXX_STANDARD 14)
13+
include_directories("/usr/local/include" "/usr/local/opt/llvm/include")
14+
link_directories("/usr/local/lib" "/usr/local/opt/llvm/lib")
15+
endif()
16+
17+
include(FetchContent)
18+
19+
FetchContent_Declare(
20+
pybind11
21+
GIT_REPOSITORY https://github.com/pybind/pybind11
22+
GIT_TAG v2.2.4
23+
)
24+
FetchContent_GetProperties(pybind11)
25+
if(NOT pybind11_POPULATED)
26+
FetchContent_Populate(pybind11)
27+
add_subdirectory(${pybind11_SOURCE_DIR} ${pybind11_BINARY_DIR})
28+
endif()
29+
pybind11_add_module(qsim_avx512 pybind_main_avx512.cpp)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
#include "pybind_main_avx512.h"
16+
17+
#include "../../lib/simulator_avx512.h"
18+
19+
namespace qsim {
20+
template <typename For>
21+
using Simulator = SimulatorAVX512<For>;
22+
}
23+
24+
#include "../pybind_main.cpp"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
#include "../pybind_main.h"
16+
17+
PYBIND11_MODULE(qsim_avx512, m) { MODULE_BINDINGS }

pybind_interface/basic/CMakeLists.txt

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
cmake_minimum_required(VERSION 3.11)
2+
project(qsim)
3+
4+
IF (WIN32)
5+
set(CMAKE_CXX_FLAGS "/O2 /openmp")
6+
ELSE()
7+
set(CMAKE_CXX_FLAGS "-O3 -fopenmp")
8+
ENDIF()
9+
10+
11+
if(APPLE)
12+
set(CMAKE_CXX_STANDARD 14)
13+
include_directories("/usr/local/include" "/usr/local/opt/llvm/include")
14+
link_directories("/usr/local/lib" "/usr/local/opt/llvm/lib")
15+
endif()
16+
17+
include(FetchContent)
18+
19+
FetchContent_Declare(
20+
pybind11
21+
GIT_REPOSITORY https://github.com/pybind/pybind11
22+
GIT_TAG v2.2.4
23+
)
24+
FetchContent_GetProperties(pybind11)
25+
if(NOT pybind11_POPULATED)
26+
FetchContent_Populate(pybind11)
27+
add_subdirectory(${pybind11_SOURCE_DIR} ${pybind11_BINARY_DIR})
28+
endif()
29+
pybind11_add_module(qsim_basic pybind_main_basic.cpp)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
#include "pybind_main_basic.h"
16+
17+
#include "../../lib/simulator_basic.h"
18+
19+
namespace qsim {
20+
template <typename For>
21+
using Simulator = SimulatorBasic<For>;
22+
}
23+
24+
#include "../pybind_main.cpp"
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
#include "../pybind_main.h"
16+
17+
PYBIND11_MODULE(qsim_basic, m) { MODULE_BINDINGS }
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cmake_minimum_required(VERSION 3.11)
2+
project(qsim)
3+
4+
IF (WIN32)
5+
set(CMAKE_CXX_FLAGS "/O2 /openmp")
6+
ELSE()
7+
set(CMAKE_CXX_FLAGS "-O3 -fopenmp")
8+
ENDIF()
9+
10+
if(APPLE)
11+
set(CMAKE_CXX_STANDARD 14)
12+
include_directories("/usr/local/include" "/usr/local/opt/llvm/include")
13+
link_directories("/usr/local/lib" "/usr/local/opt/llvm/lib")
14+
endif()
15+
16+
include(FetchContent)
17+
18+
FetchContent_Declare(
19+
pybind11
20+
GIT_REPOSITORY https://github.com/pybind/pybind11
21+
GIT_TAG v2.2.4
22+
)
23+
FetchContent_GetProperties(pybind11)
24+
if(NOT pybind11_POPULATED)
25+
FetchContent_Populate(pybind11)
26+
add_subdirectory(${pybind11_SOURCE_DIR} ${pybind11_BINARY_DIR})
27+
endif()
28+
pybind11_add_module(qsim_decide decide.cpp)

pybind_interface/decide/decide.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <pybind11/pybind11.h>
2+
3+
namespace py = pybind11;
4+
5+
#ifdef _WIN32
6+
// Windows
7+
#include <intrin.h>
8+
#define cpuid(info, x) __cpuidex(info, x, 0)
9+
10+
#else
11+
// GCC Intrinsics
12+
#include <cpuid.h>
13+
void cpuid(int info[4], int infoType){
14+
__cpuid_count(infoType, 0, info[0], info[1], info[2], info[3]);
15+
}
16+
17+
#endif
18+
19+
enum Instructions { AVX512F = 0, AVX2 = 1, SSE4_1 = 2, BASIC = 3};
20+
21+
int detect_instructions() {
22+
Instructions instr = BASIC;
23+
int info[4];
24+
25+
cpuid(info, 0);
26+
27+
int nIds = info[0];
28+
if (nIds >= 1) {
29+
cpuid(info, 1);
30+
if ((info[2] & (1 << 19)) != 0) {
31+
instr = SSE4_1;
32+
}
33+
}
34+
if (nIds >= 7) {
35+
cpuid(info, 7);
36+
if ((info[1] & (1 << 5) )!= 0) {
37+
instr = AVX2;
38+
}
39+
if ((info[1] & (1 << 16)) != 0) {
40+
instr = AVX512F;
41+
}
42+
43+
}
44+
45+
return static_cast<int>(instr);
46+
}
47+
48+
PYBIND11_MODULE(qsim_decide, m) {
49+
m.doc() = "pybind11 plugin"; // optional module docstring
50+
51+
// Methods for returning amplitudes
52+
m.def("detect_instructions", &detect_instructions, "Detect SIMD");
53+
}

pybind_interface/pybind_main.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include "../lib/qtrajectory.h"
3131
#include "../lib/run_qsim.h"
3232
#include "../lib/run_qsimh.h"
33-
#include "../lib/simmux.h"
3433
#include "../lib/util.h"
3534

3635
using namespace qsim;

0 commit comments

Comments
 (0)