1 | #include <stdio.h> |
---|
2 | #include <pthread.h> |
---|
3 | #include <mpi.h> |
---|
4 | #include "constants.h" |
---|
5 | #include "Master.h" |
---|
6 | #include "Environment.h" |
---|
7 | #include "BulletObject.h" |
---|
8 | #include "SimpleAgent.h" |
---|
9 | |
---|
10 | #ifndef USE_REMOTE_GUI |
---|
11 | #define USE_REMOTE_GUI 0 |
---|
12 | #endif |
---|
13 | |
---|
14 | #if USE_REMOTE_GUI |
---|
15 | #include "RemoteGui.h" |
---|
16 | #else |
---|
17 | #include "LocalGui.h" |
---|
18 | #endif |
---|
19 | |
---|
20 | typedef struct { |
---|
21 | int argc; |
---|
22 | char **argv; |
---|
23 | Environment *env; |
---|
24 | } threadParam; |
---|
25 | |
---|
26 | int rank, nprocs = MAX_PROCS; |
---|
27 | MPI_Status status; |
---|
28 | Master *master; |
---|
29 | |
---|
30 | pthread_t guiThread, masterThread; |
---|
31 | |
---|
32 | void cleanup() |
---|
33 | { |
---|
34 | while (!master) ; |
---|
35 | master->stop(); |
---|
36 | while (1) ; |
---|
37 | } |
---|
38 | |
---|
39 | void *guiThreadCode(void *args) |
---|
40 | { |
---|
41 | threadParam *param = (threadParam*) args; |
---|
42 | |
---|
43 | char **argv = param->argv; |
---|
44 | int argc = param->argc; |
---|
45 | Environment *env = param->env; |
---|
46 | |
---|
47 | #if USE_REMOTE_GUI |
---|
48 | if (argc != 2) { |
---|
49 | fprintf(stderr, "Usage: %s <port>\n", argv[0]); |
---|
50 | cleanup(); |
---|
51 | return NULL; |
---|
52 | } |
---|
53 | |
---|
54 | RemoteGui *rg = new RemoteGui(env); |
---|
55 | rg->init(atoi(param->argv[1])); |
---|
56 | rg->run(); |
---|
57 | |
---|
58 | cleanup(); |
---|
59 | |
---|
60 | #else |
---|
61 | atexit(cleanup); |
---|
62 | int ret = glutmain(argc, argv, env); |
---|
63 | |
---|
64 | if(ret < 0) { |
---|
65 | fprintf(stderr, "Error creating LocalGui\n"); |
---|
66 | cleanup(); |
---|
67 | return NULL; |
---|
68 | } |
---|
69 | #endif |
---|
70 | |
---|
71 | return NULL; |
---|
72 | } |
---|
73 | |
---|
74 | static void runMaster(int argc, char **argv) { |
---|
75 | /// Initialize the virtual world environment |
---|
76 | Environment *env = new Environment(); |
---|
77 | |
---|
78 | env->setupEnvironment(); |
---|
79 | env->createMagicCarpet(WIDTH, HEIGHT); |
---|
80 | |
---|
81 | /// Pack gui-thread parameters in structure |
---|
82 | threadParam param; |
---|
83 | param.argv = argv; |
---|
84 | param.argc = argc; |
---|
85 | param.env = env; |
---|
86 | |
---|
87 | /// Run pthread for gui object |
---|
88 | pthread_create(&guiThread, NULL, guiThreadCode, ¶m); |
---|
89 | |
---|
90 | master = new Master(env, nprocs); |
---|
91 | master->discoverAgents(); |
---|
92 | master->createProviders(); |
---|
93 | master->sendAll(); |
---|
94 | |
---|
95 | MPI_Barrier(MPI_COMM_WORLD); |
---|
96 | |
---|
97 | master->run(); |
---|
98 | } |
---|
99 | |
---|
100 | static void runSlave() |
---|
101 | { |
---|
102 | int size; |
---|
103 | vector<Sensor *> sensorList; |
---|
104 | vector<Actuator *> actuatorList; |
---|
105 | vector<CommChannel *> channelList; |
---|
106 | |
---|
107 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
---|
108 | |
---|
109 | // send hello-message: mpi-thread rank |
---|
110 | MPI_Send(&rank, 1, MPI_INT, MASTER_ID, HELLO_MSG, MPI_COMM_WORLD); |
---|
111 | |
---|
112 | // wait to receive from master the number of channels to be created |
---|
113 | MPI_Recv(&size, 1, MPI_INT, 0, CHANNEL_MSG, MPI_COMM_WORLD, &status); |
---|
114 | |
---|
115 | // allocate buffer |
---|
116 | int intSize, floatSize; |
---|
117 | MPI_Pack_size(1, MPI_INT, MPI_COMM_WORLD, &intSize); |
---|
118 | MPI_Pack_size(1, MPI_FLOAT, MPI_COMM_WORLD, &floatSize); |
---|
119 | |
---|
120 | int bufSize = size * (4 * intSize + 3 * floatSize); |
---|
121 | char *buffer = (char*) malloc(bufSize); |
---|
122 | |
---|
123 | // wait to receive the list of channels parameters |
---|
124 | MPI_Recv(buffer, bufSize, MPI_PACKED, 0, CHANNEL_MSG, MPI_COMM_WORLD, &status); |
---|
125 | |
---|
126 | // debug info |
---|
127 | int n_sensors=0, n_actuators=0, n_channels=0; |
---|
128 | int i; |
---|
129 | int position = 0; |
---|
130 | |
---|
131 | //printf("RECV : %d - ", rank); |
---|
132 | for(i = 0; i < size; i++) |
---|
133 | { |
---|
134 | Sensor *x; |
---|
135 | Actuator *y; |
---|
136 | CommChannel *z; |
---|
137 | Channel *channel; |
---|
138 | channelData cparam; |
---|
139 | |
---|
140 | MPI_Unpack(buffer, bufSize, &position, &cparam.tag, 1, MPI_INT, MPI_COMM_WORLD); |
---|
141 | MPI_Unpack(buffer, bufSize, &position, &cparam.type, 1, MPI_INT, MPI_COMM_WORLD); |
---|
142 | MPI_Unpack(buffer, bufSize, &position, &cparam.orient, 1, MPI_INT, MPI_COMM_WORLD); |
---|
143 | MPI_Unpack(buffer, bufSize, &position, &cparam.dest, 1, MPI_INT, MPI_COMM_WORLD); |
---|
144 | MPI_Unpack(buffer, bufSize, &position, &cparam.minAngle, 1, MPI_FLOAT, MPI_COMM_WORLD); |
---|
145 | MPI_Unpack(buffer, bufSize, &position, &cparam.maxAngle, 1, MPI_FLOAT, MPI_COMM_WORLD); |
---|
146 | MPI_Unpack(buffer, bufSize, &position, &cparam.angle, 1, MPI_FLOAT, MPI_COMM_WORLD); |
---|
147 | |
---|
148 | //printf("%d ", cparam.tag); |
---|
149 | switch(cparam.type) { |
---|
150 | case SENSOR: |
---|
151 | channel = new MPIChannel(status.MPI_SOURCE, cparam.tag, MPI_COMM_WORLD); |
---|
152 | x = new Sensor(channel, &cparam); |
---|
153 | sensorList.push_back(x); |
---|
154 | n_sensors++; |
---|
155 | break; |
---|
156 | case ACTUATOR: |
---|
157 | channel = new MPIChannel(status.MPI_SOURCE, cparam.tag, MPI_COMM_WORLD); |
---|
158 | y = new Actuator(channel, &cparam); |
---|
159 | actuatorList.push_back(y); |
---|
160 | n_actuators++; |
---|
161 | break; |
---|
162 | case COMM_CHANNEL: |
---|
163 | channel = new MPIChannel(cparam.dest, cparam.tag, MPI_COMM_WORLD); |
---|
164 | z = new CommChannel(channel, cparam.orient); |
---|
165 | channelList.push_back(z); |
---|
166 | n_channels++; |
---|
167 | break; |
---|
168 | default: break; |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | SimpleAgent *agent = new SimpleAgent(sensorList, actuatorList, channelList); |
---|
173 | |
---|
174 | int ret; |
---|
175 | MPI_Recv(&ret, 1, MPI_INT, 0, CHANNEL_MSG, MPI_COMM_WORLD, &status); |
---|
176 | printf("Agent %d [%d] is running with %d sensors, %d actuators and %d channels\n", rank, ret, n_sensors, n_actuators, n_channels); |
---|
177 | |
---|
178 | MPI_Barrier(MPI_COMM_WORLD); |
---|
179 | |
---|
180 | agent->run(); |
---|
181 | } |
---|
182 | |
---|
183 | |
---|
184 | int main(int argc, char **argv) |
---|
185 | { |
---|
186 | /// MPI initial stuff |
---|
187 | MPI_Init(&argc, &argv); |
---|
188 | MPI_Comm_size(MPI_COMM_WORLD, &nprocs); |
---|
189 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
---|
190 | |
---|
191 | /// Distribute the tasks |
---|
192 | if(rank == MASTER_ID) { |
---|
193 | printf("Starting SimeoEngine....\n"); |
---|
194 | runMaster(argc, argv); |
---|
195 | } else { |
---|
196 | runSlave(); |
---|
197 | } |
---|
198 | |
---|
199 | MPI_Finalize(); |
---|
200 | |
---|
201 | return 0; |
---|
202 | } |
---|
203 | |
---|
204 | |
---|