Esempio n. 1
0
 def test_all_even(self):
    """
    @summary: This test creates a surface and runs each implementation of the
                 algorithm and checks that the results are the same
    """
    SURFACE_WIDTH = 1000
    SURFACE_HEIGHT = 1000
    STEP_SIZE = .2
    CELL_SIZE = .005
    TILE_SIZE = 1.0
    NUM_CONES = 100
    NUM_ELLIPSOIDS = 100
    MAX_HEIGHT = 50
    MAX_RADIUS = 200
    NUM_WORKERS = 1
    
    # Generate surface
    sg = SurfaceGenerator(SURFACE_HEIGHT, SURFACE_WIDTH, 0.0, 0.0, CELL_SIZE, 
                          defVal=0)
    sg.addRandom(numCones=NUM_CONES, numEllipsoids=NUM_ELLIPSOIDS, 
                 maxHeight=MAX_HEIGHT, maxRad=MAX_RADIUS)
    
    # Make sure that we have at least one source cell
    if np.min(sg.grid) > 0:
       sg.grid[0,0] = 0
    
    # Write out grid
    sg.writeGrid(self.surfaceFn)
    
    inputGrid = np.loadtxt(self.surfaceFn, dtype=int, comments='', skiprows=6)
    
    # Run Dijkstra
    serialInstance = SingleTileSerialDijkstraLCP(self.surfaceFn, 
                                                 self.serialCostFn, 
                                                 seaLevelRiseCostFn)
    serialInstance.findSourceCells()
    serialInstance.calculate()
 
    # Verify grid
    serialAry = np.loadtxt(self.serialCostFn, dtype=int, comments='', skiprows=6)
    self._verifyGrid(serialAry, inputGrid)
 
    # Run parallel Dijkstra
    parInstance = SingleTileParallelDijkstraLCP(self.surfaceFn,
                                                self.parCostFn,
                                                seaLevelRiseCostFn)
    parInstance.setMaxWorkers(16)
    parInstance.setStepSize(.20)
    parInstance.findSourceCells()
    parInstance.calculate()
    
    # Compare serial and parallel single tile
    parAry = np.loadtxt(self.parCostFn, dtype=int, comments='', skiprows=6)
    
    # Verify parallel run
    self._verifyGrid(parAry, inputGrid)
    
    #print len(np.where(serialAry != parAry))
    assert np.array_equal(serialAry, parAry)
    
    # Split surface
    splitTile(self.surfaceFn, TILE_SIZE, self.mtSurfaceDir)
    
    # Split cost surface
    splitTile(self.parCostFn, TILE_SIZE, self.mtCmpDir)
    
    # Run multi-tile
    mtInstance = MultiTileWqParallelDijkstraLCP(self.mtSurfaceDir,
                                                self.mtCostDir,
                                                self.mtOutDir,
                                                TILE_SIZE,
                                                STEP_SIZE,
                                                summaryFn=self.mtSummaryFn)
 
    # Run
    print "Starting workers"
    mtInstance.startWorkers(NUM_WORKERS)
    mtInstance.calculate()
    
    # Only on success
    print "Stopping workers"
    mtInstance.stopWorkers()
    
    # Compare output directories
    assert self._checkOutputs(self.mtCostDir, self.mtCmpDir)
 
 # Write headers if new file
 if writeHeaders:
    outF.write("%s\n" % ','.join(headers))
 
 for ss in STEP_SIZES:
    for fn in glob.glob(searchPath):
       print "Step size:", ss
       print "Filename:", fn
       
       tileStatsFn = os.path.join(args.outputDir, '%s-%s.csv' % (ss, os.path.basename(fn)))
       if not os.path.exists(tileStatsFn):
          try:
             costFn = 'cost.asc'
             atime = time.time()
             tile = SingleTileParallelDijkstraLCP(fn, costFn, seaLevelRiseCostFn)
             tile.setMaxWorkers(50)
             tile.findSourceCells()
             tile.setStepSize(ss)
             tile.calculate()
             btime = time.time()
             tile.writeStats(os.path.join(args.outputDir, '%s-%s.csv' % (ss, os.path.basename(fn))))
             
             dtime = btime - atime
       
             mtx = np.loadtxt(fn, dtype=int, skiprows=6)
             rows, cols = mtx.shape
             
             outF.write("%s," % fn) # File name
             outF.write("%s," % rows) # Rows
             outF.write("%s," % cols) # Columns