Post your speeds - Calculate 50 Million Factorials
Here are single-threaded and multithreaded versions of a simple C app I threw together last night that calculates a factorial 50 million times.
Right-click and save link to disk for both, then compile and run from the Terminal or paste into new projects in Project Builder. (To just read the code, left-click on the links)
My dual- 1 gHz G4 tower gets about 25 seconds with default gcc compiler settings and 12 seconds with compiler optimization set to the "-O3" flag.
Since this app uses 64-bit arithmetic, anyone with a G5 please let us see what kinds of times you are getting.
http://www.johnnylundy.com/unthreaded_factorial.c Single-Threaded; Right-Click and Download Link To Disk
http://www.johnnylundy.com/threaded_factorial.c Multi-Threaded; Right-Click and Download Link To Disk
Right-click and save link to disk for both, then compile and run from the Terminal or paste into new projects in Project Builder. (To just read the code, left-click on the links)
My dual- 1 gHz G4 tower gets about 25 seconds with default gcc compiler settings and 12 seconds with compiler optimization set to the "-O3" flag.
Since this app uses 64-bit arithmetic, anyone with a G5 please let us see what kinds of times you are getting.
http://www.johnnylundy.com/unthreaded_factorial.c Single-Threaded; Right-Click and Download Link To Disk
http://www.johnnylundy.com/threaded_factorial.c Multi-Threaded; Right-Click and Download Link To Disk
Comments
THREAD_COUNT should be 1, not 2, for the single-threaded version. Also, threads[i].numLoops should be set equal to ITERATIONS/THREAD_COUNT and not ITERATIONS/2.
With these changes:
Dual 2.8 GHz Xeon running Red Hat Linux 7.2
single-threaded 20 sec.
double-threaded 10 sec.
800 MHz Powerbook G4 (DVI)
single-threaded 28 sec.
AMD Athlon 1.4 GHz (Thunderbird)
single-threaded 21 sec.
-- Mark
Originally posted by mark_wilkins
You've got some problems with your code:
THREAD_COUNT should be 1, not 2, for the single-threaded version. Also, threads[i].numLoops should be set equal to ITERATIONS/THREAD_COUNT and not ITERATIONS/2.
-- Mark
Thanks for pointing that out - I uploaded the same file twice I think.
I'll see if I accidently overwrote the original.
I also found that running RC5 takes 40% of the processor(s) even though it is supposed to be zero-priority.
All G5 owners please feel free to participate.
Thanks...
Johnny
EDIT: Unthreaded version now uploaded properly. Link fixed.
Originally posted by Luca Rescigno
How do you run this in Terminal? I remember running a Perl program once, I just typed "perl (name of program)" and it ran. But I don't know what the command is for compiling C.
Multi-proc:
cc -O3 -lpthread -o threaded_factorial threaded_factorial.c
One proc:
cc -O3 -lpthread -o unthreaded_factorial unthreaded_factorial.c
-- Mark
Mac OS X 10.2.6
GCC version 1161, based on gcc version 3.1 20020420 (prerelease)
% gcc -O3 -o unthreaded_factorial unthreaded_factorial.c
% ./unthreaded_factorial
Start: 1063076495 End: 1063076565
i= 50000001
Time=70
% gcc -O3 -o threaded_factorial threaded_factorial.c
% ./threaded_factorial
Creating Thread Number: 0
Creating Thread Number: 1
Loop Done; Time=71 secs for thread#:0, Loops=25000000
Loop Done; Time=71 secs for thread#:1, Loops=25000000
Originally posted by Luca Rescigno
It didn't work. Says "cc: command not found"
You have to install the contents of the optional Developer Tools disk that came with Mac OS X (or your computer, if it came with OS X preinstalled.)
-- Mark
> cc -O3 -o unthreaded_factorial unthreaded_factorial.c
> ./unthreaded_factorial
Start: 1063079616 End: 1063079637
i= 50000001
Time=21
> cc -O3 -o threaded_factorial threaded_factorial.c
> ./threaded_factorial
Creating Thread Number: 0
Creating Thread Number: 1
Loop Done; Time=11 secs for thread#:0, Loops=25000000
Loop Done; Time=11 secs for thread#:1, Loops=25000000
> xlc -O5 -o unthreaded_factorial unthreaded_factorial.c
> ./unthreaded_factorial
Start: 1063079525 End: 1063079542
i= 50000001
Time=17
> /opt/ibmcmp/vac/6.0/bin/xlc -O5 -o threaded_factorial threaded_factorial.c
> ./threaded_factorial
Creating Thread Number: 0
Creating Thread Number: 1
Loop Done; Time=9 secs for thread#:1, Loops=25000000
Loop Done; Time=9 secs for thread#:0, Loops=25000000
Multithreaded:
Loop Done; Time=14 secs for thread#:1, Loops=25000000
Loop Done; Time=14 secs for thread#:0, Loops=25000000
Single threaded:
Time=27
14 and 27 seconds, not bad at all considering the age of my machine and the results of the competition above.
Originally posted by Luca Rescigno
Ah, well mine probably didn't come with DevTools installed. But I don't care enough to install them so I'll just leave it be
I'm in the same boat: I'm curious to see results from my machine, but I have no real need to install the Dev Tools. Though if anyone could get this running as some type of little app, I have a 1.8 GHz G5 all ready to be tested...
Originally posted by Gabid
I'm in the same boat: I'm curious to see results from my machine, but I have no real need to install the Dev Tools. Though if anyone could gte this running as some type of little app, I have a 1.8 GHz G5 all ready to be tested...
I'll put up a binary - I don't recall the G5-specific compiler flags though.
Anybody remember what those are?
EDIT: I think this zipped binary should download OK:
Right-click, save and unzip
Originally posted by lundy
I'll put up a binary - I don't recall the G5-specific compiler flags though.
Anybody remember what those are?
EDIT: I think this zipped binary should download OK:
Right-click, save and unzip
Ok. Got it. Now what? Unfortunately my knowledge of the Terminal stops at the "cd" command...and I think I can open things as well
./unthreaded_factorial
Start: 1063087627 End: 1063087840
i= 50000001
Time=213
./threaded_factorial
Creating Thread Number: 0
Creating Thread Number: 1
Loop Done; Time=107 secs for thread#:0, Loops=25000000
Loop Done; Time=103 secs for thread#:1, Loops=25000000
Oddly, top showed only one processor being used at once. The two threads did not end anywhere close to the same time.
Can we have a 4-proc version?
Creating Thread Number: 0
Creating Thread Number: 1
Loop Done; Time=29 secs for thread#:1, Loops=25000000
Loop Done; Time=29 secs for thread#:0, Loops=25000000
Originally posted by qazII
Sun Fire 880: 4x Sun UltraSPARC-III @ 750Mhz:
Oddly, top showed only one processor being used at once. The two threads did not end anywhere close to the same time.
Can we have a 4-proc version?
The output shows they were a few seconds apart. You mean the threads appeared to run sequentially? That could be - nondetached is assumed by this code, and isn't necessarily true for every C implementation.
To enable for 4 processors, find this code in the source:
#define THREAD_COUNT 2 //Two processors
#define ITERATIONS 50000000 //Change this to however many loops desired
ThreadInfo threads[THREAD_COUNT]; //Array of 2 thread info structures
int result, i;
// initialize the Thread Info for threads 0 and 1:
for (i = 0; i < THREAD_COUNT; i++) {
threads[i].index = i; //Set thread ID
threads[i].numLoops = ITERATIONS/2; //Divide by # CPUs
Change the 2 to a 4 in the first line
Change the "ITERATIONS/2" to "ITERATIONS/THREAD_COUNT" in the last line
Originally posted by Gabid
Ok. Got it. Now what? Unfortunately my knowledge of the Terminal stops at the "cd" command...and I think I can open things as well
Sorry. cd to the directory that the unzipped file is in, and type
./unthreaded_factorial
That should execute the binary, hopefully is not corrupted.
paul% gcc -O3 -lpthread -o threaded_factorial threaded_factorial.c
paul% ./threaded_factorial
Creating Thread Number: 0
Creating Thread Number: 1
Loop Done; Time=22 secs for thread#:1, Loops=25000000
Loop Done; Time=22 secs for thread#:0, Loops=25000000
paul% gcc -O3 -lpthread -o unthreaded unthreaded_factorial.c
paul% ./unthreaded
Start: 1063092344 End: 1063092367
i= 50000001
Time=23
paul% gcc -o unthreaded unthreaded_factorial.c[82-42-73-213:~/
paul% ./unthreaded
Start: 1063092384 End: 1063092430
i= 50000001
Time=46
Originally posted by lundy
I'll put up a binary - I don't recall the G5-specific compiler flags though.
Anybody remember what those are?
For GCC 3.3:
-mcpu=970
-mtune=970
-mpowerpc64
That's a good place to start.
Originally posted by lundy
Here are single-threaded and multithreaded versions of a simple C app I threw together last night that calculates a factorial 50 million times.
This is an interesting test. It would be, however, more interesting to see how the relative rating, between the several configurations, changes when you make your code a little more complicated. For example, how about calculating the factorials for all integers between 1 and, say, 20000?