/***************************************************************/ /* pi.c : Test program for calculating pi */ /* */ /* Written by : Whoo Soon Han */ /* Date : 11/18/1996 */ /* */ /* PI may be calculated by calculating the area under */ /* the curve 4/(1+x*x) in the interval from 0 to 1. */ /* */ /* Programming Technique : multi-processing & */ /* remote procedure call */ /* */ /***************************************************************/ #include "amdc96.h" #include #include #define NUM_SEC 1000000 float sum=0.0; int count=0; void show_table(Message msg, Location loc) { float *i = msg; int nodes; nodes = amdc_getNumNodes(); count += 1; printf("The Result of calculating from node %d = %f\n", amdc_getTag(msg),*(float*)msg); sum += *(float*)msg; if(count == nodes) { printf("pi can be calculated by adding all results from each node\n"); printf("pi = %f\n",sum);} } /***************************************************************/ /* cal_pi() : divides the area under the curve 4/(1+x*x) */ /* by number of nodes in the interval from 0 to 1. */ /* Each node calculates each allocated section */ /* and sends messages to the master node 0 */ /* as the result of calculation */ /***************************************************************/ void cal_pi() { int i, my_id, nodes; float *m; double delta_x, pi, x, y; LocName L; nodes = amdc_getNumNodes(); my_id = amdc_getMyId(); delta_x = 1.0/(nodes*NUM_SEC); x = (1.0*my_id/nodes)-(0.5*delta_x); for(i=1; i<=NUM_SEC;i++) { x += delta_x; pi += 4.0/(1.0+x*x); } pi = pi*delta_x; L = amdc_0DLocName(AMDC_SYMB_0); m = (float*)amdc_newMsg((Script)show_table, my_id, sizeof(float)); *m = pi; amdc_sendTo((Message)m,L); } void Main() { cal_pi(); while (1) amdc_pollBlock(); }