From f8098b5bd09dce5f8f40633c305c6a7c63a786f0 Mon Sep 17 00:00:00 2001 From: Brett Best Date: Thu, 30 May 2019 19:39:11 +1000 Subject: [PATCH] Updated package file to be compatible with Swift 5. Added small example app. --- Package.swift | 33 ++++++++-- README.md | 9 +++ .../CMPI/module.modulemap | 0 shim.h => Sources/CMPI/shim.h | 0 Sources/COpenMPI-Example/main.swift | 62 +++++++++++++++++++ 5 files changed, 99 insertions(+), 5 deletions(-) rename module.modulemap => Sources/CMPI/module.modulemap (100%) rename shim.h => Sources/CMPI/shim.h (100%) create mode 100644 Sources/COpenMPI-Example/main.swift diff --git a/Package.swift b/Package.swift index ca97ea4..9d65655 100644 --- a/Package.swift +++ b/Package.swift @@ -1,3 +1,5 @@ +// swift-tools-version:5.0 + /* * This file is part of COpenMPI. * @@ -18,9 +20,30 @@ import PackageDescription let package = Package( - name: "COpenMPI", - pkgConfig: "ompi-c", - providers: [ - .Brew("open-mpi") - ] + name: "COpenMPI", + products: [ + .library( + name: "COpenMPI", + targets: ["CMPI"] + ), + .executable( + name: "COpenMPI-Example", + targets: ["COpenMPI-Example"] + ) + ], + targets: [ + .systemLibrary( + name: "CMPI", + pkgConfig: "ompi-c", + providers: [ + .brew(["open-mpi"]) + ] + ), + .target( + name: "COpenMPI-Example", + dependencies: [ + "CMPI" + ] + ) + ] ) diff --git a/README.md b/README.md index 993c8d9..9616f34 100644 --- a/README.md +++ b/README.md @@ -6,3 +6,12 @@ The package exports a single module, `CMPI`. Due to the use of function-like macros in `mpi.h`, a shim header that wraps these is required. `shim.h` creates inline functions for the constants and maps them to read-only global computed properties. A simple MPI program (à la '[Hello World](http://mpitutorial.com/tutorials/mpi-hello-world/)') appears to work correctly, but this has not been extensively tested. Feedback is requested! + +## Example + +An example of using the library is included. To clone and run the example run the following commands in Terminal. + +1. `git clone https://github.com/omor1/COpenMPI.git` +2. `cd COpenMPI` +3. `swift build -c release` +4. `mpirun -c 2 .build/*/release/COpenMPI-Example` diff --git a/module.modulemap b/Sources/CMPI/module.modulemap similarity index 100% rename from module.modulemap rename to Sources/CMPI/module.modulemap diff --git a/shim.h b/Sources/CMPI/shim.h similarity index 100% rename from shim.h rename to Sources/CMPI/shim.h diff --git a/Sources/COpenMPI-Example/main.swift b/Sources/COpenMPI-Example/main.swift new file mode 100644 index 0000000..2185a94 --- /dev/null +++ b/Sources/COpenMPI-Example/main.swift @@ -0,0 +1,62 @@ +/* + * This file is part of COpenMPI. + * + * COpenMPI is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * COpenMPI is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with COpenMPI. If not, see . + */ + +import CMPI + +var argc = CommandLine.argc +var argv: UnsafeMutablePointer?>? = CommandLine.unsafeArgv + +MPI_Init(&argc, &argv) + +var worldSize = Int32() +MPI_Comm_size(MPI_COMM_WORLD, &worldSize) + +var worldRank = Int32() +MPI_Comm_rank(MPI_COMM_WORLD, &worldRank) + +if (worldRank == 0) { + var majorVersion = Int32() + var minorVersion = Int32() + + MPI_Get_version(&majorVersion, &minorVersion) + + var cLibraryVersion = [CChar](repeating: 0, count: Int(MPI_MAX_LIBRARY_VERSION_STRING)) + var libraryVersionResultLen = Int32() + + MPI_Get_library_version(&cLibraryVersion, &libraryVersionResultLen) + + let libraryVersion = withUnsafePointer(to: &cLibraryVersion[0]) { + return String(cString: $0) + } + + print("MPI Version: \(majorVersion).\(minorVersion), \(libraryVersion)\n") +} + +MPI_Barrier(MPI_COMM_WORLD) + +var cProcessorName = [CChar](repeating: 0, count: Int(MPI_MAX_PROCESSOR_NAME)) +var processorNameResultLen = Int32() + +MPI_Get_processor_name(&cProcessorName, &processorNameResultLen) + +let processorName = withUnsafePointer(to: &cProcessorName[0]) { + return String(cString: $0) +} + +print("👋🗺 World Rank: \(worldRank), World Size: \(worldSize), Processor Name: \(processorName)") + +MPI_Finalize()