Changeset 6232
- Timestamp:
- Jan 23, 2009, 2:44:41 AM (16 years ago)
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/caching/caching.py
r6230 r6232 47 47 import os 48 48 if os.name in ['nt', 'dos', 'win32', 'what else?']: 49 unix = 049 unix = False 50 50 else: 51 unix = 151 unix = True 52 52 53 53 import Numeric as num … … 82 82 'cachedir': cachedir, # Default cache directory 83 83 'maxfiles': 1000000, # Maximum number of cached files 84 'savestat': 1,# Log caching info to stats file85 'verbose': 1,# Write messages to standard output86 'bin': 1,# Use binary format (more efficient)87 'compression': 1,# Use zlib compression88 'bytecode': 1,# Recompute if bytecode has changed89 'expire': 0# Automatically remove files that have been accessed84 'savestat': True, # Log caching info to stats file 85 'verbose': True, # Write messages to standard output 86 'bin': True, # Use binary format (more efficient) 87 'compression': True, # Use zlib compression 88 'bytecode': True, # Recompute if bytecode has changed 89 'expire': False # Automatically remove files that have been accessed 90 90 # least recently 91 91 } … … 116 116 # Function cache - the main routine 117 117 118 def cache(func, args=(), kwargs = {}, dependencies=None , cachedir=None, 119 verbose=None, compression=None, evaluate=0, test=0, clear=0, 120 return_filename=0): 121 """Supervised caching of function results. 118 def cache(func, 119 args=(), 120 kwargs={}, 121 dependencies=None, 122 cachedir=None, 123 verbose=None, 124 compression=None, 125 evaluate=False, 126 test=False, 127 clear=False, 128 return_filename=False): 129 """Supervised caching of function results. Also known as memoization. 122 130 123 131 USAGE: … … 134 142 (Default: options['verbose']) 135 143 compression -- Flag zlib compression (Default: options['compression']) 136 evaluate -- Flag forced evaluation of func (Default: 0)137 test -- Flag test for cached results (Default: 0)138 clear -- Flag delete cached results (Default: 0)139 return_filename -- Flag return of cache filename (Default: 0)144 evaluate -- Flag forced evaluation of func (Default: False) 145 test -- Flag test for cached results (Default: False) 146 clear -- Flag delete cached results (Default: False) 147 return_filename -- Flag return of cache filename (Default: False) 140 148 141 149 DESCRIPTION: … … 190 198 Explicit dependencies: 191 199 The call 192 cache(func,(arg1,...,argn), dependencies = <list of filenames>)200 cache(func,(arg1,...,argn), dependencies = <list of filenames>) 193 201 Checks the size, creation time and modification time of each listed file. 194 202 If any file has changed the function is recomputed and the results stored … … 204 212 Silent operation: 205 213 The call 206 cache(func,(arg1,...,argn), verbose=0)214 cache(func,(arg1,...,argn), verbose=False) 207 215 suppresses messages to standard output. 208 216 209 217 Compression: 210 218 The call 211 cache(func,(arg1,...,argn), compression=0)212 disables compression. (Default: compression= 1). If the requested compressed219 cache(func,(arg1,...,argn), compression=False) 220 disables compression. (Default: compression=True). If the requested compressed 213 221 or uncompressed file is not there, it'll try the other version. 214 222 215 223 Forced evaluation: 216 224 The call 217 cache(func,(arg1,...,argn), evaluate=1)225 cache(func,(arg1,...,argn), evaluate=True) 218 226 forces the function to evaluate even though cached data may exist. 219 227 220 228 Testing for presence of cached result: 221 229 The call 222 cache(func,(arg1,...,argn), test=1)230 cache(func,(arg1,...,argn), test=True) 223 231 retrieves cached result if it exists, otherwise None. The function will not 224 232 be evaluated. If both evaluate and test are switched on, evaluate takes … … 226 234 ??NOTE: In case of hash collisions, this may return the wrong result as 227 235 ??it only checks if *a* cached result is present. 236 # I think this was due to the bytecode option being False for some reason. (23/1/2009). 228 237 229 238 Obtain cache filenames: 230 239 The call 231 cache(func,(arg1,...,argn), return_filename=1)240 cache(func,(arg1,...,argn), return_filename=True) 232 241 returns the hashed base filename under which this function and its 233 242 arguments would be cached … … 244 253 245 254 New form of clear: 246 cache(func,(arg1,...,argn), clear=1)255 cache(func,(arg1,...,argn), clear=True) 247 256 clears cached data for particular combination func and args 248 257 … … 325 334 326 335 # Check if previous computation has been cached 327 if evaluate :336 if evaluate is True: 328 337 Retrieved = None # Force evaluation of func regardless of caching status. 329 338 reason = 5 … … 341 350 T = None 342 351 else: # Evaluate function and save to cache 343 if verbose :352 if verbose is True: 344 353 345 354 msg1(funcname, args, kwargs,reason) … … 359 368 comptime = time.time()-t0 360 369 361 if verbose :370 if verbose is True: 362 371 msg2(funcname,args,kwargs,comptime,reason) 363 372 … … 365 374 loadtime = save_results_to_cache(T, CD, FN, func, deps, comptime, \ 366 375 funcname, dependencies, compression) 367 if verbose :376 if verbose is True: 368 377 msg3(loadtime, CD, FN, deps, compression) 369 378 compressed = compression … … 413 422 # What remains here includes example of the 414 423 # cache statistics form. 415 def test(cachedir=None, verbose=0,compression=None):424 def test(cachedir=None, verbose=False, compression=None): 416 425 """Test the functionality of caching. 417 426 … … 420 429 421 430 ARGUMENTS: 422 verbose -- Flag whether caching will output its statistics (default= 0)431 verbose -- Flag whether caching will output its statistics (default=False) 423 432 cachedir -- Directory for cache files (Default: options['cachedir']) 424 433 compression -- Flag zlib compression (Default: options['compression']) … … 1115 1124 # ----------------------------------------------------------------------------- 1116 1125 1117 def load_from_cache(CD, FN,compression):1126 def load_from_cache(CD, FN, compression): 1118 1127 """Load previously cached data from file FN 1119 1128 … … 1135 1144 # ----------------------------------------------------------------------------- 1136 1145 1137 def myopen(FN, mode,compression=1):1146 def myopen(FN, mode, compression=True): 1138 1147 """Open file FN using given mode 1139 1148 1140 1149 USAGE: 1141 myopen(FN, mode,compression=1)1150 myopen(FN, mode, compression=True) 1142 1151 1143 1152 ARGUMENTS: … … 1259 1268 # ----------------------------------------------------------------------------- 1260 1269 1261 def mysave(T, file,compression):1270 def mysave(T, file, compression): 1262 1271 """Save data T to file 1263 1272 1264 1273 USAGE: 1265 mysave(T, file,compression)1274 mysave(T, file, compression) 1266 1275 1267 1276 """ … … 1724 1733 # ----------------------------------------------------------------------------- 1725 1734 1726 def checkdir(CD, verbose=None, warn=False):1735 def checkdir(CD, verbose=None, warn=False): 1727 1736 """Check or create caching directory 1728 1737 … … 1774 1783 #============================================================================== 1775 1784 1776 def addstatsline(CD, funcname,FN,Retrieved,reason,comptime,loadtime,1785 def addstatsline(CD, funcname, FN, Retrieved, reason, comptime, loadtime, 1777 1786 compression): 1778 1787 """Add stats entry … … 1852 1861 # FIXME: should take cachedir as an optional arg 1853 1862 # 1854 def __cachestat(sortidx=4, period=-1,showuser=None,cachedir=None):1863 def __cachestat(sortidx=4, period=-1, showuser=None, cachedir=None): 1855 1864 """ List caching statistics. 1856 1865 … … 1915 1924 print 'Reading file ', SD+FN 1916 1925 1917 while 1:1926 while True: 1918 1927 A = input.readlines(blocksize) 1919 1928 if len(A) == 0: break … … 2062 2071 #============================================================================== 2063 2072 2064 def UpdateDict(Dict, key,info):2073 def UpdateDict(Dict, key, info): 2065 2074 """Update dictionary by adding new values to existing. 2066 2075 … … 2081 2090 # ----------------------------------------------------------------------------- 2082 2091 2083 def SortDict(Dict, sortidx=0):2092 def SortDict(Dict, sortidx=0): 2084 2093 """Sort dictionary 2085 2094 … … 2135 2144 #============================================================================== 2136 2145 2137 def msg1(funcname, args,kwargs,reason):2146 def msg1(funcname, args, kwargs, reason): 2138 2147 """Message 1 2139 2148 2140 2149 USAGE: 2141 msg1(funcname, args,kwargs,reason):2150 msg1(funcname, args, kwargs, reason): 2142 2151 """ 2143 2152 … … 2184 2193 # ----------------------------------------------------------------------------- 2185 2194 2186 def msg2(funcname, args,kwargs,comptime,reason):2195 def msg2(funcname, args, kwargs, comptime, reason): 2187 2196 """Message 2 2188 2197 2189 2198 USAGE: 2190 msg2(funcname, args,kwargs,comptime,reason)2199 msg2(funcname, args, kwargs, comptime, reason) 2191 2200 """ 2192 2201 … … 2208 2217 # ----------------------------------------------------------------------------- 2209 2218 2210 def msg3(savetime, CD, FN, deps, compression):2219 def msg3(savetime, CD, FN, deps, compression): 2211 2220 """Message 3 2212 2221 2213 2222 USAGE: 2214 msg3(savetime, CD, FN, deps, compression)2223 msg3(savetime, CD, FN, deps, compression) 2215 2224 """ 2216 2225 … … 2222 2231 # ----------------------------------------------------------------------------- 2223 2232 2224 def msg4(funcname, args,kwargs,deps,comptime,loadtime,CD,FN,compression):2233 def msg4(funcname, args, kwargs, deps, comptime, loadtime, CD, FN, compression): 2225 2234 """Message 4 2226 2235 2227 2236 USAGE: 2228 msg4(funcname, args,kwargs,deps,comptime,loadtime,CD,FN,compression)2237 msg4(funcname, args, kwargs, deps, comptime, loadtime, CD, FN, compression) 2229 2238 """ 2230 2239 … … 2242 2251 # ----------------------------------------------------------------------------- 2243 2252 2244 def msg5(CD, FN,deps,compression):2253 def msg5(CD, FN, deps, compression): 2245 2254 """Message 5 2246 2255 2247 2256 USAGE: 2248 msg5(CD, FN,deps,compression)2257 msg5(CD, FN, deps, compression) 2249 2258 2250 2259 DESCRIPTION: … … 2304 2313 # ----------------------------------------------------------------------------- 2305 2314 2306 def msg6(funcname, args,kwargs):2315 def msg6(funcname, args, kwargs): 2307 2316 """Message 6 2308 2317 2309 2318 USAGE: 2310 msg6(funcname, args,kwargs)2319 msg6(funcname, args, kwargs) 2311 2320 """ 2312 2321 … … 2507 2516 print 2508 2517 2509 #import sys2510 #sys.exit()2511 2518 raise StandardError 2512 2519 -
anuga_core/source/anuga/caching/test_caching.py
r6231 r6232 433 433 res1 = cache(f1, x, test=True, verbose=False) 434 434 assert num.allclose(res1, ref1) 435 436 # Test that f2(x) is still clear 437 cache(f2, x, clear=True, verbose=False) 438 flag = cache(f2, x, test=True, verbose=False) 439 assert flag is None 435 440 436 441 # Run f2(x) and test result -
anuga_core/source/anuga/fit_interpolate/fit.py
r6191 r6232 457 457 alpha=DEFAULT_ALPHA, 458 458 verbose=False, 459 acceptable_overshoot=1.01, 459 acceptable_overshoot=1.01, # FIXME: Move to config - this value is assumed in caching test 460 460 mesh_origin=None, 461 461 data_origin=None, … … 478 478 'data_origin': data_origin, 479 479 'max_read_lines': max_read_lines, 480 'attribute_name': attribute_name, 481 'use_cache': use_cache 480 'attribute_name': attribute_name 482 481 } 483 482 … … 494 493 #from caching import myhash 495 494 #import copy 495 #print args 496 496 #print kwargs 497 497 #print 'hashing:' … … 534 534 data_origin=None, 535 535 max_read_lines=None, 536 attribute_name=None, 537 use_cache = False): 536 attribute_name=None): 538 537 """ 539 538 Fit a smooth surface to a triangulation, -
anuga_validation/okushiri_2005/test_caching_of_set_quantity.py
r6196 r6232 26 26 # Module imports 27 27 from anuga.shallow_water import Domain 28 from anuga.caching import cache 29 from anuga.fit_interpolate.fit import _fit_to_mesh 28 30 import project 31 import Numeric as num 29 32 33 internal_verbose = False # Verbose used in set_quantity and passed into fit_to_mesh 34 35 filename=project.bathymetry_filename 36 alpha=0.02 37 from anuga.config import points_file_block_line_size as max_read_lines 30 38 31 39 #------------------------- 32 40 # Create Domain from mesh 33 41 #------------------------- 34 domain = Domain(project.mesh_filename, use_cache=True, verbose=True) 42 domain = cache(Domain, (project.mesh_filename, {'verbose': True}), verbose=False) 43 44 # Clear caching of underlying function 45 args = (filename, ) 46 kwargs = {'vertex_coordinates': None, 47 'triangles': None, 48 'mesh': domain.mesh, 49 'point_attributes': None, 50 'alpha': alpha, 51 'verbose': internal_verbose, 52 'acceptable_overshoot': 1.01, # This is the default value in _fit_to_mesh 53 'mesh_origin': None, 54 'data_origin': None, 55 'max_read_lines': max_read_lines, 56 'attribute_name': None 57 } 58 59 60 cache(_fit_to_mesh, 61 args, 62 kwargs, 63 verbose=False, 64 dependencies=[filename], 65 clear=True) 66 67 # Check that cache is empty 68 flag = cache(_fit_to_mesh, 69 args, 70 kwargs, 71 verbose=False, 72 dependencies=[filename], 73 test=True) 74 assert flag is None 75 35 76 36 77 … … 38 79 # Initial Conditions 39 80 #------------------------- 40 print 'Set elevation and cache'81 #print 'Set elevation and cache' 41 82 domain.set_quantity('elevation', 42 filename= project.bathymetry_filename,83 filename=filename, 43 84 alpha=0.02, 44 verbose= True,85 verbose=internal_verbose, 45 86 use_cache=True) 87 88 ref = domain.get_quantity('elevation').get_values() 46 89 47 print 'Try to read in via cache' 90 # Check that cache is now present (and correct) 91 flag = cache(_fit_to_mesh, 92 args, 93 kwargs, 94 verbose=False, 95 dependencies=[filename], 96 test=True) 97 assert flag is not None 98 res = domain.get_quantity('elevation').get_values() 99 assert num.allclose(res, ref) 100 101 # Now check this using the high level call 102 #print 'Try to read in via cache' 48 103 domain.set_quantity('elevation', 49 filename= project.bathymetry_filename,104 filename=filename, 50 105 alpha=0.02, 51 verbose= True,106 verbose=internal_verbose, 52 107 use_cache=True) 108 109 res = domain.get_quantity('elevation').get_values() 110 assert num.allclose(res, ref)
Note: See TracChangeset
for help on using the changeset viewer.