Guest User

Untitled

a guest
Oct 21st, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. /* * Compile: gcc -g -Wall -o practica_mpi practica_mpi.c
  2. Compile mpi: mpicc -g -Wall -o practica_mpi practric_mpi.c
  3. * Run: ./vector_add
  4. Run mpi: mpiexec -n <numero de procesos> ./practica_mpi <tamaño del vector>
  5. mpirun -n < numero de procesos> ./practica_mpi <tamaño del vector>*/
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <mpi.h>
  9.  
  10. void Leer_n(int* n);
  11. void CrearVector(double** v1,double** v2,double** re, int n);
  12. void Llenar_vector(double vec1[],double vec2[],int n);
  13. void Imprimir(double vec[],int n);
  14. void Sum(double vec1[],double vec2[],double vec_result[], int n);
  15. double** CrearMatriz(int n);
  16. void Llenar_matriz(double **mat,int n);
  17. void MostrarMatriz(double **mat,int n);
  18. double* Producto(double **mat, double *vector,int n);
  19.  
  20. int main(int argc, char const *argv[])
  21. {
  22.  
  23. int n;
  24.  
  25. //Leer_n(&n);
  26. n = atoi(argv[1]);
  27. int comm_sz;
  28. int my_rank;
  29. double l_start,l_finish,l_elapsed,elapsed,sum_t=0;
  30.  
  31.  
  32. MPI_Init(NULL,NULL);
  33. MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
  34. MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
  35.  
  36. MPI_Barrier(MPI_COMM_WORLD);
  37. l_start=MPI_Wtime();
  38.  
  39. double *vec1,*vec2,*vec_result;
  40. double *l_vec1,*l_vec2,*l_vec_r;
  41. double *prod;
  42. double **mat;
  43. int loc_n = n / comm_sz;
  44.  
  45.  
  46. l_vec1 = (double*) malloc(sizeof(double)*loc_n);
  47. l_vec2 = (double*) malloc(sizeof(double)*loc_n);
  48. l_vec_r = (double*) malloc(sizeof(double)*loc_n);
  49.  
  50. // Crear y distribuir vectores de entrada
  51. if (my_rank==0)
  52. {
  53. CrearVector(&vec1,&vec2,&vec_result,n);
  54. //mat=CrearMatriz(n);
  55. Llenar_vector(vec1,vec2,n);
  56.  
  57. MPI_Scatter(vec1,loc_n,MPI_DOUBLE, l_vec1, loc_n, MPI_DOUBLE,0,MPI_COMM_WORLD);
  58. MPI_Scatter(vec2,loc_n,MPI_DOUBLE, l_vec2, loc_n, MPI_DOUBLE,0,MPI_COMM_WORLD);
  59. free(vec1);
  60. free(vec2);
  61. }else{
  62. MPI_Scatter(vec1,loc_n,MPI_DOUBLE, l_vec1, loc_n, MPI_DOUBLE,0,MPI_COMM_WORLD);
  63. MPI_Scatter(vec2,loc_n,MPI_DOUBLE, l_vec2, loc_n, MPI_DOUBLE,0,MPI_COMM_WORLD);
  64. }
  65.  
  66.  
  67. Sum(l_vec1,l_vec2,l_vec_r,loc_n);
  68.  
  69.  
  70. l_finish=MPI_Wtime();
  71. l_elapsed=l_finish - l_start;
  72. MPI_Reduce(&l_elapsed,&elapsed,1,MPI_DOUBLE,MPI_MAX,0,MPI_COMM_WORLD);
  73.  
  74.  
  75. if (my_rank==0)
  76. {
  77. MPI_Gather(l_vec_r, loc_n, MPI_DOUBLE, vec_result, loc_n, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  78. //Imprimir(vec_result,n);
  79. free(vec_result);
  80. //l_finish=MPI_Wtime();
  81. //l_elapsed=l_finish - l_start;
  82. //MPI_Reduce(&l_elapsed,&elapsed,1,MPI_DOUBLE,MPI_MAX,0,MPI_COMM_WORLD);
  83.  
  84. printf("Tiempo de ejecucion=%5.2f segundos\n",elapsed );
  85.  
  86. } else{
  87. MPI_Gather(l_vec_r, loc_n, MPI_DOUBLE, vec_result, loc_n, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  88. //l_finish=MPI_Wtime();
  89. //l_elapsed=l_finish - l_start;
  90. //MPI_Reduce(&l_elapsed,&elapsed,1,MPI_DOUBLE,MPI_MAX,0,MPI_COMM_WORLD);
  91.  
  92. }
  93.  
  94.  
  95. MPI_Finalize();
  96. return 0;
  97. }
  98.  
  99. /*--------------------------------------
  100.  
  101. */
  102. void Leer_n(int* n){
  103. printf("Ingrese tamaño del vector \n");
  104. scanf("%d",n);
  105. if (*n<=0)
  106. {
  107. printf("el valor debe ser positivo o mayor a 0\n");
  108. exit(-1);
  109. }
  110. }
  111. /*--------------------------------------
  112.  
  113. */
  114. void CrearVector(double** v1,double** v2,double** re, int n){
  115. *v1 = malloc(n*sizeof(double));
  116. *v2 = malloc(n*sizeof(double));
  117. *re = malloc(n*sizeof(double));
  118. if (*v1 == NULL || *v2 == NULL || *re == NULL) {
  119. printf("no se pudo crear el vector\n");
  120. exit(-1);
  121.  
  122. }
  123.  
  124. }
  125. /*--------------------------------------
  126.  
  127. */
  128. void Llenar_vector(double vec1[],double vec2[],int n){
  129.  
  130. for (int i = 0; i < n; ++i)
  131. {
  132. vec1[i]=2;
  133. vec2[i]=2;
  134. }
  135. }
  136.  
  137. /*--------------------------------------
  138.  
  139. */
  140. void Imprimir(double vec[],int n){
  141. for (int i = 0; i < n; ++i)
  142. {
  143. printf("%lf ",vec[i] );
  144. }
  145. }
  146. /*--------------------------------------
  147.  
  148. */
  149. void Sum(double vec1[],double vec2[],double vec_result[], int n){
  150.  
  151. for (int i = 0; i < n; ++i)
  152. {
  153. vec_result[i]=vec1[i]+vec2[i];
  154. }
  155. }
  156. /*--------------------------------------
  157.  
  158. */
  159. double** CrearMatriz(int n){
  160.  
  161. double **mat;
  162. mat=(double**)malloc(n*sizeof(double*));
  163. if (mat==NULL)
  164. {
  165. printf("No se pudo reservar memoria");
  166. exit(-1);
  167. }
  168.  
  169. for (int i = 0; i < n; ++i)
  170. {
  171. mat[i]=(double*)malloc(n*sizeof(double));
  172. if (mat[i]==NULL)
  173. {
  174. printf("No se pudo reservar memoria");
  175. exit(-1);
  176. }
  177. }
  178. return mat;
  179. }
  180.  
  181. /*--------------------------------------
  182.  
  183. */
  184. void Llenar_matriz(double **mat,int n){
  185.  
  186. for (int i = 0; i < n; ++i)
  187. {
  188. for (int j = 0; j < n; ++j)
  189. {
  190. mat[i][j]=2.0;
  191. }
  192. }
  193. }
  194.  
  195. /*--------------------------------------
  196.  
  197. */
  198. void MostrarMatriz(double **mat,int n){
  199.  
  200. for (int i = 0; i < n; ++i)
  201. {
  202. for (int j = 0; j < n; ++j)
  203. {
  204. printf("%lf ",mat[i][j]);
  205. }
  206. printf("\n");
  207. }
  208.  
  209. }
  210.  
  211. /*--------------------------------------
  212.  
  213. */
  214. double* Producto(double **mat, double *vector,int n){
  215.  
  216. double *z,pre=0;
  217. z=(double*)malloc(n*sizeof(double));
  218. for (int i = 0; i < n; ++i)
  219. {
  220. for (int j = 0; j < n; ++j)
  221. {
  222. pre+=(mat[i][j]*vector[j]);
  223. }
  224. z[i]=pre;
  225. pre=0;
  226. }
  227. return z;
  228.  
  229. }
Add Comment
Please, Sign In to add comment