int main(int argc, char** argv)
{
    int my_rank;
    int p;
    int source;
    int dest;
    int tag=0;
    char sendMessage[1];
    char recMessage[1];
    MPI_Status status;
    
    /* Let the system do what it needs to start up MPI */
    MPI_Init(&argc, &argv);
    /* Get my process rank */
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
    /* Find out how many processes are being used */
    MPI_Comm_size(MPI_COMM_WORLD, &p);
    
    double start, end;
    int n;
    MATRIX_T  A;
    MATRIX_T  B;
    MATRIX_T  C;

    void Read_matrix(char* prompt, MATRIX_T A, int n);
    void Serial_matrix_mult(MATRIX_T A, MATRIX_T B, MATRIX_T C, int n);
    void Print_matrix(char* title, MATRIX_T C, int n);
    if (my_rank == 0)
    {
        start = MPI_Wtime();

/*      MPI_Send(sendMessage, 2, MPI_CHAR, 1, tag, MPI_COMM_WORLD);
        MPI_Recv(recMessage, 2, MPI_CHAR, 1, tag, MPI_COMM_WORLD, &status);*/
        printf("What's the order of the matrices?\n");
        scanf("%d", &n);
        Read_matrix("Enter A", A, n);
        Print_matrix("A = ", A, n);
        Read_matrix("Enter B", B, n);
        Print_matrix("B = ", B, n);
        Serial_matrix_mult(A, B, C, n);
        Print_matrix("Their product is", C, n);

        end = MPI_Wtime();
    }

    /* Shut down MPI */
    MPI_Finalize();
    
    return 0;
    
}  /* main */
예제 #2
0
파일: mat_vect_mult.c 프로젝트: 7ohn/NDSU
/*-------------------------------------------------------------------*/
int main(void) {
   double* A = NULL;
   double* x = NULL;
   double* y = NULL;
   int m, n;

   Get_dims(&m, &n);
   A = malloc(m*n*sizeof(double));
   x = malloc(n*sizeof(double));
   y = malloc(m*sizeof(double));
   if (A == NULL || x == NULL || y == NULL) {
      fprintf(stderr, "Can't allocate storage\n");
      exit(-1);
   }
   Read_matrix("A", A, m, n);
#  ifdef DEBUG
   Print_matrix("A", A, m, n);
#  endif
   Read_vector("x", x, n);
#  ifdef DEBUG
   Print_vector("x", x, n);
#  endif

   Mat_vect_mult(A, x, y, m, n);

   Print_vector("y", y, m);

   free(A);
   free(x);
   free(y);
   return 0;
}  /* main */
예제 #3
0
int main(int argc, char *argv[]) {
   double* A = NULL;
   double* x = NULL;
   double* y = NULL;
   long long int size;
   int m, n;

   n= atoi(argv[1]);
   m= n;

   size=m*n*sizeof(double);
   A = malloc(size);
   x = malloc(n*sizeof(double));
   y = malloc(m*sizeof(double));
   
   Read_matrix("A", A, m, n);
   Read_vector("x", x, n);

   gettimeofday(&start_time, NULL);
   Mat_vect_mult(A, x, y, m, n);

   gettimeofday(&end_time, NULL);
   print_times();

   free(A);
   free(x);
   free(y);
   return 0;
}  /* main */
예제 #4
0
int main(int argc, char* argv[]) {
   int  n;
   int* local_mat;
   MPI_Comm comm;
   int p, my_rank;

   MPI_Init(&argc, &argv);
   comm = MPI_COMM_WORLD;
   MPI_Comm_size(comm, &p);
   MPI_Comm_rank(comm, &my_rank);

   if (my_rank == 0) {
      printf("How many vertices?\n");
      scanf("%d", &n);
   }
   MPI_Bcast(&n, 1, MPI_INT, 0, comm);
   local_mat = malloc(n*n/p*sizeof(int));

   if (my_rank == 0) printf("Enter the local_matrix\n");
   Read_matrix(local_mat, n, my_rank, p, comm);
   if (my_rank == 0) printf("We got\n");
   Print_matrix(local_mat, n, my_rank, p, comm);
   if (my_rank == 0) printf("\n");

   Floyd(local_mat, n, my_rank, p, comm);

   if (my_rank == 0) printf("The solution is:\n");
   Print_matrix(local_mat, n, my_rank, p, comm);

   free(local_mat);
   MPI_Finalize();

   return 0;
}  /* main */
예제 #5
0
int main(void) {

   int p, my_rank, n;
   MPI_Comm comm;
   int* local_mat;
   int* temp_mat;
   MPI_Init(NULL, NULL);
   comm = MPI_COMM_WORLD;
   MPI_Comm_size(comm, &p);
   MPI_Comm_rank(comm, &my_rank);

   if (my_rank == 0) {
   
      printf("How many vertices?\n");
      scanf("%d", &n);
   }
   MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
   local_mat = malloc((n*n/p)*sizeof(int));
   
   if (my_rank == 0) {
   
      temp_mat = malloc((n*n)*sizeof(int));
      printf("Enter the matrix\n");
      Read_matrix(temp_mat, n);
   }
   MPI_Scatter(temp_mat, n*n/p, MPI_INT, local_mat, n*n/p, MPI_INT, 0, comm);
   Floyd(local_mat, my_rank, n, p);
   MPI_Gather(local_mat, n*n/p, MPI_INT, temp_mat, n*n/p, MPI_INT, 0, comm);
   free(local_mat);
   
   if (my_rank == 0) {
   
      printf("The solution is:\n");
      Print_matrix(temp_mat, n);
      free(temp_mat);
   }
   MPI_Finalize();
   return 0;
}  /* main */
예제 #6
0
/*------------------------------------------------------------------*/
int main(int argc, char* argv[]) {
    int     thread_count;
    int     m, n;
    double* A;
    double* x;
    double* y;

    Get_args(argc, argv, &thread_count, &m, &n);

    A = malloc(m*n*sizeof(double));
    x = malloc(n*sizeof(double));
    y = malloc(m*sizeof(double));

#  ifdef DEBUG
    Read_matrix("Enter the matrix", A, m, n);
    Print_matrix("We read", A, m, n);
    Read_vector("Enter the vector", x, n);
    Print_vector("We read", x, n);
#  else
    Gen_matrix(A, m, n);
    /* Print_matrix("We generated", A, m, n); */
    Gen_vector(x, n);
    /* Print_vector("We generated", x, n); */
#  endif

    Omp_mat_vect(A, x, y, m, n, thread_count);

#  ifdef DEBUG
    Print_vector("The product is", y, m);
#  else
    /* Print_vector("The product is", y, m); */
#  endif

    free(A);
    free(x);
    free(y);

    return 0;
}  /* main */
예제 #7
0
main(int argc, char* argv[]) {
    int        p;
    int        my_rank;
    MATRIX_T   A_local;
    float      x_local[MAX_DIM];
    float      b_local[MAX_DIM];
    int        n;
    float      tol;
    int        max_iter;
    int        converged;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &p);
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

    if (my_rank == 0) {
        printf("Enter n, tolerance, and max number of iterations\n");
        scanf("%d %f %d", &n, &tol, &max_iter);
    }
    MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
    MPI_Bcast(&tol, 1, MPI_FLOAT, 0, MPI_COMM_WORLD);
    MPI_Bcast(&max_iter, 1, MPI_INT, 0, MPI_COMM_WORLD);

    Read_matrix("Enter the matrix", A_local, n, my_rank, p);
    Read_vector("Enter the right-hand side", b_local, n, my_rank, p);

    converged = Parallel_jacobi(A_local, x_local, b_local, n,
        tol, max_iter, p, my_rank);

    if (converged)
        Print_vector("The solution is", x_local, n, my_rank, p);
    else
        if (my_rank == 0)
            printf("Failed to converge in %d iterations\n", max_iter);

    MPI_Finalize();
}  /* main */
예제 #8
0
int main(int argc, char const *argv[])
{
	int matrixSize = strtol(argv[1], NULL, 10);
	int coreCount = omp_get_num_procs();
	int threadCount = strtol(argv[2], NULL, 10);
	double startTime, finishTime;
	double **a_augmented, **a;	// n x n Matrix as a 2D array
	double diagonalElement, bestElement, factor;
	int bestRowIndex = 0; 	// used in partial pivoting (index of row having greatest absolute value)
	int i, j, k;			// for loop counters
	double *x;				// Solutions
	double *b;

	printf("Matrix Size: %d\n", matrixSize);
	printf("Number of Cores: %d\n", coreCount);

	#pragma omp parallel num_threads(threadCount)
	{
		if (omp_get_thread_num() == 0)
			printf("Thread Count: %d\n", omp_get_num_threads());
	}

	// Start Timer
	startTime = omp_get_wtime();

	// Allocate memory
	// a_augmented will be the augmented matrix
	a_augmented = (double **) malloc(matrixSize * sizeof(double *));
	// a will be the randomly generated matrix
	a = (double **) malloc(matrixSize * sizeof(double *));
	x = (double *) malloc(matrixSize * sizeof(double));
	b = (double *) malloc(matrixSize * sizeof(double));
	
	if (DEBUG == 1)
		Read_matrix(&a, &a_augmented, matrixSize);
	else
		Gen_matrix(&a, &a_augmented, matrixSize, threadCount);

	// a will not be modified after this point
	// Only the a_augmented will be modified 

	// Display generated matrix:
	displayMatrix(a, matrixSize);

	for (i = 0; i < matrixSize - 1; ++i)
	{
		// Partial Pivoting: 
		// the algorithm selects the entry with largest absolute value from 
		// the column of the matrix that is currently being considered as 
		// the pivot element. 

		// Diagonal Element
		diagonalElement = a_augmented[i][i];
		// debug_printf("diagonalElement%d = %f\n", i, diagonalElement);

		// Find the best row (the one with the largest absolute value in the 
		// column being worked on)
		bestRowIndex = i;
		bestElement = diagonalElement;
		for (j = i + 1; j < matrixSize; ++j)
		{
			if (fabs(a_augmented[j][i]) > fabs(bestElement))
			{
				bestRowIndex = j;
				bestElement = a_augmented[j][i];
				// debug_printf("bestElement = %f\n", a_augmented[j][i]);
			}
		}

		// Swap the rows
		if (i != bestRowIndex)
		{
			// debug_printf("Row %d needs to be swapped with Row %d\n", i, bestRowIndex );
			swapRow(&a_augmented[i], &a_augmented[bestRowIndex]);	
			// Update the diagonal element
			diagonalElement = a_augmented[i][i];
			// debug_printf("diagonalElement%d = %f\n", i, diagonalElement);
			// displayMatrix(a_augmented, matrixSize);
		}

		// End of Partial Pivoting

		// To make the diagonal element 1, 
		// divide the whole row with the diagonal element
		// debug_printf("Row %d = Row %d / %f\n", i, i, diagonalElement);
		for (j = 0; j < matrixSize + 1; ++j)
		{
			a_augmented[i][j] = a_augmented[i][j] / diagonalElement;
		}

		// Force the diagonal to be 1 (to avoid any roundoff errors in dividing above)
		a_augmented[i][i] = 1;
		diagonalElement = 1;

		// debug_printf("Annihilation of column %d...\n", i);
		// Annihilation: Zero all the elements in the column below the diagonal element
		#pragma omp parallel for num_threads(threadCount) \
			default(none) private(j, factor, k) shared(i, matrixSize, a_augmented)
		for (j = i + 1; j < matrixSize; ++j)
		{
			// sleep(1);
			factor = a_augmented[j][i];
			if (factor != 0)
			{
				// debug_printf("Row %d = Row %d - %f*Row %d\n", j, j, factor, i);
				for (k = i; k < matrixSize + 1; ++k)
				{
					a_augmented[j][k] = a_augmented[j][k] - factor * a_augmented[i][k];
				}
				// displayAugmentedMatrix(a, matrixSize);
			}
		}
	}

	// Make the diagonal element of the last row 1
	a_augmented[matrixSize-1][matrixSize] = a_augmented[matrixSize-1][matrixSize] / a_augmented[matrixSize-1][matrixSize-1];
	a_augmented[matrixSize-1][matrixSize-1] = 1;

	// Display augmented matrix:
	displayMatrix(a_augmented, matrixSize);

	// Back substitution (parallelized)
	backSubstitution(&a_augmented, matrixSize, threadCount);

	// Record the finish time
	finishTime = omp_get_wtime();

	displayMatrix(a_augmented, matrixSize);

	// Matrix X from augmented matrix
	// Vector b from matrix A
	for (i = 0; i < matrixSize; ++i)
	{
		x[i] = a_augmented[i][matrixSize];
		b[i] = a[i][matrixSize];
	}

	// Find I^2 norm
	iSquaredNorm(&a, x, b, matrixSize, threadCount);

	// Print the time taken
	printf("Time taken = %f\n", finishTime - startTime);


	// Free memory
	for (i = 0; i < matrixSize; ++i)
	{
		free(a[i]);
		free(a_augmented[i]);
	}
	free(a);
	free(a_augmented);
	free(x);
	free(b);
	return 0;
}
예제 #9
0
파일: fox.c 프로젝트: BoyzInB/Assignment-1
int main(int argc, char* argv[]) {
    int              p;
    int              my_rank;
    GRID_INFO_T      grid;
    LOCAL_MATRIX_T*  local_A;
    LOCAL_MATRIX_T*  local_B;
    LOCAL_MATRIX_T*  local_C;
    int              n;
    int              n_bar;
    double starttime, endtime;
    
    srand(time(NULL));
    starttime = MPI_Wtime();
    
    void Setup_grid(GRID_INFO_T*  grid);
    void Fox(int n, GRID_INFO_T* grid, LOCAL_MATRIX_T* local_A,
             LOCAL_MATRIX_T* local_B, LOCAL_MATRIX_T* local_C);
    
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
    
    
    /* Need to change the below. Want random matrices.*/
    Setup_grid(&grid);
    if (my_rank == 0) {
        //printf("What's the order of the matrices?\n");
        n = atoi(argv[1]);
    }
    
    MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
    n_bar = n/grid.q;
    
    local_A = Local_matrix_allocate(n_bar);
    Order(local_A) = n_bar;
    Read_matrix("Generating and distributing A...", local_A, &grid, n);
    Print_matrix("Matrix A:", local_A, &grid, n);
    
    local_B = Local_matrix_allocate(n_bar);
    Order(local_B) = n_bar;
    Read_matrix("Generating and distributing B...", local_B, &grid, n);
    Print_matrix("Matrix B:", local_B, &grid, n);
    
    
    Build_matrix_type(local_A);
    temp_mat = Local_matrix_allocate(n_bar);
    
    local_C = Local_matrix_allocate(n_bar);
    Order(local_C) = n_bar;
    Fox(n, &grid, local_A, local_B, local_C);
    
    endtime   = MPI_Wtime();
    
    Print_matrix("The product is", local_C, &grid, n);
    
    if(my_rank == 0)
        printf("The time it took was %f\n", endtime - starttime);
    
    
    Free_local_matrix(&local_A);
    Free_local_matrix(&local_B);
    Free_local_matrix(&local_C);
    
    MPI_Type_free(&local_matrix_mpi_t);
    
    MPI_Finalize();
    return 0;
}  /* main */
예제 #10
0
main(int argc, char * argv[]) {
	srand(time(NULL));
	int 	my_rank, size, stage, temp;
	int 	n, local_n, i, j, k, source, dest, q, ind;
	float	*matrix_A;
	float	*matrix_B;
	float	*matrix_C;
	float   *local_A;
	float   *local_B;
	float	*local_C;
	double	start, end;
	MPI_Datatype	column;
	MPI_Status	status;
	MPI_Init(&argc, &argv);
	MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
	MPI_Comm_size(MPI_COMM_WORLD, &size);

	if (my_rank == 0) {
		printf("\t\t****************************************************\n");
		printf("\t\t*   Descomposición en Bloques de Rayas por Filas   *\n");
		printf("\t\t****************************************************\n\n");
	}

	if (my_rank == 0) {
		matrix_A = (float *)malloc(MAX*MAX*sizeof(float));
		matrix_B = (float *)malloc(MAX*MAX*sizeof(float));
		matrix_C = (float *)malloc(MAX*MAX*sizeof(float));
		if (argc == 2) {
			sscanf(argv[1], "%d", &n);
		} else if (my_rank == 0) {
			printf("¿ Cuál es el orden de las matrices ? \n");
			scanf("%d", &n);
		}
		local_n = n / size;
		/* Se lee la matriz A */
		Read_matrix ("Ingrese A :", matrix_A, n);
		Print_matrix ("Se leyó A :", matrix_A, n);

		/* Se lee la matriz B */
		Read_matrix ("Ingrese B :", matrix_B, n);
		Print_matrix ("Se leyó B :", matrix_B, n);

	}

	MPI_Bcast(&local_n, 1, MPI_INT, 0, MPI_COMM_WORLD);
	MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);

	local_A = (float *) malloc(MAX*MAX*sizeof(float));
	local_B = (float *) malloc(MAX*MAX*sizeof(float));
	local_C = (float *) malloc(MAX*MAX*sizeof(float));

