Debian Codegraph @

cextern/cfitsio/utilities/speed.c

raw
1 #include <string.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <time.h>
5
6 #ifndef _MSC_VER
7 #include <sys/time.h>
8 #endif
9
10 /*
11 Every program which uses the CFITSIO interface must include the
12 the fitsio.h header file. This contains the prototypes for all
13 the routines and defines the error status values and other symbolic
14 constants used in the interface.
15 */
16 #include "fitsio.h"
17
18 #define minvalue(A,B) ((A) < (B) ? (A) : (B))
19
20 /* size of the image */
21 #define XSIZE 3000
22 #define YSIZE 3000
23
24 /* size of data buffer */
25 #define SHTSIZE 20000
26 static long sarray[ SHTSIZE ] = {SHTSIZE * 0};
27 static short ssarray[ SHTSIZE ] = {SHTSIZE * 0};
28
29 /* no. of rows in binary table */
30 #define BROWS 2500000
31
32 /* no. of rows in ASCII table */
33 #define AROWS 400000
34
35 /* CLOCKS_PER_SEC should be defined by most compilers */
36 #if defined(CLOCKS_PER_SEC)
37 #define CLOCKTICKS CLOCKS_PER_SEC
38 #else
39 /* on SUN OS machine, CLOCKS_PER_SEC is not defined, so set its value */
40 #define CLOCKTICKS 1000000
41 #define difftime(A,B) ((double) A - (double) B)
42 #endif
43
44 /* define variables for measuring elapsed time */
45 clock_t scpu, ecpu;
46 time_t start, finish;
47 long startsec; /* start of elapsed time interval */
48 int startmilli; /* start of elapsed time interval */
49
50 int writeimage(fitsfile *fptr, int *status);
51 int writesimage(fitsfile *fptr, int *status);
52 int writebintable(fitsfile *fptr, int *status);
53 int writeasctable(fitsfile *fptr, int *status);
54 int readimage(fitsfile *fptr, int *status);
55 int readatable(fitsfile *fptr, int *status);
56 int readbtable(fitsfile *fptr, int *status);
57 void printerror( int status);
58 int marktime(int *status);
59 int gettime(double *elapse, float *elapscpu, int *status);
60 int main(void);
61
62 int main()
63 {
64 /*************************************************************************
65 This program tests the speed of writing/reading FITS files with cfitsio
66 **************************************************************************/
67
68 FILE *diskfile;
69 fitsfile *fptr; /* pointer to the FITS file, defined in fitsio.h */
70 int status, ii;
71 long rawloop;
72 char filename[] = "speedcc.fit"; /* name for new FITS file */
73 char buffer[2880] = {2880 * 0};
74 #ifndef _MSC_VER
75 struct timeval tbegin, tend;
76 #else
77 time_t tbegin, tend;
78 #endif
79 float rate, size, elapcpu, cpufrac;
80 double elapse;
81
82 #ifndef _MSC_VER
83 gettimeofday(&tbegin, NULL);
84 #else
85 tbegin = time(0);
86 #endif
87
88 remove(filename); /* Delete old file if it already exists */
89
90 diskfile = fopen(filename,"w+b");
91 rawloop = XSIZE * YSIZE / 720;
92
93 printf(" ");
94 printf(" SIZE / ELAPSE(%%CPU) = RATE\n");
95 printf("RAW fwrite (2880 bytes/loop)... ");
96 marktime(&status);
97
98 for (ii = 0; ii < rawloop; ii++)
99 if (fwrite(buffer, 1, 2880, diskfile) != 2880)
100 printf("write error \n");
101
102 fflush(diskfile); /* flush all buffers to disk */
103
104 gettime(&elapse, &elapcpu, &status);
105
106 cpufrac = elapcpu / elapse * 100.;
107 size = 2880. * rawloop / 1000000.;
108 rate = size / elapse;
109 printf(" %4.1fMB/%6.3fs(%3.0f) = %5.2fMB/s\n", size, elapse, cpufrac,rate);
110
111 /* read back the binary records */
112 fseek(diskfile, 0, 0);
113
114 printf("RAW fread (2880 bytes/loop)... ");
115 marktime(&status);
116
117 for (ii = 0; ii < rawloop; ii++)
118 if (fread(buffer, 1, 2880, diskfile) != 2880)
119 printf("read error \n");
120
121 gettime(&elapse, &elapcpu, &status);
122
123 cpufrac = elapcpu / elapse * 100.;
124 size = 2880. * rawloop / 1000000.;
125 rate = size / elapse;
126 printf(" %4.1fMB/%6.3fs(%3.0f) = %5.2fMB/s\n", size, elapse, cpufrac,rate);
127
128 fclose(diskfile);
129 remove(filename);
130
131 status = 0;
132 fptr = 0;
133
134 if (fits_create_file(&fptr, filename, &status)) /* create new FITS file */
135 printerror( status);
136
137 if (writesimage(fptr, &status))
138 printerror( status);
139
140 if (fits_close_file(fptr, &status))
141 printerror( status );
142
143 remove(filename); /* Delete old file if it already exists */
144
145 if (fits_create_file(&fptr, filename, &status)) /* create new FITS file */
146 printerror( status);
147
148 if (writeimage(fptr, &status))
149 printerror( status);
150
151 if (writebintable(fptr, &status))
152 printerror( status);
153
154 if (writeasctable(fptr, &status))
155 printerror( status);
156
157 if (readimage(fptr, &status))
158 printerror( status);
159
160 if (readbtable(fptr, &status))
161 printerror( status);
162
163 if (readatable(fptr, &status))
164 printerror( status);
165
166 if (fits_close_file(fptr, &status))
167 printerror( status );
168
169 #ifndef _MSC_VER
170 gettimeofday(&tend, NULL);
171 elapse = tend.tv_sec-tbegin.tv_sec+(tend.tv_usec-tbegin.tv_usec)*1.e-6 ;
172 #else
173 tend = time(0);
174 elapse = difftime(tend, tbegin) + 0.5;
175 #endif
176 printf("Total elapsed time = %.3fs, status = %d\n",elapse, status);
177 return(0);
178 }
179 /*--------------------------------------------------------------------------*/
180 int writeimage(fitsfile *fptr, int *status)
181
182 /**************************************************/
183 /* write the primary array containing a 2-D image */
184 /**************************************************/
185 {
186 long nremain, ii;
187 float rate, size, elapcpu, cpufrac;
188 double elapse;
189
190 /* initialize FITS image parameters */
191 int bitpix = 32; /* 32-bit signed integer pixel values */
192 long naxis = 2; /* 2-dimensional image */
193 long naxes[2] = {XSIZE, YSIZE }; /* image size */
194
195 /* write the required keywords for the primary array image */
196 if ( fits_create_img(fptr, bitpix, naxis, naxes, status) )
197 printerror( *status );
198
199 printf("\nWrite %dx%d I*4 image, %d pixels/loop: ",XSIZE,YSIZE,SHTSIZE);
200 marktime(status);
201
202 nremain = XSIZE * YSIZE;
203 for (ii = 1; ii <= nremain; ii += SHTSIZE)
204 {
205 ffpprj(fptr, 0, ii, SHTSIZE, sarray, status);
206 }
207
208 ffflus(fptr, status); /* flush all buffers to disk */
209
210 gettime(&elapse, &elapcpu, status);
211
212 cpufrac = elapcpu / elapse * 100.;
213 size = XSIZE * 4. * YSIZE / 1000000.;
214 rate = size / elapse;
215 printf(" %4.1fMB/%6.3fs(%3.0f) = %5.2fMB/s\n", size, elapse, cpufrac,rate);
216
217 return( *status );
218 }
219 /*--------------------------------------------------------------------------*/
220 int writesimage(fitsfile *fptr, int *status)
221
222 /**************************************************/
223 /* write the primary array containing a 2-D image */
224 /**************************************************/
225 {
226 long nremain, ii;
227 float rate, size, elapcpu, cpufrac;
228 double elapse;
229
230 /* initialize FITS image parameters */
231 int bitpix = 16; /* 16-bit signed integer pixel values */
232 long naxis = 2; /* 2-dimensional image */
233 long naxes[2] = {XSIZE, YSIZE }; /* image size */
234
235 /* write the required keywords for the primary array image */
236 if ( fits_create_img(fptr, bitpix, naxis, naxes, status) )
237 printerror( *status );
238
239 printf("\nWrite %dx%d I*2 image, %d pixels/loop: ",XSIZE,YSIZE,SHTSIZE);
240 marktime(status);
241
242 nremain = XSIZE * YSIZE;
243 for (ii = 1; ii <= nremain; ii += SHTSIZE)
244 {
245 ffppri(fptr, 0, ii, SHTSIZE, ssarray, status);
246 }
247
248 ffflus(fptr, status); /* flush all buffers to disk */
249
250 gettime(&elapse, &elapcpu, status);
251
252 cpufrac = elapcpu / elapse * 100.;
253 size = XSIZE * 2. * YSIZE / 1000000.;
254 rate = size / elapse;
255 printf(" %4.1fMB/%6.3fs(%3.0f) = %5.2fMB/s\n", size, elapse, cpufrac,rate);
256
257 return( *status );
258 }
259 /*--------------------------------------------------------------------------*/
260 int writebintable (fitsfile *fptr, int *status)
261
262 /*********************************************************/
263 /* Create a binary table extension containing 3 columns */
264 /*********************************************************/
265 {
266 int tfields = 2;
267 long nremain, ntodo, firstrow = 1, firstelem = 1, nrows;
268 float rate, size, elapcpu, cpufrac;
269 double elapse;
270
271 char extname[] = "Speed_Test"; /* extension name */
272
273 /* define the name, datatype, and physical units for the columns */
274 char *ttype[] = { "first", "second" };
275 char *tform[] = {"1J", "1J" };
276 char *tunit[] = { " ", " " };
277
278 /* append a new empty binary table onto the FITS file */
279
280 if ( fits_create_tbl( fptr, BINARY_TBL, BROWS, tfields, ttype, tform,
281 tunit, extname, status) )
282 printerror( *status );
283
284 /* get table row size and optimum number of rows to write per loop */
285 fits_get_rowsize(fptr, &nrows, status);
286 nrows = minvalue(nrows, SHTSIZE);
287 nremain = BROWS;
288
289 printf("Write %7drow x %dcol bintable %4ld rows/loop:", BROWS, tfields,
290 nrows);
291 marktime(status);
292
293 while(nremain)
294 {
295 ntodo = minvalue(nrows, nremain);
296 ffpclj(fptr, 1, firstrow, firstelem, ntodo, sarray, status);
297 ffpclj(fptr, 2, firstrow, firstelem, ntodo, sarray, status);
298 firstrow += ntodo;
299 nremain -= ntodo;
300 }
301
302 ffflus(fptr, status); /* flush all buffers to disk */
303
304 gettime(&elapse, &elapcpu, status);
305
306 cpufrac = elapcpu / elapse * 100.;
307 size = BROWS * 8. / 1000000.;
308 rate = size / elapse;
309 printf(" %4.1fMB/%6.3fs(%3.0f) = %5.2fMB/s\n", size, elapse, cpufrac,rate);
310
311 return( *status );
312 }
313 /*--------------------------------------------------------------------------*/
314 int writeasctable (fitsfile *fptr, int *status)
315
316 /*********************************************************/
317 /* Create an ASCII table extension containing 2 columns */
318 /*********************************************************/
319 {
320 int tfields = 2;
321 long nremain, ntodo, firstrow = 1, firstelem = 1;
322 long nrows;
323 float rate, size, elapcpu, cpufrac;
324 double elapse;
325
326 char extname[] = "Speed_Test"; /* extension name */
327
328 /* define the name, datatype, and physical units for the columns */
329 char *ttype[] = { "first", "second" };
330 char *tform[] = {"I6", "I6" };
331 char *tunit[] = { " ", " " };
332
333 /* append a new empty ASCII table onto the FITS file */
334 if ( fits_create_tbl( fptr, ASCII_TBL, AROWS, tfields, ttype, tform,
335 tunit, extname, status) )
336 printerror( *status );
337
338 /* get table row size and optimum number of rows to write per loop */
339 fits_get_rowsize(fptr, &nrows, status);
340 nrows = minvalue(nrows, SHTSIZE);
341 nremain = AROWS;
342
343 printf("Write %7drow x %dcol asctable %4ld rows/loop:", AROWS, tfields,
344 nrows);
345 marktime(status);
346
347 while(nremain)
348 {
349 ntodo = minvalue(nrows, nremain);
350 ffpclj(fptr, 1, firstrow, firstelem, ntodo, sarray, status);
351 ffpclj(fptr, 2, firstrow, firstelem, ntodo, sarray, status);
352 firstrow += ntodo;
353 nremain -= ntodo;
354 }
355
356 ffflus(fptr, status); /* flush all buffers to disk */
357
358 gettime(&elapse, &elapcpu, status);
359
360 cpufrac = elapcpu / elapse * 100.;
361 size = AROWS * 13. / 1000000.;
362 rate = size / elapse;
363 printf(" %4.1fMB/%6.3fs(%3.0f) = %5.2fMB/s\n", size, elapse, cpufrac,rate);
364
365 return( *status );
366 }
367 /*--------------------------------------------------------------------------*/
368 int readimage( fitsfile *fptr, int *status )
369
370 /*********************/
371 /* Read a FITS image */
372 /*********************/
373 {
374 int anynull, hdutype;
375 long nremain, ii;
376 long longnull = 0;
377 float rate, size, elapcpu, cpufrac;
378 double elapse;
379
380 /* move to the primary array */
381 if ( fits_movabs_hdu(fptr, 1, &hdutype, status) )
382 printerror( *status );
383
384 printf("\nRead back image ");
385 marktime(status);
386
387 nremain = XSIZE * YSIZE;
388 for (ii=1; ii <= nremain; ii += SHTSIZE)
389 {
390 ffgpvj(fptr, 0, ii, SHTSIZE, longnull, sarray, &anynull, status);
391 }
392
393 gettime(&elapse, &elapcpu, status);
394
395 cpufrac = elapcpu / elapse * 100.;
396 size = XSIZE * 4. * YSIZE / 1000000.;
397 rate = size / elapse;
398 printf(" %4.1fMB/%6.3fs(%3.0f) = %5.2fMB/s\n", size, elapse, cpufrac,rate);
399
400 return( *status );
401 }
402 /*--------------------------------------------------------------------------*/
403 int readbtable( fitsfile *fptr, int *status )
404
405 /************************************************************/
406 /* read and print data values from the binary table */
407 /************************************************************/
408 {
409 int hdutype, anynull;
410 long nremain, ntodo, firstrow = 1, firstelem = 1;
411 long nrows;
412 long lnull = 0;
413 float rate, size, elapcpu, cpufrac;
414 double elapse;
415
416 /* move to the table */
417 if ( fits_movrel_hdu(fptr, 1, &hdutype, status) )
418 printerror( *status );
419
420 /* get table row size and optimum number of rows to read per loop */
421 fits_get_rowsize(fptr, &nrows, status);
422 nrows = minvalue(nrows, SHTSIZE);
423
424 /* read the columns */
425 nremain = BROWS;
426
427 printf("Read back BINTABLE ");
428 marktime(status);
429
430 while(nremain)
431 {
432 ntodo = minvalue(nrows, nremain);
433 ffgcvj(fptr, 1, firstrow, firstelem, ntodo,
434 lnull, sarray, &anynull, status);
435 ffgcvj(fptr, 2, firstrow, firstelem, ntodo,
436 lnull, sarray, &anynull, status);
437 firstrow += ntodo;
438 nremain -= ntodo;
439 }
440
441 gettime(&elapse, &elapcpu, status);
442
443 cpufrac = elapcpu / elapse * 100.;
444 size = BROWS * 8. / 1000000.;
445 rate = size / elapse;
446 printf(" %4.1fMB/%6.3fs(%3.0f) = %5.2fMB/s\n", size, elapse, cpufrac,rate);
447
448 return( *status );
449 }
450 /*--------------------------------------------------------------------------*/
451 int readatable( fitsfile *fptr, int *status )
452
453 /************************************************************/
454 /* read and print data values from an ASCII or binary table */
455 /************************************************************/
456 {
457 int hdutype, anynull;
458 long nremain, ntodo, firstrow = 1, firstelem = 1;
459 long nrows;
460 long lnull = 0;
461 float rate, size, elapcpu, cpufrac;
462 double elapse;
463
464 /* move to the table */
465 if ( fits_movrel_hdu(fptr, 1, &hdutype, status) )
466 printerror( *status );
467
468 /* get table row size and optimum number of rows to read per loop */
469 fits_get_rowsize(fptr, &nrows, status);
470 nrows = minvalue(nrows, SHTSIZE);
471
472 /* read the columns */
473 nremain = AROWS;
474
475 printf("Read back ASCII Table ");
476 marktime(status);
477
478 while(nremain)
479 {
480 ntodo = minvalue(nrows, nremain);
481 ffgcvj(fptr, 1, firstrow, firstelem, ntodo,
482 lnull, sarray, &anynull, status);
483 ffgcvj(fptr, 2, firstrow, firstelem, ntodo,
484 lnull, sarray, &anynull, status);
485 firstrow += ntodo;
486 nremain -= ntodo;
487 }
488
489 gettime(&elapse, &elapcpu, status);
490
491 cpufrac = elapcpu / elapse * 100.;
492 size = AROWS * 13. / 1000000.;
493 rate = size / elapse;
494 printf(" %4.1fMB/%6.3fs(%3.0f) = %5.2fMB/s\n", size, elapse, cpufrac,rate);
495
496 return( *status );
497 }
498 /*--------------------------------------------------------------------------*/
499 void printerror( int status)
500 {
501 /*****************************************************/
502 /* Print out cfitsio error messages and exit program */
503 /*****************************************************/
504
505 char status_str[FLEN_STATUS], errmsg[FLEN_ERRMSG];
506
507 if (status)
508 fprintf(stderr, "\n*** Error occurred during program execution ***\n");
509
510 fits_get_errstatus(status, status_str); /* get the error description */
511 fprintf(stderr, "\nstatus = %d: %s\n", status, status_str);
512
513 /* get first message; null if stack is empty */
514 if ( fits_read_errmsg(errmsg) )
515 {
516 fprintf(stderr, "\nError message stack:\n");
517 fprintf(stderr, " %s\n", errmsg);
518
519 while ( fits_read_errmsg(errmsg) ) /* get remaining messages */
520 fprintf(stderr, " %s\n", errmsg);
521 }
522
523 exit( status ); /* terminate the program, returning error status */
524 }
525 /*--------------------------------------------------------------------------*/
526 int marktime(int *status)
527 {
528 #ifndef _MSC_VER
529 double telapse;
530 time_t temp;
531 struct timeval tv;
532
533 temp = time(0);
534
535 /* Since elapsed time is only measured to the nearest second */
536 /* keep getting the time until the seconds tick just changes. */
537 /* This provides more consistent timing measurements since the */
538 /* intervals all start on an integer seconds. */
539
540 telapse = 0.;
541
542 scpu = clock();
543 start = time(0);
544 /*
545 while (telapse == 0.)
546 {
547 scpu = clock();
548 start = time(0);
549 telapse = difftime( start, temp );
550 }
551 */
552 gettimeofday(&tv, NULL);
553
554 startsec = tv.tv_sec;
555 startmilli = tv.tv_usec/1000;
556 #else
557 /* don't support high timing precision on Windows machines */
558 startsec = 0;
559 startmilli = 0;
560
561 scpu = clock();
562 #endif
563 return( *status );
564 }
565 /*--------------------------------------------------------------------------*/
566 int gettime(double *elapse, float *elapscpu, int *status)
567 {
568 #ifndef _MSC_VER
569 struct timeval tv;
570 int stopmilli;
571 long stopsec;
572
573 gettimeofday(&tv, NULL);
574 ecpu = clock();
575 finish = time(0);
576
577 stopmilli = tv.tv_usec/1000;
578 stopsec = tv.tv_sec;
579
580 *elapse = (stopsec - startsec) + (stopmilli - startmilli)/1000.;
581
582 /* *elapse = difftime(finish, start) + 0.5; */
583 *elapscpu = (ecpu - scpu) * 1.0 / CLOCKTICKS;
584 #else
585 /* set the elapsed time the same as the CPU time on Windows machines */
586 *elapscpu = (float) ((ecpu - scpu) * 1.0 / CLOCKTICKS);
587 *elapse = *elapscpu;
588 #endif
589 return( *status );
590 }