Sunday, April 27, 2008

B.Tech 8 sem ACA Lab Program

// prefix.cpp : main project file.
#include "stdafx.h"
#include "omp.h"
#include "stdio.h"
#include "conio.h"
#include "math.h"

using namespace System;

int main(array ^args)
{
// Console::WriteLine(L"Hello World");
int a[100],j,k,n,i;
//int i,j,k;
//float n;
//float n,i;
int p;
printf("enter the count of nos. to be added"); //no of processors spawned
scanf("%d", &n);
for(k = 0 ; k {
printf("enter the no. to be added"); //value at each processor
scanf("%d ", &a[k]);
}

#pragma omp parallel private(x,y) shared(sum,n)
{
/*for(i=0;i {
printf(" \nthe value of n is %f", n);
float z= log(n);
for (j=0; j<=(ceil(z-1));j++)
{
printf(" \nthe value of z is %f", z);
printf(" \nthe value of j is %d", j);
if ((i - (2^j)) >= 0)
{
a[i]=a[i]+a[(i-(2^j))];
printf("\nthe sum is %d ", a[i]);
}
}
}*/

p=floor(log(n));
for (j=0;j<=p;j++)
{
if((i-(2^j))>=0))
{
a[i]=a[i]+a[i-(2^j)];

}
printf("\n the value of a[%d] = %d",i,a[i]);
}

//printf("the sum is %d ", a[0]);
}
getch();
return 0;
}











///////////////////////////////////////////////
//
// Java PROGRAM of Gaussian Elimination
// Name: Gaussian_dyn.java
// Programmed by Jun Ni, Ph.D.M.E., at jni@hpcsoft.com
// Last modified July 17, 2003
// Reference: Numerical Methods for Engineers
// by Griffiths and Smith, 1991, CRC Press
//
///////////////////////////////////////////////

