Okay, I'm gonna try to include the code...

Oh, I forgot, it was an MPI project. Just strip all that crap out...

Chris

/***********************************************************

*

* Program: num_int.c

* Name: Chris Kelly -

kelly@cs.utk.edu
* Date: 11/14/99

* Class: CS140 - 31788 - Wed. 5:45-8:35 - Lab 8

* Description: This program uses MPI to parallel-process

* a large problem. I have no idea what problem

* at this time, probably computing pi or

* some such. Okay, I've decided what to do. I'm

* gonna set it up to process a segment of a

* given function, expressed in RPN. Yep, good

* ol' numerical integration. If time

* allows, I'll use the process in ch. 3.3 to

* translate algebraic to RPN, making it slicker.

*

***********************************************************/

#include <stdio.h>

#include <mpi.h>

#include <string.h>

#define TRUE 1

#define FALSE 0

float evaluate (char *, float);

void alg(char *);

main(int argc,char *argv[]) {

int rank,size,tag=0,retval,i,source;

char expr[40],mode[4];

MPI_Status sts;

float area,seg,myrange,eachseg,mystart,mystop,f,limits[3];

float temp;

retval = MPI_Init(&argc,&argv);

retval = MPI_Comm_size(MPI_COMM_WORLD,&size);

retval = MPI_Comm_rank(MPI_COMM_WORLD,&rank);

/* find out who we are */

if (rank == 0) { /* we're boss, divvy up the work */

printf("Need Help? (y/n) ");

scanf("%s",expr);

if (expr[0] == 'y') {

printf("This program calculates the area under a \n");

printf("expression, between given limits, by adding\n");

printf("up the requested number of calculated rect-\n");

printf("angles. Just like in the calculus book, the\n");

printf("expression must be a FUNCTION, and it must\n");

printf("be CONTINUOUS between the given limits.\n");

printf("I only support +-/* , x and SINGLE digits.\n");

printf("The expression is entered in RPN or Algebraic,\n");

printf("Algebraic supports parenthesis, but the RPN\n");

printf("is more stable -\n");

printf("e.g. RPN 2x*x*3x*+2+\n\n");

printf("e.g. Alg 2*x*x+3*x+2\n\n");

printf("Note - unary minus not supported.\n\n");

}

printf("Will expression be in (r)pn or (a)lgebraic :");

scanf("%s",mode);

printf("Enter expression (NO SPACES) :");

scanf("%s",expr);

printf("Enter beginning of interval :");

scanf("%f",&limits[0]);

printf("Enter end of interval :");

scanf("%f",&temp);

limits[1]=(double) temp;

printf("Enter # of slices per processor :");

scanf("%f",&limits[2]);

if (mode[0] == 'a')

alg(expr);

for (i=1;i<size;i++) {

MPI_Send(expr,40,MPI_CHAR,i,tag,MPI_COMM_WORLD);

MPI_Send(limits,3,MPI_FLOAT,i,tag,MPI_COMM_WORLD);

}

area = 0.;

for (i = 1;i<size;i++) {

MPI_Recv(&seg,1,MPI_FLOAT,i,tag,MPI_COMM_WORLD,&sts);

area = area + seg;

}

printf("The area under the function is %f\n",area);

}

else { /* slave, accept work */

MPI_Recv(expr,40,MPI_CHAR,0,tag,MPI_COMM_WORLD,&sts);

MPI_Recv(limits,3,MPI_FLOAT,0,tag,MPI_COMM_WORLD,&sts);

myrange = limits[1] - limits[0];

eachseg = myrange/(size-1);

mystart = limits[0] + (rank-1)*eachseg;

mystop = mystart + eachseg;

area = 0.0;

for (f=mystart;f<mystop;f=f+eachseg/limits[2]) {

area = area + eachseg/limits[2] * evaluate(expr,f);

}

MPI_Send(&area,1,MPI_FLOAT,0,tag,MPI_COMM_WORLD);

}

retval = MPI_Finalize();

}

/**********************************

* evaluate - figure out value of given

* RPN function at given point

* WARNING - this sucker does NO error

* checking! expression MUST be right!

* code is mine, but concept is from

* Weiss text, page 72.

**********************************/

float evaluate (char *expr, float x) {

float stack[30];

int ptr=0,i;

for (i = 0; i<(strlen(expr));i++) {

if (expr[i] == ' ') {

/* ignore for now */

}

if (expr[i] == 'x') {

/* move current val onto stack */

stack[ptr] = x;

ptr++;

}

if ((expr[i] >= '0') && (expr[i] <='9')) {

/* is digit, stack it as value */

stack[ptr] = (int)expr[i] - 48;

ptr++;

}

if (expr[i] == '+') {

/* is add, add two vals on stack */

stack[ptr-2]=stack[ptr-2]+stack[ptr-1];

ptr = ptr -1;

}

if (expr[i] == '-') {

/* is minus, subtract two vals on stack */

stack[ptr-2]=stack[ptr-2]-stack[ptr-1];

ptr = ptr -1;

}

if (expr[i] == '*') {

/* is asterisk, multiply two vals on stack */

stack[ptr-2]=stack[ptr-2]*stack[ptr-1];

ptr = ptr -1;

}

if (expr[i] == '/') {

/* is slash, divide two vals on stack */

stack[ptr-2]=stack[ptr-2]/stack[ptr-1];

ptr = ptr -1;

}

}

if (ptr != 1) {

printf("Evaluate - stack failure!\n");

exit();

}

return(stack[0]);

}

/***********************************

* alg - infix->postfix converter -

* this damn routine was harder than the MPI!

* the code is mine, but the process comes

* from Weiss pg. 74.

***********************************/

void alg(char *expr) {

char expr2[40];

char stack[20];

int i,sptr = 0,eptr = 0,val,done = 0,sval;

for (i = 0; i<(strlen(expr));i++) {

val = 0;

if (expr[i] == ' ') {

/* ignore for now */

}

if (expr[i] == 'x') {

expr2[eptr] = 'x';

eptr++;

}

if ((expr[i] >= '0') && (expr[i] <='9')) {

expr2[eptr++] = expr[i];

}

if (expr[i] == '(') {

stack[sptr++]=expr[i];

}

if (expr[i] == ')') {

/* pop until left found */

while ((sptr > 0) && (stack[sptr-1] != '(')) {

expr2[eptr++]=stack[sptr-1];

sptr--;

}

sptr--; /* get rid of ( */

}

if (expr[i] == '+')

val = 1;

if (expr[i] == '-')

val = 1;

if (expr[i] == '*')

val = 2;

if (expr[i] == '/')

val = 2;

if (val > 0) {

done = 0;

while (done == 0) {

if (sptr == 0) { /* stack empty */

stack [sptr++] = expr[i];

done = 1;

}

else {

/* check for ( */

if (stack[sptr-1] == '(') {

stack[sptr++] = expr[i];

done = 1;

}

else {

if (stack[sptr-1] == '+')

sval = 1;

if (stack[sptr-1] == '-')

sval = 1;

if (stack[sptr-1] == '*')

sval = 2;

if (stack[sptr-1] == '/')

sval = 2;

/* pop til LWR stck val fnd */

if (sval<val) {

stack[sptr++]=expr[i];

done = 1;

}

else {

expr2[eptr++]=stack[sptr-1];

sptr--;

}

}

}

}

}

}

while (sptr>0) {

/* pop last of stack */

expr2[eptr++]=stack[sptr-1];

sptr--;

}

expr2[eptr]='\0';

strcpy(expr,expr2);

}