/******************************************************************************/
/* Distribuir los bloques de filas de A y los bloques de filas de B entre 
   los procesadores del comunicador global */

	// Enviar los bloques de filas de A y B a todos los procesadores
	MPI_Scatter(matrix_A, local_n*n, MPI_FLOAT, local_A, local_n*n, MPI_FLOAT, 0, MPI_COMM_WORLD);
	MPI_Scatter(matrix_B, local_n*n, MPI_FLOAT, local_B, local_n*n, MPI_FLOAT, 0, MPI_COMM_WORLD);
/*****************************************************************************/
/* Algoritmo de Descomposición por Bloques de Rayas por filas para la matriz A 
   y por columnas para la matriz B */
	
	source = (my_rank - 1 + size) % size;
	dest = (my_rank + 1) % size;
	q = n / local_n;
	start = MPI_Wtime();
	for (stage = 0; stage < q; stage++) {
		ind = (my_rank - stage + size) % size;
		for (j = 0; j < local_n; j++) {
			for (i = 0; i < n; i++) {
				for (k = 0; k < local_n; k++) {
					local_C[i + j*n] += local_A[local_n*ind + k + j*n] * local_B[i + k*n];
				}
			}
		}
		MPI_Sendrecv_replace(local_B, local_n*n, MPI_FLOAT, dest, 0, source, 0, MPI_COMM_WORLD, &status);
	}
	end = MPI_Wtime();

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

	// Recolectar los bloques local_C de cada procesador en matrix_C en le procesador 0
	MPI_Gather(local_C, local_n*n, MPI_FLOAT, matrix_C, local_n*n, MPI_FLOAT, 0, MPI_COMM_WORLD);

	if (my_rank == 0) {
		Print_matrix ("El producto es C : ", matrix_C, n);
	}

	if (my_rank == 0)
		printf("n : %d\nDesc. por filas : %f segundos.\n", n, end - start);
	
	MPI_Finalize();

} /* main */