-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslave.cpp
More file actions
106 lines (85 loc) · 2.44 KB
/
slave.cpp
File metadata and controls
106 lines (85 loc) · 2.44 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
#include <iostream>
#include <fstream>
#include <mpi.h>
void writeBlock(int rank, MPI_Comm parent, MPI_Status status)
{
int block = -1;
int size = -1;
char* binaryData = NULL;
MPI_Recv(&block, 1, MPI_INT, 0, 0, parent, &status);
MPI_Recv(&size, 1, MPI_INT, 0, 0, parent, &status);
binaryData = (char*)malloc(sizeof(char)*size);
MPI_Recv(binaryData, size, MPI_CHAR, 0, 0, parent, &status);
std::string fileName = "disk" + std::to_string(rank) + ".dat";
std::ofstream binaryFile;
binaryFile.open(fileName, std::ios::out | std::ios::binary | std::ios::in);
binaryFile.seekp(size*block);
binaryFile.write((char*)binaryData, sizeof(char)*size);
binaryFile.close();
free(binaryData);
}
void readBlock(int rank, MPI_Comm parent, MPI_Status status)
{
int block = -1;
int size = -1;
char* binaryData = NULL;
std::string fileName = "disk" + std::to_string(rank) + ".dat";
std::ifstream disk;
disk.open(fileName, std::ios::out | std::ios::binary | std::ios::in);
MPI_Recv(&block, 1, MPI_INT, 0, 0, parent, &status);
MPI_Recv(&size, 1, MPI_INT, 0, 0, parent, &status);
//lee disco
disk.seekg(block*size);
binaryData = (char*)malloc(sizeof(char)*size);
disk.read((char*)binaryData, sizeof(char)*size);
//manda data
MPI_Send(binaryData, size, MPI_CHAR, 0, 0, parent);
free(binaryData);
}
int recv_ID()
{
int id = -1;
MPI_Comm parent;
MPI_Status status;
MPI_Comm_get_parent(&parent);
MPI_Recv(&id, 1, MPI_INT, 0, MPI_ANY_TAG, parent, &status);
return id;
}
int main(int argc, char** argv)
{
// bool flag = true;
// while(flag)
// {
// ;
// }
int rank = -1;
int quit = 0;
int option = -1;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
rank = recv_ID();
MPI_Comm parent;
MPI_Comm_get_parent(&parent);
MPI_Status status;
while(!quit)
{
MPI_Recv(&option, 1, MPI_INT, 0, MPI_ANY_TAG, parent, &status);
if (option==10) //write
{
writeBlock(rank, parent, status);
}
else if (option == 11) //read
{
readBlock(rank, parent, status);
}
else if (option == 12) //quit
{
quit = 1;
}
else
{
std::cout << "Option not found" << std::endl;
}
}
std::cout << "Closing slave " << std::to_string(rank) << "..." << std::endl;
}