├── .gitignore ├── GPUExample.xcodeproj ├── project.pbxproj └── project.xcworkspace │ └── contents.xcworkspacedata ├── GPUExample ├── AppDelegate.swift ├── Base.lproj │ ├── LaunchScreen.xib │ └── Main.storyboard ├── Images.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Info.plist ├── KernelSelectionController.swift ├── ViewController.swift └── kernel.metal ├── GPUExampleTests ├── GPUExampleTests.swift └── Info.plist ├── README.md ├── reduce1.png ├── reduce2.png └── reduce3.png /.gitignore: -------------------------------------------------------------------------------- 1 | reated by https://www.gitignore.io 2 | 3 | ### Xcode ### 4 | build/ 5 | *.pbxuser 6 | !default.pbxuser 7 | *.mode1v3 8 | !default.mode1v3 9 | *.mode2v3 10 | !default.mode2v3 11 | *.perspectivev3 12 | !default.perspectivev3 13 | xcuserdata 14 | *.xccheckout 15 | *.moved-aside 16 | DerivedData 17 | *.xcuserstate 18 | 19 | 20 | ### Swift ### 21 | # Xcode 22 | # 23 | build/ 24 | *.pbxuser 25 | !default.pbxuser 26 | *.mode1v3 27 | !default.mode1v3 28 | *.mode2v3 29 | !default.mode2v3 30 | *.perspectivev3 31 | !default.perspectivev3 32 | xcuserdata 33 | *.xccheckout 34 | *.moved-aside 35 | DerivedData 36 | *.hmap 37 | *.ipa 38 | *.xcuserstate 39 | 40 | # CocoaPods 41 | # 42 | # We recommend against adding the Pods directory to your .gitignore. However 43 | # you should judge for yourself, the pros and cons are mentioned at: 44 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control 45 | # 46 | # Pods/ 47 | 48 | # Carthage 49 | # 50 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 51 | # Carthage/Checkouts 52 | 53 | Carthage/Build 54 | 55 | 56 | ### OSX ### 57 | .DS_Store 58 | .AppleDouble 59 | .LSOverride 60 | 61 | # Icon must end with two \r 62 | Icon 63 | 64 | 65 | # Thumbnails 66 | ._* 67 | 68 | # Files that might appear on external disk 69 | .Spotlight-V100 70 | .Trashes 71 | 72 | # Directories potentially created on remote AFP share 73 | .AppleDB 74 | .AppleDesktop 75 | Network Trash Folder 76 | Temporary Items 77 | .apdisk 78 | 79 | 80 | -------------------------------------------------------------------------------- /GPUExample.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 6163403D1ADAFFFC00B00C25 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6163403C1ADAFFFC00B00C25 /* AppDelegate.swift */; }; 11 | 6163403F1ADAFFFC00B00C25 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6163403E1ADAFFFC00B00C25 /* ViewController.swift */; }; 12 | 616340421ADAFFFC00B00C25 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 616340401ADAFFFC00B00C25 /* Main.storyboard */; }; 13 | 616340441ADAFFFC00B00C25 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 616340431ADAFFFC00B00C25 /* Images.xcassets */; }; 14 | 616340471ADAFFFC00B00C25 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 616340451ADAFFFC00B00C25 /* LaunchScreen.xib */; }; 15 | 616340531ADAFFFD00B00C25 /* GPUExampleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 616340521ADAFFFD00B00C25 /* GPUExampleTests.swift */; }; 16 | 6163405D1ADB163500B00C25 /* kernel.metal in Sources */ = {isa = PBXBuildFile; fileRef = 6163405C1ADB163500B00C25 /* kernel.metal */; }; 17 | 61C75A2D1B71F4DF0082735A /* KernelSelectionController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61C75A2C1B71F4DF0082735A /* KernelSelectionController.swift */; }; 18 | /* End PBXBuildFile section */ 19 | 20 | /* Begin PBXContainerItemProxy section */ 21 | 6163404D1ADAFFFD00B00C25 /* PBXContainerItemProxy */ = { 22 | isa = PBXContainerItemProxy; 23 | containerPortal = 6163402F1ADAFFFC00B00C25 /* Project object */; 24 | proxyType = 1; 25 | remoteGlobalIDString = 616340361ADAFFFC00B00C25; 26 | remoteInfo = GPUExample; 27 | }; 28 | /* End PBXContainerItemProxy section */ 29 | 30 | /* Begin PBXFileReference section */ 31 | 616340371ADAFFFC00B00C25 /* GPUExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = GPUExample.app; sourceTree = BUILT_PRODUCTS_DIR; }; 32 | 6163403B1ADAFFFC00B00C25 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 33 | 6163403C1ADAFFFC00B00C25 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 34 | 6163403E1ADAFFFC00B00C25 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 35 | 616340411ADAFFFC00B00C25 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 36 | 616340431ADAFFFC00B00C25 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 37 | 616340461ADAFFFC00B00C25 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = ""; }; 38 | 6163404C1ADAFFFD00B00C25 /* GPUExampleTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GPUExampleTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 39 | 616340511ADAFFFD00B00C25 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 40 | 616340521ADAFFFD00B00C25 /* GPUExampleTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GPUExampleTests.swift; sourceTree = ""; }; 41 | 6163405C1ADB163500B00C25 /* kernel.metal */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.metal; path = kernel.metal; sourceTree = ""; }; 42 | 61C75A2C1B71F4DF0082735A /* KernelSelectionController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KernelSelectionController.swift; sourceTree = ""; }; 43 | /* End PBXFileReference section */ 44 | 45 | /* Begin PBXFrameworksBuildPhase section */ 46 | 616340341ADAFFFC00B00C25 /* Frameworks */ = { 47 | isa = PBXFrameworksBuildPhase; 48 | buildActionMask = 2147483647; 49 | files = ( 50 | ); 51 | runOnlyForDeploymentPostprocessing = 0; 52 | }; 53 | 616340491ADAFFFD00B00C25 /* Frameworks */ = { 54 | isa = PBXFrameworksBuildPhase; 55 | buildActionMask = 2147483647; 56 | files = ( 57 | ); 58 | runOnlyForDeploymentPostprocessing = 0; 59 | }; 60 | /* End PBXFrameworksBuildPhase section */ 61 | 62 | /* Begin PBXGroup section */ 63 | 6163402E1ADAFFFC00B00C25 = { 64 | isa = PBXGroup; 65 | children = ( 66 | 616340391ADAFFFC00B00C25 /* GPUExample */, 67 | 6163404F1ADAFFFD00B00C25 /* GPUExampleTests */, 68 | 616340381ADAFFFC00B00C25 /* Products */, 69 | ); 70 | sourceTree = ""; 71 | }; 72 | 616340381ADAFFFC00B00C25 /* Products */ = { 73 | isa = PBXGroup; 74 | children = ( 75 | 616340371ADAFFFC00B00C25 /* GPUExample.app */, 76 | 6163404C1ADAFFFD00B00C25 /* GPUExampleTests.xctest */, 77 | ); 78 | name = Products; 79 | sourceTree = ""; 80 | }; 81 | 616340391ADAFFFC00B00C25 /* GPUExample */ = { 82 | isa = PBXGroup; 83 | children = ( 84 | 6163403C1ADAFFFC00B00C25 /* AppDelegate.swift */, 85 | 61C75A2C1B71F4DF0082735A /* KernelSelectionController.swift */, 86 | 6163403E1ADAFFFC00B00C25 /* ViewController.swift */, 87 | 6163405C1ADB163500B00C25 /* kernel.metal */, 88 | 616340401ADAFFFC00B00C25 /* Main.storyboard */, 89 | 616340431ADAFFFC00B00C25 /* Images.xcassets */, 90 | 616340451ADAFFFC00B00C25 /* LaunchScreen.xib */, 91 | 6163403A1ADAFFFC00B00C25 /* Supporting Files */, 92 | ); 93 | path = GPUExample; 94 | sourceTree = ""; 95 | }; 96 | 6163403A1ADAFFFC00B00C25 /* Supporting Files */ = { 97 | isa = PBXGroup; 98 | children = ( 99 | 6163403B1ADAFFFC00B00C25 /* Info.plist */, 100 | ); 101 | name = "Supporting Files"; 102 | sourceTree = ""; 103 | }; 104 | 6163404F1ADAFFFD00B00C25 /* GPUExampleTests */ = { 105 | isa = PBXGroup; 106 | children = ( 107 | 616340521ADAFFFD00B00C25 /* GPUExampleTests.swift */, 108 | 616340501ADAFFFD00B00C25 /* Supporting Files */, 109 | ); 110 | path = GPUExampleTests; 111 | sourceTree = ""; 112 | }; 113 | 616340501ADAFFFD00B00C25 /* Supporting Files */ = { 114 | isa = PBXGroup; 115 | children = ( 116 | 616340511ADAFFFD00B00C25 /* Info.plist */, 117 | ); 118 | name = "Supporting Files"; 119 | sourceTree = ""; 120 | }; 121 | /* End PBXGroup section */ 122 | 123 | /* Begin PBXNativeTarget section */ 124 | 616340361ADAFFFC00B00C25 /* GPUExample */ = { 125 | isa = PBXNativeTarget; 126 | buildConfigurationList = 616340561ADAFFFD00B00C25 /* Build configuration list for PBXNativeTarget "GPUExample" */; 127 | buildPhases = ( 128 | 616340331ADAFFFC00B00C25 /* Sources */, 129 | 616340341ADAFFFC00B00C25 /* Frameworks */, 130 | 616340351ADAFFFC00B00C25 /* Resources */, 131 | ); 132 | buildRules = ( 133 | ); 134 | dependencies = ( 135 | ); 136 | name = GPUExample; 137 | productName = GPUExample; 138 | productReference = 616340371ADAFFFC00B00C25 /* GPUExample.app */; 139 | productType = "com.apple.product-type.application"; 140 | }; 141 | 6163404B1ADAFFFD00B00C25 /* GPUExampleTests */ = { 142 | isa = PBXNativeTarget; 143 | buildConfigurationList = 616340591ADAFFFD00B00C25 /* Build configuration list for PBXNativeTarget "GPUExampleTests" */; 144 | buildPhases = ( 145 | 616340481ADAFFFD00B00C25 /* Sources */, 146 | 616340491ADAFFFD00B00C25 /* Frameworks */, 147 | 6163404A1ADAFFFD00B00C25 /* Resources */, 148 | ); 149 | buildRules = ( 150 | ); 151 | dependencies = ( 152 | 6163404E1ADAFFFD00B00C25 /* PBXTargetDependency */, 153 | ); 154 | name = GPUExampleTests; 155 | productName = GPUExampleTests; 156 | productReference = 6163404C1ADAFFFD00B00C25 /* GPUExampleTests.xctest */; 157 | productType = "com.apple.product-type.bundle.unit-test"; 158 | }; 159 | /* End PBXNativeTarget section */ 160 | 161 | /* Begin PBXProject section */ 162 | 6163402F1ADAFFFC00B00C25 /* Project object */ = { 163 | isa = PBXProject; 164 | attributes = { 165 | LastUpgradeCheck = 0620; 166 | ORGANIZATIONNAME = Antipattern; 167 | TargetAttributes = { 168 | 616340361ADAFFFC00B00C25 = { 169 | CreatedOnToolsVersion = 6.2; 170 | LastSwiftMigration = 0830; 171 | }; 172 | 6163404B1ADAFFFD00B00C25 = { 173 | CreatedOnToolsVersion = 6.2; 174 | LastSwiftMigration = 0830; 175 | TestTargetID = 616340361ADAFFFC00B00C25; 176 | }; 177 | }; 178 | }; 179 | buildConfigurationList = 616340321ADAFFFC00B00C25 /* Build configuration list for PBXProject "GPUExample" */; 180 | compatibilityVersion = "Xcode 3.2"; 181 | developmentRegion = English; 182 | hasScannedForEncodings = 0; 183 | knownRegions = ( 184 | en, 185 | Base, 186 | ); 187 | mainGroup = 6163402E1ADAFFFC00B00C25; 188 | productRefGroup = 616340381ADAFFFC00B00C25 /* Products */; 189 | projectDirPath = ""; 190 | projectRoot = ""; 191 | targets = ( 192 | 616340361ADAFFFC00B00C25 /* GPUExample */, 193 | 6163404B1ADAFFFD00B00C25 /* GPUExampleTests */, 194 | ); 195 | }; 196 | /* End PBXProject section */ 197 | 198 | /* Begin PBXResourcesBuildPhase section */ 199 | 616340351ADAFFFC00B00C25 /* Resources */ = { 200 | isa = PBXResourcesBuildPhase; 201 | buildActionMask = 2147483647; 202 | files = ( 203 | 616340421ADAFFFC00B00C25 /* Main.storyboard in Resources */, 204 | 616340471ADAFFFC00B00C25 /* LaunchScreen.xib in Resources */, 205 | 616340441ADAFFFC00B00C25 /* Images.xcassets in Resources */, 206 | ); 207 | runOnlyForDeploymentPostprocessing = 0; 208 | }; 209 | 6163404A1ADAFFFD00B00C25 /* Resources */ = { 210 | isa = PBXResourcesBuildPhase; 211 | buildActionMask = 2147483647; 212 | files = ( 213 | ); 214 | runOnlyForDeploymentPostprocessing = 0; 215 | }; 216 | /* End PBXResourcesBuildPhase section */ 217 | 218 | /* Begin PBXSourcesBuildPhase section */ 219 | 616340331ADAFFFC00B00C25 /* Sources */ = { 220 | isa = PBXSourcesBuildPhase; 221 | buildActionMask = 2147483647; 222 | files = ( 223 | 6163403F1ADAFFFC00B00C25 /* ViewController.swift in Sources */, 224 | 61C75A2D1B71F4DF0082735A /* KernelSelectionController.swift in Sources */, 225 | 6163405D1ADB163500B00C25 /* kernel.metal in Sources */, 226 | 6163403D1ADAFFFC00B00C25 /* AppDelegate.swift in Sources */, 227 | ); 228 | runOnlyForDeploymentPostprocessing = 0; 229 | }; 230 | 616340481ADAFFFD00B00C25 /* Sources */ = { 231 | isa = PBXSourcesBuildPhase; 232 | buildActionMask = 2147483647; 233 | files = ( 234 | 616340531ADAFFFD00B00C25 /* GPUExampleTests.swift in Sources */, 235 | ); 236 | runOnlyForDeploymentPostprocessing = 0; 237 | }; 238 | /* End PBXSourcesBuildPhase section */ 239 | 240 | /* Begin PBXTargetDependency section */ 241 | 6163404E1ADAFFFD00B00C25 /* PBXTargetDependency */ = { 242 | isa = PBXTargetDependency; 243 | target = 616340361ADAFFFC00B00C25 /* GPUExample */; 244 | targetProxy = 6163404D1ADAFFFD00B00C25 /* PBXContainerItemProxy */; 245 | }; 246 | /* End PBXTargetDependency section */ 247 | 248 | /* Begin PBXVariantGroup section */ 249 | 616340401ADAFFFC00B00C25 /* Main.storyboard */ = { 250 | isa = PBXVariantGroup; 251 | children = ( 252 | 616340411ADAFFFC00B00C25 /* Base */, 253 | ); 254 | name = Main.storyboard; 255 | sourceTree = ""; 256 | }; 257 | 616340451ADAFFFC00B00C25 /* LaunchScreen.xib */ = { 258 | isa = PBXVariantGroup; 259 | children = ( 260 | 616340461ADAFFFC00B00C25 /* Base */, 261 | ); 262 | name = LaunchScreen.xib; 263 | sourceTree = ""; 264 | }; 265 | /* End PBXVariantGroup section */ 266 | 267 | /* Begin XCBuildConfiguration section */ 268 | 616340541ADAFFFD00B00C25 /* Debug */ = { 269 | isa = XCBuildConfiguration; 270 | buildSettings = { 271 | ALWAYS_SEARCH_USER_PATHS = NO; 272 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 273 | CLANG_CXX_LIBRARY = "libc++"; 274 | CLANG_ENABLE_MODULES = YES; 275 | CLANG_ENABLE_OBJC_ARC = YES; 276 | CLANG_WARN_BOOL_CONVERSION = YES; 277 | CLANG_WARN_CONSTANT_CONVERSION = YES; 278 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 279 | CLANG_WARN_EMPTY_BODY = YES; 280 | CLANG_WARN_ENUM_CONVERSION = YES; 281 | CLANG_WARN_INT_CONVERSION = YES; 282 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 283 | CLANG_WARN_UNREACHABLE_CODE = YES; 284 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 285 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 286 | COPY_PHASE_STRIP = NO; 287 | ENABLE_STRICT_OBJC_MSGSEND = YES; 288 | GCC_C_LANGUAGE_STANDARD = gnu99; 289 | GCC_DYNAMIC_NO_PIC = NO; 290 | GCC_OPTIMIZATION_LEVEL = 0; 291 | GCC_PREPROCESSOR_DEFINITIONS = ( 292 | "DEBUG=1", 293 | "$(inherited)", 294 | ); 295 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 296 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 297 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 298 | GCC_WARN_UNDECLARED_SELECTOR = YES; 299 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 300 | GCC_WARN_UNUSED_FUNCTION = YES; 301 | GCC_WARN_UNUSED_VARIABLE = YES; 302 | IPHONEOS_DEPLOYMENT_TARGET = 8.1; 303 | MTL_ENABLE_DEBUG_INFO = YES; 304 | SDKROOT = iphoneos; 305 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 306 | TARGETED_DEVICE_FAMILY = "1,2"; 307 | }; 308 | name = Debug; 309 | }; 310 | 616340551ADAFFFD00B00C25 /* Release */ = { 311 | isa = XCBuildConfiguration; 312 | buildSettings = { 313 | ALWAYS_SEARCH_USER_PATHS = NO; 314 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 315 | CLANG_CXX_LIBRARY = "libc++"; 316 | CLANG_ENABLE_MODULES = YES; 317 | CLANG_ENABLE_OBJC_ARC = YES; 318 | CLANG_WARN_BOOL_CONVERSION = YES; 319 | CLANG_WARN_CONSTANT_CONVERSION = YES; 320 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 321 | CLANG_WARN_EMPTY_BODY = YES; 322 | CLANG_WARN_ENUM_CONVERSION = YES; 323 | CLANG_WARN_INT_CONVERSION = YES; 324 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 325 | CLANG_WARN_UNREACHABLE_CODE = YES; 326 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 327 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 328 | COPY_PHASE_STRIP = NO; 329 | ENABLE_NS_ASSERTIONS = NO; 330 | ENABLE_STRICT_OBJC_MSGSEND = YES; 331 | GCC_C_LANGUAGE_STANDARD = gnu99; 332 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 333 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 334 | GCC_WARN_UNDECLARED_SELECTOR = YES; 335 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 336 | GCC_WARN_UNUSED_FUNCTION = YES; 337 | GCC_WARN_UNUSED_VARIABLE = YES; 338 | IPHONEOS_DEPLOYMENT_TARGET = 8.1; 339 | MTL_ENABLE_DEBUG_INFO = NO; 340 | SDKROOT = iphoneos; 341 | TARGETED_DEVICE_FAMILY = "1,2"; 342 | VALIDATE_PRODUCT = YES; 343 | }; 344 | name = Release; 345 | }; 346 | 616340571ADAFFFD00B00C25 /* Debug */ = { 347 | isa = XCBuildConfiguration; 348 | buildSettings = { 349 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 350 | INFOPLIST_FILE = GPUExample/Info.plist; 351 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 352 | PRODUCT_NAME = "$(TARGET_NAME)"; 353 | SWIFT_VERSION = 3.0; 354 | }; 355 | name = Debug; 356 | }; 357 | 616340581ADAFFFD00B00C25 /* Release */ = { 358 | isa = XCBuildConfiguration; 359 | buildSettings = { 360 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 361 | INFOPLIST_FILE = GPUExample/Info.plist; 362 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 363 | PRODUCT_NAME = "$(TARGET_NAME)"; 364 | SWIFT_VERSION = 3.0; 365 | }; 366 | name = Release; 367 | }; 368 | 6163405A1ADAFFFD00B00C25 /* Debug */ = { 369 | isa = XCBuildConfiguration; 370 | buildSettings = { 371 | BUNDLE_LOADER = "$(TEST_HOST)"; 372 | FRAMEWORK_SEARCH_PATHS = ( 373 | "$(SDKROOT)/Developer/Library/Frameworks", 374 | "$(inherited)", 375 | ); 376 | GCC_PREPROCESSOR_DEFINITIONS = ( 377 | "DEBUG=1", 378 | "$(inherited)", 379 | ); 380 | INFOPLIST_FILE = GPUExampleTests/Info.plist; 381 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 382 | PRODUCT_NAME = "$(TARGET_NAME)"; 383 | SWIFT_VERSION = 3.0; 384 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/GPUExample.app/GPUExample"; 385 | }; 386 | name = Debug; 387 | }; 388 | 6163405B1ADAFFFD00B00C25 /* Release */ = { 389 | isa = XCBuildConfiguration; 390 | buildSettings = { 391 | BUNDLE_LOADER = "$(TEST_HOST)"; 392 | FRAMEWORK_SEARCH_PATHS = ( 393 | "$(SDKROOT)/Developer/Library/Frameworks", 394 | "$(inherited)", 395 | ); 396 | INFOPLIST_FILE = GPUExampleTests/Info.plist; 397 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 398 | PRODUCT_NAME = "$(TARGET_NAME)"; 399 | SWIFT_VERSION = 3.0; 400 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/GPUExample.app/GPUExample"; 401 | }; 402 | name = Release; 403 | }; 404 | /* End XCBuildConfiguration section */ 405 | 406 | /* Begin XCConfigurationList section */ 407 | 616340321ADAFFFC00B00C25 /* Build configuration list for PBXProject "GPUExample" */ = { 408 | isa = XCConfigurationList; 409 | buildConfigurations = ( 410 | 616340541ADAFFFD00B00C25 /* Debug */, 411 | 616340551ADAFFFD00B00C25 /* Release */, 412 | ); 413 | defaultConfigurationIsVisible = 0; 414 | defaultConfigurationName = Release; 415 | }; 416 | 616340561ADAFFFD00B00C25 /* Build configuration list for PBXNativeTarget "GPUExample" */ = { 417 | isa = XCConfigurationList; 418 | buildConfigurations = ( 419 | 616340571ADAFFFD00B00C25 /* Debug */, 420 | 616340581ADAFFFD00B00C25 /* Release */, 421 | ); 422 | defaultConfigurationIsVisible = 0; 423 | defaultConfigurationName = Release; 424 | }; 425 | 616340591ADAFFFD00B00C25 /* Build configuration list for PBXNativeTarget "GPUExampleTests" */ = { 426 | isa = XCConfigurationList; 427 | buildConfigurations = ( 428 | 6163405A1ADAFFFD00B00C25 /* Debug */, 429 | 6163405B1ADAFFFD00B00C25 /* Release */, 430 | ); 431 | defaultConfigurationIsVisible = 0; 432 | defaultConfigurationName = Release; 433 | }; 434 | /* End XCConfigurationList section */ 435 | }; 436 | rootObject = 6163402F1ADAFFFC00B00C25 /* Project object */; 437 | } 438 | -------------------------------------------------------------------------------- /GPUExample.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /GPUExample/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // GPUExample 4 | // 5 | // Created by Mateusz Buda on 12/04/15. 6 | // Copyright (c) 2015 inFullMobile. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(_ application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(_ application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(_ application: UIApplication) { 33 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(_ application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(_ application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /GPUExample/Base.lproj/LaunchScreen.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 20 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /GPUExample/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 186 | 195 | 217 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | -------------------------------------------------------------------------------- /GPUExample/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } -------------------------------------------------------------------------------- /GPUExample/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | com.infullmobile.$(PRODUCT_NAME:rfc1034identifier) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | UISupportedInterfaceOrientations~ipad 40 | 41 | UIInterfaceOrientationPortrait 42 | UIInterfaceOrientationPortraitUpsideDown 43 | UIInterfaceOrientationLandscapeLeft 44 | UIInterfaceOrientationLandscapeRight 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /GPUExample/KernelSelectionController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // KernelSelectionController.swift 3 | // GPUExample 4 | // 5 | // Created by Mateusz Buda on 05/08/15. 6 | // Copyright (c) 2015 inFullMobile. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class KernelSelectionController: UITableViewController { 12 | 13 | var selectedKernel: String! 14 | 15 | // MARK: - Delegate 16 | 17 | override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { 18 | selectedKernel = indexPath.row > 0 ? "reduce\(indexPath.row)" : "map" 19 | 20 | return indexPath 21 | } 22 | 23 | // MARK: - Navigation 24 | 25 | override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 26 | let destController: ViewController = segue.destination as! ViewController 27 | destController.kernelName = selectedKernel 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /GPUExample/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // GPUExample 4 | // 5 | // Created by Mateusz Buda on 12/04/15. 6 | // Copyright (c) 2015 inFullMobile. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import Metal 11 | import QuartzCore 12 | import Darwin 13 | import Accelerate 14 | 15 | let PROBLEM_SIZE = 16777216 // 2^24 16 | let RESULT_SIZE = 1 17 | let THREADGROUP_SIZE = 256 18 | 19 | class ViewController: UIViewController { 20 | 21 | var kernelName: String! 22 | 23 | @IBOutlet weak var execTimeGPU: UILabel! 24 | @IBOutlet weak var execTimeCPU: UILabel! 25 | 26 | var input: [Int32] = [Int32](repeating: 0, count: PROBLEM_SIZE) 27 | var result: [Int32] = [Int32](repeating: 0, count: RESULT_SIZE) 28 | 29 | override func viewDidLoad() { 30 | super.viewDidLoad() 31 | 32 | title = kernelName 33 | } 34 | 35 | @IBAction func runGPU(_ sender: UIButton) { 36 | let (device, _, defaultLibrary, commandBuffer, computeCommandEncoder) = initMetal() 37 | 38 | let resultSize = kernelName == "map" ? PROBLEM_SIZE : RESULT_SIZE 39 | input = [Int32](repeating: (kernelName == "map" ? 0 : 1), count: PROBLEM_SIZE) 40 | result = [Int32](repeating: 0, count: resultSize) 41 | 42 | // set up a compute pipeline with kernel function and add it to encoder 43 | let kernel = defaultLibrary.makeFunction(name: kernelName) 44 | 45 | 46 | do { 47 | let computePipelineState = try device.makeComputePipelineState(function: kernel!) 48 | computeCommandEncoder.setComputePipelineState(computePipelineState) 49 | } catch { 50 | computeCommandEncoder.endEncoding() 51 | return 52 | } 53 | 54 | 55 | 56 | // calculate byte length of input and output data 57 | let inputByteLength = input.count * MemoryLayout.size(ofValue: input[0]) 58 | var resultByteLength = result.count * MemoryLayout.size(ofValue: result[0]) 59 | 60 | // create a MTLBuffer - input data for GPU (<= 256 MB) 61 | let inputBuffer = device.makeBuffer(bytes: &input, length: inputByteLength, options: []) 62 | 63 | // set the input vector for the kernel function, 64 | // atIndex: 0 here corresponds to buffer(0) in the kernel function 65 | computeCommandEncoder.setBuffer(inputBuffer, offset: 0, at: 0) 66 | 67 | // create the output buffer for the kernel function, 68 | // atIndex: 1 here corresponds to buffer(1) in the kernel function 69 | let resultBuffer = device.makeBuffer(bytes: &result, length: resultByteLength, options: []) 70 | computeCommandEncoder.setBuffer(resultBuffer, offset: 0, at: 1) 71 | 72 | // make grid 73 | let threadgroupSizeMultiplier = kernelName.contains("4") ? 2 : 1 74 | let threadsPerGroup = MTLSize(width: THREADGROUP_SIZE, height: 1, depth: 1) 75 | let numThreadgroups = MTLSize(width: (PROBLEM_SIZE / (THREADGROUP_SIZE * threadgroupSizeMultiplier)), height: 1, depth:1) 76 | 77 | print("Block: \(threadsPerGroup.width) x \(threadsPerGroup.height)\n" + 78 | "Grid: \(numThreadgroups.width) x \(numThreadgroups.height) x \(numThreadgroups.depth)") 79 | 80 | computeCommandEncoder.dispatchThreadgroups(numThreadgroups, threadsPerThreadgroup: threadsPerGroup) 81 | 82 | // compute and wait for result 83 | computeCommandEncoder.endEncoding() 84 | 85 | let start = CACurrentMediaTime() 86 | 87 | commandBuffer.commit() 88 | commandBuffer.waitUntilCompleted() 89 | 90 | let stop = CACurrentMediaTime() 91 | 92 | if (commandBuffer.error != nil) { 93 | execTimeGPU.text = "error" 94 | print("Command buffer error: \(String(describing: commandBuffer.error))") 95 | return 96 | } 97 | 98 | execTimeGPU.text = String.localizedStringWithFormat("%.2f ms", (stop-start) * 1000) 99 | 100 | // Get GPU data 101 | 102 | let data = Data(bytesNoCopy: resultBuffer.contents(), count: resultByteLength, deallocator: .none) 103 | // get data from GPU into Swift array 104 | data.getBytes(&result, length: resultByteLength) 105 | } 106 | 107 | @IBAction func runCPU(_ sender: AnyObject) { 108 | let resultSize = kernelName == "map" ? PROBLEM_SIZE : RESULT_SIZE 109 | input = [Int32](repeating: (kernelName == "map" ? 0 : 1), count: PROBLEM_SIZE) 110 | result = [Int32](repeating: 0, count: resultSize) 111 | 112 | let start = CACurrentMediaTime() 113 | 114 | if kernelName == "map" { 115 | 116 | for i in 0 ..< input.count { 117 | result[i] = Int32(cos(CDouble(input[i]))) 118 | } 119 | 120 | } else { // reduce 121 | 122 | for i in 0 ..< input.count { 123 | result[0] += input[i] 124 | } 125 | 126 | } 127 | 128 | let stop = CACurrentMediaTime() 129 | 130 | execTimeCPU.text = String.localizedStringWithFormat("%.2f ms", (stop-start) * 1000) 131 | 132 | print("result = \(result[0])") 133 | } 134 | 135 | // MARK: - Metal 136 | 137 | // source: 138 | // DATA-PARALLEL PROGRAMMING WITH METAL AND SWIFT FOR IPHONE/IPAD GPU by Amund Tveit 139 | // http://memkite.com/blog/2014/12/15/data-parallel-programming-with-metal-and-swift-for-iphoneipad-gpu/ 140 | func initMetal() -> (MTLDevice, MTLCommandQueue, MTLLibrary, MTLCommandBuffer, MTLComputeCommandEncoder) { 141 | // Get access to iPhone or iPad GPU 142 | let device = MTLCreateSystemDefaultDevice() 143 | 144 | // Queue to handle an ordered list of command buffers 145 | let commandQueue = device!.makeCommandQueue() 146 | 147 | // Access to Metal functions that are stored in Kernel.metal file, e.g. reduce() 148 | let defaultLibrary = device!.newDefaultLibrary() 149 | 150 | // Buffer for storing encoded commands that are sent to GPU 151 | let commandBuffer = commandQueue.makeCommandBuffer() 152 | 153 | // Encoder for GPU commands 154 | let computeCommandEncoder = commandBuffer.makeComputeCommandEncoder() 155 | 156 | return (device!, commandQueue, defaultLibrary!, commandBuffer, computeCommandEncoder) 157 | } 158 | 159 | } 160 | 161 | -------------------------------------------------------------------------------- /GPUExample/kernel.metal: -------------------------------------------------------------------------------- 1 | // 2 | // kernel.metal 3 | // GPUExample 4 | // 5 | // Created by Mateusz Buda on 12/04/15. 6 | // Copyright (c) 2015 inFullMobile. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace metal; 11 | 12 | constant int THREADGROUP_SIZE = 256; 13 | 14 | /* map */ 15 | 16 | kernel void map(const device int *array [[ buffer(0) ]], 17 | device int *result [[ buffer(1) ]], 18 | uint id [[ thread_position_in_grid ]], 19 | uint tid [[ thread_index_in_threadgroup ]], 20 | uint bid [[ threadgroup_position_in_grid ]], 21 | uint blockDim [[ threads_per_threadgroup ]]) { 22 | 23 | uint i = bid * blockDim + tid; 24 | 25 | result[i] = int(cos(float(array[i]))); 26 | } 27 | 28 | /* naive reduction */ 29 | 30 | // (kernel | vertex | fragment) 31 | kernel void reduce1(const device int *array [[ buffer(0) ]], 32 | volatile device atomic_int *result [[ buffer(1) ]], 33 | uint id [[ thread_position_in_grid ]], 34 | uint tid [[ thread_index_in_threadgroup ]], 35 | uint bid [[ threadgroup_position_in_grid ]], 36 | uint blockDim [[ threads_per_threadgroup ]]) { 37 | 38 | threadgroup int shared_memory[THREADGROUP_SIZE]; 39 | 40 | uint i = bid * blockDim + tid; 41 | shared_memory[tid] = array[i]; 42 | 43 | threadgroup_barrier(mem_flags::mem_none); 44 | 45 | // reduction in shared memory 46 | for (uint s = 1; s < blockDim; s *= 2) { 47 | if (tid % (2 * s) == 0) { 48 | shared_memory[tid] += shared_memory[tid + s]; 49 | } 50 | threadgroup_barrier(mem_flags::mem_none); 51 | } 52 | 53 | // it's not recommended (just to show atomic operation capability)! 54 | if (0 == tid) { 55 | atomic_fetch_add_explicit(result, shared_memory[0], memory_order_relaxed); 56 | } 57 | } 58 | 59 | /* changed thred id performing reduction */ 60 | 61 | kernel void reduce2(const device int *array [[ buffer(0) ]], 62 | volatile device atomic_int *result [[ buffer(1) ]], 63 | uint id [[ thread_position_in_grid ]], 64 | uint tid [[ thread_index_in_threadgroup ]], 65 | uint bid [[ threadgroup_position_in_grid ]], 66 | uint blockDim [[ threads_per_threadgroup ]]) { 67 | 68 | threadgroup int shared_memory[THREADGROUP_SIZE]; 69 | 70 | uint i = bid * blockDim + tid; 71 | 72 | shared_memory[tid] = array[i]; 73 | 74 | threadgroup_barrier(mem_flags::mem_none); 75 | 76 | // reduction in shared memory 77 | for (uint s = 1; s < blockDim; s *= 2) { 78 | uint index = 2 * s * tid; 79 | 80 | if (index < blockDim) { 81 | shared_memory[index] += shared_memory[index + s]; 82 | } 83 | threadgroup_barrier(mem_flags::mem_none); 84 | } 85 | 86 | // it's not recommended (just to show atomic operation capability)! 87 | if (0 == tid) { 88 | atomic_fetch_add_explicit(result, shared_memory[0], memory_order_relaxed); 89 | } 90 | } 91 | 92 | /* connected memory space */ 93 | 94 | kernel void reduce3(const device int *array [[ buffer(0) ]], 95 | volatile device atomic_int *result [[ buffer(1) ]], 96 | uint id [[ thread_position_in_grid ]], 97 | uint tid [[ thread_index_in_threadgroup ]], 98 | uint bid [[ threadgroup_position_in_grid ]], 99 | uint blockDim [[ threads_per_threadgroup ]]) { 100 | 101 | threadgroup int shared_memory[THREADGROUP_SIZE]; 102 | 103 | uint i = bid * blockDim + tid; 104 | 105 | shared_memory[tid] = array[i]; 106 | 107 | threadgroup_barrier(mem_flags::mem_none); 108 | 109 | // reduction in shared memory 110 | for (uint s = blockDim / 2; s > 0; s >>= 1) { 111 | if (tid < s) { 112 | shared_memory[tid] += shared_memory[tid + s]; 113 | } 114 | threadgroup_barrier(mem_flags::mem_none); 115 | } 116 | 117 | // it's not recommended (just to show atomic operation capability)! 118 | if (0 == tid) { 119 | atomic_fetch_add_explicit(result, shared_memory[0], memory_order_relaxed); 120 | } 121 | } 122 | 123 | /* halved number of blocks */ 124 | 125 | kernel void reduce4(const device int *array [[ buffer(0) ]], 126 | volatile device atomic_int *result [[ buffer(1) ]], 127 | uint id [[ thread_position_in_grid ]], 128 | uint tid [[ thread_index_in_threadgroup ]], 129 | uint bid [[ threadgroup_position_in_grid ]], 130 | uint blockDim [[ threads_per_threadgroup ]]) { 131 | 132 | threadgroup int shared_memory[THREADGROUP_SIZE]; 133 | 134 | uint i = bid * (blockDim * 2) + tid; 135 | 136 | shared_memory[tid] = array[i] + array[i + blockDim]; 137 | 138 | threadgroup_barrier(mem_flags::mem_none); 139 | 140 | // reduction in shared memory 141 | for (uint s = blockDim / 2; s > 0; s >>= 1) { 142 | if (tid < s) { 143 | shared_memory[tid] += shared_memory[tid + s]; 144 | } 145 | threadgroup_barrier(mem_flags::mem_none); 146 | } 147 | 148 | // it's not recommended (just to show atomic operation capability)! 149 | if (0 == tid) { 150 | atomic_fetch_add_explicit(result, shared_memory[0], memory_order_relaxed); 151 | } 152 | } 153 | 154 | // source for parallel reduction: 155 | // Optimizing Parallel Reduction in CUDA by Mark Harris 156 | // https://docs.nvidia.com/cuda/samples/6_Advanced/reduction/doc/reduction.pdf 157 | -------------------------------------------------------------------------------- /GPUExampleTests/GPUExampleTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // GPUExampleTests.swift 3 | // GPUExampleTests 4 | // 5 | // Created by Mateusz Buda on 12/04/15. 6 | // Copyright (c) 2015 Antipattern. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import XCTest 11 | 12 | class GPUExampleTests: XCTestCase { 13 | 14 | override func setUp() { 15 | super.setUp() 16 | // Put setup code here. This method is called before the invocation of each test method in the class. 17 | } 18 | 19 | override func tearDown() { 20 | // Put teardown code here. This method is called after the invocation of each test method in the class. 21 | super.tearDown() 22 | } 23 | 24 | func testExample() { 25 | // This is an example of a functional test case. 26 | XCTAssert(true, "Pass") 27 | } 28 | 29 | func testPerformanceExample() { 30 | // This is an example of a performance test case. 31 | self.measure() { 32 | // Put the code you want to measure the time of here. 33 | } 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /GPUExampleTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | com.infullmobile.$(PRODUCT_NAME:rfc1034identifier) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GPUExample 2 | GPGPU Example with Apple's Metal API 3 | 4 | ## Project overview 5 | ### KernelSelectionController.swift 6 | Table View with available kernels to compare CPU and GPU performance 7 | 8 | ### ViewController.swift 9 | Performs CPU and GPU computations. Shows execution times. 10 | 11 | ### kernel.metal 12 | Kernels that are executed on GPU with Metal API. 13 | 14 | #### map 15 | Simple map that applies cosine function to each element of input array. 16 | 17 | #### reduce1 18 | Naive parallel reduction (computes sum of cosine of each input array element). 19 | ![reduce1](https://raw.githubusercontent.com/mateuszbuda/GPUExample/master/reduce1.png) 20 | 21 | #### reduce2 22 | Changed threads performing reduction. 23 | ![reduce2](https://raw.githubusercontent.com/mateuszbuda/GPUExample/master/reduce2.png) 24 | 25 | #### reduce3 26 | Accessing connected memory space. 27 | ![reduce3](https://raw.githubusercontent.com/mateuszbuda/GPUExample/master/reduce3.png) 28 | 29 | #### reduce4 30 | The same as in reduce3 but first reduction step is performed when copying data to shared memory, so we need half the number of threads that we needed in the previous reduce versions. 31 | 32 | ## NOTICE 33 | * Graphics presenting reduction optimization steps source: 34 | Optimizing Parallel Reduction in CUDA by Mark Harris 35 | https://docs.nvidia.com/cuda/samples/6_Advanced/reduction/doc/reduction.pdf 36 | 37 | * This example only works for input array which size is a positive integer power of 2. As a simple exercise, you can try to make it more flexible. 38 | -------------------------------------------------------------------------------- /reduce1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mateuszbuda/GPUExample/e3a591f62743a09ffcf97ba3ec2f5f1e1fe7886e/reduce1.png -------------------------------------------------------------------------------- /reduce2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mateuszbuda/GPUExample/e3a591f62743a09ffcf97ba3ec2f5f1e1fe7886e/reduce2.png -------------------------------------------------------------------------------- /reduce3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mateuszbuda/GPUExample/e3a591f62743a09ffcf97ba3ec2f5f1e1fe7886e/reduce3.png --------------------------------------------------------------------------------