Skip to content

Commit

Permalink
feat: integrate bucket wise and precompute msm + UI redesign
Browse files Browse the repository at this point in the history
  • Loading branch information
FoodChain1028 committed Jul 24, 2024
1 parent 20fb4e9 commit 309acbf
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 126 deletions.
19 changes: 10 additions & 9 deletions gpu-acceleration-app/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

236 changes: 119 additions & 117 deletions gpu-acceleration-app/ios/gpu-acceleration/MSMBenchmarkView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,178 +6,180 @@
// Copyright © 2024 CocoaPods. All rights reserved.
//

import Foundation
import SwiftUI
import moproFFI


struct AlgorithmBenchmark {
struct AlgorithmBenchmark: Identifiable {
let id = UUID()
var algorithm: String
var avgMsmTime: Double
var diffWithBaseline: Double // Baseline is Arkwork Vanilla MSM
var diffWithBaseline: Double
}

struct MSMBenchmarkView: View {
@State private var selectedAlgorithms: Set<Int> = [0] // Default to select the baseline MSM algorithm
let algorithms = ["Arkwork (Baseline)", "Metal (GPU)"]
@State private var selectedAlgorithms: Set<String> = ["Arkwork (Baseline)", "Metal (GPU)"]
let algorithms = ["Arkwork (Baseline)", "Metal (GPU)", "Bucket Wise Msm", "Precompute Msm"]
@State private var benchmarkResults: [AlgorithmBenchmark] = []
@State private var isSubmitting: Bool = false

// setting up msm algorithm mapping
let msmBenchmarkMapping:
[ String: (
UInt32,
UInt32,
String
) throws -> BenchmarkResult] = [
let msmBenchmarkMapping: [String: (UInt32, UInt32, String) throws -> BenchmarkResult] = [
"Arkwork (Baseline)": arkworksPippenger,
"Metal (GPU)": metalMsm,
// "TrapdoorTech Zprize": trapdoortechZprizeMsm,
"Bucket Wise Msm": bucketWiseMsm,
"Precompute Msm": precomputeMsm
]

var body: some View {
NavigationView {
VStack {
// The MSM algorithm Lists
List {
Section(header: Text("Select MSM Algorithms")) {
ForEach(algorithms.indices, id: \.self) { index in
HStack {
Text("\(index + 1). \(algorithms[index])")
Spacer()
if selectedAlgorithms.contains(index) {
Image(systemName: "checkmark")
}
}
.onTapGesture {
// select the algorithms
if selectedAlgorithms.contains(index) {
selectedAlgorithms.remove(index)
} else {
selectedAlgorithms.insert(index)
ZStack {
Color.black.edgesIgnoringSafeArea(.all) // Set the background to gray

ScrollView {
VStack(spacing: 20) {
Text("MSM Benchmark")
.font(.title)
.fontWeight(.bold)
.foregroundColor(.white)

VStack(alignment: .leading, spacing: 10) {
Text("SELECT MSM ALGORITHMS")
.font(.subheadline)
.foregroundColor(.white)

VStack(spacing: 0) {
ForEach(algorithms, id: \.self) { algorithm in
Button(action: {
toggleAlgorithm(algorithm)
}) {
HStack {
Text(algorithm)
Spacer()
if selectedAlgorithms.contains(algorithm) {
Image(systemName: "checkmark")
}
}
.padding()
.background(selectedAlgorithms.contains(algorithm) ? Color.blue.opacity(0.3) : Color.gray.opacity(0.3))
.foregroundColor(.white)
}
.buttonStyle(PlainButtonStyle())
Divider().background(Color.white.opacity(0.3))
}
.foregroundColor(.black)
.listRowBackground(Color.white)
}
.background(Color.gray.opacity(0.2))
.cornerRadius(10)
}

// result lists
Section(header: Text("Benchmark Results")) {
// Adding titles to the table-like structure
HStack {
Text("Algorithm")
.bold()
.frame(width: 120, alignment: .leading)
Spacer()
Text("Avg Time (ms)")
.bold()
.frame(width: 120, alignment: .trailing)
Text("Diff(%)")
.bold()
.frame(width: 80, alignment: .trailing)
}
.foregroundColor(.white)
.listRowBackground(Color.gray)

VStack(alignment: .leading, spacing: 10) {
Text("BENCHMARK RESULTS")
.font(.subheadline)
.foregroundColor(.white)

// List of results
ForEach(benchmarkResults, id: \.algorithm) { result in
VStack(spacing: 0) {
HStack {
Text(result.algorithm)
.frame(width: 120, alignment: .leading)
Text("Algorithm")
Spacer()
Text("\(String(format: "%.2f", result.avgMsmTime))")
.frame(width: 120, alignment: .trailing)
Text("\(String(format: result.diffWithBaseline > 0 ? "+%.2f" : "%.2f", result.diffWithBaseline))")
.frame(width: 80, alignment: .trailing)
Text("Avg Time\n(ms)")
Text("Diff(%)")
.frame(width: 70, alignment: .trailing)
}
.padding()
.background(Color.gray.opacity(0.3))
.font(.subheadline)
.foregroundColor(.white)

if benchmarkResults.isEmpty {
Text("No results yet")
.padding()
.frame(maxWidth: .infinity, alignment: .center)
.foregroundColor(.white)
} else {
ForEach(benchmarkResults) { result in
HStack {
Text(result.algorithm)
Spacer()
Text(String(format: "%.2f", result.avgMsmTime))
.frame(width: 80, alignment: .trailing)
Text(String(format: "%.2f", result.diffWithBaseline))
.frame(width: 70, alignment: .trailing)
}
.padding()
.foregroundColor(.white)
if result.id != benchmarkResults.last?.id {
Divider().background(Color.white.opacity(0.3))
}
}
}
.foregroundColor(.black)
.listRowBackground(Color.white)
}
.background(Color.gray.opacity(0.2))
.cornerRadius(10)
}

Spacer()

Button(action: submitAction) {
Text("Generate Benchmarks")
.fontWeight(.semibold)
.foregroundColor(.white)
.frame(maxWidth: .infinity)
.padding()
.background(Color.blue)
.cornerRadius(10)
}
.disabled(isSubmitting || selectedAlgorithms.isEmpty)
}
.listStyle(DefaultListStyle())
.background(Color.black.edgesIgnoringSafeArea(.all))

Button("Generate Benchmarks") {
submitAction()
}

.padding()
.background(isSubmitting ? Color.gray : Color.blue)
.foregroundColor(.white)
.cornerRadius(5)
.disabled(isSubmitting)
}
.navigationBarTitle("MSM Benchmark", displayMode: .inline)
.navigationBarHidden(false)
}
}

func toggleAlgorithm(_ algorithm: String) {
if selectedAlgorithms.contains(algorithm) {
selectedAlgorithms.remove(algorithm)
} else {
selectedAlgorithms.insert(algorithm)
}
}

func submitAction() {
let instanceSize: UInt32 = 10;
let numInstance: UInt32 = 5;
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let documentsPath = documentsUrl.path
let instanceSize: UInt32 = 10
let numInstance: UInt32 = 5
let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.path

isSubmitting = true
benchmarkResults.removeAll()

// File deletion logic
let fileManager = FileManager.default
if let documentsUrl = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first {
let fileURL = documentsUrl.appendingPathComponent("yourFileName.txt") // Replace with your actual file name
if fileManager.fileExists(atPath: fileURL.path) {
do {
try fileManager.removeItem(at: fileURL)
print("File deleted successfully.")
} catch {
print("Could not delete the file: \(error)")
}
} else {
print("File does not exist.")
}
}
print("Selected algorithms: \(selectedAlgorithms.map { algorithms[$0] })")
DispatchQueue.global(qos: .userInitiated).async {
var tempResults: [AlgorithmBenchmark] = []
var baselineTiming: Double = 0.0

for index in self.selectedAlgorithms.sorted() {
let algorithm = self.algorithms[index]

if let benchmarkFunction = self.msmBenchmarkMapping[algorithm] {
for algorithm in selectedAlgorithms.sorted() {
if let benchmarkFunction = msmBenchmarkMapping[algorithm] {
do {
print("Running MSM in algorithm: \(algorithm)...")
let benchData: BenchmarkResult =
try benchmarkFunction(
instanceSize,
numInstance,
documentsPath
)
let benchData = try benchmarkFunction(instanceSize, numInstance, documentsPath)

if algorithm == "Arkwork (Baseline)" {
baselineTiming = benchData.avgProcessingTime
}

let algorithmBenchmark = AlgorithmBenchmark(algorithm: algorithm, avgMsmTime: benchData.avgProcessingTime, diffWithBaseline: (baselineTiming - benchData.avgProcessingTime) / baselineTiming * 100
let diff = algorithm == "Arkwork (Baseline)" ? 0.0 :
(baselineTiming - benchData.avgProcessingTime) / baselineTiming * 100

let algorithmBenchmark = AlgorithmBenchmark(
algorithm: algorithm,
avgMsmTime: benchData.avgProcessingTime,
diffWithBaseline: diff
)
tempResults.append(algorithmBenchmark)
print("Result of \(algorithmBenchmark.algorithm): \n \(algorithmBenchmark.avgMsmTime) ms (diff: \(algorithmBenchmark.diffWithBaseline) %)"
)
} catch {
print("Error running benchmark: \(error)")
print("Error running benchmark for \(algorithm): \(error)")
}
} else {
print("No benchmark function found for \(algorithm)")
tempResults.append(AlgorithmBenchmark(algorithm: algorithm, avgMsmTime: Double.nan, diffWithBaseline: Double.nan))
}
}

DispatchQueue.main.async {
self.benchmarkResults = tempResults
self.isSubmitting = false
benchmarkResults = tempResults
isSubmitting = false
}
}
}
}

0 comments on commit 309acbf

Please sign in to comment.