<!-- TE10 Studio documentation: /docs/api/clusters-cloud -->
# Clusters and cloud

Solves run on a cluster or in the cloud through the same `Setup.solve()` / `Setup.submit()` calls, with
`backend=` set to a cluster profile name or `"skypilot"`. Set a cluster up once, from the app
(**Add Cluster...**), the command line (`te10 cluster add`) or Python. See the user guide's
[cluster page](/docs/app/clusters) for what TE10 does on the cluster, and [cloud page](/docs/app/cloud)
for AWS.

## `em.clusters`

```python
from te10_studio import api as em
```

### `em.clusters.add_cluster`

```python
profile, ok, message = em.clusters.add_cluster(
    target, name="", partition="", account=None, scratch="", solver="",
    identity_file="", jump_host="", key_login=False, test=True)
```

Connects to `target` (`"user@host"`, or a host alias from `~/.ssh/config`), finds the scheduler, queues,
accounts and a scratch folder, uploads TE10's solver, runs a test solve and saves the profile. Returns
`(profile, ok, message)`; the profile is saved only when the test passed (or `test=False`).

| Parameter | Meaning |
|---|---|
| `name` | The name to save it under (default: the host's short name) |
| `partition` | Always use this queue (default: chosen per job) |
| `account` | The allocation jobs are charged to |
| `scratch` | Folder for job files (default: the cluster's scratch space) |
| `solver` | `"te10"` (TE10's solver, default), `"te10-image"` (in a container, for old Linux) or `"site"` (the cluster's own Palace) |
| `identity_file`, `jump_host` | SSH key file and gateway host |
| `key_login` | Create a TE10 key so later logins need no password |

### Other functions

| Function | Does |
|---|---|
| `load_profiles(path=None)` | `list[ClusterProfile]`: the saved clusters |
| `get_profile(name)` | One profile |
| `test_cluster(profile, two_nodes=False)` | `(ok, message)`: the test solve again (`two_nodes` checks MPI between nodes) |
| `refresh_cluster(name)` | Reads the queues and limits again; returns the updated profile |
| `remove_cluster(name)` | Deletes the profile |
| `export_shared(profile, path)` | Writes a `.te10cluster` file (no user name, key or account in it) |
| `add_shared(path, user="", name="", account="", test=True)` | `(profile, ok, message)`: adds a cluster from a `.te10cluster` file for `user` |
| `here_profile()` | A profile for the login node the script runs on (`backend="here"` does this) |

`prj.compute.names()` and `prj.compute["name"]` give the same profiles from a project.

## Job resources

Keyword arguments to `submit()` / `solve()` / `prj.sweep()` override the profile's defaults (fields of
`em.JobSpec`):

| Field | Default | |
|---|---|---|
| `nodes` | `1` | Nodes |
| `threads_per_rank` | `1` | OpenMP threads per solver process |
| `walltime` | `"04:00:00"` | Time limit |
| `memory_gb` | `0` | Memory per node (0 = the scheduler's default) |
| `partition`, `account` | `""` | Queue and allocation |
| `modules` | `[]` | `module load` lines run before the solver, e.g. `["gcc/12", "openmpi/4.1"]` |
| `container` | `""` | An Apptainer / Singularity image |
| `extra_directives` | `[]` | Raw scheduler lines, e.g. `["--gres=gpu:1", "--exclusive"]` |

`cores` sets the number of solver processes. With no resources given, TE10 sizes the job from the mesh
and the cluster's partitions, as the app does.

```python
em.clusters.add_cluster("you@login.cluster.edu", name="mycluster")      # once
job = prj.setups["S1"].submit(backend="mycluster", cores=64, walltime="01:00:00", account="proj-rf")
prj.save("model.te10")
job.wait(poll=30)
job.result().write_touchstone("model.s2p")
```

## Cloud (AWS)

```python
setup = prj.setups["S1"]
print(setup.estimate())                     # machine, $/h, model size, the time limit and the cost cap
job = setup.submit(backend="skypilot", max_cost=1.50)
print(job.estimate)                         # once started: the time and cost estimate
result = job.result()                       # downloads, then deletes the machine
```

- Connect an AWS account first: `te10 cloud connect` (an access key) or **Connect Cloud...** in the app
  (sign in with AWS).
- The machine is sized from the mesh (about 60,000 unknowns per core) and priced before anything starts.
- `max_cost` (dollars) refuses a job whose cap (price per hour × time limit) is above it. A job is also
  not started when this month's AWS spend is over the account's AWS Budget, unless `over_budget=True`.
- `job.cleanup()` deletes the machine of a failed or cancelled job; `te10 cloud leftovers` lists any still
  running.
