fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <mpi.h>
  4.  
  5. void generar_serie(int inicio, int fin, int *serie) {
  6. int valor = inicio * 2;
  7. for (int i = inicio; i <= fin; i++) {
  8. serie[i - inicio] = valor;
  9. valor += 2;
  10. }
  11. }
  12.  
  13. int main(int argc, char **argv) {
  14. int mi_rango, tamano_mundo;
  15.  
  16. MPI_Init(&argc, &argv);
  17. MPI_Comm_rank(MPI_COMM_WORLD, &mi_rango);
  18. MPI_Comm_size(MPI_COMM_WORLD, &tamano_mundo);
  19.  
  20. int N, M;
  21. if (mi_rango == 0) {
  22. printf("Ingrese el numero de terminos: ");
  23. scanf("%d", &N);
  24. printf("Ingrese el numero de procesadores: ");
  25. scanf("%d", &M);
  26. }
  27.  
  28. MPI_Bcast(&N, 1, MPI_INT, 0, MPI_COMM_WORLD);
  29. MPI_Bcast(&M, 1, MPI_INT, 0, MPI_COMM_WORLD);
  30.  
  31. int terminos_por_proceso = N / M;
  32. int inicio = mi_rango * terminos_por_proceso + 1;
  33. int fin = (mi_rango + 1) * terminos_por_proceso;
  34.  
  35. int *serie_local = (int *)malloc(terminos_por_proceso * sizeof(int));
  36. generar_serie(inicio, fin, serie_local);
  37.  
  38. int *serie_completa = NULL;
  39. if (mi_rango == 0) {
  40. serie_completa = (int *)malloc(N * sizeof(int));
  41. }
  42.  
  43. MPI_Gather(serie_local, terminos_por_proceso, MPI_INT, serie_completa, terminos_por_proceso, MPI_INT, 0, MPI_COMM_WORLD);
  44.  
  45. if (mi_rango == 0) {
  46. printf("Serie completa:\n");
  47. for (int i = 0; i < N; i++) {
  48. printf("%d ", serie_completa[i]);
  49. }
  50. printf("\n");
  51.  
  52. free(serie_completa);
  53. }
  54.  
  55. free(serie_local);
  56. MPI_Finalize();
  57.  
  58. return 0;
  59. }
  60.  
  61.  
Success #stdin #stdout #stderr 0.28s 40932KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "void generar_serie"
Execution halted