#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char** argv) {
int rank, size;
int number;
int* numbers = NULL;
int group_size = 0;
int group_sum = 0;
// Inicializar MPI
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// Semilla para números aleatorios
number
= rand() % 101; // Generar un número aleatorio entre 0 y 100
// Determinar el tamaño del grupo
// Contar cuántos procesos tienen números < 50 y cuántos >= 50
int count_less_than_50 = 0;
int count_greater_equal_50 = 0;
MPI_Allreduce(&number, &count_less_than_50, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
for (int i = 0; i < size; i++) {
if (number < 50) {
count_less_than_50++;
} else {
count_greater_equal_50++;
}
}
// Crear un nuevo comunicador basado en el grupo
MPI_Comm group_comm;
if (number < 50) {
MPI_Comm_create_group(MPI_COMM_WORLD, MPI_GROUP_NULL, 0, &group_comm);
} else {
MPI_Comm_create_group(MPI_COMM_WORLD, MPI_GROUP_NULL, 0, &group_comm);
}
// Recopilar números en el grupo correspondiente
if (number < 50) {
int* sendbuf
= (int*)malloc(count_less_than_50
* sizeof(int)); int* recvbuf
= (int*)malloc(count_less_than_50
* sizeof(int));
// Enviar y recibir números del grupo
MPI_Gather(&number, 1, MPI_INT, sendbuf, 1, MPI_INT, 0, group_comm);
MPI_Reduce(sendbuf, recvbuf, count_less_than_50, MPI_INT, MPI_SUM, 0, group_comm);
// Sumar los números en el proceso de menor rango
if (rank == 0) {
for (int i = 0; i < count_less_than_50; i++) {
group_sum += recvbuf[i];
}
printf("Proceso %d: Suma de números menores que 50: %d ", rank, group_sum);
}
} else {
int* sendbuf
= (int*)malloc(count_greater_equal_50
* sizeof(int)); int* recvbuf
= (int*)malloc(count_greater_equal_50
* sizeof(int));
// Enviar y recibir números del grupo
MPI_Gather(&number, 1, MPI_INT, sendbuf, 1, MPI_INT, 0, group_comm);
MPI_Reduce(sendbuf, recvbuf, count_greater_equal_50, MPI_INT, MPI_SUM, 0, group_comm);
// Sumar los números en el proceso de menor rango
if (rank == 0) {
for (int i = 0; i < count_greater_equal_50; i++) {
group_sum += recvbuf[i];
}
printf("Proceso %d: Suma de números mayores o iguales a 50: %d ", rank, group_sum);
}
}
// Finalizar MPI
MPI_Finalize();
return 0;
}
I2luY2x1ZGUgPG1waS5oPgojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPHN0ZGxpYi5oPgojaW5jbHVkZSA8dGltZS5oPgoKaW50IG1haW4oaW50IGFyZ2MsIGNoYXIqKiBhcmd2KSB7CiAgICBpbnQgcmFuaywgc2l6ZTsKICAgIGludCBudW1iZXI7CiAgICBpbnQqIG51bWJlcnMgPSBOVUxMOwogICAgaW50IGdyb3VwX3NpemUgPSAwOwogICAgaW50IGdyb3VwX3N1bSA9IDA7CgogICAgLy8gSW5pY2lhbGl6YXIgTVBJCiAgICBNUElfSW5pdCgmYXJnYywgJmFyZ3YpOwogICAgTVBJX0NvbW1fcmFuayhNUElfQ09NTV9XT1JMRCwgJnJhbmspOwogICAgTVBJX0NvbW1fc2l6ZShNUElfQ09NTV9XT1JMRCwgJnNpemUpOwoKICAgIC8vIFNlbWlsbGEgcGFyYSBuw7ptZXJvcyBhbGVhdG9yaW9zCiAgICBzcmFuZCh0aW1lKE5VTEwpICsgcmFuayk7CiAgICBudW1iZXIgPSByYW5kKCkgJSAxMDE7ICAvLyBHZW5lcmFyIHVuIG7Dum1lcm8gYWxlYXRvcmlvIGVudHJlIDAgeSAxMDAKCiAgICAvLyBEZXRlcm1pbmFyIGVsIHRhbWHDsW8gZGVsIGdydXBvCiAgICAvLyBDb250YXIgY3XDoW50b3MgcHJvY2Vzb3MgdGllbmVuIG7Dum1lcm9zIDwgNTAgeSBjdcOhbnRvcyA+PSA1MAogICAgaW50IGNvdW50X2xlc3NfdGhhbl81MCA9IDA7CiAgICBpbnQgY291bnRfZ3JlYXRlcl9lcXVhbF81MCA9IDA7CiAgICAKICAgIE1QSV9BbGxyZWR1Y2UoJm51bWJlciwgJmNvdW50X2xlc3NfdGhhbl81MCwgMSwgTVBJX0lOVCwgTVBJX1NVTSwgTVBJX0NPTU1fV09STEQpOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBzaXplOyBpKyspIHsKICAgICAgICBpZiAobnVtYmVyIDwgNTApIHsKICAgICAgICAgICAgY291bnRfbGVzc190aGFuXzUwKys7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgY291bnRfZ3JlYXRlcl9lcXVhbF81MCsrOwogICAgICAgIH0KICAgIH0KCiAgICAvLyBDcmVhciB1biBudWV2byBjb211bmljYWRvciBiYXNhZG8gZW4gZWwgZ3J1cG8KICAgIE1QSV9Db21tIGdyb3VwX2NvbW07CiAgICBpZiAobnVtYmVyIDwgNTApIHsKICAgICAgICBNUElfQ29tbV9jcmVhdGVfZ3JvdXAoTVBJX0NPTU1fV09STEQsIE1QSV9HUk9VUF9OVUxMLCAwLCAmZ3JvdXBfY29tbSk7CiAgICB9IGVsc2UgewogICAgICAgIE1QSV9Db21tX2NyZWF0ZV9ncm91cChNUElfQ09NTV9XT1JMRCwgTVBJX0dST1VQX05VTEwsIDAsICZncm91cF9jb21tKTsKICAgIH0KCiAgICAvLyBSZWNvcGlsYXIgbsO6bWVyb3MgZW4gZWwgZ3J1cG8gY29ycmVzcG9uZGllbnRlCiAgICBpZiAobnVtYmVyIDwgNTApIHsKICAgICAgICBpbnQqIHNlbmRidWYgPSAoaW50KiltYWxsb2MoY291bnRfbGVzc190aGFuXzUwICogc2l6ZW9mKGludCkpOwogICAgICAgIGludCogcmVjdmJ1ZiA9IChpbnQqKW1hbGxvYyhjb3VudF9sZXNzX3RoYW5fNTAgKiBzaXplb2YoaW50KSk7CgogICAgICAgIC8vIEVudmlhciB5IHJlY2liaXIgbsO6bWVyb3MgZGVsIGdydXBvCiAgICAgICAgTVBJX0dhdGhlcigmbnVtYmVyLCAxLCBNUElfSU5ULCBzZW5kYnVmLCAx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