Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Multithreaded arithmetic benchmark  (Read 929 times)
0 Members and 1 Guest are viewing this topic.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 290
Exp: 5 years



« Posted 2014-03-17 20:36:36 »

Was curious about scalability/practicality of threading arithmetic over large arrays.
Put what ever you want in op() to measure your own system! Post results!

Also tell me if I did anything dumb in the bench code, wanna make sure I'm doing that right.  Pointing

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
import java.util.*;
import java.util.concurrent.*;

public class ArrayThreadingTest {
   
   public static void main(String[] args) throws InterruptedException {
      int numCores = Runtime.getRuntime().availableProcessors();
      ExecutorService exec = Executors.newFixedThreadPool(numCores);
     
      System.out.println("Warming up... " + numCores + " cores detected");
      for (int n = 0; n < 2; n++)
         for (int i = 3; i <= 7; i++) {
            final int numThreads = numCores;
            final int testSize = (int) Math.pow(10, i);
            final int[] a = new int[testSize];
            final int[] b = new int[testSize];
            final int[] c = new int[testSize];
           
            Arrays.fill(a, 56456810);
            Arrays.fill(b, 74779);
           
            List<Callable<Void>> tasks = new ArrayList<Callable<Void>>();
            for (int y = 0; y < numThreads; y++) {
               final int h = y;
               tasks.add(new Callable<Void>() {
                  @Override
                  public Void call() throws Exception {
                     for (int z = h * testSize / numThreads; z < (h + 1) * testSize / numThreads; z++) {
                        c[z] = op(a[z], b[z]);
                     }
                     return null;
                  }
               });
            }
            exec.invokeAll(tasks);
         }
     
      System.out.println("Performing tests...\n");
     
      for (int n = 1; n <= numCores; n++) {
         for (int i = 3; i <= 8; i++) {
            final int numThreads = n;
            exec.shutdownNow();
            exec.awaitTermination(1, TimeUnit.SECONDS);
            exec = Executors.newFixedThreadPool(numThreads);
            final int testSize = (int) Math.pow(10, i);
            final int[] a = new int[testSize];
            final int[] b = new int[testSize];
            final int[] c = new int[testSize];
           
            Arrays.fill(a, 56456810);
            Arrays.fill(b, 74779);
           
            List<Callable<Void>> tasks = new ArrayList<Callable<Void>>();
            for (int y = 0; y < numThreads; y++) {
               final int h = y;
               tasks.add(new Callable<Void>() {
                  @Override
                  public Void call() {
                     for (int z = h * testSize / numThreads; z < (h + 1) * testSize / numThreads; z++) {
                        c[z] = op(a[z], b[z]);
                     }
                     return null;
                  }
               });
            }
           
            long time = System.nanoTime();
           
            exec.invokeAll(tasks);
           
            time = System.nanoTime() - time;
           
            int errors = 0;
            final int desiredResult = op(56456810, 74779);
            for (int u = 0; u < c.length; u++) {
               if (c[u] != desiredResult) errors++;
            }
            if (errors > 0) System.out.println("\t" + errors * 100. / testSize + "% errors!");
            double nsPerOp = (double) time / testSize;
            double opsPerSecondPerThread = testSize / (time / 1000000000d) / numThreads;
            System.out.printf("Finished: %d Thread" + (numThreads > 1 ? "s" : "") + " performing %s operations at %.2f ns/op (%dK op/s/thread)\n", numThreads, 10 + "^" +(int) Math.log10(testSize), nsPerOp, (int) (opsPerSecondPerThread / 1000));
         }
         System.out.println();
      }
      System.out.println("All tests finished, shutting down...");
      exec.shutdownNow();
      exec.awaitTermination(3, TimeUnit.SECONDS);
   }
   
   //give your ALU a workout!
   //default: testing small loop optimization and integer arithmetic
   private static final int op(int a, int b) {
      int r = 0;
      for (int i = 1; i <= 10; i++)
         r += ((a + b) * (a / b + b + a) & i % b << a / b + a - 7 * b + (a - b + a * b ^ b & a)) % (a * 7 + b * (b / a + 5 ^ a * b) & b - a) * 8 + (i + 1) / a * a ^ b * a << b / b >> a;
      return r;
   }
}


