Skip to content

Commit

Permalink
#10 - initial 64bit from #19
Browse files Browse the repository at this point in the history
  • Loading branch information
obriensystems committed Jan 2, 2025
1 parent ee71ca1 commit 341e710
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>dev.obrienlabs.performance</groupId>
<artifactId>performance-root</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>performance-nbi</artifactId>
<name>performance-nbi</name>
<description>Performance backend</description>
<packaging>jar</packaging>
<properties>
<java.version>21</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>21</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package dev.obrienlabs.performance.nbi;

import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
import java.util.stream.LongStream;

/**
* 20250101
* Michael O'Brien michael at obrienlabs.dev
*
* Architecture
* map the search space by interleaved UOW (1,3,5,7) - to 4 threads
* reduce the result by comparing thread local maximums
*
*/
public class Collatz {

private long secondsLast = System.currentTimeMillis();

private AtomicLong globalMaxValue = new AtomicLong(1L);
private AtomicLong globalMaxPath = new AtomicLong(1L);

public boolean isCollatzMax(long oddSearchCurrent, long secondsStart) {
boolean result = false;
//Long result = 0L;
long current = oddSearchCurrent;
long path = 0L;
long maxValue = 1L;

for (;;) {
if (current % 2 == 0) {
current = current >> 1;
} else {
/**
if even divide by 2, if odd multiply by 3 and add 1
or for odd numbers do 2 steps to optimize (n + n/2 + 1) - because we truncate divides
6% speed up for Java, 20% for C, 6% for Go
*/
current = (current >> 1) + current + 1L; // optimize
//current = (current << 1) + current + 1L
path++;
if (current > maxValue) { // check limits
maxValue = current;
}
}

path++;

// check completion of this number
if (current < 2L) {
// check limits
if (maxValue > globalMaxValue.get()) {
globalMaxValue.set(maxValue);
System.out.println("m0: " + oddSearchCurrent + " p: " + path + " m: " + (maxValue << 1) + " ms: "
+ (System.currentTimeMillis() - secondsLast) + " dur: " + ((System.currentTimeMillis() - secondsStart) / 1000));
secondsLast = System.currentTimeMillis();
result = true;
//result = Long.valueOf(current);
}
if (path > globalMaxPath.get()) {
globalMaxPath.set(maxValue);
System.out.println("mp: " + oddSearchCurrent + " p: " + path + " m: " + (maxValue << 1) + " ms: "
+ (System.currentTimeMillis() - secondsLast) + " dur: " + ((System.currentTimeMillis() - secondsStart) / 1000));
secondsLast = System.currentTimeMillis();
result = true;
//result = Long.valueOf(current);
}
break;
}
}
return result;
}

public void searchCollatzParallel(long oddSearchCurrent, long secondsStart) {
long batchBits = 12; // adjust this based on the chip architecture

long searchBits = 32;
long batches = 1 << batchBits;
long threadBits = searchBits - batchBits;
long threads = 1 << threadBits;

System.out.println("Searching: " + searchBits + " space, batch " + "0" + " of "
+ batches + " with " + threadBits +" bits of " + threads + " threads" );

for (long part = 0; part < (batches + 1) ; part++) {
// generate a limited collection (CopyOnWriteArrayList not required as r/o) for the search space - 32 is a good
List<Long> oddNumbers = LongStream
.range(1L + (part * threads), ((1 + part) * threads) - 1)
.filter(x -> x % 2 != 0) // TODO: find a way to avoid this filter using range above
.boxed()
.collect(Collectors.toList());

// filter on max value or path
List<Long> results = oddNumbers
.parallelStream()
.filter(num -> isCollatzMax(num.longValue(), secondsStart))
.collect(Collectors.toList());

results.stream().sorted().forEach(x -> System.out.println(x));
}
System.out.println("last number: " + ((1 + (batches) * threads) - 1));
}

public static void main(String[] args) {
System.out.println("Collatz multithreaded 2025 michael at obrienlabs.dev");
Collatz collatz = new Collatz();

//long oddSearchStart = 1L; // must be odd
//long oddSearchEnd = 4294967295L; //18446744073709551615 // must be odd
//long oddSearchIncrement = 2L;
long oddSearchCurrent = 1L;
long secondsStart = System.currentTimeMillis();

collatz.searchCollatzParallel(oddSearchCurrent, secondsStart);
/*for(oddSearchCurrent = oddSearchStart; oddSearchCurrent < oddSearchEnd; oddSearchCurrent += oddSearchIncrement) {
collatz.searchCollatzParallel(oddSearchCurrent, secondsStart);
}*/
System.out.println("completed: " + (System.currentTimeMillis() - secondsStart));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.obrienlabs.performance.nbi;

class CollatzTest {

//@Test
void contextLoads() {
}

}
44 changes: 44 additions & 0 deletions cpu/virtual/multithreaded/128bit/java-benchmark-128-cli/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2024 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>dev.obrienlabs.performance</groupId>
<artifactId>performance-root</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>performance-root</name>
<description>performance root project</description>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<!--configuration>
<source>21</source>
<target>21</target>
</configuration-->
</plugin>
</plugins>
</build>
<modules>
<module>performance-nbi</module>
</modules>
</project>

0 comments on commit 341e710

Please sign in to comment.