Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate simd compilation units #378

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
cmake_minimum_required(VERSION 3.11)
project(qsim)

set(CMAKE_CXX_FLAGS "-O3 -march=native -fopenmp")

include(FetchContent)

FetchContent_Declare(
pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11
GIT_TAG v2.2.4
)
FetchContent_GetProperties(pybind11)
if(NOT pybind11_POPULATED)
FetchContent_Populate(pybind11)
add_subdirectory(${pybind11_SOURCE_DIR} ${pybind11_BINARY_DIR})
endif()
pybind11_add_module(qsim pybind_interface/pybind_main.cpp)
ADD_SUBDIRECTORY(pybind_interface/sse)
ADD_SUBDIRECTORY(pybind_interface/avx512)
ADD_SUBDIRECTORY(pybind_interface/avx2)
ADD_SUBDIRECTORY(pybind_interface/basic)
ADD_SUBDIRECTORY(pybind_interface/decide)
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ TARGETS = qsim
TESTS = run-cxx-tests

CXX=g++
CXXFLAGS = -O3 -march=native -fopenmp
CXXFLAGS = -O3 -fopenmp
PYBIND11 = true

export CXX
Expand Down
28 changes: 22 additions & 6 deletions pybind_interface/Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
# The name of the shared library that results after compiling qsim for Pybind11
QSIMLIB = ../qsimcirq/qsim`python3-config --extension-suffix`
# The names of the shared libraries that result after compiling qsim for Pybind11
QSIMLIB_BASIC = ../qsimcirq/qsim_basic`python3-config --extension-suffix`
QSIMLIB_SSE = ../qsimcirq/qsim_sse`python3-config --extension-suffix`
QSIMLIB_AVX2 = ../qsimcirq/qsim_avx2`python3-config --extension-suffix`
QSIMLIB_AVX512 = ../qsimcirq/qsim_avx512`python3-config --extension-suffix`
QSIMLIB_DECIDE = ../qsimcirq/qsim_decide`python3-config --extension-suffix`

# The flags for the compilation of the Pybind11 interface
PYBINDFLAGS = -Wall -shared -std=c++17 -fPIC `python3 -m pybind11 --includes`

# The flags for the compilation of the simd-specific Pybind11 interfaces
PYBINDFLAGS_BASIC = -Wall -shared -std=c++17 -fPIC `python3 -m pybind11 --includes`
PYBINDFLAGS_SSE = -msse4.1 -Wall -shared -std=c++17 -fPIC `python3 -m pybind11 --includes`
PYBINDFLAGS_AVX2 = -mavx2 -mfma -Wall -shared -std=c++17 -fPIC `python3 -m pybind11 --includes`
PYBINDFLAGS_AVX512 = -mavx512f -Wall -shared -std=c++17 -fPIC `python3 -m pybind11 --includes`

.PHONY: pybind
pybind:
$(CXX) pybind_main.cpp -o $(QSIMLIB) $(CXXFLAGS) $(PYBINDFLAGS)
$(CXX) basic/pybind_main_basic.cpp -o $(QSIMLIB_BASIC) $(CXXFLAGS) $(PYBINDFLAGS_BASIC)
$(CXX) sse/pybind_main_sse.cpp -o $(QSIMLIB_SSE) $(CXXFLAGS) $(PYBINDFLAGS_SSE)
$(CXX) avx2/pybind_main_avx2.cpp -o $(QSIMLIB_AVX2) $(CXXFLAGS) $(PYBINDFLAGS_AVX2)
$(CXX) avx512/pybind_main_avx512.cpp -o $(QSIMLIB_AVX512) $(CXXFLAGS) $(PYBINDFLAGS_AVX512)
$(CXX) decide/decide.cpp -o $(QSIMLIB_DECIDE) $(CXXFLAGS) $(PYBINDFLAGS_BASIC)

