Révision 78

Pi/C/OpenMP/Pi_OpenMP.c (revision 78)
50 50
   return(total);
51 51
}
52 52

  
53
LENGTH splitter(LENGTH iterations,unsigned int process,
54
		unsigned int seed_w,unsigned int seed_z)
55
{
56
  LENGTH inside[1024],insides=0;
57

  
58
#pragma omp parallel for
59
  for (int i=0 ; i<process; i++) {
60
    inside[i]=MainLoopGlobal(iterations/process,seed_w+process,seed_z+process);
61
    printf("\tFound %lld for process %i\n",(long long)inside[i],i);
62
  }
63
  printf("\n");
64

  
65
  for (int i=0 ; i<process; i++) {
66
    insides+=inside[i];
67
  }
68

  
69
  return(insides);
70
} 
71

  
72

  
53 73
int main(int argc, char *argv[]) {
54 74

  
55 75
  unsigned int seed_w=10,seed_z=10,process=PROCESS;
56
  LENGTH iterations=ITERATIONS;
57
  LENGTH inside[1024],insides=0;
58

  
76
  LENGTH iterations=ITERATIONS,insides=0;
77
  
59 78
  if (argc > 1) {
60 79
    iterations=(LENGTH)atoll(argv[1]);
61 80
    process=atoi(argv[2]);
......
76 95
  printf ("\tMax long = %ld\n", LONG_MAX);
77 96
  printf ("\tMax long long = %lld\n\n", LLONG_MAX);
78 97

  
79
#pragma omp parallel for
80
  for (int i=0 ; i<process; i++) {
81
    inside[i]=MainLoopGlobal(iterations/process,seed_w,seed_z);
82
    printf("\tFound %lld for process %i\n",(long long)inside[i],i);
83
  }
84
  printf("\n");
85

  
86
  for (int i=0 ; i<process; i++) {
87
    insides+=inside[i];
88
  }
98
  insides=splitter(iterations,process,seed_w,seed_z);
89 99
  
90 100
  float pi=4.*(float)insides/(float)iterations;
91 101

  
Pi/C/OpenMP/XeonPhi/Pi_OpenMP.c (revision 78)
1
//
2
// Estimation of Pi using Monte Carlo exploration process
3
// gcc -std=c99 -O3 -o Pi Pi.c -lm 
4
//
5

  
6
#include <math.h>
7
#include <stdio.h>
8
#include <stdlib.h>
9
#include <omp.h>
10
#include <limits.h>
11

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

  
20
#define MWCfp MWC * 2.328306435454494e-10f
21
#define KISSfp KISS * 2.328306435454494e-10f
22

  
23
#define ITERATIONS 1000000000
24

  
25
#define PROCESS 4
26

  
27
#ifdef LONG
28
#define LENGTH long long
29
#else
30
#define LENGTH int
31
#endif
32

  
33
#pragma omp declare target
34
LENGTH splitter(int,int,int,LENGTH);
35

  
36
LENGTH MainLoopGlobal(LENGTH iterations,unsigned int seed_w,unsigned int seed_z)
37
{
38
   unsigned int z=seed_z;
39
   unsigned int w=seed_w;
40

  
41
   LENGTH total=0;
42

  
43
   for (LENGTH i=0;i<iterations;i++) {
44

  
45
      float x=MWCfp ;
46
      float y=MWCfp ;
47

  
48
      // Matching test
49
      int inside=((x*x+y*y) < 1.0f) ? 1:0;
50
      total+=inside;
51
   }
52

  
53
   return(total);
54
}
55

  
56
LENGTH splitter(int process,int seed_w,int seed_z,LENGTH iterations) {
57

  
58
  LENGTH inside[1024],insides=0;
59
  int i;
60
  
61
#pragma omp target device(0)
62
#pragma omp teams num_teams(60) thread_limit(4)
63
#pragma omp distribute
64
  for (int i=0 ; i<process; i++) {
65
    inside[i]=MainLoopGlobal(iterations/process,seed_w+process,seed_z+process);
66
    //    printf("\tFound %lld for process %i\n",(long long)inside[i],i);
67
  }
68
  //printf("\n");
69

  
70
  for (int i=0 ; i<process; i++) {
71
    insides+=inside[i];
72
  }
73
  
74
  return(insides);
75
}
76
 
77
int main(int argc, char *argv[]) {
78

  
79
  unsigned int seed_w=10,seed_z=10,process=PROCESS;
80
  LENGTH iterations=ITERATIONS;
81
  LENGTH inside[1024],insides=0;
82

  
83
  if (argc > 1) {
84
    iterations=(LENGTH)atoll(argv[1]);
85
    process=atoi(argv[2]);
86
  }
87
  else {
88
    printf("\n\tPi : Estimate Pi with Monte Carlo exploration\n\n");
89
    printf("\t\t#1 : number of iterations (default 1 billion)\n");
90
    printf("\t\t#2 : number of process (default 4)\n\n");
91
  }
92

  
93
  printf ("\n\tInformation about architecture:\n\n");
94

  
95
  printf ("\tSizeof int = %lld bytes.\n", (long long)sizeof(int));
96
  printf ("\tSizeof long = %lld bytes.\n", (long long)sizeof(long));
97
  printf ("\tSizeof long long = %lld bytes.\n\n", (long long)sizeof(long long));
98

  
99
  printf ("\tMax int = %u\n", INT_MAX);
100
  printf ("\tMax long = %ld\n", LONG_MAX);
101
  printf ("\tMax long long = %lld\n\n", LLONG_MAX);
102

  
103
  insides=splitter(process,seed_w,seed_z,iterations);
104

  
105
  float pi=4.*(float)insides/(float)iterations;
106

  
107
  printf("\tPi=%f with error %f and %lld iterations\n\n",pi,
108
         fabs(pi-4*atan(1))/pi,(long long)iterations);
109
  
110
}
Pi/C/OpenMP/XeonPhi/bench.sh (revision 78)
1
#!/bin/bash
2

  
3
EXE=Pi_OpenMP_LONG
4
ITERATIONS=10000000000
5
MYTIME=/usr/bin/time
6

  
7
REPEAT=10
8
PROCESS=480
9

  
10
[ ! $1 == '' ] && EXE=$1
11
[ ! $2 == '' ] && ITERATIONS=$2
12
[ ! $3 == '' ] && PROCESS=$3
13

  
14
LOGFILE=${EXE}_${HOSTNAME}_${ITERATIONS}.log
15

  
16
> $LOGFILE
17
p=1
18
while [ $p -le $PROCESS ]
19
do
20
    export OMP_NUM_THREADS=$p
21
    echo -e "Process $p" >> $LOGFILE
22
    echo -ne "Start $EXE with $ITERATIONS and $p : "
23
    i=1
24
    while [ $i -le $REPEAT ]
25
    do 
26
        echo -ne "$i "
27
        $MYTIME ./$EXE $ITERATIONS $p >> $LOGFILE 2>&1 
28
        i=$(($i+1))
29
    done
30
    echo
31
    p=$(($p+1))
32
done
0 33

  
Pi/C/OpenMP/XeonPhi/Makefile (revision 78)
1
EXECUTABLE=Pi_OpenMP_LONG Pi_OpenMP_INT
2

  
3
SOURCE=Pi_OpenMP.c
4

  
5
CC=icpc
6
CFLAGS=-Wall -O3 -openmp -g
7
LIBRARY=-lm
8

  
9
all: $(EXECUTABLE)
10

  
11
Pi_OpenMP_LONG: $(SOURCE)
12

  
13
	$(CC) $(CFLAGS) $(DIRECTIVES) -DLONG -o $@ $< $(LIBRARY)
14

  
15
Pi_OpenMP_INT: $(SOURCE)
16

  
17
	$(CC) $(CFLAGS) $(DIRECTIVES) -DINTEGER -o $@ $< $(LIBRARY)
18

  
19
.PHONY: clean check mrproper
20

  
21
mrproper: 
22
	rm -rf $(EXECUTABLE)
23
	find . -name "*~" -exec rm {} \;
24

  
25
clean:
26
	find . -name "*~" -exec rm {} \;
27

  
28
check: $(EXECUTABLE)
29

  
30
	@echo "To be Defined"

Formats disponibles : Unified diff