[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

B. Example `TOP-C' Application

There are several example `TOP-C' programs in the `topc/examples' subdirectory. We include one example in this manual. It does not contain any UPDATE actions, and therefore illustrates only a trivial form of parallelism (with no interaction among the slaves). The `topc/examples' subdirectory should be inspected for more sophisticated examples. After understanding this example, you may also want to look at 8. Advanced Features of `TOP-C', or if you are parallelizing a sequential program, then you may want to look at 9. `TOP-C' Raw Interface for Parallelizing Sequential Code.

This program produces an array of 10,000,000 random integers in one pass, and then finds the maximum value in a second pass. It would be compiled by: topcc MODE `file.out', where MODE is one of --seq, --mpi, or --pthread. One can control the number of slaves by executing: ./a.out --TOPC-num-slaves=num.

 
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <topc.h>

#define MAX 2000000
#define INCR MAX/10 /* We assume INCR divides MAX exactly */

int array[MAX];
int idx;
int max_int;

TOPC_BUF GenerateTaskInput() {
  int input_task;
  if (idx >= MAX) return NOTASK;
  input_task = idx;
  idx = idx + INCR;
  return TOPC_MSG( &input_task, sizeof(input_task) );
}
TOPC_BUF DoTaskRandom( int *ignore ) {
  int rand_int[INCR];
  int i;
  for ( i = 0; i < INCR; i++)
    rand_int[i] = rand();
  return TOPC_MSG( rand_int, INCR * sizeof(int) );
}
TOPC_ACTION CheckTaskRandom( int *input, int rand_vals[] ) {
  int curr_idx = *input;
  memcpy( array+curr_idx, rand_vals, INCR * sizeof(int) );
  return NO_ACTION;
}

TOPC_BUF GenerateTaskInMax() {
  int *input_task;
  if (idx >= MAX) return NOTASK;
  input_task = array + idx;
  idx = idx + INCR;
  return TOPC_MSG( input_task, INCR * sizeof(int) );
}
TOPC_BUF DoTaskMax( int subarray[] ) {
  int i;
  int max=0;
  for ( i = 0; i < INCR; i++)
    if ( subarray[i] > max )
      max = subarray[i];
  return TOPC_MSG( &max, sizeof(max) );
}
TOPC_ACTION CheckTaskMax( int ignore[], int *output ) {
  int curr_max = *output;
  if ( curr_max > max_int )
    max_int = curr_max;
  return NO_ACTION;
}

int main( int argc, char **argv ) {
  /* Set default to no trace; Override with:  ./a.out --TOPC-trace=1 */
  TOPC_OPT_trace = 0;
  TOPC_init( &argc, &argv );
  idx = 0; /* Initialize idx, and randomize values of array[] */
  TOPC_master_slave(GenerateTaskInput, DoTaskRandom, CheckTaskRandom,
                    NULL);
  if (TOPC_is_master())
    printf("Finished randomizing integers.\n");
  idx = 0; /* Re-initialize idx to 0, and find max. value in array[] */
  TOPC_master_slave( GenerateTaskInMax, DoTaskMax, CheckTaskMax, NULL );
  TOPC_finalize();
  printf("The maximum integer is:  %d\n", max_int);
  exit(0);
}


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Gene Cooperman on October, 6 2004 using texi2html