Skip to content
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
49 changes: 31 additions & 18 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,23 @@ build:windows --cxxopt=/std:c++17
build --copt=-D_GLIBCXX_USE_CXX11_ABI=1
build --cxxopt=-D_GLIBCXX_USE_CXX11_ABI=1

# Test flags
test --test_output=errors
test --test_timeout=600
# The default for vector instruction sets is to exclude them.
# Config options later in this file can be layered to enable them.
build --build_tag_filters=-avx,-sse
test --test_tag_filters=-avx,-sse
# "bazel run" inherits options from "build". Our run targets don't have tags,
# so we must clear the filter explicitly or we get "No targets found to run".
run:sse --build_tag_filters=
run:avx --build_tag_filters=

# CUDA options
build:cuda --@local_config_cuda//:enable_cuda
build:cuda --define=using_cuda=true --define=using_cuda_nvcc=true

# Test flags
test --test_output=errors
test --test_timeout=600

# Configs for verbose builds & tests
common:verbose --announce_rc
common:verbose --auto_output_filter=none
Expand All @@ -54,18 +63,18 @@ test:verbose --test_summary=detailed

# Shared config for sanitizers
build:sanitizer --strip=never
build:sanitizer --copt -O1
build:sanitizer --copt -fno-omit-frame-pointer
build:sanitizer --copt=-O1
build:sanitizer --copt=-fno-omit-frame-pointer

# Address sanitizer
build:asan --config=sanitizer
build:asan --copt -fsanitize=address
build:asan --linkopt -fsanitize=address
build:asan --copt=-fsanitize=address
build:asan --linkopt=-fsanitize=address

# Memory sanitizer
build:msan --config=sanitizer
build:msan --copt -fsanitize=leak
build:msan --linkopt -fsanitize=leak
build:msan --copt=-fsanitize=leak
build:msan --linkopt=-fsanitize=leak

# No sanitizers
build:nosan --
Expand All @@ -74,23 +83,27 @@ build:nosan --
# ~~~~ Instruction set options (choose one) ~~~~

# Build with AVX2 + FMA
build:avx --copt -O3
build:avx --copt -mavx2
build:avx --copt -mfma
build:avx --copt=-O3
build:avx --copt=-mavx2
build:avx --copt=-mfma
build:avx --build_tag_filters=avx --ui_event_filters=-WARNING
test:avx --test_tag_filters=avx --ui_event_filters=-WARNING

# Build with SSE
build:sse --copt -O3
build:sse --copt -msse4
build:sse --copt=-O3
build:sse --copt=-msse4
build:sse --build_tag_filters=sse --ui_event_filters=-WARNING
test:sse --test_tag_filters=sse --ui_event_filters=-WARNING

# Build without AVX or SSE
build:basic --copt -O3
build:basic --copt=-O3


# ~~~~ Parallelization (choose one, or nopenmp for none) ~~~~

# Build with OpenMP
build:openmp --copt -fopenmp
build:openmp --linkopt -lgomp
build:openmp --copt=-fopenmp
build:openmp --linkopt=-lgomp

# No OpenMP
build:nopenmp --
Expand All @@ -99,7 +112,7 @@ build:nopenmp --
# ~~~~ Memory handler (choose one) ~~~~

# Build with tcmalloc
build:tcmalloc --linkopt="-ltcmalloc"
build:tcmalloc --linkopt=-ltcmalloc

# Build using malloc (default)
build:malloc --
11 changes: 5 additions & 6 deletions dev_tools/test_libs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ if [[ "$1" == "-h" || "$1" == "--help" || "$1" == "help" ]]; then
exit 0
fi

# Unless we can tell this system supports AVX, we skip those tests.
# Look for AVX and SSE in the processor's feature flags.
declare features=""
declare filters=""
features="$(python -c 'import cpuinfo; print(" ".join(cpuinfo.get_cpu_info().get("flags", [])))')"
[[ "$features" == *avx2* ]] || filters+=",-avx"
[[ "$features" == *sse* ]] || filters+=",-sse"
[[ "$features" == *avx2* ]] && filters+=",avx"
[[ "$features" == *sse* ]] && filters+=",sse"
Comment on lines +35 to +36
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This runs into the same problem as in #710 - the shell has the -e option so the script will exit here if any of avx2 or sse features were missing. To have a positive condition, you need to rewrite this as

if [[ "$features" == *avx2* ]]; then
    filters+=",avx"
