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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 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
|
// Radix2CooleyTukey benchmark
//
// Originally written by @owensd. Used with his permission.
#if canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#elseif os(Windows)
import MSVCRT
#else
import Darwin
#endif
import TestsUtils
public let benchmarks = [
BenchmarkInfo(
name: "Radix2CooleyTukey",
runFunction: run_Radix2CooleyTukey,
tags: [.validation, .algorithm],
setUpFunction: setUpRadix2CooleyTukey,
tearDownFunction: tearDownRadix2CooleyTukey,
legacyFactor: 48),
BenchmarkInfo(
name: "Radix2CooleyTukeyf",
runFunction: run_Radix2CooleyTukeyf,
tags: [.validation, .algorithm],
setUpFunction: setUpRadix2CooleyTukeyf,
tearDownFunction: tearDownRadix2CooleyTukeyf,
legacyFactor: 48),
]
//===----------------------------------------------------------------------===//
// Double Benchmark
//===----------------------------------------------------------------------===//
var double_input_real: UnsafeMutablePointer<Double>?
var double_input_imag: UnsafeMutablePointer<Double>?
var double_output_real: UnsafeMutablePointer<Double>?
var double_output_imag: UnsafeMutablePointer<Double>?
var double_temp_real: UnsafeMutablePointer<Double>?
var double_temp_imag: UnsafeMutablePointer<Double>?
let doubleN = 2_048
let doubleSize = { MemoryLayout<Double>.size * doubleN }()
func setUpRadix2CooleyTukey() {
let size = doubleSize
double_input_real = UnsafeMutablePointer<Double>.allocate(capacity: size)
double_input_imag = UnsafeMutablePointer<Double>.allocate(capacity: size)
double_output_real = UnsafeMutablePointer<Double>.allocate(capacity: size)
double_output_imag = UnsafeMutablePointer<Double>.allocate(capacity: size)
double_temp_real = UnsafeMutablePointer<Double>.allocate(capacity: size)
double_temp_imag = UnsafeMutablePointer<Double>.allocate(capacity: size)
}
func tearDownRadix2CooleyTukey() {
double_input_real?.deallocate()
double_input_imag?.deallocate()
double_output_real?.deallocate()
double_output_imag?.deallocate()
double_temp_real?.deallocate()
double_temp_imag?.deallocate()
}
func radix2CooleyTukey(_ level: Int,
input_real: UnsafeMutablePointer<Double>,
input_imag: UnsafeMutablePointer<Double>,
stride: Int, output_real: UnsafeMutablePointer<Double>,
output_imag: UnsafeMutablePointer<Double>,
temp_real: UnsafeMutablePointer<Double>,
temp_imag: UnsafeMutablePointer<Double>) {
if level == 0 {
output_real[0] = input_real[0];
output_imag[0] = input_imag[0];
return
}
let length = 1 << level
let half = length >> 1
radix2CooleyTukey(level - 1,
input_real: input_real,
input_imag: input_imag,
stride: stride << 1,
output_real: temp_real,
output_imag: temp_imag,
temp_real: output_real,
temp_imag: output_imag)
radix2CooleyTukey(level - 1,
input_real: input_real + stride,
input_imag: input_imag + stride,
stride: stride << 1,
output_real: temp_real + half,
output_imag: temp_imag + half,
temp_real: output_real + half,
temp_imag: output_imag + half)
for idx in 0..<half {
let angle = -Double.pi * Double(idx) / Double(half)
let _cos = cos(angle)
let _sin = sin(angle)
output_real[idx] = temp_real[idx] + _cos *
temp_real[idx + half] - _sin * temp_imag[idx + half]
output_imag[idx] = temp_imag[idx] + _cos *
temp_imag[idx + half] + _sin *
temp_real[idx + half]
output_real[idx + half] = temp_real[idx] - _cos *
temp_real[idx + half] + _sin *
temp_imag[idx + half]
output_imag[idx + half] = temp_imag[idx] - _cos *
temp_imag[idx + half] - _sin *
temp_real[idx + half]
}
}
func testDouble(iter: Int) {
let stride = 1
let size = doubleSize
let level = Int(log2(Double(doubleN)))
let input_real = double_input_real.unsafelyUnwrapped
let input_imag = double_input_imag.unsafelyUnwrapped
let output_real = double_output_real.unsafelyUnwrapped
let output_imag = double_output_imag.unsafelyUnwrapped
let temp_real = double_temp_real.unsafelyUnwrapped
let temp_imag = double_temp_imag.unsafelyUnwrapped
for _ in 0..<iter {
memset(UnsafeMutableRawPointer(input_real), 0, size)
memset(UnsafeMutableRawPointer(input_imag), 0, size)
memset(UnsafeMutableRawPointer(output_real), 0, size)
memset(UnsafeMutableRawPointer(output_imag), 0, size)
memset(UnsafeMutableRawPointer(temp_real), 0, size)
memset(UnsafeMutableRawPointer(temp_imag), 0, size)
radix2CooleyTukey(level,
input_real: input_real,
input_imag: input_imag,
stride: stride,
output_real: output_real,
output_imag: output_imag,
temp_real: temp_real,
temp_imag: temp_imag)
}
}
@inline(never)
public func run_Radix2CooleyTukey(_ n: Int) {
testDouble(iter: n)
}
//===----------------------------------------------------------------------===//
// Float Benchmark
//===----------------------------------------------------------------------===//
let floatN = 2_048
let floatSize = { MemoryLayout<Float>.size * floatN }()
var float_input_real: UnsafeMutablePointer<Float>?
var float_input_imag: UnsafeMutablePointer<Float>?
var float_output_real: UnsafeMutablePointer<Float>?
var float_output_imag: UnsafeMutablePointer<Float>?
var float_temp_real: UnsafeMutablePointer<Float>?
var float_temp_imag: UnsafeMutablePointer<Float>?
func setUpRadix2CooleyTukeyf() {
let size = floatSize
float_input_real = UnsafeMutablePointer<Float>.allocate(capacity: size)
float_input_imag = UnsafeMutablePointer<Float>.allocate(capacity: size)
float_output_real = UnsafeMutablePointer<Float>.allocate(capacity: size)
float_output_imag = UnsafeMutablePointer<Float>.allocate(capacity: size)
float_temp_real = UnsafeMutablePointer<Float>.allocate(capacity: size)
float_temp_imag = UnsafeMutablePointer<Float>.allocate(capacity: size)
}
func tearDownRadix2CooleyTukeyf() {
float_input_real?.deallocate()
float_input_imag?.deallocate()
float_output_real?.deallocate()
float_output_imag?.deallocate()
float_temp_real?.deallocate()
float_temp_imag?.deallocate()
}
func radix2CooleyTukeyf(_ level: Int,
input_real: UnsafeMutablePointer<Float>,
input_imag: UnsafeMutablePointer<Float>,
stride: Int, output_real: UnsafeMutablePointer<Float>,
output_imag: UnsafeMutablePointer<Float>,
temp_real: UnsafeMutablePointer<Float>,
temp_imag: UnsafeMutablePointer<Float>) {
if level == 0 {
output_real[0] = input_real[0];
output_imag[0] = input_imag[0];
return
}
let length = 1 << level
let half = length >> 1
radix2CooleyTukeyf(level - 1,
input_real: input_real,
input_imag: input_imag,
stride: stride << 1,
output_real: temp_real,
output_imag: temp_imag,
temp_real: output_real,
temp_imag: output_imag)
radix2CooleyTukeyf(level - 1,
input_real: input_real + stride,
input_imag: input_imag + stride,
stride: stride << 1,
output_real: temp_real + half,
output_imag: temp_imag + half,
temp_real: output_real + half,
temp_imag: output_imag + half)
for idx in 0..<half {
let angle = -Float.pi * Float(idx) / Float(half)
let _cos = cosf(angle)
let _sin = sinf(angle)
output_real[idx] = temp_real[idx] + _cos *
temp_real[idx + half] - _sin * temp_imag[idx + half]
output_imag[idx] = temp_imag[idx] + _cos *
temp_imag[idx + half] + _sin * temp_real[idx + half]
output_real[idx + half] = temp_real[idx] - _cos *
temp_real[idx + half] + _sin *
temp_imag[idx + half]
output_imag[idx + half] = temp_imag[idx] - _cos *
temp_imag[idx + half] - _sin *
temp_real[idx + half]
}
}
func testFloat(iter: Int) {
let stride = 1
let n = floatN
let size = floatSize
let input_real = float_input_real.unsafelyUnwrapped
let input_imag = float_input_imag.unsafelyUnwrapped
let output_real = float_output_real.unsafelyUnwrapped
let output_imag = float_output_imag.unsafelyUnwrapped
let temp_real = float_temp_real.unsafelyUnwrapped
let temp_imag = float_temp_imag.unsafelyUnwrapped
let level = Int(log2(Float(n)))
for _ in 0..<iter {
memset(UnsafeMutableRawPointer(input_real), 0, size)
memset(UnsafeMutableRawPointer(input_imag), 0, size)
memset(UnsafeMutableRawPointer(output_real), 0, size)
memset(UnsafeMutableRawPointer(output_imag), 0, size)
memset(UnsafeMutableRawPointer(temp_real), 0, size)
memset(UnsafeMutableRawPointer(temp_imag), 0, size)
radix2CooleyTukeyf(level,
input_real: input_real,
input_imag: input_imag,
stride: stride,
output_real: output_real,
output_imag: output_imag,
temp_real: temp_real,
temp_imag: temp_imag)
}
}
@inline(never)
public func run_Radix2CooleyTukeyf(_ n: Int) {
testFloat(iter: n)
}
|