Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collatz / hailstone numbers on various IA64, ARM64, ASM, C, ObjectiveC, Swift, Java, Python, Rust, Go, CUDA, Metal in GPU single, multi threaded and GPU SIMD environments #91

Open
obriensystems opened this issue Dec 29, 2024 · 1 comment
Assignees

Comments

@obriensystems
Copy link
Member Author

option 3: Java 8 lambda/streams parallelization

see ObrienlabsDev/performance#19

	public void searchCollatzParallel(long oddSearchCurrent, long secondsStart) {
		long batchBits = 5; // adjust this based on the chip architecture 
		long searchBits = 32;
		long batches = 1 << batchBits;
		long threadBits = searchBits - batchBits;
		long threads = 1 << threadBits;
		
		for (long part = 0; part < (batches + 1) ; part++) {	
			// generate a limited collection for the search space - 32 is a good
			System.out.println("Searching: " + searchBits + " space, batch " + part + " of " 
					+ batches + " with " + threadBits +" bits of " + threads + " threads"  );
			
			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());
			
			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));
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant