Patch: Revise Manual Example Codes

Jeffrey Oldham oldham at codesourcery.com
Tue Dec 11 20:25:01 UTC 2001


The example (tutorial) programs changed to become more consistent,
easier to use, and, most importantly, more correct.  These are stored
in examples/Manual/Doof2d/.

2001-Dec-11  Jeffrey D. Oldham  <oldham at codesourcery.com>

	* Doof2d-Array-distributed.cpp: Remove <iostream>, which is not
	used.
	(DoofNinePt): Fix typo in comment.
	(main): Revise to use command-line arguments and Informs, not
	standard IO.  Modify to ensure domain size is a multiple of the
	number of processors.  Add blockAndEvaluate().
	* Doof2d-Array-element.cpp (main): Replace data-parallel
	initialization with loops.  Fix typo in comments.  Remove
	unnecessary blockAndEvaluate().
	* Doof2d-Array-parallel.cpp (main): Add
	blockAndEvaluate().  Fix typo in comment.
	* Doof2d-Array-stencil.cpp (DoofNinePt):
	Fix typo in comment.
	(main): Add blockAndEvaluate().
	* Doof2d-C-element.cpp (main): Fix typo in comment.
	* Doof2d-Field-distributed.cpp: Remove <iostream>, which is not
	used.
	(main): Revise to use command-line arguments and Informs, not
	standard IO.  Modify to ensure domain size is a multiple of the
	number of processors.  Add blockAndEvaluate().  Fix typo in
	comment.
	* Doof2d-Field-parallel.cpp (main): Revise input to use cin, not
	hard-coded constants.  Add blockAndEvaluate().  Fix typo in
	comment.

Applied to	mainline

Thanks,
Jeffrey D. Oldham
oldham at codesourcery.com
-------------- next part --------------
Index: Doof2d-Array-distributed.cpp
===================================================================
RCS file: /home/pooma/Repository/r2/examples/Manual/Doof2d/Doof2d-Array-distributed.cpp,v
retrieving revision 1.1
diff -c -p -r1.1 Doof2d-Array-distributed.cpp
*** Doof2d-Array-distributed.cpp	2001/12/04 00:07:00	1.1
--- Doof2d-Array-distributed.cpp	2001/12/11 19:01:38
***************
*** 1,4 ****
- #include <iostream>		// has std::cout, ...
  #include <stdlib.h>		// has EXIT_SUCCESS
  #include "Pooma/Arrays.h"	// has Pooma's Array
  
--- 1,3 ----
*************** public:
*** 30,36 ****
  
  private:
  
!   // In the average, weight element with this value.
    const double weight;
  };
  
--- 29,35 ----
  
  private:
  
!   // In the average, weight elements with this value.
    const double weight;
  };
  
*************** int main(int argc, char *argv[])
*** 38,60 ****
  {
    // Prepare the Pooma library for execution.
    Pooma::initialize(argc,argv);
!   
!   // Ask the user for the number of processors.
    long nuProcessors;
!   std::cout << "Please enter the number of processors: ";
!   std::cin >> nuProcessors;
  
!   // Ask the user for the number of averagings.
    long nuAveragings, nuIterations;
!   std::cout << "Please enter the number of averagings: ";
!   std::cin >> nuAveragings;
    nuIterations = (nuAveragings+1)/2; // Each iteration performs two averagings.
  
    // Ask the user for the number n of elements along one dimension of
    // the grid.
    long n;
!   std::cout << "Please enter the array size: ";
!   std::cin >> n;
  
    // Specify the arrays' domains [0,n) x [0,n).
    Interval<1> N(0, n-1);
--- 37,73 ----
  {
    // Prepare the Pooma library for execution.
    Pooma::initialize(argc,argv);
! 
!   // Since multiple copies of this program may simultaneously run, we
!   // canot use standard input and output.  Instead we use command-line
!   // arguments, which are replicated, for input, and we use an Inform
!   // stream for output.
!   Inform output;
! 
!   // Read the program input from the command-line arguments.
!   if (argc != 4) {
!     // Incorrect number of command-line arguments.
!     output << argv[0] << ": number-of-processors number-of-averagings number-of-values" << std::endl;
!     return EXIT_FAILURE;
!   }
!   char *tail;
! 
!   // Determine the number of processors.
    long nuProcessors;
!   nuProcessors = strtol(argv[1], &tail, 0);
  
!   // Determine the number of averagings.
    long nuAveragings, nuIterations;
!   nuAveragings = strtol(argv[2], &tail, 0);
    nuIterations = (nuAveragings+1)/2; // Each iteration performs two averagings.
  
    // Ask the user for the number n of elements along one dimension of
    // the grid.
    long n;
!   n = strtol(argv[3], &tail, 0);
!   // The dimension must be a multiple of the number of processors
!   // since we are using a UniformGridLayout.
!   n = ((n+nuProcessors-1) / nuProcessors) * nuProcessors;
  
    // Specify the arrays' domains [0,n) x [0,n).
    Interval<1> N(0, n-1);
*************** int main(int argc, char *argv[])
*** 85,90 ****
--- 98,105 ----
    // Set up the initial conditions.
    // All grid values should be zero except for the central value.
    a = b = 0.0;
+   // Ensure all data-parallel computation finishes before accessing a value.
+   Pooma::blockAndEvaluate();
    b(n/2,n/2) = 1000.0;
  
    // Create the stencil performing the computation.
*************** int main(int argc, char *argv[])
*** 101,107 ****
  
    // Print out the final central value.
    Pooma::blockAndEvaluate();	// Ensure all computation has finished.
!   std::cout << (nuAveragings % 2 ? a(n/2,n/2) : b(n/2,n/2)) << std::endl;
  
    // The arrays are automatically deallocated.
  
--- 116,122 ----
  
    // Print out the final central value.
    Pooma::blockAndEvaluate();	// Ensure all computation has finished.
!   output << (nuAveragings % 2 ? a(n/2,n/2) : b(n/2,n/2)) << std::endl;
  
    // The arrays are automatically deallocated.
  
Index: Doof2d-Array-element.cpp
===================================================================
RCS file: /home/pooma/Repository/r2/examples/Manual/Doof2d/Doof2d-Array-element.cpp,v
retrieving revision 1.1
diff -c -p -r1.1 Doof2d-Array-element.cpp
*** Doof2d-Array-element.cpp	2001/12/04 00:07:00	1.1
--- Doof2d-Array-element.cpp	2001/12/11 19:01:38
*************** int main(int argc, char *argv[])
*** 33,42 ****
  
    // Set up the initial conditions.
    // All grid values should be zero except for the central value.
!   a = b = 0.0;
    b(n/2,n/2) = 1000.0;
  
!   // In the average, weight element with this value.
    const double weight = 1.0/9.0;
  
    // Perform the simulation.
--- 33,44 ----
  
    // Set up the initial conditions.
    // All grid values should be zero except for the central value.
!   for (int j = 1; j < n-1; j++)
!     for (int i = 1; i < n-1; i++)
!       a(i,j) = b(i,j) = 0.0;
    b(n/2,n/2) = 1000.0;
  
!   // In the average, weight elements with this value.
    const double weight = 1.0/9.0;
  
    // Perform the simulation.
*************** int main(int argc, char *argv[])
*** 59,65 ****
    }
  
    // Print out the final central value.
-   Pooma::blockAndEvaluate();	// Ensure all computation has finished.
    std::cout << (nuAveragings % 2 ? a(n/2,n/2) : b(n/2,n/2)) << std::endl;
  
    // The arrays are automatically deallocated.
--- 61,66 ----
Index: Doof2d-Array-parallel.cpp
===================================================================
RCS file: /home/pooma/Repository/r2/examples/Manual/Doof2d/Doof2d-Array-parallel.cpp,v
retrieving revision 1.1
diff -c -p -r1.1 Doof2d-Array-parallel.cpp
*** Doof2d-Array-parallel.cpp	2001/12/04 00:07:00	1.1
--- Doof2d-Array-parallel.cpp	2001/12/11 19:01:38
*************** int main(int argc, char *argv[])
*** 38,46 ****
    // Set up the initial conditions.
    // All grid values should be zero except for the central value.
    a = b = 0.0;
    b(n/2,n/2) = 1000.0;
  
!   // In the average, weight element with this value.
    const double weight = 1.0/9.0;
  
    // Perform the simulation.
--- 38,48 ----
    // Set up the initial conditions.
    // All grid values should be zero except for the central value.
    a = b = 0.0;
+   // Ensure all data-parallel computation finishes before accessing a value.
+   Pooma::blockAndEvaluate();
    b(n/2,n/2) = 1000.0;
  
!   // In the average, weight elements with this value.
    const double weight = 1.0/9.0;
  
    // Perform the simulation.
Index: Doof2d-Array-stencil.cpp
===================================================================
RCS file: /home/pooma/Repository/r2/examples/Manual/Doof2d/Doof2d-Array-stencil.cpp,v
retrieving revision 1.1
diff -c -p -r1.1 Doof2d-Array-stencil.cpp
*** Doof2d-Array-stencil.cpp	2001/12/04 00:07:00	1.1
--- Doof2d-Array-stencil.cpp	2001/12/11 19:01:38
*************** public:
*** 30,36 ****
  
  private:
  
!   // In the average, weight element with this value.
    const double weight;
  };
  
