Thank you all. It runs beautifully...
I think...
Final code:
package benchmark;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class Run {
public static long score(long time, long ipers, long threads)
{
return (ipers/time)*threads;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
long lim = (long)4294967295.0;//upper limit of loop
JOptionPane.showMessageDialog(null, "Welcome to the 64-bit prime benchmark. It measures how efficiently your machine can run through the algorithm.");
long threads = Runtime.getRuntime().availableProcessors()*2;//calculates the number of threads to use.
JOptionPane.showMessageDialog(null, "It will run using " + threads + " threads.\n\nPress OK to build the threads.");
long tStart = System.currentTimeMillis();
ArrayList<Thread> th = new ArrayList<Thread>();//arraylist of threads
long inc = lim/threads, s = 2;
for(long i = 0; i < threads; i++)
{
th.add(new Algorithm(s,inc+s));//creates each thread and adds it to the array list
}
long tEnd = System.currentTimeMillis();
JOptionPane.showMessageDialog(null, "Threads built.\nTime: " + (tEnd-tStart) + " ms\n\nPress OK to run benchmark.");
long start = System.currentTimeMillis();//times the amount of time it takes
for(Thread t: th)
{
t.start();
}
for(Thread t : th)
{
try {
t.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
long end = System.currentTimeMillis();//end of the timer
JOptionPane.showMessageDialog(null,"Statistics:\n" +
"Time: " + (double)(end - start)/1000.0 + " ms.\n" +
"Total iterations: " + lim/2 + "\n" +
"Iterations per second: " + ((lim/2)/(end-start))*1000+"\n" +
"Number of threads: "+ th.size() +"\n" +
"Score (higher=better): " + score(end-start,((lim/2)/(end-start))*1000, threads));//prints some stats
System.out.println("Statistics:\n" +
"Time: " + (double)(end - start)/1000.0 + " ms.\n" +
"Total iterations: " + lim/2 + "\n" +
"Iterations per second: " + ((lim/2)/(end-start))*1000+"\n" +
"Number of threads: "+ th.size() +"\n" +
"Score (higher=better): " + score(end-start,((lim/2)/(end-start))*1000, threads));
System.exit(1);
}
}
Algorithm:
package benchmark;
import org.jscience.mathematics.number.LargeInteger;
public class Algorithm extends Thread{
/**
* @param args
*/
private LargeInteger val = LargeInteger.valueOf("18446744073709551557");//number being factored
private long start, end, increment = 2;//runtime variables that specify where to start, stop, and the increment for each number
/**
* @param start - value to start at
* @param end - value to end at
*/
public Algorithm(long start, long end)
{
this.start =start;
this.end = end;
}
public void run()
{
//System.out.println("Thread: " + threadNum + " starting. Thread ID: " + getId());
LargeInteger lim = LargeInteger.valueOf(end);//LargeInteger containing the end value
for(LargeInteger i = LargeInteger.valueOf(start); lim.isGreaterThan(i); i = i.plus(increment))//loop for primality check
{
if(val.mod(i).equals(0))//checks for accuracy of the thread
{
throw new RuntimeException("Inaccurate Process.");//throughs an exception if an error is made showing the value not prime.
}
}
}
}
The LargeInteger class was posted earlier in the thread.
Zip file containing the compiled program: