-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
146 lines (132 loc) · 4.42 KB
/
main.cpp
File metadata and controls
146 lines (132 loc) · 4.42 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstring> // For strcmp
#include <mpi.h>
// --- Function for File Transfer ---
void perform_file_transfer(int world_rank)
{
if (world_rank == 0)
{ // Sender Logic
int file_size = 0;
const int MAX_FILE_SIZE = 6000;
std::vector<char> buffer(MAX_FILE_SIZE);
std::ifstream r_file("whatsapp.webp.gz", std::ios::binary);
if (!r_file)
{
std::cerr << "Error: Could not open file on process 0!" << std::endl;
MPI_Abort(MPI_COMM_WORLD, 1);
}
r_file.seekg(0, std::ios::end);
file_size = r_file.tellg();
r_file.seekg(0, std::ios::beg);
r_file.read(buffer.data(), file_size);
r_file.close();
MPI_Send(&file_size, 1, MPI_INT, 1, 0, MPI_COMM_WORLD);
std::cout << "Process 0 sent file size of " << file_size << " bytes to Process 1!" << std::endl;
MPI_Send(buffer.data(), file_size, MPI_CHAR, 1, 1, MPI_COMM_WORLD);
std::cout << "Process 0 sent file of size " << file_size << " bytes to Process 1." << std::endl;
}
else if (world_rank == 1)
{ // Receiver Logic
int file_size = 0;
const int MAX_FILE_SIZE = 6000;
std::vector<char> buffer(MAX_FILE_SIZE);
MPI_Recv(&file_size, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if (file_size <= 0)
{
std::cerr << "Invalid file size received!" << std::endl;
MPI_Abort(MPI_COMM_WORLD, 1);
}
std::cout << "Process 1 received file size of " << file_size << " bytes from Process 0!" << std::endl;
MPI_Recv(buffer.data(), file_size, MPI_CHAR, 0, 1, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
std::ofstream w_file("whatsapp_received.webp", std::ios::binary);
if (!w_file)
{
std::cerr << "Error: Could not create output file on process 1!" << std::endl;
MPI_Abort(MPI_COMM_WORLD, 1);
}
w_file.write(buffer.data(), file_size);
w_file.close();
std::cout << "Process 1 received file and saved it as 'whatsapp_received.webp'." << std::endl;
}
}
// --- Function for Broadcast Demo ---
void demonstrate_broadcast(int world_rank)
{
int data;
if (world_rank == 0)
{
data = 10;
std::cout << "Process 0 is broadcasting the value: " << data << std::endl;
}
MPI_Bcast(&data, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (world_rank != 0)
{
std::cout << "Process " << world_rank << " received broadcasted data: " << data << std::endl;
}
}
// --- Function for Array Sending Demo ---
void demonstrate_array_send(int world_rank)
{
if (world_rank == 0)
{
int arr[5] = {1, 2, 3, 4, 5};
std::cout << "Sender: Getting ready for sending the data..." << std::endl;
MPI_Send(arr, 5, MPI_INT, 1, 0, MPI_COMM_WORLD);
std::cout << "Sender: The array is sent successfully!" << std::endl;
}
else if (world_rank == 1)
{
int brr[5] = {0};
std::cout << "Receiver: Waiting to receive the data..." << std::endl;
MPI_Recv(brr, 5, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
std::cout << "Receiver: The array is received successfully!" << std::endl;
std::cout << "The contents of array are: ";
for (int i = 0; i < 5; i++)
{
std::cout << brr[i] << " ";
}
std::cout << std::endl;
}
}
// --- Main Application Router ---
int main(int argc, char **argv)
{
MPI_Init(&argc, &argv);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
std::string mode = "";
for (int i = 1; i < argc; ++i)
{
if (std::strcmp(argv[i], "--mode") == 0 && i + 1 < argc)
{
mode = argv[i + 1];
break;
}
}
if (mode == "file_transfer")
{
perform_file_transfer(world_rank);
}
else if (mode == "broadcast")
{
demonstrate_broadcast(world_rank);
}
else if (mode == "array_send")
{
demonstrate_array_send(world_rank);
}
else
{
if (world_rank == 0)
{
std::cerr << "Error: Invalid or no mode specified." << std::endl;
std::cerr << "Usage: mpirun -np <num_procs> ./app --mode <mode_name>" << std::endl;
std::cerr << "Available modes: file_transfer, broadcast, array_send" << std::endl;
}
}
MPI_Finalize();
return 0;
}