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
33 changes: 33 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Benchmark

on:
push:
branches: [main]
pull_request:

permissions:
contents: write

jobs:
benchmark:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Update submodules
run: git submodule update --init --recursive
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y g++ make clang-tidy clang-format libbenchmark-dev python3 python3-pip
pip3 install pandas pyarrow pytest
- name: Build benchmark
run: make bench
- name: Run benchmark
run: ./bench --benchmark_out=benchmark.json --benchmark_out_format=json
- name: Store benchmark result
uses: benchmark-action/github-action-benchmark@v1
with:
tool: 'googlecpp'
output-file-path: benchmark.json
github-token: ${{ secrets.GITHUB_TOKEN }}
auto-push: true
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
#!/usr/bin/env make

TARGET = main
BENCH = bench
OBJ = main_bench.o

$(TARGET): main.cpp
$(CXX) -O3 -std=c++20 -fopenmp -march=native -o $(TARGET) $<

$(OBJ): main.cpp
$(CXX) -O3 -std=c++20 -fopenmp -march=native -DBENCH_LIB -c main.cpp -o $(OBJ)

$(BENCH): $(OBJ) bench.cpp
$(CXX) -O3 -std=c++20 -fopenmp -march=native bench.cpp $(OBJ) -lbenchmark -lpthread -o $(BENCH)

format:
clang-format -i main.cpp

tidy:
clang-tidy main.cpp -- -std=c++20

clean:
rm -f $(TARGET)
rm -f $(TARGET) $(BENCH) $(OBJ)

test: $(TARGET)
pytest -q

submodule:
git submodule update --init --recursive

.PHONY: clean format tidy test submodule
.PHONY: clean format tidy test submodule bench
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ pip install -r requirements.txt # pandas and pyarrow are required
pytest
```

## Benchmark

Build and run the Google Benchmark suite to measure performance:

```bash
make bench
./bench --benchmark_out=benchmark.json --benchmark_out_format=json
```

## License

This project is provided for educational purposes and comes with no warranty.
20 changes: 20 additions & 0 deletions bench.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <benchmark/benchmark.h>
#include <string>
#include <cstdio>

size_t run_simulation(const std::string &csv,
const std::string &act,
const std::string &sil,
size_t T);

static void BM_Run(benchmark::State &state) {
for (auto _ : state) {
size_t spikes = run_simulation("test/test_connectome.csv",
"test/active.txt",
"test/silent.txt", 5);
benchmark::DoNotOptimize(spikes);
std::remove("spikes.bin");
}
}
BENCHMARK(BM_Run);
BENCHMARK_MAIN();
3 changes: 2 additions & 1 deletion docs/AGENT_PROMPTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ When preparing pull requests:

1. Ensure `make` builds without warnings.
2. Run `pytest` to verify all tests pass.
3. Update this document and `README.md` if usage or behavior changes.
3. Run `make bench` to build benchmarks when performance changes are expected.
4. Update this document and `README.md` if usage or behavior changes.

54 changes: 31 additions & 23 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,24 +169,8 @@ struct Simulator {
}
};
/* ---------- main ---------- */
int main(int argc, char **argv) {
string csv, act, sil;
size_t T = 1000;
for (int i = 1; i < argc; ++i) {
string a = argv[i];
if (a == "--csv")
csv = argv[++i];
else if (a == "--active")
act = argv[++i];
else if (a == "--silent")
sil = argv[++i];
else if (a == "--t")
T = stoul(argv[++i]);
}
if (csv.empty()) {
cerr << "--csv required\n";
return 1;
}
size_t run_simulation(const string &csv, const string &act, const string &sil,
size_t T) {
/* step 1: count neurons (max id+1) */
size_t N = 0;
{
Expand All @@ -199,15 +183,39 @@ int main(int argc, char **argv) {
N = max(N, size_t(max(pre, post) + 1));
}
}
vector<uint8_t> silent(N, 0);
vector<uint8_t> silent_vec(N, 0);
for (auto id : load_list(sil))
if (id < N)
silent[id] = 1;
silent_vec[id] = 1;

CSR G = load_csv_to_csr(csv, N, silent);
CSR G = load_csv_to_csr(csv, N, silent_vec);
Params P;
Simulator S(N, G, P, silent);
Simulator S(N, G, P, silent_vec);
S.run(T, load_list(act));
S.save_bin("spikes.bin");
cerr << "Spikes: " << S.spikes.size() << "\n";
return S.spikes.size();
}

#ifndef BENCH_LIB
int main(int argc, char **argv) {
string csv, act, sil;
size_t T = 1000;
for (int i = 1; i < argc; ++i) {
string a = argv[i];
if (a == "--csv")
csv = argv[++i];
else if (a == "--active")
act = argv[++i];
else if (a == "--silent")
sil = argv[++i];
else if (a == "--t")
T = stoul(argv[++i]);
}
if (csv.empty()) {
cerr << "--csv required\n";
return 1;
}
size_t spikes = run_simulation(csv, act, sil, T);
cerr << "Spikes: " << spikes << "\n";
}
#endif