|
| 1 | +package library |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os/exec" |
| 6 | + "regexp" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/supercontainers/compspec-go/pkg/extractor" |
| 10 | + "github.com/supercontainers/compspec-go/pkg/utils" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + MPIRunExec = "mpirun" |
| 15 | +) |
| 16 | + |
| 17 | +var ( |
| 18 | + regexIntelMPIVersion = regexp.MustCompile(`Version (.*) `) |
| 19 | +) |
| 20 | + |
| 21 | +// getMPIInformation returns info on mpi versions and variant |
| 22 | +// yes, fairly janky, please improve upon! This is for a prototype |
| 23 | +func getMPIInformation() (extractor.ExtractorSection, error) { |
| 24 | + info := extractor.ExtractorSection{} |
| 25 | + |
| 26 | + // Do we even have mpirun? |
| 27 | + path, err := exec.LookPath(MPIRunExec) |
| 28 | + if err != nil { |
| 29 | + return info, nil |
| 30 | + } |
| 31 | + |
| 32 | + // Get output from the tool |
| 33 | + command := []string{MPIRunExec, "--version"} |
| 34 | + output, err := utils.RunCommand(command) |
| 35 | + if err != nil { |
| 36 | + return info, err |
| 37 | + } |
| 38 | + |
| 39 | + // This is really simple - if we find Open MPI in a line, that's the variant |
| 40 | + lines := strings.Split(output, "\n") |
| 41 | + for _, line := range lines { |
| 42 | + line = strings.TrimSpace(line) |
| 43 | + if strings.Contains(line, "Open MPI") { |
| 44 | + info["mpi.variant"] = "OpenMPI" |
| 45 | + parts := strings.Split(line, " ") |
| 46 | + info["mpi.version"] = parts[len(parts)-1] |
| 47 | + return info, nil |
| 48 | + } |
| 49 | + |
| 50 | + // Intel(R) MPI Library for Linux* OS, Version 2021.8 Build 20221129 (id: 339ec755a1) |
| 51 | + if strings.Contains(line, "Intel") { |
| 52 | + info["mpi.variant"] = "intel-mpi" |
| 53 | + match := regexIntelMPIVersion.FindStringSubmatch(line) |
| 54 | + if match != nil { |
| 55 | + parts := strings.Split(match[0], " ") |
| 56 | + info["mpi.version"] = parts[1] |
| 57 | + } |
| 58 | + return info, nil |
| 59 | + } |
| 60 | + |
| 61 | + // Note that for mpich there is a LOT more metadata |
| 62 | + // Right now I'm assuming if we find Version: it's for Open MPI |
| 63 | + if strings.Contains(line, "Version:") { |
| 64 | + info["mpi.variant"] = "mpich" |
| 65 | + parts := strings.Split(line, " ") |
| 66 | + info["mpi.version"] = parts[len(parts)-1] |
| 67 | + return info, nil |
| 68 | + } |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + fmt.Println(output) |
| 73 | + fmt.Printf("%s is available at %s\n", MPIRunExec, path) |
| 74 | + return info, nil |
| 75 | +} |
0 commit comments