diff options
| author | nasr <nsrddyn@gmail.com> | 2025-11-27 19:30:44 +0100 |
|---|---|---|
| committer | nasr <nsrddyn@gmail.com> | 2025-11-27 19:30:44 +0100 |
| commit | 356d86e2a9ca4145db17cbe8e5ee3e671239075f (patch) | |
| tree | c31f143afd5e562072869c21d14e81d5377d697c /src/main/scala | |
| parent | 5c90505fe7b6566049bead5e36a5e3f73d844413 (diff) | |
refactor: refactored folder structures and packaging for better clarity
Diffstat (limited to 'src/main/scala')
| -rw-r--r-- | src/main/scala/main/Main.scala | 22 | ||||
| -rw-r--r-- | src/main/scala/main/Ops/Ops.scala | 113 | ||||
| -rw-r--r-- | src/main/scala/main/Tests/Tests.scala | 18 | ||||
| -rw-r--r-- | src/main/scala/main/Tools/Benchmark.scala | 42 | ||||
| -rw-r--r-- | src/main/scala/main/Traits/Workload.scala | 13 |
5 files changed, 208 insertions, 0 deletions
diff --git a/src/main/scala/main/Main.scala b/src/main/scala/main/Main.scala new file mode 100644 index 0000000..d03eeb9 --- /dev/null +++ b/src/main/scala/main/Main.scala @@ -0,0 +1,22 @@ +package main + +import Ops.* +import Tests.* +import tools.* +import java.time.Instant + +object Torque { + + @main + def main(args: String*): Unit = { println("\u001b[2J\u001b[H") + println("--- TORQUE STRESS TESTING UTILITY ---") + + var cdt: CholeskyDecompositionTest = new CholeskyDecompositionTest + // returns an out of bounds error + // println(cdt.test()) + var p: Prime = new Prime + p.run(1000000000, true) + + } +} + diff --git a/src/main/scala/main/Ops/Ops.scala b/src/main/scala/main/Ops/Ops.scala new file mode 100644 index 0000000..6db51a9 --- /dev/null +++ b/src/main/scala/main/Ops/Ops.scala @@ -0,0 +1,113 @@ +package main.Ops + +import main.tools.Benchmark +import main.Traits.* + +import scala.util.hashing +import scala.util.hashing.MurmurHash3 +import scala.math._ +import scala.collection.immutable.ListSet +import scala.collection.mutable.ArrayBuffer + + +class Prime() { + + /* + * Calculate all primes up to limit + * This should stress the ALU in someway, + * doing this in a predictable manner, + * will hopefully keep the cpu pipeline busy + * and that way stress the branch predictor + * + * math.sqrt(n) => a prime number has 2 factors, one of the factors + * of the prime numbers has to be smaller then n + * after that we check if the number is whole number and thereby checking if its a prime + * + */ + + + /* + * TODO: I did the countrary of what i wanted to accieve with the is prime function + * We want the function to be less optimized so that the CPU has more work == more stress + */ + + + def isPrime(n: Int): Boolean = { + if n <= 1 then false + else !(2 to math.sqrt(n).toInt).exists(i => n % i == 0) + + + } + + def run(n: Int, result: Boolean): Unit = { + + for i <- 0 to n do if isPrime(i) == result then println("true") else println("false") + } +} + +class Hash { + + def run(word: String, loopSize: Int): Unit = { + + /* TODO: implement ALU friendly, so high speed hashing + * to continuously loop over voor stressing + * ALU + * + * While looking for hashing algorithmes to implement I stumbled on: + * https://scala-lang.org/api/3.x/scala/util/hashing/MurmurHash3$.html + * + * which is an implemntation of **smasher** http://github.com/aappleby/smhasher + * the exact type of hashing algorithm I was looking for + * + * In the scala description they state: "This algorithm is designed to generate + * well-distributed non-cryptographic hashes. It is designed to hash data in 32 bit chunks (ints). " + * + * (ints) -> ALU + * + */ + + for i <- 0 to loopSize do MurmurHash3.stringHash(word) + + } +} + +class CholeskyDecomposition { + + /* + * Floating point operation to stress the cpu + * Calculate the number of KFLOPS / FLOPS + * implementation of the Cholesky decomposition + * More information on the Cholesky decomposition at: + * https://en.wikipedia.org/wiki/Cholesky_decomposition + * + * Linpack uses the cholesky decomposition + * https://www.netlib.org/linpack/ + * + * https://www.geeksforgeeks.org/dsa/cholesky-decomposition-matrix-decomposition/ + * + * The Cholesky decomposition maps matrix A into the product of A = L ยท LH where L is the lower triangular matrix and LH is the transposed, + * complex conjugate or Hermitian, and therefore of upper triangular form (Fig. 13.6). + * This is true because of the special case of A being a square, conjugate symmetric matrix. + */ + + def run(matrix: Vector[Vector[Int]]): Unit = { + + val size: Int = matrix.size + val lower: ArrayBuffer[ArrayBuffer[Int]] = ArrayBuffer[ArrayBuffer[Int]]() + + for + i <- 0 to size + j <- 0 until i + do + if i == j then lower(i)(j) = getSquaredSummation(lower, i, j, matrix) else lower(j)(j) = getReversedSummation(lower, i, j, matrix) + + } + + private def getReversedSummation(lower: ArrayBuffer[ArrayBuffer[Int]], i: Int, j: Int, matrix: Vector[Vector[Int]]) = { + math.sqrt(matrix(j)(j) - (0 until j).map { k => lower(i)(k) * lower(j)(k) }.sum).toInt + } + private def getSquaredSummation(lower: ArrayBuffer[ArrayBuffer[Int]], i: Int, j: Int, matrix: Vector[Vector[Int]]) = { + ((matrix(i)(j) - (0 until j).map { k => math.pow(lower(j)(k), 2)}.sum) / lower(j)(j)).toInt + } +} + diff --git a/src/main/scala/main/Tests/Tests.scala b/src/main/scala/main/Tests/Tests.scala new file mode 100644 index 0000000..851ac4d --- /dev/null +++ b/src/main/scala/main/Tests/Tests.scala @@ -0,0 +1,18 @@ +package main.Tests + +import scala.collection.immutable.ListSet +import zio._ +import main.Ops.CholeskyDecomposition + + +class CholeskyDecompositionTest { + + def test(): Unit = { + + val cdp: CholeskyDecomposition = new CholeskyDecomposition + val matrix: Vector[Vector[Int]] = Vector(Vector(1,2,3),Vector(1,2,3),Vector(1,2,3)) + + println(cdp.run(matrix)) + + } +} diff --git a/src/main/scala/main/Tools/Benchmark.scala b/src/main/scala/main/Tools/Benchmark.scala new file mode 100644 index 0000000..eb1a6d4 --- /dev/null +++ b/src/main/scala/main/Tools/Benchmark.scala @@ -0,0 +1,42 @@ +package main.tools + +import main.Ops.* + +class Benchmark { + /* + * Calculate the time between the start of the execution of the function and the end + * */ + def measureTime(work: => Unit): Long = { + + val start = System.nanoTime() + work + val end = System.nanoTime() + end - start + } + + // TODO: map this to an actual precision value + def measurePrecision(work: => Boolean, expectedResult: Boolean): Unit = if work == expectedResult then println(true) else println(false) +} + + +class PrimeRunner { + + + def run(threads: Int): Unit = { + + val pr = new Prime() + val br = new Benchmark() + + /* + * test cases + * + * 7919 true + * 2147483647 false + */ + + val time = pr.run(7919, true) + println(time) + + } +} + diff --git a/src/main/scala/main/Traits/Workload.scala b/src/main/scala/main/Traits/Workload.scala new file mode 100644 index 0000000..5fd8527 --- /dev/null +++ b/src/main/scala/main/Traits/Workload.scala @@ -0,0 +1,13 @@ +package main.Traits + +import zio._ + +enum Status: + case PASS + case FAIL + +trait Workload { + + def name: String + def run: ZIO[Any, Nothing, Unit] +} |