Yields this on an AMD Phenom II x4 955 (3.4Ghz quad), Java 7:

Quote
Warming up... 4 cores detected
Performing tests...

Finished: 1 Thread performing 10^3 operations at 645.33 ns/op (1549K op/s/thread)
Finished: 1 Thread performing 10^4 operations at 452.42 ns/op (2210K op/s/thread)
Finished: 1 Thread performing 10^5 operations at 383.04 ns/op (2610K op/s/thread)
Finished: 1 Thread performing 10^6 operations at 375.21 ns/op (2665K op/s/thread)
Finished: 1 Thread performing 10^7 operations at 376.13 ns/op (2658K op/s/thread)
Finished: 1 Thread performing 10^8 operations at 377.05 ns/op (2652K op/s/thread)

Finished: 2 Threads performing 10^3 operations at 528.98 ns/op (945K op/s/thread)
Finished: 2 Threads performing 10^4 operations at 213.94 ns/op (2337K op/s/thread)
Finished: 2 Threads performing 10^5 operations at 189.71 ns/op (2635K op/s/thread)
Finished: 2 Threads performing 10^6 operations at 189.00 ns/op (2645K op/s/thread)
Finished: 2 Threads performing 10^7 operations at 188.49 ns/op (2652K op/s/thread)
Finished: 2 Threads performing 10^8 operations at 188.90 ns/op (2646K op/s/thread)

Finished: 3 Threads performing 10^3 operations at 579.33 ns/op (575K op/s/thread)
Finished: 3 Threads performing 10^4 operations at 157.18 ns/op (2120K op/s/thread)
Finished: 3 Threads performing 10^5 operations at 128.45 ns/op (2595K op/s/thread)
Finished: 3 Threads performing 10^6 operations at 125.93 ns/op (2647K op/s/thread)
Finished: 3 Threads performing 10^7 operations at 125.51 ns/op (2655K op/s/thread)
Finished: 3 Threads performing 10^8 operations at 125.86 ns/op (2648K op/s/thread)

Finished: 4 Threads performing 10^3 operations at 681.02 ns/op (367K op/s/thread)
Finished: 4 Threads performing 10^4 operations at 133.96 ns/op (1866K op/s/thread)
Finished: 4 Threads performing 10^5 operations at 97.46 ns/op (2565K op/s/thread)
Finished: 4 Threads performing 10^6 operations at 158.06 ns/op (1581K op/s/thread)
Finished: 4 Threads performing 10^7 operations at 106.08 ns/op (2356K op/s/thread)
Finished: 4 Threads performing 10^8 operations at 96.27 ns/op (2596K op/s/thread)

All tests finished, shutting down...
Online Spasi
« Reply #1 - Posted 2014-03-17 21:53:43 »

I highly recommend JMH for all kinds of benchmarking, for many important reasons.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 290
Exp: 5 years



« Reply #2 - Posted 2014-03-17 22:13:56 »

I highly recommend JMH for all kinds of benchmarking, for many important reasons.

Ah, thanks for that, it looks impressive. I should really pay more attention to OpenJDK projects.

I realize it's very difficult to make a good microbenchmark in Java due to the unpredictable nature of the JIT, but wanted to attempt it anyway, taking any results with a grain or five of salt, plus give anyone here an example snippet for simple multithreaded number crunching, though I don't claim to have the best methodology. Lately I've seen too many posts about "should I multithread blah," quickly followed by "why is stuff broken?"
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (36 views)
2014-12-15 09:26:44

Mr.CodeIt (29 views)
2014-12-14 19:50:38

BurntPizza (61 views)
2014-12-09 22:41:13

BurntPizza (98 views)
2014-12-08 04:46:31

JscottyBieshaar (58 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (76 views)
2014-11-29 21:32:03

toopeicgaming1999 (137 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (37 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!