root / Pi / C / Pthreads / PiMP_orig2.c @ 286
Historique | Voir | Annoter | Télécharger (2,9 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 4 |
16 |
|
17 |
struct thread_data
|
18 |
{ |
19 |
int thread_id;
|
20 |
unsigned int seed_w; |
21 |
unsigned int seed_z; |
22 |
unsigned int iterations; |
23 |
unsigned int 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 int iterations,unsigned int seed_w,unsigned int seed_z) |
42 |
{ |
43 |
unsigned int z=seed_z; |
44 |
unsigned int w=seed_w; |
45 |
|
46 |
int total=0; |
47 |
|
48 |
for (unsigned int i=0;i<iterations;i++) { |
49 |
|
50 |
float x=MWCfp ;
|
51 |
float y=MWCfp ;
|
52 |
|
53 |
// Matching test
|
54 |
int 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 int iterations,z,w,total=0; |
66 |
|
67 |
struct thread_data *my_data;
|
68 |
|
69 |
my_data=(struct thread_data *) threadarg;
|
70 |
|
71 |
taskid = my_data->thread_id; |
72 |
iterations = my_data->iterations; |
73 |
z = my_data->seed_z; |
74 |
w = my_data->seed_w; |
75 |
iterations = my_data->iterations; |
76 |
|
77 |
printf("Hello World! It's me, thread #%ld, with seeds (%i,%i) and %i !\n", taskid,z,w,iterations);
|
78 |
|
79 |
for (unsigned int i=0;i<iterations;i++) { |
80 |
|
81 |
float x=MWCfp ;
|
82 |
float y=MWCfp ;
|
83 |
|
84 |
// Matching test
|
85 |
int inside=((x*x+y*y) < 1.0f) ? 1:0; |
86 |
total+=inside; |
87 |
} |
88 |
|
89 |
printf("In #%ld, found %i inside\n",taskid,total);
|
90 |
|
91 |
my_data->inside=total; |
92 |
|
93 |
pthread_exit(NULL);
|
94 |
} |
95 |
|
96 |
int main(int argc, char *argv[]) |
97 |
{ |
98 |
|
99 |
pthread_t threads[NUM_THREADS]; |
100 |
int *taskids[NUM_THREADS];
|
101 |
int rc, t, sum;
|
102 |
|
103 |
sum=0;
|
104 |
|
105 |
for(t=0;t<NUM_THREADS;t++) { |
106 |
|
107 |
thread_data_array[t].thread_id = t; |
108 |
thread_data_array[t].iterations = ITERATIONS/NUM_THREADS; |
109 |
thread_data_array[t].seed_w = (t+1)<<4; |
110 |
thread_data_array[t].seed_z = (1048576*(t+1))>>4; |
111 |
thread_data_array[t].inside = 0;
|
112 |
|
113 |
printf("Creating thread %d\n", t);
|
114 |
rc = pthread_create(&threads[t], NULL, MainLoopThread, (void *) |
115 |
&thread_data_array[t]); |
116 |
printf("%i inside\n", thread_data_array[t].inside);
|
117 |
if (rc) {
|
118 |
printf("ERROR; return code from pthread_create() is %d\n", rc);
|
119 |
exit(-1);
|
120 |
} |
121 |
} |
122 |
pthread_exit(NULL);
|
123 |
|
124 |
|
125 |
} |