Re: Multithreading / Scalability



In article <CEBFf.1864$cm2.572@xxxxxxxxxxxx>,
Knute Johnson <nospam@xxxxxxxxxxxxxxxxx> wrote:

[ snip ]

I wrote a
couple of tests of my own to try and got more predictable results.
Although not quite.

The first program below times the calculations as yours did but I think
it is still not optimum for this test.

The second program calculates the number of calculation cycles per ms
which I think is more to the point. I got very similar results to the
first test though and that is that one thread is definitely slower than
two threads but more than that don't really improve performance. On my
single processor machine, every increase in threads reduced the number
of calculations that could be performed although again not as
dramatically as I expected with the increase in number of threads.

[ snip ]

Interesting second test program (test3 below). Some questions, though:

Why do you have this?

Thread.sleep(2000); // wait till all threads are created

This seems like an ugly hack to avoid writing proper code to wait
until the threads are created? ugly hacks are not terrible in
quick-and-dirty code, but still.

Also, "calculations" is shared among threads, but you're not ensuring
one-at-a-time access with "synchronized" or some other mechanism.
Why do you think this will work? You do declare it "volatile",
but as I understand it, this only ensures atomic loads and stores,
while you also have a "++calculations". I would have said this
was not guaranteed to be atomic on all processors. No?



import java.util.concurrent.*;

public class test3 implements Runnable {
volatile long calculations;
volatile boolean runFlag = true;
Object o = new Object();
Semaphore sem;

public test3(String[] args) {
int numberOfThreads = Integer.parseInt(args[0]);
Thread[] thread = new Thread[numberOfThreads];
sem = new Semaphore(numberOfThreads);
try {
sem.acquire(numberOfThreads);

for (int i=0; i<numberOfThreads; i++) {
thread[i] = new Thread(this);
thread[i].start();
}
Thread.sleep(2000); // wait till all threads are created

long then = System.currentTimeMillis();
sem.release(numberOfThreads);
Thread.sleep(20000);
runFlag = false;
long now = System.currentTimeMillis();

System.out.println(calculations/(now-then));
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}

public void run() {
try {
sem.acquire();
while (runFlag) {
double d = Math.sqrt(1234.56789);
double t = Math.tan(d);
++calculations;
}
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}

public static void main(String[] args) {
new test3(args);
}
}

--

Knute Johnson
email s/nospam/knute/


--
| B. L. Massingill
| ObDisclaimer: I don't speak for my employers; they return the favor.
.



Relevant Pages

  • Re: A "slanted edge" analysis program
    ... so film/sensor non-linearity and gamma adjustments can't influence the calculations. ... SNIP influence the MTF results, ... "for each line of pixels perpendicular to the edge, the edge is differentiated using the discrete derivative "-0,5; +0,5", meaning that the derivative value for pixel "X" is equal to -1/2 times the value of the pixel immediately to the left, plus 1/2 times the ... What's worse, the PSF can change throughout the image, but a symmetrical PSF will already allow to improve image quality. ...
    (comp.periphs.scanners)
  • Re: Strategy for revolutionizing math and physics
    ... [snip crap] ...    1) Calculate Mercury's perihelion precession over time. ... Unfortunately traditional calculations do NOT consider points to be ...
    (sci.physics)
  • Re: Cuba says United Nations accepts GDP formula - UN denies
    ... calculations of the communist-run country's Gross Domestic Product, ... they shouldn't just accept unsubstantiated propaganda figures. ... that isn't the issue, Dan, the issue is that these numbers are arbitrarily created by the Cuban regime and added without. ...
    (soc.culture.cuba)
  • Re: Myth about mathematicians and mental arithmetic
    ... I could manage multiple items, in the order of 10-15 different kinds, doing ... An interesting aspect of such calculations for me is that certain numbers are ... "nicer" in my mind and I have less problems calculating combos of them. ...
    (sci.math)
  • Re: Multithreading / Scalability
    ... Knute Johnson wrote: ... if they have an idea why it would do more calculations on a single processor machine with more threads. ... 1.6Ghz XP Home SP2 ...
    (comp.lang.java.programmer)