/*-------------------------------------------------------------------*/ 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 */
/*------------------------------------------------------------------*/ 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(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 */
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 */