Difference between revisions of "Madagascar Code Patterns"

From Madagascar
Jump to navigation Jump to search
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
All patterns are in C, unless otherwise noted.
 
All patterns are in C, unless otherwise noted.
 +
 +
==Loop over ensembles==
 +
This is a simple loop over reading 1-D arrays ("traces"), 2-D ("gathers"), etc.
 +
 +
It is necessary in the case of algorithms that act on one ensemble at a time. Parallelizable with OMP.
  
 
==I/O-optimized loop over samples==
 
==I/O-optimized loop over samples==
Line 37: Line 42:
  
 
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>.
 +
 
==OMP parallelized loop==
 
==OMP parallelized loop==
 
===Description and usage===
 
===Description and usage===

Latest revision as of 18:20, 28 October 2011

All patterns are in C, unless otherwise noted.

Loop over ensembles

This is a simple loop over reading 1-D arrays ("traces"), 2-D ("gathers"), etc.

It is necessary in the case of algorithms that act on one ensemble at a time. Parallelizable with OMP.

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.

OMP parallelized loop

Description and usage

Shared-memory parallelization using the OpenMP library.

Example

Potential for improvement

Parallelized, I/O-optimized loop over samples

Description and usage

Example

Potential for improvement