Package Biskit :: Package PVM :: Module ExampleMaster
[hide private]
[frames] | no frames]

Source Code for Module Biskit.PVM.ExampleMaster

  1  ## 
  2  ## Biskit, a toolkit for the manipulation of macromolecular structures 
  3  ## Copyright (C) 2004-2005 Raik Gruenberg & Johan Leckner 
  4  ## 
  5  ## This program is free software; you can redistribute it and/or 
  6  ## modify it under the terms of the GNU General Public License as 
  7  ## published by the Free Software Foundation; either version 2 of the 
  8  ## License, or any later version. 
  9  ## 
 10  ## This program is distributed in the hope that it will be useful, 
 11  ## but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 13  ## General Public License for more details. 
 14  ## 
 15  ## You find a copy of the GNU General Public License in the file 
 16  ## license.txt along with this program; if not, write to the Free 
 17  ## Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 18  ## 
 19  ## 
 20  ## $Revision: 2.7 $ 
 21  ## last $Date: 2006/09/13 10:28:14 $ 
 22  ## last $Author: leckner $ 
 23   
 24  """ 
 25  An example of a Master/Slave setup 
 26  """ 
 27   
 28  from Biskit.PVM.TrackingJobMaster import TrackingJobMaster 
 29   
 30  from Biskit.hosts import nodes_all, cpus_all 
 31  from Biskit.tools import projectRoot 
 32   
 33   
34 -class Master(TrackingJobMaster):
35 36 ## Slave script that ges with this master 37 slave_script = projectRoot() + '/Biskit/PVM/ExampleSlave.py' 38 39
40 - def __init__(self, verbose=1, *args, **kw):
41 """ 42 Parameters nedded by master and/or slave. 43 """ 44 self.verbose = verbose 45 46 TrackingJobMaster.__init__(self, verbose=verbose, *args, **kw)
47 48
49 - def getInitParameters(self, slave_tid):
50 """ 51 Hand over parameters to slave once. 52 53 @param slave_tid: slave task id 54 @type slave_tid: int 55 56 @return: dictionary with init parameters 57 @rtype: {param:value} 58 """ 59 return {'progress_str':'slave calculating..'}
60 61
62 - def cleanup( self ):
63 """ 64 Tidy up tasks. 65 """ 66 if self.verbose: print "Cleaning up..."
67 68
69 - def done( self ):
70 """ 71 Called when master is done. 72 """ 73 if self.verbose: print "Now we are done."
74 75 76 77 ############# 78 ## TESTING 79 ############# 80
81 -class Test:
82 """ 83 Test class 84 """ 85
86 - def run( self, local=0 ):
87 """ 88 run function test 89 90 @param local: transfer local variables to global and perform 91 other tasks only when run locally 92 @type local: 1|0 93 94 @return: 1 95 @rtype: int 96 """ 97 import time 98 from Biskit.PVM.ExampleMaster import Master 99 100 hosts = cpus_all[:8] 101 102 niceness = {'default': 20} 103 104 data = {} 105 for i in range(100): 106 data[i]=i+1 107 108 ## data - dictionary of some_id : some_object pairs 109 ## 10 - how many items to pass to each slave in one go 110 ## niceness - dict {'hostname':nice_value, .. or 'default':nice_value} 111 ## slave_script - name of slave.py 112 ## in the end get results from master.result 113 ## -> dictionary of some_id : result_object pairs 114 115 master = Master( data=data, chunk_size=10, 116 hosts=hosts, niceness=niceness, 117 slave_script=Master.slave_script, 118 show_output=local, redistribute=1, 119 verbose=local ) 120 121 ## blocking call 122 r = master.calculateResult() 123 124 ## Example for non-blocking call with saving of restart info 125 ## master.start() 126 ## time.sleep( 10 ) 127 ## rst = master.getRst() 128 129 if local: 130 globals().update( locals() ) 131 132 return 1
133 134
135 - def expected_result( self ):
136 """ 137 Precalculated result to check for consistent performance. 138 139 @return: 1 140 @rtype: int 141 """ 142 return 1
143 144 145 if __name__ == '__main__': 146 147 test = Test() 148 149 assert test.run( local=1 ) == test.expected_result() 150