Difference between revisions of "Madagascar Code Patterns"

From Madagascar
Jump to navigation Jump to search
Line 37: Line 37:
  
 
The [http://www.delorie.com/gnu/docs/glibc/libc_226.html GNU C Library documentation] states that when doing I/O on a file (as opposed to a stream), the <tt>st_blksize</tt> field of the file attributes is a better choice than <tt>BUFSIZ</tt>.
 
The [http://www.delorie.com/gnu/docs/glibc/libc_226.html GNU C Library documentation] states that when doing I/O on a file (as opposed to a stream), the <tt>st_blksize</tt> field of the file attributes is a better choice than <tt>BUFSIZ</tt>.
==Parallelized, I/O-optimized loop over traces==
+
==Parallelized loop==
 +
===Description and usage===
 +
===Example===
 +
===Potential for improvement===
 +
 
 +
==Parallelized, I/O-optimized loop over samples==
 
===Description and usage===
 
===Description and usage===
 
===Example===
 
===Example===
 
===Potential for improvement===
 
===Potential for improvement===

Revision as of 06:29, 24 July 2011

All patterns are in C, unless otherwise noted.

I/O-optimized loop over samples

Description and usage

It consists of looping over an entire dataset and applying a given procedure to every single sample in a dataset, regardless of what "trace"/"frame"/"volume" it belongs to. Example: computing the sum of all elements of a dataset; computing a histogram; performing a clip operation; etc. It uses the BUFSIZ macro defined in stdio.h to ensure efficient stream I/O. Its occurences can be easily found by grepping for BUFSIZ in the codebase.

Example

<c> int n; /* Total number of elements in dataset */ int nbuf; /* Number of elements in I/O buffer */ float *fbuf; /* I/O array */ sf_file in=NULL; /* Input file. Here is stdin, but this is not compulsory */

in = sf_input("in");

n = sf_filesize(in);

/* This example uses float as data type. Any other data type (int, sf_complex, etc) can be used, as appropriate */ nbuf = BUFSIZ/sizeof(float);

fbuf = sf_floatalloc(nbuf);

for (; n > 0; n -= nbuf) {

   if (nbuf > n) nbuf = n;
   sf_floatread(fbuf, nbuf, in);
   for (i=0; i < nbuf; i++) {
       /* Do computations here */
   }

} </c>

Potential for improvement

This pattern should be parallelized using OpenMP.

The GNU C Library documentation states that when doing I/O on a file (as opposed to a stream), the st_blksize field of the file attributes is a better choice than BUFSIZ.

Parallelized loop

Description and usage

Example

Potential for improvement

Parallelized, I/O-optimized loop over samples

Description and usage

Example

Potential for improvement