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 */
/*-------------------------------------------------------------------*/ int main(int argc, char **argv) { double* local_A; double* local_B; double* local_C; int m, local_m, n, local_n; int my_rank, comm_sz; MPI_Comm comm; double start, finish, loc_elapsed, elapsed; MPI_Init(&argc, &argv); comm = MPI_COMM_WORLD; MPI_Comm_size(comm, &comm_sz); MPI_Comm_rank(comm, &my_rank); n=atoi(argv[1]); m=n; local_m = m/comm_sz; local_n = n/comm_sz; /*Get_dims(&m, &local_m, &n, &local_n, my_rank, comm_sz, comm);*/ Allocate_arrays(&local_A, &local_B, &local_C, local_m, n, local_n, comm); // Read_matrix("A", local_A, m, local_m, n, my_rank, comm); srandom(my_rank); Generate_matrix(local_A, local_m, n); Generate_matrix(local_B, local_m, n); # ifdef DEBUG Print_matrix("A", local_A, m, local_m, n, my_rank, comm); Print_matrix("B", local_B, m, local_m, n, my_rank, comm); # endif // Read_vector("x", local_x, n, local_n, my_rank, comm); /*Generate_vector(local_x, local_n);*/ # ifdef DEBUG /*Print_vector("x", local_x, n, local_n, my_rank, comm);*/ # endif MPI_Barrier(comm); for(i = 0; i < 100; i++) { start = MPI_Wtime(); Mat_vect_mult(local_A, local_B, local_C, local_m, n, local_n, comm); finish = MPI_Wtime(); loc_elapsed = finish-start; MPI_Reduce(&loc_elapsed, &elapsed, 1, MPI_DOUBLE, MPI_MAX, 0, comm); if (my_rank == 0) printf("Elapsed time = %e\n", elapsed); } # ifdef DEBUG Print_matrix("C", local_C, m, local_m, n, my_rank, comm); # endif free(local_A); free(local_B); free(local_C); MPI_Finalize(); return 0; } /* main */
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 */
/*-------------------------------------------------------------------*/ 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 */
/* MATRIX_T is a two-dimensional array of floats */ void Serial_matrix_mult( MATRIX_T A /* in */, MATRIX_T B /* in */, MATRIX_T C /* out */, int n /* in */) { int i, j, k; void Print_matrix(char* title, MATRIX_T C, int n); Print_matrix("In Serial_matrix_mult A = ", A, n); Print_matrix("In Serial_matrix_mult B = ", B, n); for (i = 0; i < n; i++) for (j = 0; j < n; j++) { C[i][j] = 0.0; for (k = 0; k < n; k++) C[i][j] = C[i][j] + A[i][k]*B[k][j]; printf("i = %d, j = %d, c_ij = %f\n", i, j, C[i][j]); } } /* Serial_matrix_mult */
/*------------------------------------------------------------------*/ int main(int argc, char* argv[]) { long thread; pthread_t* thread_handles; double start, finish; if (argc != 4) Usage(argv[0]); thread_count = strtol(argv[1], NULL, 10); m = strtol(argv[2], NULL, 10); n = strtol(argv[3], NULL, 10); # ifdef DEBUG printf("thread_count = %d, m = %d, n = %d\n", thread_count, m, n); # endif thread_handles = malloc(thread_count*sizeof(pthread_t)); sem_init(&sem, 0, 1); /* Initial value is 1 */ A = malloc(m*n*sizeof(double)); x = malloc(n*sizeof(double)); y = malloc(m*sizeof(double)); srandom(1); Gen_matrix(A, m, n); # ifdef DEBUG Print_matrix("We generated", A, m, n); # endif Gen_vector(x, n); # ifdef DEBUG Print_vector("We generated", x, n); # endif GET_TIME(start); for (thread = 0; thread < thread_count; thread++) pthread_create(&thread_handles[thread], NULL, Pth_mat_vect, (void*) thread); for (thread = 0; thread < thread_count; thread++) pthread_join(thread_handles[thread], NULL); GET_TIME(finish); # ifdef DEBUG Print_vector("The product is", y, m); # endif printf("Elapsed time = %e seconds\n", finish - start); free(A); free(x); free(y); sem_destroy(&sem); free(thread_handles); return 0; } /* main */
/*-------------------------------------------------------------------*/ int main(void) { double* local_A; double* local_x; double* local_y; int m, local_m, n, local_n; int my_rank, comm_sz; MPI_Comm comm; double start, finish, loc_elapsed, elapsed; MPI_Init(NULL, NULL); comm = MPI_COMM_WORLD; MPI_Comm_size(comm, &comm_sz); MPI_Comm_rank(comm, &my_rank); Get_dims(&m, &local_m, &n, &local_n, my_rank, comm_sz, comm); Allocate_arrays(&local_A, &local_x, &local_y, local_m, n, local_n, comm); // Read_matrix("A", local_A, m, local_m, n, my_rank, comm); srandom(my_rank); Generate_matrix(local_A, local_m, n); # ifdef DEBUG Print_matrix("A", local_A, m, local_m, n, my_rank, comm); # endif // Read_vector("x", local_x, n, local_n, my_rank, comm); Generate_vector(local_x, local_n); # ifdef DEBUG Print_vector("x", local_x, n, local_n, my_rank, comm); # endif MPI_Barrier(comm); start = MPI_Wtime(); Mat_vect_mult(local_A, local_x, local_y, local_m, n, local_n, comm); finish = MPI_Wtime(); loc_elapsed = finish-start; MPI_Reduce(&loc_elapsed, &elapsed, 1, MPI_DOUBLE, MPI_MAX, 0, comm); # ifdef DEBUG Print_vector("y", local_y, m, local_m, my_rank, comm); # endif if (my_rank == 0) printf("Elapsed time = %e\n", elapsed); free(local_A); free(local_x); free(local_y); MPI_Finalize(); return 0; } /* main */
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 */
/*------------------------------------------------------------------*/ 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 */
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 */
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 */