From 0bff775d54bd14b1d8fd94eb1c815553700b28b0 Mon Sep 17 00:00:00 2001 From: li3zhen1 Date: Mon, 30 Oct 2023 10:41:42 -0400 Subject: [PATCH 1/2] Add -cross-module-optimization flag --- Package.swift | 47 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/Package.swift b/Package.swift index 81230cf..ebe6ef9 100644 --- a/Package.swift +++ b/Package.swift @@ -6,7 +6,7 @@ import PackageDescription let package = Package( name: "Grape", platforms: [ - .macOS(.v11), + .macOS(.v11), .iOS(.v14), .watchOS(.v7), ], @@ -23,35 +23,62 @@ let package = Package( name: "ForceSimulation", targets: ["ForceSimulation"] ), - + ], dependencies: [ // other dependencies - .package(url: "https://github.com/apple/swift-docc-plugin", exact: "1.0.0"), + .package(url: "https://github.com/apple/swift-docc-plugin", exact: "1.0.0") ], targets: [ - + .target( name: "NDTree", - path: "Sources/NDTree" - // , swiftSettings:[.unsafeFlags(["-whole-module-optimization", "-Ounchecked"])] + path: "Sources/NDTree", + swiftSettings: [ + .unsafeFlags([ + "-cross-module-optimization", + // "-whole-module-optimization", + // "-whole-module-optimization", + // "-Ounchecked", + ]) + ] ), .testTarget( name: "NDTreeTests", - dependencies: ["NDTree"]), - + dependencies: ["NDTree"], + swiftSettings: [ + .unsafeFlags([ + "-cross-module-optimization", + // "-whole-module-optimization", + ]) + ] + ), .target( name: "ForceSimulation", dependencies: ["NDTree"], - path: "Sources/ForceSimulation" + path: "Sources/ForceSimulation", + swiftSettings: [ + .unsafeFlags([ + "-cross-module-optimization", + // "-whole-module-optimization", + // "-Ounchecked", + ]) + ] // , swiftSettings:[.unsafeFlags(["-whole-module-optimization", "-Ounchecked"])] ), .testTarget( name: "ForceSimulationTests", - dependencies: ["ForceSimulation", "NDTree"]), + dependencies: ["ForceSimulation", "NDTree"], + swiftSettings: [ + .unsafeFlags([ + "-cross-module-optimization", + // "-whole-module-optimization", + // "-Ounchecked", + ]) + ]), ] ) From 65c7dd4c215c431e9d077a0c8422184aa5da0cac Mon Sep 17 00:00:00 2001 From: li3zhen1 Date: Mon, 30 Oct 2023 11:04:45 -0400 Subject: [PATCH 2/2] Performance optimization --- README.md | 4 +- Sources/NDTree/simd+VectorLike.swift | 62 ++++++++++++++-------------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index cdcf843..c69a581 100644 --- a/README.md +++ b/README.md @@ -146,9 +146,9 @@ typealias Simulation4D = SimulationKD ## Performance -Grape uses simd to calculate position and velocity. Currently it takes ~0.05 seconds to iterate 120 times over the example graph(2D). (77 vertices, 254 edges, with manybody, center, collide and link forces. Release build on a M1 Max, tested with command `swift test -c release`) +Grape uses simd to calculate position and velocity. Currently it takes ~0.04 seconds to iterate 120 times over the example graph(2D). (77 vertices, 254 edges, with manybody, center, collide and link forces. Release build on a M1 Max, tested with command `swift test -c release`) -For 3D simulation, it takes ~0.07 seconds for the same graph and same configs. +For 3D simulation, it takes ~0.05 seconds for the same graph and same configs.
diff --git a/Sources/NDTree/simd+VectorLike.swift b/Sources/NDTree/simd+VectorLike.swift index b5a41a4..564dcc9 100644 --- a/Sources/NDTree/simd+VectorLike.swift +++ b/Sources/NDTree/simd+VectorLike.swift @@ -7,48 +7,48 @@ #if canImport(simd) -import simd + import simd -extension simd_double2: VectorLike { - @inlinable public func lengthSquared() -> Scalar { - return simd_length_squared(self) - } + extension simd_double2: VectorLike { + @inlinable public func lengthSquared() -> Scalar { + return simd_length_squared(self) + } - @inlinable public func length() -> Scalar { - return simd_length(self) - } + @inlinable public func length() -> Scalar { + return simd_length(self) + } - @inlinable public func distanceSquared(to: SIMD2) -> Scalar { - return simd_length_squared(self - to) - } + @inlinable public func distanceSquared(to: SIMD2) -> Scalar { + return simd_length_squared(self - to) + } + + @inlinable public func distance(to: SIMD2) -> Scalar { + return simd_length(self - to) + } - @inlinable public func distance(to: SIMD2) -> Scalar { - return simd_length(self - to) } -} + extension simd_float3: VectorLike { -extension simd_float3: VectorLike { + @inlinable public func lengthSquared() -> Scalar { + return simd_length_squared(self) + } - @inlinable public func lengthSquared() -> Scalar { - return simd_length_squared(self) - } + @inlinable public func length() -> Scalar { + return simd_length(self) + } - @inlinable public func length() -> Scalar { - return simd_length(self) - } + @inlinable public func distanceSquared(to: SIMD3) -> Scalar { + return simd_length_squared(self - to) + } - @inlinable public func distanceSquared(to: SIMD3) -> Scalar { - return simd_length_squared(self - to) - } + @inlinable public func distance(to: SIMD3) -> Scalar { + return simd_length(self - to) + } - @inlinable public func distance(to: SIMD3) -> Scalar { - return simd_length(self - to) } -} - -public typealias QuadBox = NDBox -public typealias OctBox = NDBox + public typealias QuadBox = NDBox + public typealias OctBox = NDBox -#endif \ No newline at end of file +#endif