|
| 1 | +# Parallel worktrees |
| 2 | + |
| 3 | +Quick setup for working on multiple branches at the same time (e.g. one for |
| 4 | +feature work, one for code review, one for a bug fix) without thrashing a |
| 5 | +single checkout. Container-dedicated: paths assume the |
| 6 | +`ghcr.io/psal-postech/torchsim-ci` layout. |
| 7 | + |
| 8 | +## Why a script |
| 9 | + |
| 10 | +Three things have to line up for parallel worktrees to actually work in this |
| 11 | +repo: |
| 12 | + |
| 13 | +1. **Worktree-scoped env vars.** `PyTorchSimFrontend/extension_config.py` |
| 14 | + anchors output / log / config paths on `TORCHSIM_DIR`. Without an override |
| 15 | + every worktree dumps into the same `outputs/` and `togsim_results/`. |
| 16 | +2. **`PYTHONPATH` override.** `pip install -e PyTorchSimDevice` writes a |
| 17 | + single editable record into conda's site-packages that points at one |
| 18 | + worktree. The override forces `import torch_openreg` to resolve to the |
| 19 | + active worktree's code and `.so` first. |
| 20 | +3. **Branch tracking.** `git worktree add` from a remote ref sets upstream to |
| 21 | + that ref, so `git push` would target `develop`. The script unsets it so |
| 22 | + first `git push -u origin <branch>` creates the right remote branch. |
| 23 | + |
| 24 | +The script bundles all three. |
| 25 | + |
| 26 | +## Create a worktree |
| 27 | + |
| 28 | +```bash |
| 29 | +scripts/setup_worktree.sh <purpose> [base-ref] |
| 30 | +``` |
| 31 | + |
| 32 | +`<purpose>` becomes the branch suffix and the dir suffix: |
| 33 | + |
| 34 | +| Command | Worktree dir | Branch | |
| 35 | +|---|---|---| |
| 36 | +| `setup_worktree.sh feature` | `/workspace/PyTorchSim-feature` | `feature/scratch` | |
| 37 | +| `setup_worktree.sh review` | `/workspace/PyTorchSim-review` | `refactor/scratch` (rename after) | |
| 38 | +| `setup_worktree.sh bugfix/issue-198` | `/workspace/PyTorchSim-bugfix` | `bugfix/issue-198` | |
| 39 | +| `setup_worktree.sh feature origin/master` | `/workspace/PyTorchSim-feature` | `feature/scratch` (off master) | |
| 40 | + |
| 41 | +Default base is `origin/develop` (per `CONTRIBUTING.md`). |
| 42 | + |
| 43 | +## Activate |
| 44 | + |
| 45 | +```bash |
| 46 | +cd /workspace/PyTorchSim-bugfix |
| 47 | +source .envrc |
| 48 | +# → Activated worktree: /workspace/PyTorchSim-bugfix |
| 49 | +# → prompt: [torchsim:PyTorchSim-bugfix] ... |
| 50 | +``` |
| 51 | + |
| 52 | +`.envrc` is local to each worktree and not committed. Re-source it whenever |
| 53 | +you open a new shell in that worktree. |
| 54 | + |
| 55 | +## Build once per worktree |
| 56 | + |
| 57 | +The compiled `_C.cpython-*.so` lives under `PyTorchSimDevice/torch_openreg/` |
| 58 | +and is not shared across worktrees. After activation: |
| 59 | + |
| 60 | +```bash |
| 61 | +(cd PyTorchSimDevice && python setup.py build_ext --inplace) |
| 62 | +``` |
| 63 | + |
| 64 | +Use `build_ext --inplace` instead of `pip install -e` so the editable |
| 65 | +record in `/opt/conda/lib/python3.11/site-packages` keeps pointing at |
| 66 | +whichever worktree it already pointed at — `PYTHONPATH` from `.envrc` does |
| 67 | +the per-worktree routing. (Running `pip install -e` again rewrites that |
| 68 | +record and will pin "default" Python to the new worktree.) |
| 69 | + |
| 70 | +## What the env looks like |
| 71 | + |
| 72 | +Worktree-scoped (auto-set by `.envrc`): |
| 73 | + |
| 74 | +| Var | Value | |
| 75 | +|---|---| |
| 76 | +| `TORCHSIM_DIR` | `$PWD` of the worktree | |
| 77 | +| `TORCHSIM_DUMP_PATH` | `$PWD/outputs` | |
| 78 | +| `TORCHSIM_LOG_PATH` | `$PWD/togsim_results` | |
| 79 | +| `TOGSIM_CONFIG` | `$PWD/configs/systolic_ws_128x128_c1_simple_noc_tpuv3.yml` | |
| 80 | +| `PYTHONPATH` | `$PWD/PyTorchSimDevice:$PWD:$PYTHONPATH` | |
| 81 | + |
| 82 | +Shared (container-dedicated, set the same in every `.envrc`): |
| 83 | + |
| 84 | +| Var | Value | |
| 85 | +|---|---| |
| 86 | +| `GEM5_PATH` | `/gem5/release/gem5.opt` | |
| 87 | +| `TORCHSIM_LLVM_PATH` | `/riscv-llvm/bin` | |
| 88 | +| `RISCV` | `/workspace/riscv` | |
| 89 | + |
| 90 | +## Cleanup |
| 91 | + |
| 92 | +```bash |
| 93 | +git worktree remove /workspace/PyTorchSim-feature |
| 94 | +git branch -D feature/scratch # if you do not want to keep the branch |
| 95 | +``` |
| 96 | + |
| 97 | +`git worktree list` shows the current set. |
| 98 | + |
| 99 | +## Gotchas |
| 100 | + |
| 101 | +- **Do not commit `.envrc`.** It is per-worktree state. Add to your |
| 102 | + personal global gitignore if needed. |
| 103 | +- **Editable install conflict.** If you run `pip install -e PyTorchSimDevice` |
| 104 | + in worktree A, then again in worktree B, Python's default `import |
| 105 | + torch_openreg` flips to B. With `PYTHONPATH` from `.envrc` this still |
| 106 | + resolves correctly in either worktree's shell, but a shell with no |
| 107 | + `.envrc` sourced will see whichever was installed last. |
| 108 | +- **TOGSim FIFO files.** `/tmp/togsim_fifo_<pid>` is keyed on PID, not on |
| 109 | + worktree — concurrent runs from different worktrees do not collide. |
| 110 | +- **`_C.cpython-*.so` rebuild on PyTorch update.** If you `pip install` |
| 111 | + a different torch version in the conda env, every worktree's `.so` is |
| 112 | + stale; rebuild each. |
0 commit comments