.PHONY: clean
clean:
-rm -f ./*.x ./*.a ./*.so ./*.mod $(QSIMLIB)
-rm -f ./basic/*.x ./basic/*.a ./basic/*.so ./basic/*.mod $(QSIMLIB_BASIC)
-rm -f ./sse/*.x ./sse/*.a ./sse/*.so ./sse/*.mod $(QSIMLIB_SSE)
-rm -f ./avx2/*.x ./avx2/*.a ./avx2/*.so ./avx2/*.mod $(QSIMLIB_AVX2)
-rm -f ./avx512/*.x ./avx512/*.a ./avx512/*.so ./avx512/*.mod $(QSIMLIB_AVX512)
-rm -f ./decide/*.x ./decide/*.a ./decide/*.so ./decide/*.mod $(QSIMLIB_DECIDE)
28 changes: 28 additions & 0 deletions pybind_interface/avx2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
cmake_minimum_required(VERSION 3.11)
project(qsim)

IF (WIN32)
set(CMAKE_CXX_FLAGS "/arch:AVX2 /O2 /openmp")
ELSE()
set(CMAKE_CXX_FLAGS "-mavx2 -mfma -O3 -fopenmp")
ENDIF()

if(APPLE)
set(CMAKE_CXX_STANDARD 14)
include_directories("/usr/local/include" "/usr/local/opt/llvm/include")
link_directories("/usr/local/lib" "/usr/local/opt/llvm/lib")
endif()

include(FetchContent)

FetchContent_Declare(
pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11
GIT_TAG v2.2.4
)
FetchContent_GetProperties(pybind11)
if(NOT pybind11_POPULATED)
FetchContent_Populate(pybind11)
add_subdirectory(${pybind11_SOURCE_DIR} ${pybind11_BINARY_DIR})
endif()
pybind11_add_module(qsim_avx2 pybind_main_avx2.cpp)
24 changes: 24 additions & 0 deletions pybind_interface/avx2/pybind_main_avx2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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.

#include "pybind_main_avx2.h"

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

namespace qsim {
template <typename For>
using Simulator = SimulatorAVX<For>;
}

#include "../pybind_main.cpp"
17 changes: 17 additions & 0 deletions pybind_interface/avx2/pybind_main_avx2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.

#include "../pybind_main.h"

PYBIND11_MODULE(qsim_avx2, m) { MODULE_BINDINGS }
29 changes: 29 additions & 0 deletions pybind_interface/avx512/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
cmake_minimum_required(VERSION 3.11)
project(qsim)


IF (WIN32)
set(CMAKE_CXX_FLAGS "/arch:AVX512 /O2 /openmp")
ELSE()
set(CMAKE_CXX_FLAGS "-mavx512f -O3 -fopenmp")
ENDIF()

if(APPLE)
set(CMAKE_CXX_STANDARD 14)
include_directories("/usr/local/include" "/usr/local/opt/llvm/include")
link_directories("/usr/local/lib" "/usr/local/opt/llvm/lib")
endif()

include(FetchContent)

FetchContent_Declare(
pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11
GIT_TAG v2.2.4
)
FetchContent_GetProperties(pybind11)
if(NOT pybind11_POPULATED)
FetchContent_Populate(pybind11)
add_subdirectory(${pybind11_SOURCE_DIR} ${pybind11_BINARY_DIR})
endif()
pybind11_add_module(qsim_avx512 pybind_main_avx512.cpp)
24 changes: 24 additions & 0 deletions pybind_interface/avx512/pybind_main_avx512.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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.

#include "pybind_main_avx512.h"

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

namespace qsim {
template <typename For>
using Simulator = SimulatorAVX512<For>;
}

#include "../pybind_main.cpp"
17 changes: 17 additions & 0 deletions pybind_interface/avx512/pybind_main_avx512.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.

#include "../pybind_main.h"

PYBIND11_MODULE(qsim_avx512, m) { MODULE_BINDINGS }
29 changes: 29 additions & 0 deletions pybind_interface/basic/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
cmake_minimum_required(VERSION 3.11)
project(qsim)

IF (WIN32)
set(CMAKE_CXX_FLAGS "/O2 /openmp")
ELSE()
set(CMAKE_CXX_FLAGS "-O3 -fopenmp")
ENDIF()


if(APPLE)
set(CMAKE_CXX_STANDARD 14)
include_directories("/usr/local/include" "/usr/local/opt/llvm/include")
link_directories("/usr/local/lib" "/usr/local/opt/llvm/lib")
endif()

include(FetchContent)

FetchContent_Declare(
pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11
GIT_TAG v2.2.4
)
FetchContent_GetProperties(pybind11)
if(NOT pybind11_POPULATED)
FetchContent_Populate(pybind11)
add_subdirectory(${pybind11_SOURCE_DIR} ${pybind11_BINARY_DIR})
endif()
pybind11_add_module(qsim_basic pybind_main_basic.cpp)
24 changes: 24 additions & 0 deletions pybind_interface/basic/pybind_main_basic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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.

#include "pybind_main_basic.h"

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

namespace qsim {
template <typename For>
using Simulator = SimulatorBasic<For>;
}

#include "../pybind_main.cpp"
17 changes: 17 additions & 0 deletions pybind_interface/basic/pybind_main_basic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.

#include "../pybind_main.h"

PYBIND11_MODULE(qsim_basic, m) { MODULE_BINDINGS }
28 changes: 28 additions & 0 deletions pybind_interface/decide/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
cmake_minimum_required(VERSION 3.11)
project(qsim)

IF (WIN32)
set(CMAKE_CXX_FLAGS "/O2 /openmp")
ELSE()
set(CMAKE_CXX_FLAGS "-O3 -fopenmp")
ENDIF()

if(APPLE)
set(CMAKE_CXX_STANDARD 14)
include_directories("/usr/local/include" "/usr/local/opt/llvm/include")
link_directories("/usr/local/lib" "/usr/local/opt/llvm/lib")
endif()

include(FetchContent)

FetchContent_Declare(
pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11
GIT_TAG v2.2.4
)
FetchContent_GetProperties(pybind11)
if(NOT pybind11_POPULATED)
FetchContent_Populate(pybind11)
add_subdirectory(${pybind11_SOURCE_DIR} ${pybind11_BINARY_DIR})
endif()
pybind11_add_module(qsim_decide decide.cpp)
53 changes: 53 additions & 0 deletions pybind_interface/decide/decide.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <pybind11/pybind11.h>

namespace py = pybind11;

#ifdef _WIN32
// Windows
#include <intrin.h>
#define cpuid(info, x) __cpuidex(info, x, 0)

#else
// GCC Intrinsics
#include <cpuid.h>
void cpuid(int info[4], int infoType){
__cpuid_count(infoType, 0, info[0], info[1], info[2], info[3]);
}

#endif

enum Instructions { AVX512F = 0, AVX2 = 1, SSE4_1 = 2, BASIC = 3};

int detect_instructions() {
Instructions instr = BASIC;
int info[4];

cpuid(info, 0);

int nIds = info[0];
if (nIds >= 1) {
cpuid(info, 1);
if ((info[2] & (1 << 19)) != 0) {
instr = SSE4_1;
}
}
if (nIds >= 7) {
cpuid(info, 7);
if ((info[1] & (1 << 5) )!= 0) {
instr = AVX2;
}
if ((info[1] & (1 << 16)) != 0) {
instr = AVX512F;
}

}

return static_cast<int>(instr);
}

PYBIND11_MODULE(qsim_decide, m) {
m.doc() = "pybind11 plugin"; // optional module docstring

// Methods for returning amplitudes
m.def("detect_instructions", &detect_instructions, "Detect SIMD");
}
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/simmux.h"
#include "../lib/util.h"

using namespace qsim;
Expand Down
Loading