Guest User

Untitled

a guest
Feb 24th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. import numpy as np
  2. import pyopencl as cl
  3. from boxtree import TreeBuilder
  4. from boxtree.traversal import FMMTraversalBuilder
  5. from boxtree.tools import make_normal_particle_array as p_normal
  6. from boxtree.tools import particle_array_to_host
  7. from boxtree.pyfmmlib_integration import FMMLibExpansionWrangler
  8. from boxtree.fmm import drive_fmm
  9. from pyopencl.clrandom import PhiloxGenerator
  10. import numpy.linalg as la
  11.  
  12. # parameters
  13. dims = 3
  14. nsources = 50000
  15. ntargets = 50000
  16. dtype = np.float64
  17.  
  18. # Set up PyOpenCL runtime
  19. ctx = cl.create_some_context()
  20. queue = cl.CommandQueue(ctx)
  21. print(queue.context.devices)
  22.  
  23. # Generate particles
  24. sources = p_normal(queue, nsources, dims, dtype, seed=15)
  25. targets = p_normal(queue, ntargets, dims, dtype, seed=18)
  26. sources_host = particle_array_to_host(sources)
  27. targets_host = particle_array_to_host(targets)
  28. rng = PhiloxGenerator(queue.context, seed=20)
  29. sources_weights = rng.uniform(queue, nsources, dtype=np.float64).get()
  30. rng = PhiloxGenerator(queue.context, seed=22)
  31. target_radii = rng.uniform(queue, ntargets, a=0, b=0.05, dtype=np.float64).get()
  32.  
  33. # Calculate potentials using direct evaluation
  34. # distances = la.norm(sources_host.reshape(1, nsources, 2) -
  35. # targets_host.reshape(ntargets, 1, 2),
  36. # ord=2, axis=2)
  37. # pot_naive = np.sum(-np.log(distances)*sources_weights, axis=1)/2/np.pi
  38.  
  39. # Build the tree and interaction lists
  40. tb = TreeBuilder(ctx)
  41. # tree, _ = tb(queue, sources, targets=targets, target_radii=target_radii,
  42. # stick_out_factor=0.25, max_particles_in_box=30, debug=True)
  43. tree, _ = tb(queue, sources, targets=targets, max_particles_in_box=30, debug=True)
  44. tg = FMMTraversalBuilder(ctx)
  45. trav, _ = tg(queue, tree, debug=True)
  46. trav = trav.get(queue=queue)
  47.  
  48.  
  49. # Get pyfmmlib expansion wrangler
  50. def fmm_level_to_nterms(tree, level):
  51. return 20
  52.  
  53.  
  54. wrangler = FMMLibExpansionWrangler(
  55. trav.tree, 0, fmm_level_to_nterms=fmm_level_to_nterms)
  56.  
  57. # Compute FMM using shared memory parallelism
  58. pot_fmm = drive_fmm(trav, wrangler, sources_weights)
  59.  
  60. # Print accuracy
  61. # print(la.norm(pot_fmm - pot_naive, ord=np.inf)/la.norm(pot_naive, ord=np.inf))
Add Comment
Please, Sign In to add comment