root / Pi / C / Pthreads / PiMP.c
Historique | Voir | Annoter | Télécharger (3,93 ko)
| 1 |
/******************************************************************************
|
|---|---|
| 2 |
* FILE: hello.c
|
| 3 |
* DESCRIPTION:
|
| 4 |
* A "hello world" Pthreads program. Demonstrates thread creation and
|
| 5 |
* termination.
|
| 6 |
* AUTHOR: Blaise Barney
|
| 7 |
* LAST REVISED: 08/09/11
|
| 8 |
******************************************************************************/
|
| 9 |
#include <pthread.h> |
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <math.h> |
| 13 |
#include <stdio.h> |
| 14 |
|
| 15 |
#define NUM_THREADS 1024 |
| 16 |
|
| 17 |
struct thread_data
|
| 18 |
{
|
| 19 |
int thread_id;
|
| 20 |
unsigned int seed_w; |
| 21 |
unsigned int seed_z; |
| 22 |
unsigned long iterations; |
| 23 |
unsigned long inside; |
| 24 |
}; |
| 25 |
|
| 26 |
struct thread_data thread_data_array[NUM_THREADS];
|
| 27 |
|
| 28 |
// Marsaglia RNG very simple implementation
|
| 29 |
#define znew ((z=36969*(z&65535)+(z>>16))<<16) |
| 30 |
#define wnew ((w=18000*(w&65535)+(w>>16))&65535) |
| 31 |
#define MWC (znew+wnew)
|
| 32 |
#define SHR3 (jsr=(jsr=(jsr=jsr^(jsr<<17))^(jsr>>13))^(jsr<<5)) |
| 33 |
#define CONG (jcong=69069*jcong+1234567) |
| 34 |
#define KISS ((MWC^CONG)+SHR3)
|
| 35 |
|
| 36 |
#define MWCfp MWC * 2.328306435454494e-10f |
| 37 |
#define KISSfp KISS * 2.328306435454494e-10f |
| 38 |
|
| 39 |
#define ITERATIONS 1000000000 |
| 40 |
|
| 41 |
int MainLoopGlobal(unsigned long iterations,unsigned int seed_w,unsigned int seed_z) |
| 42 |
{
|
| 43 |
unsigned int z=seed_z; |
| 44 |
unsigned int w=seed_w; |
| 45 |
|
| 46 |
unsigned long total=0; |
| 47 |
|
| 48 |
for (unsigned long i=0;i<iterations;i++) { |
| 49 |
|
| 50 |
float x=MWCfp ;
|
| 51 |
float y=MWCfp ;
|
| 52 |
|
| 53 |
// Matching test
|
| 54 |
unsigned long inside=((x*x+y*y) < 1.0f) ? 1:0; |
| 55 |
total+=inside; |
| 56 |
} |
| 57 |
|
| 58 |
return(total);
|
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
void *MainLoopThread(void *threadarg) |
| 63 |
{
|
| 64 |
int taskid;
|
| 65 |
unsigned long iterations,total=0; |
| 66 |
unsigned int z,w; |
| 67 |
|
| 68 |
struct thread_data *my_data;
|
| 69 |
|
| 70 |
my_data=(struct thread_data *) threadarg;
|
| 71 |
|
| 72 |
taskid = my_data->thread_id; |
| 73 |
iterations = my_data->iterations; |
| 74 |
z = my_data->seed_z; |
| 75 |
w = my_data->seed_w; |
| 76 |
|
| 77 |
printf("Hello World! It's me, thread #%ld, with seeds (%ld,%ld) and %ld !\n", taskid,z,w,iterations);
|
| 78 |
|
| 79 |
for (unsigned long i=0;i<iterations;i++) { |
| 80 |
|
| 81 |
float x=MWCfp ;
|
| 82 |
float y=MWCfp ;
|
| 83 |
|
| 84 |
// Matching test
|
| 85 |
unsigned long inside=((x*x+y*y) < 1.0f) ? 1:0; |
| 86 |
total+=inside; |
| 87 |
} |
| 88 |
|
| 89 |
my_data->inside=total; |
| 90 |
|
| 91 |
printf("Thread %ld done, found %ld inside.\n",taskid,total);
|
| 92 |
|
| 93 |
pthread_exit((void*) my_data);
|
| 94 |
} |
| 95 |
|
| 96 |
int main(int argc, char *argv[]) |
| 97 |
{
|
| 98 |
pthread_t threads[NUM_THREADS]; |
| 99 |
pthread_attr_t attr; |
| 100 |
int *taskids[NUM_THREADS];
|
| 101 |
int rc, t, sum;
|
| 102 |
unsigned long num_threads,iterations; |
| 103 |
sum=0;
|
| 104 |
void *status;
|
| 105 |
|
| 106 |
iterations=atol(argv[1]);
|
| 107 |
num_threads=atoi(argv[2]);
|
| 108 |
|
| 109 |
printf("Number of threads defined to %ld\n",num_threads);
|
| 110 |
printf("Number of iterations defined to %ld\n",iterations);
|
| 111 |
|
| 112 |
/* Initialize and set thread detached attribute */
|
| 113 |
pthread_attr_init(&attr); |
| 114 |
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); |
| 115 |
|
| 116 |
for(t=0;t<num_threads;t++) { |
| 117 |
|
| 118 |
thread_data_array[t].thread_id = t; |
| 119 |
thread_data_array[t].iterations = iterations/num_threads; |
| 120 |
thread_data_array[t].seed_w = (t+1)<<4; |
| 121 |
thread_data_array[t].seed_z = (1048576*(t+1))>>4; |
| 122 |
thread_data_array[t].inside = 0;
|
| 123 |
|
| 124 |
printf("Creating thread %d\n", t);
|
| 125 |
rc = pthread_create(&threads[t], &attr, MainLoopThread, (void *)
|
| 126 |
&thread_data_array[t]); |
| 127 |
|
| 128 |
if (rc) {
|
| 129 |
printf("ERROR; return code from pthread_create() is %d\n", rc);
|
| 130 |
exit(-1);
|
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/* Free attribute and wait for the other threads */
|
| 135 |
pthread_attr_destroy(&attr); |
| 136 |
|
| 137 |
for(t=0; t<num_threads; t++) { |
| 138 |
rc = pthread_join(threads[t], &status); |
| 139 |
if (rc) {
|
| 140 |
printf("ERROR; return code from pthread_join() is %d\n", rc);
|
| 141 |
exit(-1);
|
| 142 |
} |
| 143 |
printf("Main: completed join with thread %ld having a status of %ld\n",
|
| 144 |
t,(long)status);
|
| 145 |
} |
| 146 |
|
| 147 |
for(t=0;t<num_threads;t++) { |
| 148 |
printf("Return to main with %ld thread, %ld inside\n",
|
| 149 |
t,thread_data_array[t].inside); |
| 150 |
} |
| 151 |
|
| 152 |
printf("Main: program completed. Exiting.\n");
|
| 153 |
pthread_exit(NULL);
|
| 154 |
|
| 155 |
} |