-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpi-ring1.c
executable file
·57 lines (52 loc) · 1.5 KB
/
mpi-ring1.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
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include "cs160String.h"
#define DATATAG 100
#define MSGLEN (2*MPI_MAX_PROCESSOR_NAME)
int
main(int argc, char *argv[])
{
MPI_Status status;
char processor_name[MPI_MAX_PROCESSOR_NAME];
int namelen;
int numprocs;
int myid;
STRING * msg, *tmpS;
int count;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
MPI_Get_processor_name(processor_name, &namelen);
if (argc > 1)
stringDebugID(myid);
msg = newString(0);
tmpS = newString(MSGLEN);
sprintf(tmpS -> string,"Process %d on %s\n", myid, processor_name);
int to = (myid + 1) % numprocs;
int from = (numprocs + myid - 1) % numprocs;
if ( myid == 0 )
{
stringCat(msg,tmpS);
MPI_Send(msg->string, stringLen(msg)+1, MPI_CHAR, to, DATATAG, MPI_COMM_WORLD);
MPI_Probe(from, DATATAG, MPI_COMM_WORLD, &status);
MPI_Get_count(&status, MPI_CHAR,&count);
if (count > msg -> capacity)
expandCapacity(msg, count);
MPI_Recv(msg->string,count,MPI_CHAR, from, DATATAG, MPI_COMM_WORLD,&status);
printf("%s", msg -> string);
}
else
{
MPI_Probe(from, DATATAG, MPI_COMM_WORLD, &status);
MPI_Get_count(&status, MPI_CHAR,&count);
if (count > msg -> capacity)
expandCapacity(msg, count);
MPI_Recv(msg->string,count,MPI_CHAR, from, DATATAG, MPI_COMM_WORLD,&status);
stringCat(msg, tmpS);
MPI_Send(msg->string, stringLen(msg)+1, MPI_CHAR, to, DATATAG, MPI_COMM_WORLD);
}
stringFree(msg);
stringFree(tmpS);
MPI_Finalize();
}