Exemplo n.º 1
0
double * LUSolver(double **A, double *b, int n){
 
 double **L, **U, *c, *x;
 
 L=allocateDoubleMatrix(n, n);
 U=allocateDoubleMatrix(n, n);

 LUFactotisation(n,A,U,L);
 c=forwardSubstitution(n, L, b);
 x=backSubstitution(n, U, c);
 
 return x;
}
Exemplo n.º 2
0
void  testInterpolation(){

 double *fx, **a, *x;
 int dims=2;

 x=allocateDoubleVector(dims);
 a=allocateDoubleMatrix(dims,2);
 fx=allocateDoubleVector(dims*2);
 
 x[0]=0.25;
 x[1]=0.4;

 a[0][0]=0;
 a[0][1]=1;
 a[1][0]=0;
 a[1][1]=1;

 fx[0]=1;
 fx[1]=3;
 fx[2]=2;
 fx[3]=4;
 
 printf("\nInterpolation\n");
 printf("%f\n",linearInterpolation(fx, a, x, dims));

} 
Exemplo n.º 3
0
void testChiSquared(){

 printf("\nChi Squared test\n");
 printf("chisq: the result is significant at p <%4.3f\n",chiSquaredDistribution(16,3));
 
 double **observed, **observed2;

 observed=allocateDoubleMatrix(3,3);

 observed[0][0]=49;  observed[0][1]=50; observed[0][2]=69; 
 observed[1][0]=24;  observed[1][1]=36; observed[1][2]=38; 
 observed[2][0]=19;  observed[2][1]=22; observed[2][2]=28;

 printf("chisq: the result is significant at p <%4.3f\n",chiProbability(observed,3,3));
 
 observed2=allocateDoubleMatrix(2,2);

 observed2[0][0]=25;  observed2[0][1]=6; 
 observed2[1][0]=8;  observed2[1][1]=15; 
 
 printf("chisq: the result is significant at p <%4.3f\n",chiProbability(observed2,2,2));

}
Exemplo n.º 4
0
void testLUFactorisation(){
 
 double **A, **L, **U;
 int n;
 n=3;
 
 A=allocateDoubleMatrix(n, n);
 L=allocateDoubleMatrix(n, n);
 U=allocateDoubleMatrix(n, n);

 A[0][0]=6.0;  A[0][1]=0.0;  A[0][2]=2.0;
 A[1][0]=24.0;  A[1][1]=1.0;  A[1][2]=8.0;
 A[2][0]=-12.0;  A[2][1]=1.0;  A[2][2]=-3.0;
 
 LUFactotisation(n,A,U,L);
 
 fprintf(stdout,"Matrix Factorisation U:\n");
 printMatrix(U,n,n);

 fprintf(stdout,"Matrix Factorisation L:\n");
 printMatrix(L,n,n);
 
}
Exemplo n.º 5
0
void test_simplex_fit(){
 
 double **a;
 int dim=2;
 
 a = allocateDoubleMatrix(dim+1,dim);
 a[0][0]=0.9; a[0][1]=0.9;
 a[1][0]=0.9; a[1][1]=1.1;
 a[2][0]=1.1; a[2][1]=0.9; 

 double x[]={1.707106,2.490711,3.605550,4.297620,5.656853};
 double y[]={12.500000,4.166667,31.250000,81.250000,112.500000};
 simplex_fit (a, dim, x, y, 5, test_linear_model_2); 
 
}
Exemplo n.º 6
0
void testSimplex3(){
 
 double **x;
 int n=2;
 
 x = allocateDoubleMatrix(n+1,n);
 x[0][0]=0.9; x[0][1]=0.9;
 x[1][0]=0.9; x[1][1]=1.1;
 x[2][0]=1.1; x[2][1]=0.9; 
 
 printf("\nSimplex\n");
 simplex(testFunctionSimplexFit,x,n);
 printf("%f %f\n",x[0][0],x[0][1]);

}
Exemplo n.º 7
0
void testMatrixDeterminant(){

 int n;
 double **C;
 n=3;
 
 C=allocateDoubleMatrix(3, 3);

 C[0][0]=6.0;  C[0][1]=1.0;  C[0][2]=1.0;
 C[1][0]=4.0;  C[1][1]=-2.0;  C[1][2]=5.0;
 C[2][0]=2.0;  C[2][1]=8.0; C[2][2]=7.0;
 
 fprintf(stdout,"Matrix Determinant\n");
 fprintf(stdout,"%f \n", matrixDeterminant(C,n));

}
Exemplo n.º 8
0
void testSimplex(){
 
 double **x;
 int n=2; 
 
 x = allocateDoubleMatrix(n+1,n);
 x[0][0]=0.0; x[0][1]=0.0;
 x[1][0]=1.2; x[1][1]=0.0;
 x[2][0]=0.0; x[2][1]=0.8; 
 
/* x[0][0]=-1.200000; x[0][1]=1.000000; 
 x[1][0]=-0.234074; x[1][1]=1.258819; 
 x[2][0]=-0.941181; x[2][1]=1.965926; */

 printf("\nSimplex\n");
 simplex(testFunctionSimplex,x,n);
 printf("%f %f\n",x[0][0],x[0][1]);

}
Exemplo n.º 9
0
void testMatrixInverse(){
 
 double **A, **B;
 int n;
 n=3;
 
 A=allocateDoubleMatrix(3, 3);

 A[0][0]=1.0;  A[0][1]=2.0;  A[0][2]=3.0;
 A[1][0]=0.0;  A[1][1]=4.0;  A[1][2]=5.0;
 A[2][0]=1.0;  A[2][1]=0.0;  A[2][2]=6.0;
 
 
 B=matrixInverse(A,n);
 
 fprintf(stdout,"Matrix Inversion\n");
 printMatrix(B,n,n);
 
}
Exemplo n.º 10
0
double matrixDeterminant(double **A, int n){

 int i;
 double det=0;
 double **B;

 B=allocateDoubleMatrix(n-1, n-1);
 
 if(n==2){
  det=(A[0][0]*A[1][1])-(A[0][1]*A[1][0]);
 }

 else{
  for(i=0;i<n;i++){        
    B=minorMatrix(A, n, i, 0);
    det += pow(-1,i)*A[0][i]*matrixDeterminant(B,n-1);   
   }
  }

 return(det);
}
Exemplo n.º 11
0
void testMultipleRegression(){

 int i, n=3, m=6;
 double **X, *y, *x;
 
 X=allocateDoubleMatrix(m, n); 
 y=allocateDoubleVector(m);
 
 X[0][0]=1, X[1][0]=1, X[2][0]=1, X[3][0]=1, X[4][0]=1, X[5][0]=1; 
 X[0][1]=0, X[1][1]=2, X[2][1]=2.5, X[3][1]=1, X[4][1]=4, X[5][1]=7; 
 X[0][2]=0, X[1][2]=1, X[2][2]=2, X[3][2]=3, X[4][2]=6, X[5][2]=2; 
 
 y[0]=5, y[1]=10, y[2]=9, y[3]=0, y[4]=3, y[5]=27; 
 
 x=multipleLinearRegression(X,y,n,m);

  fprintf(stdout,"\nMultiple Regression\n");
 for(i=0;i<n;i++) fprintf(stdout,"%f ",x[i]);
 fprintf(stdout,"\n");
 
 
}
Exemplo n.º 12
0
void testLUSolver(){
 
 double **A, *x;
 int n;
 n=3;
 double b[n];
 
 A=allocateDoubleMatrix(n, n);
 

 A[0][0]=6.0;  A[0][1]=0.0;  A[0][2]=2.0;
 A[1][0]=24.0;  A[1][1]=1.0;  A[1][2]=8.0;
 A[2][0]=-12.0;  A[2][1]=1.0;  A[2][2]=-3.0;
 
 b[0]=4.0;
 b[1]=19.0;
 b[2]=-6.0;

 x=LUSolver(A,b,n);
 
 fprintf(stdout,"LU solver U:\n");
 printVector(x,n);

}
Exemplo n.º 13
0
void testGaussianElimination(){
 
 double **A;
 int n;
 double *x; 
 n=3;
 double b[n];

 A=allocateDoubleMatrix(3, 3);

 A[0][0]=1.0;  A[0][1]=1.0;  A[0][2]=-1.0;
 A[1][0]=1.0;  A[1][1]=2.0;  A[1][2]=1.0;
 A[2][0]=2.0;  A[2][1]=-1.0;  A[2][2]=1.0;
 
 b[0]=2.0;
 b[1]=6.0;
 b[2]=1.0;
 
 x=gaussianElimination (n, A, b); 
 
 fprintf(stdout,"Gaussian Elimination\n");
 printVector(x,n);

}
Exemplo n.º 14
0
int main(int argc, char *argv[])
{
	//initialize
	int i, j, k, l, mpi_rank, mpi_size, mpi_rowsize, mpi_colsize, subN, sqrtP;
	int row_rank, col_rank, row, col, destR, destC, src, srcR, srcC;
	// declare variables to store time of parallelism
	double execTime, execStart, execEnd;

	MPI_Comm rowComm, colComm;
	
	MPI_Init(&argc, &argv);

	MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
	MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);

	if (argc < 2) {
		printf("error: need one argument for filename");
		exit(-1);
	}

	// declare variables of type vector to read matrices
	vector * mat1;
	vector * mat2;
	float * vector1;
	float * vector2;
	//create data on root
	// read matrices 
	if (mpi_rank == ROOT)
	{
		mat1 = readfile(argv[1], N, N);
		mat2 = readfile(argv[2], N, N);
		vector1 = mat1->data;
		vector2 = mat2->data;
	}
	// find the sub matrix dimention
	sqrtP = (int) sqrt(mpi_size);
	subN = N/sqrtP;

	//allocate memory for N/4xN rows, and NxN/4 columns
	float * row_mat, *col_mat, *row_mat_rec, *col_mat_rec, *col_matT;
	double *can_res, *can_out;

	//allocate memory for the buffers
	row_mat = allocateFloatMatrix(subN, subN);
	row_mat_rec = allocateFloatMatrix(subN, subN);
	col_mat = allocateFloatMatrix(subN, subN);
	col_matT = allocateFloatMatrix(subN, subN);
	col_mat_rec = allocateFloatMatrix(subN, subN);
	can_res = allocateDoubleMatrix(subN, subN);
	can_out = allocateDoubleMatrix(N, N);

	//create and commit datatypes
	MPI_Datatype arrtype, resized_arrtype, arrtypeD, resized_arrtypeD;

	int sizes[2] = { N,N };
	int subsizes[2] = { subN,subN };
	int starts[2] = { 0,0 };

	MPI_Type_create_subarray(2, sizes, subsizes, starts, MPI_ORDER_C, MPI_FLOAT, &arrtype);
	MPI_Type_create_resized(arrtype, 0, subN * sizeof(float), &resized_arrtype);
	MPI_Type_commit(&resized_arrtype);
	
	MPI_Type_create_subarray(2, sizes, subsizes, starts, MPI_ORDER_C, MPI_DOUBLE, &arrtypeD);
	MPI_Type_create_resized(arrtypeD, 0, subN * sizeof(double), &resized_arrtypeD);
	MPI_Type_commit(&resized_arrtypeD);

	//calculate send counts and displacements
	int * counts, * displs;
	counts = (int *) malloc(mpi_size * sizeof(int));
	displs = (int *) malloc(mpi_size * sizeof(int));

	for(i = 0; i < mpi_size; i++)
	{		
		counts[i] = 1;
		displs[i] = N*(i/sqrtP) + (i%sqrtP);
	}	
	
	//start timer, compute dot product and record execution time 
	execStart = MPI_Wtime();
	
	//scatterv subarrays
	MPI_Scatterv(vector1, counts, displs, resized_arrtype, row_mat, subN*subN, MPI_FLOAT, ROOT, MPI_COMM_WORLD);
	MPI_Scatterv(vector2, counts, displs, resized_arrtype, col_mat, subN*subN, MPI_FLOAT, ROOT, MPI_COMM_WORLD);
	
	//get row comm and rank
	row = mpi_rank/sqrtP;
	MPI_Comm_split(MPI_COMM_WORLD,row,mpi_rank,&rowComm);
	MPI_Comm_rank(rowComm,&row_rank);
	MPI_Comm_size(rowComm, &mpi_rowsize);
	//get col comm and rank
	col = mpi_rank%sqrtP;
	MPI_Comm_split(MPI_COMM_WORLD,col,mpi_rank,&colComm);
	MPI_Comm_rank(colComm,&col_rank);
	MPI_Comm_size(colComm, &mpi_colsize);
	
	//MPI_Barrier(MPI_COMM_WORLD);
	
	// find the source and destination in row communicator - to left shift rows by row number
	destR = row_rank-row;
    if (destR < 0) {
        destR = destR + mpi_rowsize;
    }
	
	srcR = row_rank+row;
	if (srcR > (mpi_rowsize-1)) {
        srcR = srcR - mpi_rowsize;
    }
	
	// find the source and destination in column communicator  - to north shift columns by column number
	destC = col_rank - col;
    if (destC < 0) {
        destC = destC + mpi_colsize;
    }
	srcC = col_rank+col;
	if (srcC > (mpi_colsize-1)) {
        srcC = srcC - mpi_colsize;
    }
	// left shift rows by row number
	MPI_Sendrecv(row_mat, subN*subN, MPI_FLOAT, destR, 0, row_mat_rec, subN*subN, MPI_FLOAT, srcR, MPI_ANY_TAG, rowComm, MPI_STATUS_IGNORE);

	//north shift columns by column number
	MPI_Sendrecv(col_mat, subN*subN, MPI_FLOAT, destC, 1, col_mat_rec, subN*subN, MPI_FLOAT, srcC, MPI_ANY_TAG, colComm, MPI_STATUS_IGNORE);

	 
	
	for (l=0; l<sqrtP; l++)
	{
		memcpy(row_mat, row_mat_rec, sizeof(float)*subN*subN);
		memcpy(col_mat, col_mat_rec, sizeof(float)*subN*subN);
		
		// Finding the transpose of matrix B
		matrixTranspose(subN, col_mat, col_matT);
		//perform a partial matrix-vector multiplication on each process
		matrixMultiplyT(subN, row_mat, col_matT, can_res);
		
		// find the source and destination in row communicator  - to left shift all rows once
	    if (row_rank != 0) {
			destR = row_rank - 1;
		} else {
			destR = mpi_rowsize - 1;
		}
		
	    srcR = row_rank + 1;
		if (srcR == mpi_rowsize) {
			srcR = 0;
		}
		// find the source and destination in column communicator  - to north shift all columns once
		if (col_rank != 0) {
		destC = col_rank - 1;
		} else {
			destC = mpi_colsize - 1;
		}
		
	    srcC = col_rank + 1;
		if (srcC == mpi_colsize) {
			srcC = 0;
		}
		
		//left shift all rows once
		MPI_Sendrecv(row_mat, subN*subN, MPI_FLOAT, destR, 2, row_mat_rec, subN*subN, MPI_FLOAT, srcR, MPI_ANY_TAG, rowComm, MPI_STATUS_IGNORE);
		
		//north shift all columns once
		MPI_Sendrecv(col_mat, subN*subN, MPI_FLOAT, destC, 3, col_mat_rec, subN*subN, MPI_FLOAT, srcC, MPI_ANY_TAG, colComm, MPI_STATUS_IGNORE);
	}

	// gather the matrix multiplication results from all procs
	MPI_Gatherv(can_res, subN*subN, MPI_DOUBLE, can_out, counts, displs, resized_arrtypeD, ROOT, MPI_COMM_WORLD);
	
		//stop timer
	execEnd = MPI_Wtime();
	execTime = execEnd - execStart;

    //free datatypes
	MPI_Type_free(&resized_arrtype);
	MPI_Type_free(&resized_arrtypeD);
	if (mpi_rank == ROOT)
	{
		printf("Execution time for dot product: %f seconds\n", execTime);
		printf("Result: %f, %f, %f \n ", can_out[0], can_out[2047*N + 2047], can_out[4095*N + 4095]);
		free(vector1);
		free(vector2);
	}
	free(row_mat);
	free(col_mat);
	free(row_mat_rec);
	free(col_mat_rec);
	free(col_matT);
	free(can_res);
	free(can_out);

	//shut down MPI
	MPI_Finalize();

	return 0;
}