PIPS
jacobi.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 typedef float float_t;
4 #define SIZE 832
5 #define T 64
6 
8 // For the dataparallel semantics:
10 
11 void get_data(char filename[]) {
12  int i, j, nx, ny;
13  unsigned char c;
14  FILE *fp;
15 
16  if ((fp = fopen(filename, "r")) == NULL) {
17  perror("Error loading file");
18  exit(1);
19  }
20 
21  /* Get *.pgm file type */
22  c = fgetc(fp);
23  c = fgetc(fp);
24 
25  /* Skip comment lines */
26  do {
27  while((c = fgetc(fp)) != '\n');
28  } while((c = fgetc(fp)) == '#');
29 
30  /* Put back good char */
31  ungetc(c,fp);
32 
33  /* Get image dimensions */
34  fscanf(fp, "%d %d\n", &nx, &ny);
35  /* Get grey levels */
36  fscanf(fp,"%d",&i);
37  /* Get ONE carriage return */
38  fgetc(fp);
39  printf("Input image : x=%d y=%d grey=%d\n", nx, ny, i);
40 
41 
42  for(i = 0;i < SIZE; i++)
43  for(j = 0;j < SIZE; j++) {
44  c = fgetc(fp);
45  space[i][j] = c;
46  }
47 
48  fclose(fp);
49 }
50 
51 
52 void write_data(char filename[]) {
53  int i,j;
54  unsigned char c;
55  FILE *fp;
56 
57  if ((fp = fopen(filename, "w")) == NULL) {
58  perror("Error opening file");
59  exit(2);
60  }
61 
62  /* Write the PGM header: */
63  fprintf(fp,"P5\n%d %d\n255\n", SIZE, SIZE);
64 
65  for(i = 0;i < SIZE; i++)
66  for(j = 0;j < SIZE; j++) {
67  c = space[i][j];
68  fputc(c, fp);
69  }
70  fclose(fp);
71 }
72 
73 
74 void jacobi() {
75  int i, j;
76 
77  /* Use 2 array in flip-flop to have dataparallel forall semantics. I
78  could use also a flip-flop dimension instead... */
79  kernel1:
80  for(i = 1;i < SIZE - 1; i++)
81  for(j = 1;j < SIZE - 1; j++) {
82  save[i][j] = 0.25*(space[i - 1][j] + space[i + 1][j]
83  + space[i][j - 1] + space[i][j + 1]);
84  }
85  for(i = 1;i < SIZE - 1; i++)
86  for(j = 1;j < SIZE - 1; j++) {
87  space[i][j] = 0.25*(save[i - 1][j] + save[i + 1][j]
88  + save[i][j - 1] + save[i][j + 1]);
89  }
90 }
91 
92 
93 int main(int argc, char *argv[]) {
94  int t;
95 
96  if (argc != 2) {
97  fprintf(stderr,
98  "%s needs only one argument that is the PGM image input file\n",
99  argv[0]);
100  exit(1);
101  }
102  get_data(argv[1]);
103 
104  for(t = 0; t < T; t++)
105  jacobi();
106 
107  write_data("output.pgm");
108 
109  return 0;
110 }
#define T
Definition: jacobi.c:5
int main(int argc, char *argv[])
Definition: jacobi.c:93
float float_t
Definition: jacobi.c:3
void get_data(char filename[])
Definition: jacobi.c:11
void jacobi()
Definition: jacobi.c:74
#define SIZE
Definition: jacobi.c:4
float_t save[SIZE][SIZE]
Definition: jacobi.c:9
float_t space[SIZE][SIZE]
Definition: jacobi.c:7
void write_data(char filename[])
Definition: jacobi.c:52
#define exit(code)
Definition: misc-local.h:54
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...
int printf()