fi
...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I tested the following, and it does not exit early. Do you have an environment where it doesn't work this way?

 #!/usr/bin/env bash

 set -eo pipefail -o errtrace

 declare foo=""
 declare bar="original"

 foo="something"
 [[ "$foo" == *notthere* ]] && bar="new string"

 echo $bar

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stand corrected - per bash set -e doc for the && and || lists it is only the last command that counts for errexit. I got confused, because on my Debian box it indeed exits early in an interactive session. This happens because interactive bash runs some fancy prompt functions which try to restore the exit status of the last command and that triggers errexit.

With your snippet (and the same system bashrc) this can be reproduced with

bash -i <snippet.bash

TLDR - it is OK as is.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, that's unobvious. Thanks for the info!

filters="${filters#,}"

declare -a build_filters=()
Expand All @@ -43,11 +43,10 @@ if [[ -n "$filters" ]]; then
test_filters=( "--test_tag_filters=$filters" )
fi

# Apps are sample programs and are only meant to run on Linux.
# The apps are sample programs and are only meant to run on Linux.
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
bazel build "${build_filters[@]}" --config=sse "$@" apps:all
bazel build "${build_filters[@]}" "$@" apps:all
fi

# Run all basic tests. This should work on all platforms.
bazel test "${build_filters[@]}" "${test_filters[@]}" "$@" tests:all
bazel test "${test_filters[@]}" "$@" tests:all
51 changes: 34 additions & 17 deletions docs/bazel.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,65 @@
# Building with Bazel

qsim provides [Bazel](https://github.com/bazelbuild/bazel) build rules for its
applications and tests. To build and run all tests using Bazel, run the
following command:
qsim provides [Bazel](https://github.com/bazelbuild/bazel) build and test rules
for qsim tests and sample applications. The Bazel targets are `tests` and
`apps`; you can combine these with the bazel commands `build`, `test`, and
`run` and configuration flags suitable for your computer hardware architecture
and software environment.

On hardware and software platforms that support them, qsim can be configured to
take advantage of certain hardware optimizations, specifically AVX (a hardware
extension for optimizing vector arithmetic), SSE (streaming SIMD extensions),
and/or OpenMP (a software API for shared-memory parallel programming). By
default, the basic qsim build configuration does _not_ compile in support for
these features. (On some systems such as MacOS on Apple Silicon, they are not
available.) A basic build & test run is obtained using the following command:

```shell
bazel test tests:all
```
# AVX and OpenMP are recommended for best performance.
# See "Build configs" section below for more information.

As an example of using optimization options, if your computer has support for
AVX and OpenMP, the following command will build and run all the tests with the
appropriate config options to make use of those features:

```shell
bazel test --config=avx --config=openmp tests:all
```

To run a sample simulation, use the command below. Note that this command
requires the circuit file to be specified both on the command line and in the
`data` field of the `qsim_base` BUILD rule.
```
requires the `circuit_q24` file to be specified both on the command line and in
the `data` field of the `qsim_base` BUILD rule.

```shell
bazel run --config=avx --config=openmp apps:qsim_base -- -c circuits/circuit_q24
```

## Build configurations

Depending on the optimizers available on your machine, different config flags
(such as `--config=avx`, above) can be set to control which optimizers are
included in a given build or test run.
Depending on your computer's hardware architecture and the features available,
different Bazel config flags (such as `--config=avx`, above) can be used to
control which hardware optimizers are included in a given build or test run.

### Vector arithmetic optimizers

Pick at most one of the following options:

```
```bazel
# Use AVX instructions for vector arithmetic.
--config=avx

# Use SSE instructions for vector arithmetic.
--config=sse

# Do not use vector arithmetic optimization (default).
# Do not use vector arithmetic optimization (deault).
--config=basic
```

### Parallelism optimizers

Pick at most one of the following options:

```
```bazel
# Use OpenMP to run operations in parallel when possible.
--config=openmp

Expand All @@ -51,14 +69,13 @@ Pick at most one of the following options:

### Memory allocators


[TCMalloc](https://github.com/google/tcmalloc) is a fast, multithreaded
implementation of C's `malloc()` and C++'s `new` operator. It is an independent
open-source library developd by Google. TCMalloc can be used with qsim as an
open-source library developed by Google. TCMalloc can be used with qsim as an
alternative to the default `malloc()`. Pick at most one of the following
options:

```
```bazel
# Use TCMalloc for memory allocation.
--config=tcmalloc

Expand Down
Loading