The Q6600
Benchmarks Game

spectral-norm Swift #3 program

source code

/* The Computer Language Benchmarks Game
 https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
 contributed by Ralph Ganszky
 converted to Swift 3 by Daniel Muellenborn
 */

import Glibc
import Dispatch

// Get matrix dimension
let n: Int
if CommandLine.arguments.count > 1 {
   n = Int(CommandLine.arguments[1]) ?? 5500
} else {
   n = 5500
}

let queue = DispatchQueue.global(qos: .default)

func A(_ i: Int, _ j: Int) -> Double {
   return 1.0 / Double((i+j)*(i+j+1)/2 + (i+1))
}

func multiplyAv(_ n: Int, _ v: [Double]) -> [Double] {
   var Av = [Double](repeating: 0.0, count: n)
   DispatchQueue.concurrentPerform(iterations: n) { i in
      var AvTemp = 0.0
      for j in 0..<n {
         AvTemp += A(i,j) * v[j]
      }
      Av[i] = AvTemp      // This is unprotected and could cause
      // a problem. Maybe here some atomic update
      // or a semaphore should be used
   }
   return Av
}

func multiplyAtv(_ n: Int, _ v: [Double]) -> [Double] {
   var Atv = [Double](repeating: 0.0, count: n)
   DispatchQueue.concurrentPerform(iterations: n) { i in
      var AtvTemp = 0.0
      for j in 0..<n {
         AtvTemp += A(j,i) * v[j]
      }
      Atv[i] = AtvTemp
   }
   return Atv
}

func multiplyAtAv(_ n: Int, _ v: [Double]) -> [Double] {
   let u = multiplyAv(n, v)
   return multiplyAtv(n, u)
}

func approximate(_ n: Int) -> Double {
   var u = [Double](repeating: 1.0, count: n)
   var v = [Double]()
   for _ in 0..<10 {
      v = multiplyAtAv(n, u)
      u = multiplyAtAv(n, v)
   }
   var vBv = 0.0
   var vv = 0.0
   for i in 0..<n {
      vBv += u[i]*v[i]
      vv += v[i]*v[i]
   }
   return sqrt(vBv/vv)
}

func roundDouble(_ num: Double, precision: Int) -> String {
   let exponent = pow(10.0, Double(precision))
   let number = Double(Int(num * exponent + 0.5))/exponent
   return "\(number)"
}

print(roundDouble(approximate(n), precision: 9))
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
Swift version 5.1.3 (swift-5.1.3-RELEASE)
Target: x86_64-unknown-linux-gnu



Tue, 05 May 2020 21:13:22 GMT

MAKE:
/opt/src/swift-5.1.3-RELEASE-ubuntu18.04/usr/bin/swiftc spectralnorm.swift-3.swift -Ounchecked -target-cpu core2  -o spectralnorm.swift-3.swift_run
<unknown>:0: warning: argument unused during compilation: '-mcpu=core2'

12.83s to complete and log all make actions

COMMAND LINE:
./spectralnorm.swift-3.swift_run 5500

PROGRAM OUTPUT:
1.274224153