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 """The uni-processor fabric for running on a single CPU."""
26
27
28
29 import sys, os
30
31
32 from multi.misc import Result_string
33 from multi.processor import Processor
34 from multi.result_commands import Result_command
35
36
38 """The uni-processor class."""
39
40 - def __init__(self, processor_size, callback):
41 """Initialise the class.
42
43 @param processor_size: The number of processors.
44 @type processor_size: int
45 @param callback: The callback object.
46 @type callback: ?
47 """
48 super(Uni_processor, self).__init__(processor_size=1, callback=callback)
49
50 if processor_size > 1:
51 print('warning: uniprocessor can only support 1 processor you requested %d' % processor_size)
52 print('continuing...\n')
53
54 self.command_queue = []
55 self.memo_map = {}
56
57
63
64
66 """Make sure that this is the master processor and not a slave.
67
68 As this is the Uni-processor, the processor is always the master. Hence this method does nothing.
69 """
70
71
73 """Return the string to append to the end of the relax introduction string.
74
75 @return: The string describing this Processor fabric.
76 @rtype: str
77 """
78
79
80 return "Uni-processor."
81
82
84
85 return '%s-%s' % (os.getenv('HOSTNAME'), os.getpid())
86
87
89 """For the uni-processor fabric, we are always on the master.
90
91 @return: The flag specifying if we are on the master or slave processors.
92 @rtype: bool
93 """
94
95
96 return True
97
98
100 """Slave to master processor data transfer - send the result command from the slave.
101
102 This mimics a slave to master data transfer initiated by a slave by holding the result command so that the matching self.master_receive_result(), which is called by the master processor, can return it. As the master and slave processors are one and the same, the command is just held as a private class variable.
103
104
105 @param command: The results command to send to the master.
106 @type command: Results_command instance
107 @param dest: The destination processor's rank.
108 @type dest: int
109 """
110
111
112 self._result_command_queue = command
113
114
116 """Slave to master processor data transfer - receive the result command from the slave.
117
118 This mimics a slave to master data transfer initiated by a slave by holding the result command so that the matching self.master_receive_result(), which is called by the master processor, can return it. As the master and slave processors are one and the same, the command is just held as a private class variable.
119
120
121 @return: The result command sent by the slave.
122 @rtype: Result_command instance
123 """
124
125
126 command = self._result_command_queue
127 del self._result_command_queue
128
129
130 return command
131
132
133 - def post_run(self):
134 """Dummy function for preventing the printing of the run time."""
135
136
138 """Return 1 as this is the uni-processor.
139
140 @return: The number of processors.
141 @rtype: int
142 """
143
144 return 1
145
146
148 """The uni-processor is always of rank 0.
149
150 @return: The processor rank.
151 @rtype: int
152 """
153
154 return 0
155
156
158
159 if isinstance(result, Exception):
160
161
162 raise result
163 elif isinstance(result, Result_command):
164 memo = None
165 if result.memo_id != None:
166 memo = self.memo_map[result.memo_id]
167 result.run(self, memo)
168 if result.memo_id != None and result.completed:
169 del self.memo_map[result.memo_id]
170
171 elif isinstance(result, Result_string):
172 sys.stdout.write(result.string)
173 else:
174 message = 'Unexpected result type \n%s \nvalue%s' %(result.__class__.__name__, result)
175 raise Exception(message)
176
177
179
180
181 last_command = len(self.command_queue)-1
182 for i, command in enumerate(self.command_queue):
183 completed = (i == last_command)
184
185 command.run(self, completed)
186
187
188
189 del self.command_queue[:]
190 self.memo_map.clear()
191