Hi Zebo,

This is interesting: I've confirmed that randomly accessing the CellVariable 
value at a point on the PeriodicGrid2DTopBottom takes 1.5 seconds, while the 
same access on a UniformGrid2D takes almost no time. I broke your test case 
into two separate files, one for polar and one for Cartesian coordinates, and 
profiled using

    python -m cProfile -o profile.log script.py

then applied RunSnakeRun to the output,

    runsnake profile.log

This resulted in some interesting square map results, illustrated in the 
snapshot. They're interesting to explore, and easy to generate.

Both windows represent the same operation, __getNearestCellID, with the 
Cartesian grid operation in the left-hand window and the Polar grid operation 
in the right-hand window.


I'm still looking into it, but the problem appears to be in computing which 
block of memory holds data mapped to the point (<float> x, <float> y) in space.

On a Cartesian grid, this is a trivial computation given the grid dimensions 
and mesh spacing, so the operation __getNearestCellID takes 7 milliseconds. The 
operations involved take up the entire box in the left-hand window of the 
snashot: Python is doing a lot of things, but none of the operations are 
expensive.

On the Polar grid, the full window represents 1.5 seconds and involves both 
Numpy and BLAS: calculating a point's nearest neighbors in a space of arbitrary 
dimensions and connectivity takes serious work.


There may be a philosophical discussion over vectorized operations (applied to 
every node in the grid, which invokes high-performance numerical libraries) 
versus random access or iterating (which is pure Python, and slow). I don't 
know enough about FiPy internals to do it justice. The bottom line is, while 
randomly accessing spatial points is incredibly expensive, when FiPy applies a 
mathematical operation over the entire grid it accesses mesh elements in 
sequence, not at random, so that solving equations on Cartesian vs. polar grids 
of the same number of nodes should execute in similar amounts of time. The 
random access penalty for arbitrary grids should not be incurred. If you have 
an example demonstrating this is not the case, please send it in for closer 
scrutiny.


Hope that clarifies. For reference, the profiled scripts are here (Cartesian, 
https://gist.github.com/tkphd/c41fad935fe8516001e4) and here (polar, 
https://gist.github.com/tkphd/0cb3dda6318dd99fa773). If anyone else would like 
to weigh in, please do!


Best regards,

Trevor


Trevor Keller, Ph.D.
Materials Science and Engineering Division
National Institute of Standards and Technology
100 Bureau Dr. MS 8550; Gaithersburg, MD 20899
Office: 223/A131 or (301) 975-2889



________________________________
From: [email protected] <[email protected]> on behalf of Zebo LI 
<[email protected]>
Sent: Thursday, September 17, 2015 12:04 PM
To: FIPY
Subject: Speed for extracting data

I defined two Variables in two different meshes,
1, in Polar Coordinate system
Pmesh = PeriodicGrid2DTopBottom(dx=dLnR, dy=dCta, nx=nLnR, ny=nCta)
C_V = CellVariable(name = "Vacancy Concentration", mesh = Pmesh, value = 0.0)
2,The other is in Cartesian Coordinate system
Cmesh = Grid2D(dx=dx, dy=dy, nx=nx, ny=ny)
C_V_xy = CellVariable(name = "C_V in XY plane",mesh=Cmesh,value = 0.0)

When I extract data from two variables using for example, C_V((1.0,1.0)) and 
C_V_xy((20,20)), I found that the first one is much slower than the second one.
C_V_xy displays result immediately, but C_V takes 2-3 sec to show the result.
Why is it? Because I thought both of them are doing interpolation to get values 
at certain points, why there is a speed difference.

best,
Zebo
_______________________________________________
fipy mailing list
[email protected]
http://www.ctcms.nist.gov/fipy
  [ NIST internal ONLY: https://email.nist.gov/mailman/listinfo/fipy ]

Reply via email to