From 356d86e2a9ca4145db17cbe8e5ee3e671239075f Mon Sep 17 00:00:00 2001 From: nasr Date: Thu, 27 Nov 2025 19:30:44 +0100 Subject: refactor: refactored folder structures and packaging for better clarity --- src/Enums/Status.scala | 0 src/Main.scala | 30 ------- src/Ops/Prime.scala | 138 ------------------------------ src/Tests/Tests.scala | 28 ------ src/Tools/Benchmark.scala | 17 ---- src/Traits/Workload.scala | 7 -- src/main/scala/main/Main.scala | 22 +++++ src/main/scala/main/Ops/Ops.scala | 113 ++++++++++++++++++++++++ src/main/scala/main/Tests/Tests.scala | 18 ++++ src/main/scala/main/Tools/Benchmark.scala | 42 +++++++++ src/main/scala/main/Traits/Workload.scala | 13 +++ 11 files changed, 208 insertions(+), 220 deletions(-) delete mode 100644 src/Enums/Status.scala delete mode 100644 src/Main.scala delete mode 100644 src/Ops/Prime.scala delete mode 100644 src/Tests/Tests.scala delete mode 100644 src/Tools/Benchmark.scala delete mode 100644 src/Traits/Workload.scala create mode 100644 src/main/scala/main/Main.scala create mode 100644 src/main/scala/main/Ops/Ops.scala create mode 100644 src/main/scala/main/Tests/Tests.scala create mode 100644 src/main/scala/main/Tools/Benchmark.scala create mode 100644 src/main/scala/main/Traits/Workload.scala (limited to 'src') diff --git a/src/Enums/Status.scala b/src/Enums/Status.scala deleted file mode 100644 index e69de29..0000000 diff --git a/src/Main.scala b/src/Main.scala deleted file mode 100644 index 39851cb..0000000 --- a/src/Main.scala +++ /dev/null @@ -1,30 +0,0 @@ -package com.nsrddyn - -import com.nsrddyn.fpu.CholeskyDecomposition -import com.nsrddyn.Tests.CholeskyDecompositionTest -import java.time.Instant -import com.nsrddyn.alu.* -import com.nsrddyn.tools.Benchmark - -enum Status: - case PASS - case FAIL - - -object Torque extends ZIOAppDefault { - - println("hello world") - - @main def main(args: String*): Unit = { println("\u001b[2J\u001b[H") - println("--- TORQUE STRESS TESTING UTILITY ---") - - var tester: CholeskyDecompositionTest = new CholeskyDecompositionTest - println(tester.test()) - - } - - var p: Prime = new Prime - p.run() - -} - diff --git a/src/Ops/Prime.scala b/src/Ops/Prime.scala deleted file mode 100644 index bd93ee1..0000000 --- a/src/Ops/Prime.scala +++ /dev/null @@ -1,138 +0,0 @@ -package com.nsrddyn.ops -import com.nsrddyn.tools.Benchmark -import scala.util.hashing -import scala.util.hashing.MurmurHash3 -import com.nsrddyn.Traits.* -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 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) - - } -} - - -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/Tests/Tests.scala b/src/Tests/Tests.scala deleted file mode 100644 index 88fd139..0000000 --- a/src/Tests/Tests.scala +++ /dev/null @@ -1,28 +0,0 @@ -package com.nsrddyn.Tests - -import com.nsrddyn.fpu.CholeskyDecomposition -import scala.collection.immutable.ListSet -import zio._ - -class TestsRunner extends ZIOAppDefault { - - def run = - println("Hello world") - - -} - -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/Tools/Benchmark.scala b/src/Tools/Benchmark.scala deleted file mode 100644 index a0b388b..0000000 --- a/src/Tools/Benchmark.scala +++ /dev/null @@ -1,17 +0,0 @@ -package com.nsrddyn.tools - -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) -} diff --git a/src/Traits/Workload.scala b/src/Traits/Workload.scala deleted file mode 100644 index b547a6f..0000000 --- a/src/Traits/Workload.scala +++ /dev/null @@ -1,7 +0,0 @@ -package com.nsrddyn.Traits - -trait Workload { - - def name: String - def run: ZIO[Any, Nothing, Unit] -} 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] +} -- cgit v1.2.3-70-g09d2