PIPS
tools.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <complex.h>
10 
11 // These macros must not be used directly !
12 #ifdef __PYPS_SAC_VALIDATE
13 #define _print_array(name, ptr, n, format, stype) \
14  fwrite(ptr, n, stype, stdout); \
15  fflush(stdout);
16 #elif defined(__PYPS_SAC_BENCHMARK)
17 #define _print_array(name, ptr, n, format, stype)
18 #else
19 #define _print_array(name, ptr, n, format, stype) \
20  int i; \
21  char formatnl[10]; \
22  printf("%s :\n", name); \
23  printf("----\n"); \
24  formatnl[7] = 0; \
25  strncpy(formatnl, format, 7); \
26  strncat(formatnl, "\n", 2); \
27  for (i = 0; i < n; i++) \
28  printf(formatnl, *(ptr+i)); \
29  printf("----\n");
30 #endif
31 
32 static FILE* _f_data_file = 0;
33 int _init_data(char* ptr, const ssize_t n);
34 
35 
36 void init_data_file(const char* data_file)
37 {
38  if (_f_data_file != 0)
39  return;
40  _f_data_file = fopen(data_file, "r");
41  if (_f_data_file == 0)
42  {
43  perror("open data file");
44  exit(errno);
45  }
46 }
47 
49 {
50  if (_f_data_file != 0)
51  fclose(_f_data_file);
52 }
53 
54 void print_array_float(const char* name, const float* arr, const unsigned int n)
55 {
56  _print_array(name, arr, n, "%f", sizeof(float));
57 }
58 
59 void print_array_int(const char* name, const int* arr, const unsigned int n)
60 {
61  _print_array(name, arr, n, "%d", sizeof(int));
62 }
63 
64 void print_array_double(const char* name, const float* arr, const unsigned int n)
65 {
66  _print_array(name, arr, n, "%a", sizeof(double));
67 }
68 
69 void print_array_long(const char* name, const long* arr, const unsigned int n)
70 {
71  _print_array(name, arr, n, "%a", sizeof(long));
72 }
73 
74 void print_array_cplx(const char* name, const float complex* arr, const unsigned int n)
75 {
76  int i;
77  for (i=0; i<n;i++)
78  {
79  printf("%f %f\n",crealf(arr[i]),cimagf(arr[i]));
80  }
81 }
82 
83 int _init_data(char* ptr, const ssize_t n)
84 {
85  ssize_t nr;
86  ssize_t ntoread;
87 
88  ntoread = n;
89  if (_f_data_file == 0)
90  {
91  fprintf(stderr, "Data file must be initialized !\n");
92  exit(1);
93  }
94  while (ntoread > 0)
95  {
96  nr = fread(ptr, 1, ntoread, _f_data_file);
97  if (nr == 0 && ferror(_f_data_file))
98  {
99  perror("read data file");
100  clearerr(_f_data_file);
101  return errno;
102  }
103  if (nr < ntoread)
104  {
105  // fprintf(stderr, "%d bytes remaining...\n", ntoread-nr);
106  fseek(_f_data_file, 0L, SEEK_SET);
107  fflush(_f_data_file);
108  }
109  ntoread -= nr;
110  ptr += nr;
111  }
112 
113  // Old implementation... :
114  //fprintf(stderr, "Warning: missing %d bytes in data file ! Filling with zeros...\n", n-nr);
115  // This makes pips crashes... !!
116  //memset(ptr + nr, 0, n-nr);
117  return nr;
118 }
119 
120 int init_data_gen(void* ptr, const unsigned int n, const ssize_t stype)
121 {
122  return _init_data(ptr, (ssize_t)(n)*stype);
123 }
124 
125 int init_data_float(float* ptr, const unsigned int n)
126 {
127  int r = init_data_gen(ptr, n, sizeof(float));
128  return r;
129 }
130 
131 int init_data_double(double* ptr, const unsigned int n)
132 {
133  return init_data_gen(ptr, n, sizeof(double));
134 }
135 
136 int init_data_long(long* ptr, const unsigned int n)
137 {
138  return init_data_gen(ptr, n, sizeof(long));
139 }
140 
141 int init_data_int(int* ptr, const unsigned int n)
142 {
143  return init_data_gen(ptr, n, sizeof(int));
144 }
145 
146 int init_data_cplx(float complex* ptr, const unsigned int n)
147 {
148  return 0;
149 }
150 
151 void init_args(int argc, char** argv)
152 {
153  if (argc < 3)
154  {
155  fprintf(stderr, "Usage: %s kernel_size data_file\n", argv[0]);
156  exit(1);
157  }
158  init_data_file(argv[2]);
159 }
#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()
Definition: pip__tab.h:30
int init_data_cplx(float complex *ptr, const unsigned int n)
Definition: tools.c:146
int init_data_long(long *ptr, const unsigned int n)
Definition: tools.c:136
void print_array_long(const char *name, const long *arr, const unsigned int n)
Definition: tools.c:69
void close_data_file()
Definition: tools.c:48
void print_array_float(const char *name, const float *arr, const unsigned int n)
Definition: tools.c:54
static FILE * _f_data_file
Definition: tools.c:32
void init_args(int argc, char **argv)
Definition: tools.c:151
int init_data_gen(void *ptr, const unsigned int n, const ssize_t stype)
Definition: tools.c:120
int init_data_float(float *ptr, const unsigned int n)
Definition: tools.c:125
void print_array_double(const char *name, const float *arr, const unsigned int n)
Definition: tools.c:64
int init_data_double(double *ptr, const unsigned int n)
Definition: tools.c:131
void print_array_cplx(const char *name, const float complex *arr, const unsigned int n)
Definition: tools.c:74
int _init_data(char *ptr, const ssize_t n)
Definition: tools.c:83
void print_array_int(const char *name, const int *arr, const unsigned int n)
Definition: tools.c:59
void init_data_file(const char *data_file)
Definition: tools.c:36
int init_data_int(int *ptr, const unsigned int n)
Definition: tools.c:141
#define _print_array(name, ptr, n, format, stype)
Definition: tools.c:19