public class Gaussian_dyn
{

public static void main(String[] args)
{
int n;
System.out.println("******* Gaussian Elimination *********");
System.out.println("input number of equations n=");
n=MyInput.readInt();

double [][] a=new double[n][n];
double [] b=new double[n];
double x,sum;
int i,j,k;

System.out.println("Input matrix coefficients a(i,j)=");
for (i=0;i for (j=0;j a[i][j]=MyInput.readDouble();

System.out.println("Input right-hand side vector b(i)=");
for (i=0;i b[i]=MyInput.readDouble();


System.out.println("Coefficient matrix:");
printMatrix(a,n);
System.out.println("\nRight-hand side vector:");
printVector(b,n);
System.out.println();
// convert to upper triangular form
for (k=0;k {
if ( Math.abs(a[k][k])>=1.e-6)

{
for (i=k+1;i {
x = a[i][k]/a[k][k];
for (j=k+1;j b[i] = b[i] - b[k]*x;
}
}
else
{
System.out.println("Zero pivot found in line:"+k);
System.exit(1);
}
}

/* back substitution */
b[n-1]=b[n-1]/a[n-1][n-1];
for (i=n-2;i>=0;i--)
{
sum = b[i];
for (j=i+1;j sum = sum - a[i][j]*b[j];
b[i] = sum/a[i][i];
}
System.out.println("Solution:");
printVector(b,n);
}


static void printMatrix(double a[][], int n)
{
for (int i=0;i {
for (int j=0;j System.out.println(a[i][j]);
System.out.println();
}
}

static void printVector(double b[],int n)
{
for (int i=0;i System.out.println(b[i]);
}
} //class ends





/********************************************************/
/* HyperQuickSort Program */
/********************************************************/

#include
#include
#include
#include
#include
#include
#include
#include "hqs.h"

void merge( );
void QuickSort( );


/*********************************************************************/
/* --- SendData--- send subarray and length to each node */
/*********************************************************************/
void SendData(int numberToSort, int numberOfNodes, int *length0, int items[]) {
int
i, /* Loop control variable */
extras, /* Number of processors that get one more */
length, /* Lengths of subarrays for each node */
length1; /* Lengths of subarrays for each node */


/* Distribute array data to nodes */

/* Initialize array to sort */
srand( SEED );
for ( i = 0; i < numberToSort; i++ ) {
items[i] = rand( );
}
printf( "Data generation complete. Sorting begins.\n\n" );
fflush( stdout );

/* Load balance, at least initially */
length = numberToSort / numberOfNodes;
extras = numberToSort - length * numberOfNodes;
length1 = length + 1;

/* Send initial data to be sorted to each process */
for ( i = 1; i < numberOfNodes; i++ ) {
if ( i >= extras ) {
MPI_Send( &items[ length * i + extras ], length, MPI_INT,
i, DATA, MPI_COMM_WORLD );
} else {
MPI_Send( &items[ length * i + ( i < extras ) * i ],
length1, MPI_INT, i, DATA, MPI_COMM_WORLD );
}
}


/* Length for processor 0 */
if ( extras )
*length0 = length1;
else
*length0 = length;
}


/*********************************************************/
/* HQSnode */
/*********************************************************/
int HQSnode( int argc, char *argv[ ] ) {
int
half[ MAX_ARRAY ], /* Subarray for merging */
lower[ MAX_ARRAY ], /* Array for subdividing problem */
items[ MAX_ARRAY ], /* Array of info to be sorted */
localMedian, /* Median of all sorted substrings */
globalMedian, /* Median of all sorted substrings */
medians[ MAX_NODES ], /* Array of medians */
upper[ MAX_ARRAY ]; /* Array for subdividing problem */

char hostName[ 100 ]; /* hostname */

double
startTime,
endTime; /* variables to handle MPI timer results */

int dimension, /* Dimension of hypercube */
error=0,
first, /* Number of first node in subcube */
halfLength, /* Length of subarray for merging */
i, /* Loop control variable */
j, /* Loop control variable */
k, /* Loop control variable */
last, /* Number of last node in subcube */
length, /* Length of this node's list */
lowerLength, /* Length of subdivision */
myCubeNumber, /* Current subcube number */
myNodeNumber, /* My node number */
myRank, /* My rank in current subcube */

numberToSort, /* Size of array to sort */
numberOfNodes, /* Number of nodes in hypercube */
numberOfProcessors, /* Number of processors */
numberOfCubes, /* Number of cubes in subdivision of
hypercube */
partner, /* Number of node to exchange with in this
subdivision of hypercube */
cubeSize, /* Number of nodes in subcube */
time, /* User time in 1/10s of a second */
upperLength; /* Length of subdivision */

MPI_Status
Status; /* Status returned from MPI_Recv function */

MPI_Comm
subcubeCommunicator; /* The following variable is needed to do
subgroup communication */

struct rusage
resources; /* Structure to access time */

MPI_Comm_rank( MPI_COMM_WORLD,&myNodeNumber );
MPI_Comm_size( MPI_COMM_WORLD,&numberOfProcessors );

gethostname( hostName, 100);
printf( "Hello from process %d out of %d on %s\n",
myNodeNumber, numberOfProcessors, hostName );
fflush( stdout );

/* Initialize variables */
dimension = floor( log2( (double) numberOfProcessors ) );
numberOfNodes = 1 << dimension;

if ( argc != 3 ) {
/* Check command line arguments; "random" is not really necessary;
it's just used to show a concept */

if ( myNodeNumber == HOST ) {
printf( "Usage: %s random \n", argv[0] );
fflush( stdout );
}
error = 1;
} else if ( !(strcmp( argv[1], "random" ) == 0) ){
if ( myNodeNumber == HOST ) {
printf( "The first command line argument must be random\n");
fflush( stdout );
}
error = 1;
}

/* Abort if there's an error on the command line */
if ( error ) {
return (1);
}

numberToSort = atol( argv[2] );
if ( numberToSort < 2 || numberToSort > MAX_ARRAY ) {
if ( myNodeNumber == HOST ) {
printf( "The number to sort must be between 2 and %d\n",
MAX_ARRAY );
}
error = 1;
}

/* Abort if there's an error on the command line */
if ( error ) {
return (1);
}

/* Generate numbers to sort and split the work among the processors */
if ( myNodeNumber == HOST ) {

/* SendData will return length and array for the HOST node */
SendData( numberToSort, numberOfNodes, &length, items );

} else if ( myNodeNumber < numberOfNodes ) {

/* If this processor is actually participating */
MPI_Recv( items, MAX_ARRAY, MPI_INT, HOST, DATA, MPI_COMM_WORLD,
&Status );
MPI_Get_count( &Status, MPI_INT, &length );

} else {

/* If this processor is NOT participating */
length = 0;

}

/* NOTE: Timing is system dependent */
/* Start timing */
MPI_Barrier( MPI_COMM_WORLD );

getrusage( RUSAGE_SELF, &resources );
time = resources.ru_utime.tv_sec * 1000000 + resources.ru_utime.tv_usec;

startTime = MPI_Wtime( );

/* Do only if this processor is participating */
if ( myNodeNumber < numberOfNodes ) {

/* 2) Sort using quicksort */
QuickSort( items, 0, length-1 );

}

for ( i = dimension; i > 0; i-- ) {

/* Do only if this processor is participating */
if ( myNodeNumber < numberOfNodes ) {

/* 3) Broadcast median to rest of subcube */

/* Determine which subcube this processor is in */
numberOfCubes = 1 << ( dimension - i );
cubeSize = numberOfNodes / numberOfCubes;
myCubeNumber = myNodeNumber >> i;

/* Calculate median */
if ( length % 2 ) { /* if odd */
localMedian = items[ length / 2 ];
} else { /* if even */
localMedian = ( items[ ( length / 2 ) - 1 ]
+ items[ length / 2 ] ) / 2;
};

first = myCubeNumber * cubeSize;
last = first + cubeSize;
}

/* Set up communication subgroup */

if ( myNodeNumber < numberOfNodes ) {

/* Do only if this processor is participating */
MPI_Comm_split( MPI_COMM_WORLD, myCubeNumber, myNodeNumber,
&subcubeCommunicator );
MPI_Comm_rank( subcubeCommunicator, &myRank );

} else {

/* Do if this processor is NOT participating - Note use of
MPI_UNDEFINED */
MPI_Comm_split( MPI_COMM_WORLD, MPI_UNDEFINED, myNodeNumber,
&subcubeCommunicator );
}

/* Do only if this processor is participating */
if ( myNodeNumber < numberOfNodes ) {

/* Broadcast median to each node in subcube (except me) */
medians[ myRank ] = localMedian;
for ( j = 0; j < cubeSize; j++ ) {
MPI_Bcast( &medians[j], 1, MPI_INT, j, subcubeCommunicator );
}


/* Determine median of medians */
QuickSort( medians, 0, cubeSize-1 );

/* There are always an even number of medians */
globalMedian = ( medians[ ( cubeSize / 2 ) - 1 ]
+ medians[ cubeSize / 2 ] ) / 2;


/* 4) Create lower and upper 'halves' of items */
j = 0;
k = 0;
while ( ( items[ j ] <= globalMedian ) && ( j < length ) ) {
lower[ k ] = items[ j ];
k++;
j++;
};
lowerLength = k;
k = 0;
while ( j < length ) {
upper[ k ] = items[ j ];
k++;
j++;
};
upperLength = k;

/* 5) Send lower/upper 'half' to partner */

/* Determine partner by inverting bits of node number */
partner = ( numberOfNodes - 1 ) - myNodeNumber;
partner = ( myNodeNumber ^ ( ~( ~0 << 1 ) << ( i-1 ) ) );

/* Exchange info with partner */
if ( myNodeNumber > partner ) {

/* Send lower 'half' to partner */
MPI_Send( lower, lowerLength, MPI_INT, partner, HALF,
MPI_COMM_WORLD );

/* Receive upper 'half' from partner */
MPI_Recv( half, MAX_ARRAY, MPI_INT, partner, HALF,
MPI_COMM_WORLD,
&Status );
MPI_Get_count( &Status, MPI_INT, &halfLength );

} else {

/* Receive lower 'half' from partner */
MPI_Recv( half, MAX_ARRAY, MPI_INT, partner, HALF,
MPI_COMM_WORLD,
&Status );
MPI_Get_count( &Status, MPI_INT, &halfLength );

/* Send upper 'half' to partner */
MPI_Send( upper, upperLength, MPI_INT, partner, HALF,
MPI_COMM_WORLD );
};


/* 6) Merge lower/upper array with 'half' array into items array */
if (myNodeNumber > partner) {
merge( upper, half, items, upperLength, halfLength );
length = upperLength + halfLength;
} else {
merge( lower, half, items, lowerLength, halfLength );
length = lowerLength + halfLength;
};
};
};


/*NOTE: Timing is system dependent */
/* End timing */
getrusage( RUSAGE_SELF, &resources );
time = resources.ru_utime.tv_sec * 1000000 + resources.ru_utime.tv_usec
- time;

MPI_Barrier( MPI_COMM_WORLD );
endTime = MPI_Wtime( ) - startTime;

/* Confirm array is sorted */
for ( i = 0; i < length - 1; i++ ) {
if ( items[ i ] > items[ i + 1 ] ) {
printf("%d error --(%d:%d) (%d:%d)\n", myNodeNumber, i, items[ i ],
i + 1, items[ i + 1 ] );
fflush( stdout );
}
}

/* Report results */
if ( length != 0 ) {
printf( "Node %d (time=%6.6f seconds/%6.6f seconds, length = %d) : %d ... %d\n\n",
myNodeNumber, ( float ) time / 1000000.0, endTime, length,
items[ 0 ], items[ length - 1 ] );
fflush( stdout );
} else {
printf( "Node %d (time=%6.6f seconds/%6.6f seconds, length = %d)\n\n",
myNodeNumber, ( float ) time / 1000000.0, endTime, length);
fflush( stdout );
}
return (0);

}


/*******************************************************************/
/* --- merge --- merge and sort two sorted arrays into third array */
/*******************************************************************/
void merge(int a[ ], int b[ ], int c[ ], int m, int n) {
int i, /* Loop control for array a */
j, /* Loop control for array b */
k; /* Loop control for array c */

i = 0;
j = 0;
k = 0;

while ( ( i < m ) && ( j < n ) ) {
if ( a[ i ] < b[ j ] ) {
c[ k ] = a[ i ];
k++;
i++;
} else {
c[ k ] = b[ j ];
k++;
j++;
};
}


/* Pick up any remainder */
while ( i < m ) {
c[ k ] = a[ i ];
k++;
i++;
};
while ( j < n ) {
c[ k ] = b[ j ];
k++;
j++;
};

}


/********************************************************/
/* Swap: swap two values */
/********************************************************/
void Swap(int *i, int *j) {
int t; /* Temporary variable for swap */

t = *i;
*i = *j;
*j = t;
}


/*****************************************************************************/
/* Median3: Find the median of left, center, and right; return the median */
/* as the pivot, and "hide" the pivot as the next-to-last (right-1) element. */
/*****************************************************************************/
void Median3 (int a[], int left, int right, int *pivot) {
int center; /* Temporary variable for determining median */

center = ( left + right ) / 2;

if ( a[ left ] > a[ center ] ) {
Swap ( &a[ left ], &a[ center ] );
}
if ( a[ left ] > a[ right ] ) {
Swap ( &a[ left ], &a[ right ] );
}
if ( a[ center ] > a[ right ] ) {
Swap ( &a[ center ], &a[ right ] );
}

*pivot = a[ center ];
Swap ( &a[ center ], &a[ right - 1 ] );
}


/********************************************************/
/* QuickSort: the basic QuickSort algorithm. */
/* Note requires > 2 items to sort properly */
/********************************************************/
void QuickSort(int a[], int left, int right) {
int
i, /* Loop control variable */
j, /* Loop control variable */
pivot; /* Quicksort pivot */

if ( left < right ) {
Median3 (a, left, right, &pivot); /* Find the pivot. */

/* Do the partition. */
i = left;
j = right - 1;
if ( i < j ) {
do {
do {
i++;
} while ( a[ i ] < pivot );
do {
j-- ;
} while ( a[ j ] > pivot );
Swap( &a[ i ], &a[ j ] );
} while ( j > i );

Swap( &a[ i ], &a[ j ] ); /* Undo last swap. */
Swap( &a[ i ], &a[ right - 1 ]); /* Restore pivot. */
};

QuickSort( a, left, i - 1 );
QuickSort( a, i + 1, right );
}
}


/*******************************************************/
/* Main program - starts up on each machine */
/*******************************************************/
void main( int argc, char *argv[ ] ) {

/* Set up mpi machine */
MPI_Init( &argc, &argv );

HQSnode( argc, argv );

MPI_Finalize( );












MatrixMultiplication.java

Below is the syntax highlighted version of MatrixMultiplication.java from §9.5 Numerical Linear Algebra.

/*************************************************************************
* Compilation: javac MatrixMultiplication.java
* Execution: java MatrixMultiplication
*
* 6 different ways to multiply two N-by-N matrices.
* Illustrates importance of row-major vs. column-major ordering.
*
* % java MatrixMultiplication 400
* Generating input: 0.765 seconds
* Order ijk: 7.875 seconds
* Order ikj: 14.542 seconds
* Order jik: 7.838 seconds
* Order jki: 20.86 seconds
* Order kij: 13.132 seconds
* Order kji: 20.813 seconds
*
*************************************************************************/

public class MatrixMultiplication {
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
long start, stop;
double elapsed;


// generate input
start = System.currentTimeMillis();

double[][] A = new double[N][N];
double[][] B = new double[N][N];
double[][] C;

for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
A[i][j] = Math.random();

for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
B[i][j] = Math.random();

stop = System.currentTimeMillis();
elapsed = (stop - start) / 1000.0;
System.out.println("Generating input: " + elapsed + " seconds");


// order 1: ijk = dot product version
C = new double[N][N];
start = System.currentTimeMillis();
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
for (int k = 0; k < N; k++)
C[i][k] += A[i][j] * B[j][k];
stop = System.currentTimeMillis();
elapsed = (stop - start) / 1000.0;
System.out.println("Order ijk: " + elapsed + " seconds");


// order 2: ikj
C = new double[N][N];
start = System.currentTimeMillis();
for (int i = 0; i < N; i++)
for (int k = 0; k < N; k++)
for (int j = 0; j < N; j++)
C[i][k] += A[i][j] * B[j][k];
stop = System.currentTimeMillis();
elapsed = (stop - start) / 1000.0;
System.out.println("Order ikj: " + elapsed + " seconds");

// order 3: jik
C = new double[N][N];
start = System.currentTimeMillis();
for (int j = 0; j < N; j++)
for (int i = 0; i < N; i++)
for (int k = 0; k < N; k++)
C[i][k] += A[i][j] * B[j][k];
stop = System.currentTimeMillis();
elapsed = (stop - start) / 1000.0;
System.out.println("Order jik: " + elapsed + " seconds");

// order 4: jki = GAXPY version
C = new double[N][N];
start = System.currentTimeMillis();
for (int j = 0; j < N; j++)
for (int k = 0; k < N; k++)
for (int i = 0; i < N; i++)
C[i][k] += A[i][j] * B[j][k];
stop = System.currentTimeMillis();
elapsed = (stop - start) / 1000.0;
System.out.println("Order jki: " + elapsed + " seconds");

// order 5: kij
C = new double[N][N];
start = System.currentTimeMillis();
for (int k = 0; k < N; k++)
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
C[i][k] += A[i][j] * B[j][k];
stop = System.currentTimeMillis();
elapsed = (stop - start) / 1000.0;
System.out.println("Order kij: " + elapsed + " seconds");

// order 6: kji = outer product version
C = new double[N][N];
start = System.currentTimeMillis();
for (int k = 0; k < N; k++)
for (int j = 0; j < N; j++)
for (int i = 0; i < N; i++)
C[i][k] += A[i][j] * B[j][k];
stop = System.currentTimeMillis();
elapsed = (stop - start) / 1000.0;
System.out.println("Order kji: " + elapsed + " seconds");


// order 7: jik optimized ala JAMA
C = new double[N][N];
start = System.currentTimeMillis();
double[] bj = new double[N];
for (int j = 0; j < N; j++) {
for (int k = 0; k < N; k++) bj[k] = B[k][j];
for (int i = 0; i < N; i++) {
double[] ai = A[i];
double s = 0;
for (int k = 0; k < N; k++) {
s += ai[k] * bj[k];
}
C[i][j] = s;
}
}
stop = System.currentTimeMillis();
elapsed = (stop - start) / 1000.0;
System.out.println("Order kji: " + elapsed + " seconds");


}

}





import java.lang.*;
import java.io.*;
class MatMulti extends Thread
{
static int in1[][];
static int in2[][];
static int out[][];
static int n=2;
int row;
M// merge.cpp : main project file.

#include "stdafx.h"
#include "omp.h"
#include "stdio.h"
#include "conio.h"
#include "math.h"

using namespace System;

int main(array ^args)
{
int a[20],b[20],c[20],i,low,high,x,j,k,n,z;
int index,y;

printf("enter the no. of elements in sorted array <=20"); //value at each processor
scanf("%d ", &n);

for(k=1; k<=(n/2) ; k++)
{
printf("enter the no. IN FIRST ARRAY IN SORTED ORDER"); //value at each processor
scanf("%d ", &a[k]);
}
printf("\n\n");
for(k = (n/2)+1; k<=n ; k++)
{
printf("enter the no. IN SECOND ARRAY IN SORTED ORDER"); //value at each processor
scanf("%d ", &a[k]);
}
for(i = 1; i<=n ; i++)
{
printf("a[%d]=%d", i, a[i]);
c[i]=0;
}

#pragma omp parallel
{

for(i=1;i<=n;i++)
{
if(i<= (n/2))
{
low =(n/2)+1;
high=n;
// printf("\n%d %d", low,high);
}
else
{
low=1;
high=(n/2);
// printf("\n%d %d",low,high);
}

x=a[i];
//printf("x is %d", x);
while(low{
index= ((low+high)/2);
//printf("\n%d ", index);
//printf("a[%d]=%d ", index, a[index]);
if(x {high=index-1;
//printf("\n%d ", high);
}
else
{low=index+1;
//printf("\n%d ", low);
}
};
y=high+i-(n/2);
printf("\ny is %d", y);
c[y]=x;

}
}

printf("\nthe no.in SORTED ORDER");
for(z = 1 ; z<=n ; z++)
{
printf("\t%d ", c[z]);
}
getch();
return 0;
}atMulti(int i)
{
row=i;
this.start();
}
public void run()
{
int i,j;
for(i=0;i {
out[row][i]=0;
for(j=0;j out[row][i]=out[row][i]+in1[row][j]*in2[j][i];
}
}
public static void main(String args[])
{
int i,j;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the order of Matrix : ");
try
{
n=Integer.parseInt(br.readLine());
}catch(Exception e){}
in1=new int[n][n];
in2=new int[n][n];
out=new int[n][n];
System.out.println("Enter the First Matrix : ");
for(i=0;i {
for(j=0;j {
try
{
in1[i][j]=Integer.parseInt(br.readLine());
}catch(Exception e){}
}
}
System.out.println("Enter the Second Matrix : ");
for(i=0;i {
for(j=0;j {
try
{
in2[i][j]=Integer.parseInt(br.readLine());
}catch(Exception e){}
}
}
MatMulti mat[]=new MatMulti[n];
for(i=0;i mat[i]=new MatMulti(i);
try
{
for(i=0;i mat[i].join();
}catch(Exception e){}
System.out.println("OUTPUT :");
for(i=0;i for(j=0;j System.out.println(out[i][j]);
}
}


// abcdef.cpp : main project file.

#include "stdafx.h"
#include "omp.h"
#include "stdio.h"
#include "conio.h"
#include "math.h"

using namespace System;

int main(array ^args)
{
// Console::WriteLine(L"Hello World");
int n,a[100];
int i,j,k;

printf("enter the count of nos. to be added");
scanf("%d ", &n);
for(k = 0 ; k {
printf("enter the no. to be added");
scanf("%d ", &a[k]);
}

#pragma omp parallel private(x,y) shared(sum,n)
{
for(i=0;i<= (floor(n/2))-1;i++)
{
int z= log(n);
for (j=0; j<=(ceil(z))-1;j++)
{
if (((i % (2^j)) == 0) &&( ((2*i) +( 2^j)) {
a[2*i]=a[2*i]+a[(2*i) +(2^j)];
}
}
}

}
printf("the sum is %d ", a[0]);
getch();
return 0;
}


private static void quickSort(int worker, int left, int right) {
int pivot = nums[left]; int l = left, r = right;
// enclose what this worker is working on in a black outline rectangle
int ymin = minn(nums, left, right);
int ymax = maxx(nums, left, right);
float xpos, ypos, xsize, ysize;
xpos = scaleX(left); ypos = scaleY(ymin);
xsize = scaleX(right-left); ysize = scaleY(ymax-ymin);
xa.rectangle("rect"+worker, xpos, ypos, xsize, ysize, Color.black, xa.OUTLINE);
xa.delay(1);
// change items sorted to this worker's color
for (int i = left; i <= right; i++) {
xa.color("nums"+i, colors[worker]); // would be better to do
xa.fill("nums"+i, xa.SOLID); // these as a batch i.e.
} // no frameDelay
xa.delay(1);
// make pivot outline color
xa.fill("nums"+left, xa.OUTLINE);
// draw a black horizontal line from the pivot across the rectangle
xpos = scaleX(left); ypos = scaleY(pivot);
xsize = scaleX(right-left); ysize = 0.0f;
xa.line("lineH"+worker, xpos, ypos, xsize, ysize, Color.black, xa.THIN);
// draw two vertical lines at left+1 and right
xpos = scaleX(l+1); ypos = scaleY(pivot);
xsize = 0.0f; ysize = scaleY(ymax-pivot);
xa.line("lineVL"+worker, xpos, ypos, xsize, ysize, Color.black, xa.THIN);
xpos = scaleX(r); ypos = scaleY(ymin);
xsize = 0.0f; ysize = scaleY(pivot-ymin);
xa.line("lineVR"+worker, xpos, ypos, xsize, ysize, Color.black, xa.THIN);
xa.delay(1);
boolean done = false;
while (!done) {
if (nums[l+1] > pivot) {
while (r > l+1 && nums[r] > pivot) { r--;
// move the right vertical line one to the left
if (!done) {
xa.jumpRelative("lineVR"+worker, scaleX(-1), 0.0f);
}
}
if (r > l+1) { l++;
int temp = nums[r]; nums[r] = nums[l]; nums[l] = temp;
done = l >= r-1;
// swap locations and ids of the objects
xa.moveAsync("nums"+r, scaleX(l), scaleY(nums[l]));
xa.move("nums"+l, scaleX(r), scaleY(nums[r]));
xa.swapIds("nums"+r, "nums"+l);
// move the left vertical line one to the right
if (!done) {
xa.jumpRelative("lineVL"+worker, scaleX(1), 0.0f);
}
} else done = true;
} else { l++; done = l >= r;
// move the left vertical line one to the right
if (!done) {
xa.jumpRelative("lineVL"+worker, scaleX(1), 0.0f);
}
}
}
int temp = nums[left]; nums[left] = nums[l]; nums[l] = temp;
// swap locations and ids of the objects
xa.moveAsync("nums"+left, scaleX(l), scaleY(nums[l]));
xa.move("nums"+l, scaleX(left), scaleY(nums[left]));
xa.swapIds("nums"+left, "nums"+l);
if (right-(l+1) > 0) send(task, new Task(l+1, right));
else if (right-(l+1) == 0) { V(doneCount);
// color the object solid black to indicate it is in final position
xa.color("nums"+right, Color.black);
xa.fill("nums"+right, xa.SOLID);
}
// delete the line and rectangle objects
xa.delete("rect"+worker);
xa.delete("lineH"+worker);
xa.delete("lineVL"+worker);
xa.delete("lineVR"+worker);
V(doneCount);
// color the object solid black to indicate it is in final position
xa.color("nums"+l, Color.black);
xa.fill("nums"+l, xa.SOLID);
if ((l-1)-left > 0) send(task, new Task(left, l-1));
else if ((l-1)-left == 0) { V(doneCount);
// color the object solid black to indicate it is in final position
xa.color("nums"+left, Color.black);
xa.fill("nums"+left, xa.SOLID);
}
}





// prefix.cpp : main project file.
#include "stdafx.h"
#include "omp.h"
#include "stdio.h"
#include "conio.h"
#include "math.h"

using namespace System;

int main(array ^args)
{
int a[100],b[100],j,k,i;
float n;
printf("enter the count of nos. to be added"); //no of processors spawned
scanf("%f", &n);
for(k = 0 ; k {
printf("enter the no. to be added"); //value at each processor
scanf("%d ", &a[k]);
}

#pragma omp parallel private(x,y) shared(sum,n)
{
int z=0;
for(i=1;i{
printf("\ni value %d", i);
float p=ceil(log(n));
printf("\np value %f", p);
for (j=0;j<=p-1;j++)
{ printf("\nj value %d", j);
z= (i-(pow(2.0,j)));
printf("\nz value %d", z);
if(z>=0)
{
a[i]=a[i]+a[z];
printf("\n the value of a[%d] = %d",i,a[i]);
}

}

}
}
getch();
return 0;
}