home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / sources / tools / mem-test.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-29  |  6.0 KB  |  253 lines

  1. /*    Copyright (c)    1991    Jin, Guojun
  2. #
  3. @    MUST include <math.h>
  4. @    -t    trace long run for -r and -g.
  5. @    -p    execute part of total or percentage of total loops.
  6. @    -d    directly read and write.
  7. @    -M    memory allocate and free times.
  8. @ AUTHOR:    Jin Guojun - LBL    2/1/91
  9. */
  10.  
  11. #include <errno.h>
  12. #include <time.h>
  13. #include <math.h>
  14. #include "header.def"
  15. #include "imagedef.h"
  16.  
  17. #define    inbuf    uimg.src
  18. #define    obuf    uimg.dest
  19. #define    pxl_bytes    uimg.pxl_in
  20. #define    frm    uimg.frames
  21. #define    row    uimg.height
  22. #define    cln    uimg.width
  23.  
  24. U_IMAGE    uimg;
  25. bool    acc_dir, fast, memaf, trace, drw, total, part, pipe;
  26. char    usage[]="options\n\
  27. -s    sequential\n-m    r->c->f\n-f    f->c->r\n\
  28. -g    f->r->c\n-c    c->f->r\n-r    r->f->c\n\
  29. -R    register_mode(FAST)\n-S    F R C--set SIZE\n\
  30. -t    tracing for -r or -g\n-p    partial execution for -r og -g\n\
  31. -d[#]    directly read and write(file copy using entire memory)\n\
  32.     The # is writing times (entire size factor)\n\
  33. -M    memory allocating times\n";
  34.  
  35. time_t    tm0, tm1;
  36.  
  37.  
  38. main(argc, argv)
  39. int    argc;
  40. char**    argv;
  41. {
  42. /* !!!    input number is start from 1 and convert to from 0    */
  43. MType    i, c, fsize;
  44. float    percent;
  45.  
  46. format_init(&uimg, IMAGE_INIT_TYPE, HIPS, -1, *argv, "S20-1");
  47. uimg.pxl_out = pxl_bytes = 1;
  48.  
  49. for(i=1; i<argc; i++)
  50.     if (*argv[i] == '-')
  51.     switch(argv[i][1])
  52.     {
  53.     case 'c':
  54.     case 'r':
  55.     case 'f':
  56.     case 'g':
  57.     case 'm':
  58.     case 's':
  59.         acc_dir = argv[i][1];    break;
  60.     case 'd':
  61.         drw = atoi(argv[i]+2);
  62.         if (drw<=0)    drw = 1;
  63.         break;
  64.     case 'p':
  65.         percent = atof(argv[i]+2);    break;
  66.     case 'M':    memaf = atoi(argv[i]+2);    break;
  67.     case 'P':    pipe++;    break;
  68.     case 'R':    fast++;    break;
  69.     case 'S':
  70.         if (isdigit(*argv[i+1]))    frm=atoi(argv[++i]);
  71.         if (isdigit(*argv[i+1]))    row=atoi(argv[++i]);
  72.         if (isdigit(*argv[i+1]))    cln=atoi(argv[++i]);
  73.         if (!frm)    frm=128;
  74.         if (!row)    row=128;
  75.         if (!cln)    cln=128;
  76.         break;
  77.     case 't':    trace++;    break;
  78.     case 'o':
  79.         if (freopen(argv[i]+2, "wb", stdout))    break;
  80.         message("%s can't be opened for write", argv[i]+2);
  81.     default:
  82.         usage_n_options(usage, i, argv[i]);
  83.     }
  84.     else if ((in_fp=freopen(argv[i], "rb", stdin)) == NULL)
  85.     syserr("input file %s not found", argv[i]);
  86.  
  87. io_test(fileno(in_fp), message("Using CTRL-D to continue"));
  88.  
  89. fseek(in_fp, 0L, 0);
  90. pipe += errno;
  91.  
  92. if (!frm)    (*uimg.header_handle)(HEADER_READ, &uimg, 0, 0);
  93.  
  94. if (percent < 5)    percent = 5;
  95. part = percent * cln / 100;
  96.  
  97. time(&tm0);
  98. message("begin processing: %s", ctime(&tm0));
  99.  
  100. fsize = cln * row;
  101. total = fsize * frm;
  102.  
  103. if (memaf){
  104.     while(memaf--){
  105.     inbuf = zalloc(pxl_bytes, total, "inbuf");
  106.     time(&tm1);
  107.     message("allocating %d bytes at %u: in second %s\n",
  108.         total*pxl_bytes, inbuf, tm1-tm0);
  109.     free(inbuf);
  110.     time(&tm1);
  111.     message("free %d bytes at %u: in second %s\n",
  112.         total*pxl_bytes, inbuf, tm1-tm0);
  113.     }
  114.     exit(0);
  115. }
  116.  
  117. inbuf = zalloc(pxl_bytes, total, "inbuf");
  118. time(&tm1);
  119. message("end of allocating: at %d seconds    ", tm1-tm0);
  120.  
  121. if (pipe)    i = upread(inbuf, pxl_bytes, total, in_fp);
  122. else    i = fread(inbuf, pxl_bytes, total, in_fp);
  123.  
  124. time(&tm1);
  125. message("=>    end of reading: at %d seconds\n", tm1-tm0);
  126. if (i != total)
  127.     message("Reading image %ld cells, rest %u are 0\n", i, total-i);
  128.  
  129. if (drw){
  130.     c = total % drw;
  131.     total /= drw;
  132.     while (drw--){
  133.         i = fwrite(inbuf, pxl_bytes, total-c*!drw, out_fp);
  134.         if (i != total)
  135.         syserr("directly writing %d", i);
  136.     }
  137. }
  138. else{
  139. message("%d frames, %d rows, %d columns => memory %u\n\
  140. (execute %d columns for -r and -g options)\n", frm, row, cln, total, part);
  141. if (fast){
  142. register byte    *bp, *tmpp, tmp;
  143. register MType    f, r;
  144.     message(" -- in register mode (FAST)\n");
  145. switch(acc_dir)
  146. {
  147. default:
  148. case 's':    /*    access buf sequentially    by column    */
  149.     message("sequential access\n");
  150.     for (f=0, bp=inbuf; f<total; f++)
  151.         tmp = bp[f];
  152.     break;
  153. case 'm':
  154.     mesg("row->column->frame access\n");
  155.     for (f=0, tmpp=inbuf; f<frm; f++, tmpp+=fsize)
  156.         for (c=0; c<cln; c++)
  157.         for (r=0, bp=tmpp+c; r<row; r++, bp+=cln)
  158.             tmp += *bp;
  159.     break;
  160. case 'f':
  161.     message("frame->column->row access\n");
  162.     for (r=0; r<row; r++)
  163.         for (c=0, tmpp=(byte*)inbuf+r*cln; c<cln; c++, tmpp++)
  164.         for (f=0, bp=tmpp; f<frm; f++, bp+=fsize)
  165.             tmp = *bp;
  166.     break;
  167. case 'c':
  168.     mesg("column->frame->row access\n");
  169.     for (r=0; r<row; r++)
  170.         for (f=0, tmpp=(byte*)inbuf+r*cln; f<frm; f++, tmpp+=fsize)
  171.         for (c=0, bp=tmpp; c<cln; c++)
  172.             tmp = *bp++;
  173.     break;
  174. case 'r':
  175.     mesg("row->frame->column access\n");
  176.     for (c=0; c<cln; c++){
  177.     if (trace)    fprintf(stderr, "\r%d", c);
  178.     if (c >= part)    break;
  179.         for (f=0, tmpp=(byte*)inbuf+c; f<frm; f++, tmpp+=fsize)
  180.         for (r=0, bp=tmpp; r<row; r++, bp+=cln)
  181.             tmp = *bp;
  182.     }
  183.     break;
  184. case 'g':
  185.     mesg("frame->row->column access\n");
  186.     for (c=0; c<cln; c++){
  187.     if (trace)    fprintf(stderr, "\r%d", c);
  188.     if (c >= part)    break;
  189.         for (r=0, tmpp=(byte*)inbuf+c; r<row; r++, tmpp+=cln)
  190.         for (f=0, bp=tmpp; f<frm; f++, bp+=fsize)
  191.             tmp = *bp;
  192.     }
  193. }
  194. }
  195. else{
  196. byte    *bp, *tmpp, tmp;
  197. MType    f, r;
  198. message(" -- in memory mode (Regular)\n");
  199. switch(acc_dir)
  200. {
  201. default:
  202. case 's':    /*    access buf sequentially    by column    */
  203.     message("sequential access\n");
  204.     for (i=0, bp=inbuf; i<total; i++)
  205.         tmp = bp[i];
  206.     break;
  207. case 'm':
  208.     mesg("row->column->frame access\n");
  209.     for (f=0, tmpp=inbuf; f<frm; f++, tmpp+=fsize)
  210.         for (c=0; c<cln; c++)
  211.         for (r=0, bp=tmpp+c; r<row; r++, bp+=cln)
  212.             tmp += *bp;
  213.     break;
  214. case 'f':
  215.     message("frame->column->row access\n");
  216.     for (r=0; r<row; r++)
  217.         for (c=0, tmpp=(byte*)inbuf+r*cln; c<cln; c++, tmpp++)
  218.         for (f=0, bp=tmpp; f<frm; f++, bp+=fsize)
  219.             tmp = *bp;
  220.     break;
  221. case 'c':
  222.     mesg("column->frame->row access\n");
  223.     for (r=0; r<row; r++)
  224.         for (f=0, tmpp=(byte*)inbuf+r*cln; f<frm; f++, tmpp+=fsize)
  225.         for (c=0, bp=tmpp; c<cln; c++)
  226.             tmp = *bp++;
  227.     break;
  228. case 'r':
  229.     mesg("row->frame->column access\n");
  230.     for (c=0; c<cln; c++){
  231.     if (trace)    fprintf(stderr, "\r%d", c);
  232.     if (c >= part)    break;
  233.         for (f=0, tmpp=(byte*)inbuf+c; f<frm; f++, tmpp+=fsize)
  234.         for (r=0, bp=tmpp; r<row; r++, bp+=cln)
  235.             tmp = *bp;
  236.     }
  237.     break;
  238. case 'g':
  239.     mesg("frame->row->column access\n");
  240.     for (c=0; c<cln; c++){
  241.     if (trace)    fprintf(stderr, "\r%d", c);
  242.     if (c >= part)    break;
  243.         for (r=0, tmpp=(byte*)inbuf+c; r<row; r++, tmpp+=cln)
  244.         for (f=0, bp=tmpp; f<frm; f++, bp+=fsize)
  245.             tmp = *bp;
  246.     }
  247. }
  248. }
  249. }
  250. time(&tm1);
  251. message("finish: at %d seconds\n", tm1-tm0);
  252. }
  253.