--- 30,36 ----
  
  private:
  
!   // In the average, weight elements with this value.
    const double weight;
  };
  
*************** int main(int argc, char *argv[])
*** 68,73 ****
--- 68,75 ----
    // Set up the initial conditions.
    // All grid values should be zero except for the central value.
    a = b = 0.0;
+   // Ensure all data-parallel computation finishes before accessing a value.
+   Pooma::blockAndEvaluate();
    b(n/2,n/2) = 1000.0;
  
    // Create the stencil performing the computation.
Index: Doof2d-C-element.cpp
===================================================================
RCS file: /home/pooma/Repository/r2/examples/Manual/Doof2d/Doof2d-C-element.cpp,v
retrieving revision 1.1
diff -c -p -r1.1 Doof2d-C-element.cpp
*** Doof2d-C-element.cpp	2001/12/04 00:07:00	1.1
--- Doof2d-C-element.cpp	2001/12/11 19:01:38
*************** int main()
*** 37,43 ****
        a[i][j] = b[i][j] = 0.0;
    b[n/2][n/2] = 1000.0;
  
!   // In the average, weight element with this value.
    const double weight = 1.0/9.0;
  
    // Perform the simulation.
--- 37,43 ----
        a[i][j] = b[i][j] = 0.0;
    b[n/2][n/2] = 1000.0;
  
!   // In the average, weight elements with this value.
    const double weight = 1.0/9.0;
  
    // Perform the simulation.
Index: Doof2d-Field-distributed.cpp
===================================================================
RCS file: /home/pooma/Repository/r2/examples/Manual/Doof2d/Doof2d-Field-distributed.cpp,v
retrieving revision 1.1
diff -c -p -r1.1 Doof2d-Field-distributed.cpp
*** Doof2d-Field-distributed.cpp	2001/12/04 00:07:00	1.1
--- Doof2d-Field-distributed.cpp	2001/12/11 19:01:38
***************
*** 1,4 ****
- #include <iostream>		// has std::cout, ...
  #include <stdlib.h>		// has EXIT_SUCCESS
  #include "Pooma/Fields.h"	// has Pooma's Field
  
--- 1,3 ----
*************** int main(int argc, char *argv[])
*** 9,28 ****
    // Prepare the Pooma library for execution.
    Pooma::initialize(argc,argv);
    
!   // nuIterations is the number of simulation iterations.
!   const int nuIterations = 10/2;
! 
!   // In the average, weight element with this value.
!   const double weight = 1.0/9.0;
  
!   // nuProcessors is the number of processors along one dimension.
!   const int nuProcessors = 2;
  
    // Ask the user for the number n of elements along one dimension of
    // the grid.
    long n;
!   std::cout << "Please enter the array size: ";
!   std::cin >> n;
  
    // Specify the fields' domains [0,n) x [0,n).
    Interval<1> N(0, n-1);
--- 8,43 ----
    // Prepare the Pooma library for execution.
    Pooma::initialize(argc,argv);
    
!   // Since multiple copies of this program may simultaneously run, we
!   // canot use standard input and output.  Instead we use command-line
!   // arguments, which are replicated, for input, and we use an Inform
!   // stream for output.
!   Inform output;
! 
!   // Read the program input from the command-line arguments.
!   if (argc != 4) {
!     // Incorrect number of command-line arguments.
!     output << argv[0] << ": number-of-processors number-of-averagings number-of-values" << std::endl;
!     return EXIT_FAILURE;
!   }
!   char *tail;
  
!   // Determine the number of processors.
!   long nuProcessors;
!   nuProcessors = strtol(argv[1], &tail, 0);
! 
!   // Determine the number of averagings.
!   long nuAveragings, nuIterations;
!   nuAveragings = strtol(argv[2], &tail, 0);
!   nuIterations = (nuAveragings+1)/2; // Each iteration performs two averagings.
  
    // Ask the user for the number n of elements along one dimension of
    // the grid.
    long n;
