-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcikel.c
66 lines (50 loc) · 1.98 KB
/
cikel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Prevajanje programa:
// module load mpi
// mpicc cikel.c -o cikel
// Zagon programa:
// srun --mpi=pmix -n10 --reservation=fri cikel
#include <stdio.h>
#include <string.h>
#include <mpi.h>
int main(int argc, char *argv[]) {
int my_rank; // rank (oznaka) procesa
int num_of_processes; // število procesov
int source; // rank pošiljatelja
int destination; // rank sprejemnika
int tag = 0; // zaznamek sporoèila
char message[100]; // rezerviran prostor za sporoèilo
char buff[5];
MPI_Status status; // status sprejema
MPI_Init(&argc, &argv); // inicializacija MPI okolja
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); // poizvedba po ranku procesa
MPI_Comm_size(MPI_COMM_WORLD, &num_of_processes); // poizvedba po številu procesov
// procesi z rankom != 0 prejmejo sporocilo, dodajo svoj rank in posljejo naslednjem procesu
if( my_rank != 0 ) {
MPI_Recv(message, 100, MPI_CHAR, my_rank-1, tag, MPI_COMM_WORLD, &status);
// dodajanje my_rank v message
sprintf(buff, "%d-\0", my_rank);
strcat(message, buff);
// dolocanje naslova za posiljanje sporocila
if(my_rank < num_of_processes - 1)
destination = my_rank + 1;
else
destination = 0;
MPI_Send(message, (int)strlen(message)+1, MPI_CHAR, destination, tag, MPI_COMM_WORLD);
} else {
// proces z my_rank == 0 zacne verigo posiljanja sporocil
// dodajanje my_rank v sporocilo
sprintf(message, "%d-\0", my_rank);
// prvo sporocilo procesa 0
MPI_Send(message, (int)strlen(message)+1, MPI_CHAR, 1, tag, MPI_COMM_WORLD);
// zadnje sporocilo zadnjega procesa
MPI_Recv(message, 100, MPI_CHAR, num_of_processes-1, tag, MPI_COMM_WORLD, &status);
// dodajanje my_rank na konec sporocila
sprintf(buff, "%d\0", my_rank);
strcat(message, buff);
// izpisemo celotno sporocilo
printf("%s\n", message);
fflush(stdout);
}
MPI_Finalize();
return 0;
}