-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
125 lines (95 loc) · 2.75 KB
/
main.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
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
/*
* main.c
*
*/
#define MSG_INIT 1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tower.h"
#include "state_printer.h"
#include "analyser.h"
#include "processor.h"
#include "mpi.h"
// fflush(stdout) or cout<<flush after each printout
int process_id, processors;
int main(int argc, char *argv[]) {
Tower *towers;
int discsCount, towersCount, destTower;
/* initialize MPI */
MPI_Init(&argc, &argv);
/* find out process id */
MPI_Comm_rank(MPI_COMM_WORLD, &process_id);
/* find out number of processes */
MPI_Comm_size(MPI_COMM_WORLD, &processors);
printf("\nHello world from process %i", process_id);
if (process_id == 0) {
printf("Hello, I'm the master!");
// I'm the starting process
// I have to read the data and let others take portions of it
static const char filename[] = "enter.txt";
static const char discDelimiter[] = ",";
FILE *file = fopen(filename, "r");
if (file != NULL) {
char line[128]; /* max line size */
int i;
fgets(line, sizeof line, file);
sscanf(line, "%i", &discsCount);
fgets(line, sizeof line, file);
sscanf(line, "%i", &towersCount);
fgets(line, sizeof line, file);
sscanf(line, "%i", &destTower);
printf("Towers of Hanoi: %i towers, %i discs, %i dest tower\n",
towersCount, discsCount, destTower);
towers = (Tower*) malloc (towersCount * sizeof(Tower));
for (i = 0; i < towersCount; i++) {
char towerLine[128];
Tower tower = { 0, NULL };
char *disc;
tower.number = i + 1;
if (fgets(towerLine, sizeof towerLine, file) == NULL) {
return 1; /* bad enter */
}
printf("Created new tower: %i\n", tower.number);
disc = strtok(towerLine, discDelimiter);
while (1) {
int discSize = 0;
if (disc == NULL) {
break;
}
sscanf(disc, "%i", &discSize);
if (discSize == 0) {
break;
}
insertDics(discSize, &tower);
printf("Inserted disc of size: %i to tower %i\n", discSize,
tower.number);
disc = strtok(NULL, discDelimiter);
}
towers[i] = tower;
}
fclose(file);
// initial work split
int* inputData;
inputData = (int*) malloc(3 * sizeof(*inputData));
inputData[0] = towersCount;
inputData[1] = discsCount;
inputData[2] = destTower;
// send to all processors (except master) the basic calculation parameters
for (i = 1; i < processors; i++) {
MPI_Send(inputData, sizeof(*inputData), MPI_INT, i, MSG_INIT,
MPI_COMM_WORLD);
}
free(inputData);
//printState(towers, towersCount);
process0(towers, towersCount, discsCount, destTower);
//freeTowers(towers, &towersCount);
} else {
perror("enter.txt could not be opened");
return 1;
}
} else {
// other processes (process_id > 0)
}
run(process_id, processors);
}