An example of using the GPU on M1 arm64 Macs for basic compute functions

Overview

M1-GPU-Compute

Using Swift and Apple's Metal API to utilize the GPUs on M1 equipped Macs

Adding Two Arrays

CPUs perform computations sequentially, waiting for the previous computation to finish before moving onto the next one. Let's say we wanted to add two arrays together:

let array1 = [3, 2, 4, 1, 5]
let array2 = [1, 5, 2, 7, 3]

The CPU would go in order, adding each index from left to right. An example of CPU implementation:

let result = [0, 0, 0, 0, 0]

for i in 0..<5 {
    result[i] = array1[i] + array2[i]
}

CPUs would add 3 and 1 then place 4 into the result array, then move on to the next index:

3 + 1 = 4
2 + 5 = 7
4 + 2 = 6
1 + 7 = 8
5 + 3 = 8

While this is very easy for the CPU to do and will happen in 0.01 of a millisecond, it will get really slow, really fast as the array grows to millions or even billions in length.

This is where a GPU comes in really handy. GPUs are great at doing repetative, simple tasks, like adding two numbers, but instead of doing them one at a time, a GPU splits the task and computes the entire resulting array in one go.

This is what the GPU function inside compute.metal looks like:

kernel void addition_compute_function(constant float *arr1        [[ buffer(0) ]],
                                      constant float *arr2        [[ buffer(1) ]],
                                      device   float *resultArray [[ buffer(2) ]],
                                      uint  index        [[ thread_position_in_grid ]]) {
    resultArray[index] = arr1[index] + arr2[index];
}

It takes in the two arrays and resulting array as parameters and also uint index [[ thread_position_in_grid ]] which is a thread specifcially assigned to perform the addition on index of the array

Results

My specs: M1 Max 10-core CPU, 24-core GPU, 32GB RAM

Array Size CPU Time (seconds) GPU Time (seconds)
5 0.00001 0.00070
100,000 0.03356 0.00707
1,000,000 0.33692 0.00990
50,000,000 16.86406 0.07883
100,000,000 33.44142 0.15101
500,000,000 N/A 0.80057
1,000,000,000 N/A 1.41739
1,700,000,000 N/A 24.19244

Note: Adding two 1.7 billion length arrays uses all 32GB of memory

This time is only for the adding portion of the function. The creation of arrays and populating them with random values also takes a considerable portion of time, and can also be accelerated by the GPU

...

You might also like...
Advanced Catalyst Example with sidebar, list view, SwiftUI detail view, toolbar & AppKit bundle
Advanced Catalyst Example with sidebar, list view, SwiftUI detail view, toolbar & AppKit bundle

Advanced Catalyst Example This is an example of a Catalyst app using a three-column layout, with a primary toolbar. It includes topics such as: Drag &

Example on how to print a NSTableView from your app but then also add text to the print-out.
Example on how to print a NSTableView from your app but then also add text to the print-out.

NSTableView Printing Test This is a demo project so you can check out how printing a table could work. The goal here is to show tabular data on screen

Example for RxFeedback

RxFeedback Sample This is a repository for the RxFeedback code sample. Running Install bazel (This project is tested with Bazel 4.0.0) bazel run //App

Example project guide you schedules multiple thread for network requests in RxSwift, which is optimize your app's performance better.
Example project guide you schedules multiple thread for network requests in RxSwift, which is optimize your app's performance better.

RxSwift-Multi-Threading-Example Example project guide you schedules multiple thread for network requests in RxSwift, which is optimize your app's perf

This is example project for my presentation in iOSDC JAPAN 2021

Swift PM Project Example This is example project for my presentation in iOSDC JAPAN 2021. Package.swift based project management Multi Modules Multi P

Example Xcode swift iOS project for Core Data + iCloud syncing
Example Xcode swift iOS project for Core Data + iCloud syncing

iCloudCoreDataStarter Hello, I'm Chad. For the last several months I have been working on Sticker Doodle, an app you should go download right now! In

A repository of example plugins for Delta Client

Example Plugins for Delta Client This repository contains a few example plugins to help developers get a practical understanding of how to use the plu

CustomPod Example Axon With Swift

CustomPod_Example_Axon Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements Installa

Example App for playing around with
Example App for playing around with

BookStore πŸ‘‰ ν•œκΈ€ 버전 See new releases and search for programming books from IT Bookstore API This is a sample app to practice using Result type, stubbin

Owner
Paul Serbanescu
Junior at Stuyvesant High School
Paul Serbanescu
Code Swift iOS app showcasing basic movies list from Orange TV API.

iOS Code Test - Optiva Media Code Swift iOS app showcasing basic movies list from Orange TV API. Built using XCode 13.0 (Swift 5) How to run the examp

Manu Martinez 1 Oct 17, 2021
A simple in iOS to demonstrate a basic MVVM architecture.

Repository A Simple iOS application in swift with MVVM-C architecture. Achitecture This project build on MVVM-C (Model, View, ViewModel, Coordinator)

Pankaj Kumar Jha 0 Oct 24, 2021
This is a basic twitter app to view, compose, favorite, and retweet tweets.

Twitter - Part II This is a basic twitter app to view, compose, favorite, and retweet tweets. Time spent: 6 hours spent in total User Stories The foll

Jose G Caudillo 0 Oct 31, 2021
IOS-Application-3 - A basic calculator app for iOS compatible to any layout and screen size

Calculator It is a basic calculator app for iOS compatible to any layout and scr

Kushal Shingote 1 Feb 2, 2022
An example app with using ShazamKit

ShazamKitExample An example app with using ShazamKit. Check out a live demo on Twitter. Related Resources Introducing ShazamKit WWDC21 β€” Explore Shaza

Artem Novichkov 10 May 25, 2022
An example project of using the new Character Controller component in RealityKit 2.0

CharacterController in RealityKit 2.0 An example project of using the new Character Controller component in RealityKit 2.0. Demo Tweet Usage Install a

Sai Kambampati 8 Nov 13, 2022
An example of adding a faux notch using AppKit + SwiftUI

faux-notch An example of adding a faux notch using AppKit + SwiftUI What is this? It's a bare-bones example that renders a fake MacBook notch in macOS

Tanner Stokes 2 Nov 14, 2021
Here there is a simple example using watchOS and SwiftUI

A Simple Demonstration Project using WatchOS + SwiftUI Description This project is a simple demonstration about how to create a WatchOS App using Swif

Leandro Alves da Silva 1 Jul 19, 2022
Todolist-swiftui - An example of using SwiftUI with CoreData

todolist-swiftui An example of using SwiftUI with CoreData Installation Install

null 0 Dec 30, 2021
SwiftUI Todo app example using a React/Redux monolithic state store with flux like dispatch/reduce actions

SwiftUI-Todo-Redux SwiftUI Todo Redux app example using a React/Redux monolithic state store with flux like dispatch/reduce actions Background SwiftUI

moflo 12 Nov 29, 2022