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 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 10000000
#define INCR MAX/10 /* We assume INCR divides MAX exactly */
int array[MAX];
int idx;
int max_int;
TOPC_BUF GenerateTaskInput() {
static 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( void *input ) {
static 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( void *input, void *output ) {
int curr_idx = *(int *)input;
int *rand_vals = output;
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( void *input ) {
int i, *subarray = input;
static max=0;
for ( i = 0; i < INCR; i++)
if ( subarray[i] > max )
max = subarray[i];
return TOPC_MSG( &max, sizeof(max) );
}
TOPC_ACTION CheckTaskMax( void *input, void *output ) {
int curr_max = *(int *)output;
if ( curr_max > max_int )
max_int = curr_max;
return NO_ACTION;
}
int main( int argc, char **argv ) {
TOPC_init( &argc, &argv );
idx = 0; /* Initialize idx, and randomize values of array[] */
TOPC_master_slave( GenerateTaskInput, DoTaskRandom, CheckTaskRandom, NULL );
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);
}
Go to the first, previous, next, last section, table of contents.