-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertString.c
More file actions
71 lines (64 loc) · 2 KB
/
convertString.c
File metadata and controls
71 lines (64 loc) · 2 KB
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
67
68
69
70
71
/*
Parallel Programming
By : Abdallah Okasha
FCI-CU
*/
#include <stdio.h>
#include <string.h>
#include "mpi.h"
int main(int argc , char * argv[])
{
int my_rank; /* rank of process */
int p; /* number of process */
int source; /* rank of sender */
int dest; /* rank of reciever */
int tag = 0; /* tag for messages */
char input_str[100]; /* storage for message */
MPI_Status status; /* return status for */
/* recieve */
/* Start up MPI */
MPI_Init( &argc , &argv );
/* Find out process rank */
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
/* Find out number of process */
MPI_Comm_size(MPI_COMM_WORLD, &p);
if( my_rank != 0)
{
/* create message */
dest = 0;
int myPortion;
MPI_Recv(&myPortion, 1, MPI_INT, dest, tag, MPI_COMM_WORLD, &status);
MPI_Recv(input_str, myPortion, MPI_CHAR, dest, tag, MPI_COMM_WORLD, &status);
int i;
for(i=0; i<myPortion; i++)
input_str[i] = toupper(input_str[i]);
MPI_Send( input_str, myPortion, MPI_CHAR, dest, tag, MPI_COMM_WORLD);
}else
{
printf("Enter a string: ");
scanf("%s", input_str);
int length = strlen(input_str);
int portion = length/(p-1);
int currentPosition = 0;
printf("Sending portions to slave processes ...");
for( source = 1; source < p ; source++)
{
MPI_Send(&portion, 1, MPI_INT, source, tag, MPI_COMM_WORLD);
MPI_Send(input_str+currentPosition, portion, MPI_CHAR, source, tag, MPI_COMM_WORLD);
printf("\n\tProcess%d will convert chars [%d-%d]" , source, currentPosition, currentPosition+portion-1);
currentPosition += portion;
}
printf("\n\nReceiving converted portions from slave processes ...");
currentPosition = 0;
for( source = 1; source < p ; source++)
{
MPI_Recv(input_str+currentPosition, portion, MPI_CHAR, source, tag, MPI_COMM_WORLD, &status);
printf("\n\tProcess%d converted chars [%d-%d]" , source, currentPosition, currentPosition+portion-1);
currentPosition += portion;
}
printf("\nString after conversion: %s\n", input_str);
}
/* shutdown MPI */
MPI_Finalize();
return 0;
}