Created
December 1, 2012 07:16
-
-
Save pocketwalker/4180944 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <pthread.h> | |
#include <stdlib.h> | |
#define MAX_THREADS 512 | |
void *compute_pi (void *); | |
//.... | |
int main() { | |
//... | |
pthread_t p_threads[MAX_THREADS]; | |
pthread_attr_t attr; | |
pthread_attr_init (&attr); | |
for (i=0; i< num_threads; i++) { | |
hits[i] = i; | |
pthread_create(&p_threads[i], &attr, compute_pi, | |
(void *) &hits[i]); | |
} | |
for (i=0; i< num_threads; i++) { | |
pthread_join(p_threads[i], NULL); | |
total_hits += hits[i]; | |
} | |
//... | |
} | |
void *compute_pi (void *s) { | |
int seed, i, *hit_pointer; | |
double rand_no_x, rand_no_y; | |
int local_hits; | |
hit_pointer = (int *) s; | |
seed = *hit_pointer; | |
local_hits = 0; | |
for (i = 0; i < sample_points_per_thread; i++) { | |
rand_no_x =(double)(rand_r(&seed))/(double)((2<<14)-1); | |
rand_no_y =(double)(rand_r(&seed))/(double)((2<<14)-1); | |
if (((rand_no_x - 0.5) * (rand_no_x - 0.5) + | |
(rand_no_y - 0.5) * (rand_no_y - 0.5)) < 0.25) | |
local_hits ++; | |
seed *= i; | |
} | |
*hit_pointer = local_hits; | |
pthread_exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note the use of the function rand_r (instead of superior random
number generators such as drand48).
• Executing this on a 4-processor SGI Origin, we observe a 3.91 fold
speedup at 32 threads. This corresponds to a parallel efficiency of
0.98!
• We can also modify the program slightly to observe the effect of
false-sharing.
• The program can also be used to assess the secondary cache line
size.