Вот код, который я пытался сгенерировать первые 20 чисел, начиная с 0, в попытке узнать MPI.
Мой код приведен ниже:
#include <mpi.h>
#include <stdio.h>
int i = 0;
void test(int edge_count){
while(i < edge_count){
printf("Edge count %d\n",i);
i++;
}
}
int main(int argc, char** argv) {
int edge_count = 20;
// int *p = &i;
// Initialize the MPI environment. The two arguments to MPI Init are not
// currently used by MPI implementations, but are there in case future
// implementations might need the arguments.
MPI_Init(NULL, NULL);
// Get the number of processes
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
// Get the rank of the process
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
// Get the name of the processor
char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_len;
MPI_Get_processor_name(processor_name, &name_len);
// Print off a hello world message
printf("Hello world from processor %s, rank %d out of %d processors\n",
processor_name, world_rank, world_size);
test(edge_count);
printf("The value of i is %d \n",i);
// Finalize the MPI environment. No more MPI calls can be made after this
MPI_Finalize();
}
Мой вывод:
Hello world from processor ENG401651, rank 0 out of 2 processors
Edge count 0
Edge count 1
Edge count 2
Edge count 3
Edge count 4
Edge count 5
Edge count 6
Edge count 7
Edge count 8
Edge count 9
Edge count 10
Edge count 11
Edge count 12
Edge count 13
Edge count 14
Edge count 15
Edge count 16
Edge count 17
Edge count 18
Edge count 19
The value of i is 20
Hello world from processor ENG401651, rank 1 out of 2 processors
Edge count 0
Edge count 1
Edge count 2
Edge count 3
Edge count 4
Edge count 5
Edge count 6
Edge count 7
Edge count 8
Edge count 9
Edge count 10
Edge count 11
Edge count 12
Edge count 13
Edge count 14
Edge count 15
Edge count 16
Edge count 17
Edge count 18
Edge count 19
The value of i is 20
Код, который я использовал для его запуска:
mpirun -np 2 execFile
Я ожидал, что оба процессора будут общаться и генерировать число от 0 до 19 только один раз, но этоКажется, что каждый из процессоров генерирует свой собственный набор чисел независимо.
Что я делаю не так?Я новичок в MPI и не могу понять, в чем причина этого.