Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* * Compile: gcc -g -Wall -o practica_mpi practica_mpi.c
- Compile mpi: mpicc -g -Wall -o practica_mpi practric_mpi.c
- * Run: ./vector_add
- Run mpi: mpiexec -n <numero de procesos> ./practica_mpi <tamaño del vector>
- mpirun -n < numero de procesos> ./practica_mpi <tamaño del vector>*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <mpi.h>
- void Leer_n(int* n);
- void CrearVector(double** v1,double** v2,double** re, int n);
- void Llenar_vector(double vec1[],double vec2[],int n);
- void Imprimir(double vec[],int n);
- void Sum(double vec1[],double vec2[],double vec_result[], int n);
- double** CrearMatriz(int n);
- void Llenar_matriz(double **mat,int n);
- void MostrarMatriz(double **mat,int n);
- double* Producto(double **mat, double *vector,int n);
- int main(int argc, char const *argv[])
- {
- int n;
- //Leer_n(&n);
- n = atoi(argv[1]);
- int comm_sz;
- int my_rank;
- double l_start,l_finish,l_elapsed,elapsed,sum_t=0;
- MPI_Init(NULL,NULL);
- MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
- MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
- MPI_Barrier(MPI_COMM_WORLD);
- l_start=MPI_Wtime();
- double *vec1,*vec2,*vec_result;
- double *l_vec1,*l_vec2,*l_vec_r;
- double *prod;
- double **mat;
- int loc_n = n / comm_sz;
- l_vec1 = (double*) malloc(sizeof(double)*loc_n);
- l_vec2 = (double*) malloc(sizeof(double)*loc_n);
- l_vec_r = (double*) malloc(sizeof(double)*loc_n);
- // Crear y distribuir vectores de entrada
- if (my_rank==0)
- {
- CrearVector(&vec1,&vec2,&vec_result,n);
- //mat=CrearMatriz(n);
- Llenar_vector(vec1,vec2,n);
- MPI_Scatter(vec1,loc_n,MPI_DOUBLE, l_vec1, loc_n, MPI_DOUBLE,0,MPI_COMM_WORLD);
- MPI_Scatter(vec2,loc_n,MPI_DOUBLE, l_vec2, loc_n, MPI_DOUBLE,0,MPI_COMM_WORLD);
- free(vec1);
- free(vec2);
- }else{
- MPI_Scatter(vec1,loc_n,MPI_DOUBLE, l_vec1, loc_n, MPI_DOUBLE,0,MPI_COMM_WORLD);
- MPI_Scatter(vec2,loc_n,MPI_DOUBLE, l_vec2, loc_n, MPI_DOUBLE,0,MPI_COMM_WORLD);
- }
- Sum(l_vec1,l_vec2,l_vec_r,loc_n);
- l_finish=MPI_Wtime();
- l_elapsed=l_finish - l_start;
- MPI_Reduce(&l_elapsed,&elapsed,1,MPI_DOUBLE,MPI_MAX,0,MPI_COMM_WORLD);
- if (my_rank==0)
- {
- MPI_Gather(l_vec_r, loc_n, MPI_DOUBLE, vec_result, loc_n, MPI_DOUBLE, 0, MPI_COMM_WORLD);
- //Imprimir(vec_result,n);
- free(vec_result);
- //l_finish=MPI_Wtime();
- //l_elapsed=l_finish - l_start;
- //MPI_Reduce(&l_elapsed,&elapsed,1,MPI_DOUBLE,MPI_MAX,0,MPI_COMM_WORLD);
- printf("Tiempo de ejecucion=%5.2f segundos\n",elapsed );
- } else{
- MPI_Gather(l_vec_r, loc_n, MPI_DOUBLE, vec_result, loc_n, MPI_DOUBLE, 0, MPI_COMM_WORLD);
- //l_finish=MPI_Wtime();
- //l_elapsed=l_finish - l_start;
- //MPI_Reduce(&l_elapsed,&elapsed,1,MPI_DOUBLE,MPI_MAX,0,MPI_COMM_WORLD);
- }
- MPI_Finalize();
- return 0;
- }
- /*--------------------------------------
- */
- void Leer_n(int* n){
- printf("Ingrese tamaño del vector \n");
- scanf("%d",n);
- if (*n<=0)
- {
- printf("el valor debe ser positivo o mayor a 0\n");
- exit(-1);
- }
- }
- /*--------------------------------------
- */
- void CrearVector(double** v1,double** v2,double** re, int n){
- *v1 = malloc(n*sizeof(double));
- *v2 = malloc(n*sizeof(double));
- *re = malloc(n*sizeof(double));
- if (*v1 == NULL || *v2 == NULL || *re == NULL) {
- printf("no se pudo crear el vector\n");
- exit(-1);
- }
- }
- /*--------------------------------------
- */
- void Llenar_vector(double vec1[],double vec2[],int n){
- for (int i = 0; i < n; ++i)
- {
- vec1[i]=2;
- vec2[i]=2;
- }
- }
- /*--------------------------------------
- */
- void Imprimir(double vec[],int n){
- for (int i = 0; i < n; ++i)
- {
- printf("%lf ",vec[i] );
- }
- }
- /*--------------------------------------
- */
- void Sum(double vec1[],double vec2[],double vec_result[], int n){
- for (int i = 0; i < n; ++i)
- {
- vec_result[i]=vec1[i]+vec2[i];
- }
- }
- /*--------------------------------------
- */
- double** CrearMatriz(int n){
- double **mat;
- mat=(double**)malloc(n*sizeof(double*));
- if (mat==NULL)
- {
- printf("No se pudo reservar memoria");
- exit(-1);
- }
- for (int i = 0; i < n; ++i)
- {
- mat[i]=(double*)malloc(n*sizeof(double));
- if (mat[i]==NULL)
- {
- printf("No se pudo reservar memoria");
- exit(-1);
- }
- }
- return mat;
- }
- /*--------------------------------------
- */
- void Llenar_matriz(double **mat,int n){
- for (int i = 0; i < n; ++i)
- {
- for (int j = 0; j < n; ++j)
- {
- mat[i][j]=2.0;
- }
- }
- }
- /*--------------------------------------
- */
- void MostrarMatriz(double **mat,int n){
- for (int i = 0; i < n; ++i)
- {
- for (int j = 0; j < n; ++j)
- {
- printf("%lf ",mat[i][j]);
- }
- printf("\n");
- }
- }
- /*--------------------------------------
- */
- double* Producto(double **mat, double *vector,int n){
- double *z,pre=0;
- z=(double*)malloc(n*sizeof(double));
- for (int i = 0; i < n; ++i)
- {
- for (int j = 0; j < n; ++j)
- {
- pre+=(mat[i][j]*vector[j]);
- }
- z[i]=pre;
- pre=0;
- }
- return z;
- }
Add Comment
Please, Sign In to add comment