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
1 change: 1 addition & 0 deletions docs/docs/concepts/tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ If you don't assign a value to an environment variable (see `HF_TOKEN` above),
| `DSTACK_NODE_RANK` | The rank of the node |
| `DSTACK_MASTER_NODE_IP` | The internal IP address of the master node |
| `DSTACK_NODES_IPS` | The list of internal IP addresses of all nodes delimited by "\n" |
| `DSTACK_MPI_HOSTFILE` | The path to a pre-populated MPI hostfile |

### Spot policy

Expand Down
1 change: 1 addition & 0 deletions docs/docs/reference/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ tasks, and services:
```

- `DSTACK_NODES_IPS`{ #DSTACK_NODES_IPS } – The list of internal IP addresses of all nodes delimited by `"\n"`.
- `DSTACK_MPI_HOSTFILE`{ #DSTACK_MPI_HOSTFILE } – The path to a pre-populated MPI hostfile that can be used directly as `mpirun --hostfile $DSTACK_MPI_HOSTFILE`.

## Server

Expand Down
36 changes: 36 additions & 0 deletions runner/internal/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ func (ex *RunExecutor) execJob(ctx context.Context, jobLogFile io.Writer) error
gpus_per_node_num := ex.clusterInfo.GPUSPerJob
gpus_num := nodes_num * gpus_per_node_num

mpiHostfilePath := filepath.Join(ex.homeDir, ".dstack/mpi/hostfile")

jobEnvs := map[string]string{
"DSTACK_RUN_ID": ex.run.Id,
"DSTACK_JOB_ID": ex.jobSubmission.Id,
Expand All @@ -268,6 +270,7 @@ func (ex *RunExecutor) execJob(ctx context.Context, jobLogFile io.Writer) error
"DSTACK_NODES_NUM": strconv.Itoa(nodes_num),
"DSTACK_GPUS_PER_NODE": strconv.Itoa(gpus_per_node_num),
"DSTACK_GPUS_NUM": strconv.Itoa(gpus_num),
"DSTACK_MPI_HOSTFILE": mpiHostfilePath,
}

// Call buildLDLibraryPathEnv and update jobEnvs if no error occurs
Expand Down Expand Up @@ -390,6 +393,11 @@ func (ex *RunExecutor) execJob(ctx context.Context, jobLogFile io.Writer) error
}
}

err = writeMpiHostfile(ctx, ex.clusterInfo.JobIPs, gpus_per_node_num, mpiHostfilePath)
if err != nil {
return err
}

cmd.Env = envMap.Render()

log.Trace(ctx, "Starting exec", "cmd", cmd.String(), "working_dir", cmd.Dir, "env", cmd.Env)
Expand Down Expand Up @@ -696,6 +704,34 @@ func prepareSSHDir(uid int, gid int, homeDir string) (string, error) {
return sshDir, nil
}

func writeMpiHostfile(ctx context.Context, ips []string, gpus_per_node int, path string) error {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
file, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644)
if err != nil {
return err
}
defer file.Close()
nonEmptyIps := []string{}
for _, ip := range ips {
if ip != "" {
nonEmptyIps = append(nonEmptyIps, ip)
}
}
if len(nonEmptyIps) == len(ips) {
for _, ip := range nonEmptyIps {
line := fmt.Sprintf("%s slots=%d\n", ip, gpus_per_node)
if _, err = file.WriteString(line); err != nil {
return err
}
}
} else {
log.Info(ctx, "creating empty MPI hostfile: no internal IPs assigned")
}
return nil
}

func writeDstackProfile(env map[string]string, path string) error {
file, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644)
if err != nil {
Expand Down
Loading