Statistiques
| Révision :

root / Pi / C / Pthreads / PiMP_orig1.c @ 33

Historique | Voir | Annoter | Télécharger (2,4 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
#define NUM_THREADS        4
13

    
14
#include <math.h>
15
#include <stdio.h>
16

    
17
// Marsaglia RNG very simple implementation
18
#define znew  ((z=36969*(z&65535)+(z>>16))<<16)
19
#define wnew  ((w=18000*(w&65535)+(w>>16))&65535)
20
#define MWC   (znew+wnew)
21
#define SHR3  (jsr=(jsr=(jsr=jsr^(jsr<<17))^(jsr>>13))^(jsr<<5))
22
#define CONG  (jcong=69069*jcong+1234567)
23
#define KISS  ((MWC^CONG)+SHR3)
24

    
25
#define MWCfp MWC * 2.328306435454494e-10f
26
#define KISSfp KISS * 2.328306435454494e-10f
27

    
28
#define ITERATIONS 1000000000
29

    
30
int MainLoopGlobal(unsigned int iterations,unsigned int seed_w,unsigned int seed_z)
31
{
32
   unsigned int z=seed_z;
33
   unsigned int w=seed_w;
34

    
35
   int total=0;
36

    
37
   for (unsigned int i=0;i<iterations;i++) {
38

    
39
      float x=MWCfp ;
40
      float y=MWCfp ;
41

    
42
      // Matching test
43
      int inside=((x*x+y*y) < 1.0f) ? 1:0;
44
      total+=inside;
45
   }
46

    
47
   return(total);
48

    
49
}
50

    
51
void *PrintHello(void *threadid)
52
{
53
   long tid;
54
   tid = (long)threadid;
55
   printf("Hello World! It's me, thread #%ld!\n", tid);
56

    
57
   pthread_exit(NULL);
58
}
59

    
60
void *MainLoopThread(void *threadid)
61
{
62
   long tid;
63
   tid = (long)threadid;
64
   printf("Hello World! It's me, thread #%ld!\n", tid);
65

    
66
   /* unsigned int z=seed_z; */
67
   /* unsigned int w=seed_w; */
68
   unsigned int z=16;
69
   unsigned int w=64;
70

    
71
   int total=0;
72

    
73
   for (unsigned int i=0;i<ITERATIONS;i++) {
74

    
75
      float x=MWCfp ;
76
      float y=MWCfp ;
77

    
78
      // Matching test
79
      int inside=((x*x+y*y) < 1.0f) ? 1:0;
80
      total+=inside;
81
   }
82

    
83
   printf("In #%ld, found %i inside\n",tid,total);
84
   
85
   pthread_exit(NULL);
86
}
87

    
88
int main(int argc, char *argv[])
89
{
90
   pthread_t threads[NUM_THREADS];
91
   int rc;
92
   long t;
93
   for(t=0;t<NUM_THREADS;t++){
94
     printf("In main: creating thread %ld\n", t);
95
     rc = pthread_create(&threads[t], NULL, MainLoopThread, (void *)t);
96
     if (rc){
97
       printf("ERROR; return code from pthread_create() is %d\n", rc);
98
       exit(-1);
99
       }
100
     }
101

    
102
   /* Last thing that main() should do */
103
   pthread_exit(NULL);
104
}