!   n = strtol(argv[3], &tail, 0);
!   // The dimension must be a multiple of the number of processors
!   // since we are using a UniformGridLayout.
!   n = ((n+nuProcessors-1) / nuProcessors) * nuProcessors;
  
    // Specify the fields' domains [0,n) x [0,n).
    Interval<1> N(0, n-1);
*************** int main(int argc, char *argv[])
*** 59,66 ****
--- 74,86 ----
    // Set up the initial conditions.
    // All grid values should be zero except for the central value.
    a = b = 0.0;
+   // Ensure all data-parallel computation finishes before accessing a value.
+   Pooma::blockAndEvaluate();
    b(n/2,n/2) = 1000.0;
  
+   // In the average, weight elements with this value.
+   const double weight = 1.0/9.0;
+ 
    // Perform the simulation.
    for (int k = 0; k < nuIterations; ++k) {
      // Read from b.  Write to a.
*************** int main(int argc, char *argv[])
*** 77,83 ****
    }
  
    // Print out the final central value.
!   std::cout << b(n/2,n/2) << std::endl;
  
    // The fields are automatically deallocated.
  
--- 97,104 ----
    }
  
    // Print out the final central value.
!   Pooma::blockAndEvaluate();	// Ensure all computation has finished.
!   output << (nuAveragings % 2 ? a(n/2,n/2) : b(n/2,n/2)) << std::endl;
  
    // The fields are automatically deallocated.
  
Index: Doof2d-Field-parallel.cpp
===================================================================
RCS file: /home/pooma/Repository/r2/examples/Manual/Doof2d/Doof2d-Field-parallel.cpp,v
retrieving revision 1.1
diff -c -p -r1.1 Doof2d-Field-parallel.cpp
*** Doof2d-Field-parallel.cpp	2001/12/04 00:07:00	1.1
--- Doof2d-Field-parallel.cpp	2001/12/11 19:01:38
*************** int main(int argc, char *argv[])
*** 9,24 ****
    // Prepare the Pooma library for execution.
    Pooma::initialize(argc,argv);
    
!   // nuIterations is the number of simulation iterations.
!   const int nuIterations = 10/2;
  
-   // In the average, weight element with this value.
-   const double weight = 1.0/9.0;
- 
    // Ask the user for the number n of elements along one dimension of
    // the grid.
    long n;
!   std::cout << "Please enter the array size: ";
    std::cin >> n;
  
    // Specify the fields' domains [0,n) x [0,n).
--- 9,24 ----
    // Prepare the Pooma library for execution.
    Pooma::initialize(argc,argv);
    
!   // Ask the user for the number of averagings.
!   long nuAveragings, nuIterations;
!   std::cout << "Please enter the number of averagings: ";
!   std::cin >> nuAveragings;
!   nuIterations = (nuAveragings+1)/2; // Each iteration performs two averagings.
  
    // Ask the user for the number n of elements along one dimension of
    // the grid.
    long n;
!   std::cout << "Please enter the field size: ";
    std::cin >> n;
  
    // Specify the fields' domains [0,n) x [0,n).
*************** int main(int argc, char *argv[])
*** 44,51 ****
--- 44,56 ----
    // Set up the initial conditions.
    // All grid values should be zero except for the central value.
    a = b = 0.0;
+   // Ensure all data-parallel computation finishes before accessing a value.
+   Pooma::blockAndEvaluate();
    b(n/2,n/2) = 1000.0;
  
+   // In the average, weight elements with this value.
+   const double weight = 1.0/9.0;
+ 
    // Perform the simulation.
    for (int k = 0; k < nuIterations; ++k) {
      // Read from b.  Write to a.
*************** int main(int argc, char *argv[])
*** 62,68 ****
    }
  
    // Print out the final central value.
!   std::cout << b(n/2,n/2) << std::endl;
  
    // The fields are automatically deallocated.
  
--- 67,74 ----
    }
  
    // Print out the final central value.
!   Pooma::blockAndEvaluate();	// Ensure all computation has finished.
!   std::cout << (nuAveragings % 2 ? a(n/2,n/2) : b(n/2,n/2)) << std::endl;
  
    // The fields are automatically deallocated.


More information about the pooma-dev mailing list