Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
last changes of cpp wrappers examples
[simgrid.git] / examples / cxx / basic / Master.cxx
1 #include "Master.hpp"
2 #include "BasicTask.hpp"
3 #include "FinalizeTask.hpp"
4 #include <Host.hpp>
5 #include <HostNotFoundException.hpp>
6
7 #include <Msg.hpp>
8
9 #include <iostream>
10 using namespace std;
11
12
13 #ifndef BUFFMAX
14 #define BUFFMAX 260
15 #endif
16
17 MSG_IMPLEMENT_DYNAMIC(Master, Process)
18
19 int Master::main(int argc, char** argv)
20 {
21         int channel = 0;
22         
23         int numberOfTasks;              
24         double taskComputeSize;         
25         double taskCommunicateSize;
26         StringHelper s;
27
28         if (argc < 3) 
29         {
30                 error("Master needs 3 arguments");
31                 exit(1);
32         }
33         
34         info("Hello");
35         
36         int slaveCount = 0;
37         Host* slaves = NULL;
38
39         
40         info( TEXT_("argc=") + TEXT_(argc));
41         
42         for (int i = 0; i< argc; i++)       
43                 info(TEXT_("argv:") + TEXT_(argv[i]));
44         
45         sscanf(argv[0],"%d", &numberOfTasks);
46         
47         sscanf(argv[1],"%lg", &taskComputeSize);
48         
49         sscanf(argv[2],"%lg", &taskCommunicateSize);
50         
51         BasicTaskPtr* todo = new BasicTaskPtr[numberOfTasks];
52         
53         for (int i = 0; i < numberOfTasks; i++) 
54                 todo[i] = new BasicTask((TEXT_("Task_") + TEXT_(i)), taskComputeSize, taskCommunicateSize);
55         
56         slaveCount = argc - 3;
57         slaves = new Host[slaveCount];
58         
59         for(int i = 3; i < argc ; i++)  
60         {
61                 try 
62                 {
63                         slaves[i-3] = Host::getByName(argv[i]);
64                 }
65                 
66                 catch(HostNotFoundException e) 
67                 {
68                         cerr << e.toString() <<". Stopping Now!" << endl;
69                         exit(1);
70                 }
71         }
72
73         info(TEXT_("Got slave(s) :") + TEXT_(slaveCount));
74         
75         for (int i = 0; i < slaveCount; i++)
76                 info(TEXT_("\t") + TEXT_(slaves[i].getName()));
77         
78          info(TEXT_("Got ") + TEXT_(numberOfTasks) + TEXT_(" task to process."));
79         
80         for (int i = 0; i < numberOfTasks; i++) 
81         {
82                 info(TEXT_("Sending \"") + TEXT_(todo[i]->getName()) + TEXT_("\" to \"") + TEXT_(slaves[i % slaveCount].getName()) + TEXT_("\""));
83         
84                 if(!strcmp(Host::currentHost().getName(), slaves[i % slaveCount].getName())) 
85                         info("Hey ! It's me ! ");
86
87                 slaves[i % slaveCount].put(channel, todo[i]);
88         }
89         
90         info("Send completed");
91         
92         info("All tasks have been dispatched. Let's tell everybody the computation is over.");
93         
94         for (int i = 0; i < slaveCount; i++) 
95                         slaves[i].put(channel, new FinalizeTask());
96
97         delete[] todo;
98         delete[] slaves;
99         
100         info("Goodbye now!");
101
102         delete this;
103
104         return 0;
105 }
106