Skip to content

Commit

Permalink
fix! potential minimum search
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya Antonov committed Jul 21, 2020
1 parent 80190d7 commit 114ae94
Show file tree
Hide file tree
Showing 9 changed files with 2,261 additions and 379 deletions.
268 changes: 268 additions & 0 deletions entrypoint_cpu.ipynb

Large diffs are not rendered by default.

1,857 changes: 1,857 additions & 0 deletions entrypoint_cuda.ipynb

Large diffs are not rendered by default.

31 changes: 19 additions & 12 deletions entrypoint.py → entrypoint_cuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
from utils.array_stacker import ArrayStacker

# Parameters for simulation ###################################################
NUMBER_OF_PHI_POINTS = 10
NUMBER_OF_FIELD_POINTS = 10
NUMBER_OF_FIELD_POINTS_PER_RUN = 10
NUMBER_OF_PHI_POINTS = 101
NUMBER_OF_FIELD_POINTS = 21
NUMBER_OF_FIELD_POINTS_PER_RUN = 21
NUMBER_OF_FIELD_RUNS = (
NUMBER_OF_FIELD_POINTS - 1
) // NUMBER_OF_FIELD_POINTS_PER_RUN + 1
Expand Down Expand Up @@ -56,7 +56,9 @@
)

# Go through teach of the field section and evaluate ##########################
quadrants = defaultdict(lambda: [[None] * NUMBER_OF_FIELD_RUNS for i in range(0, NUMBER_OF_FIELD_RUNS)])
quadrants = defaultdict(
lambda: [[None] * NUMBER_OF_FIELD_RUNS for i in range(0, NUMBER_OF_FIELD_RUNS)]
)

for (L_RUN, R_RUN) in itertools.product(
range(0, NUMBER_OF_FIELD_RUNS), range(0, NUMBER_OF_FIELD_RUNS)
Expand All @@ -78,12 +80,17 @@
BLOCKS_PER_GRID, THREADS_PER_BLOCK_potential_search
](DEVICE_potential_array, DEVICE_grid_search_result_array)


grid_search_result_array = DEVICE_grid_search_result_array.copy_to_host()
quadrants["potential"][L_RUN][R_RUN] = grid_search_result_array[:,:,0]
quadrants["phi01"][L_RUN][R_RUN] = phixx_array[grid_search_result_array[:,:,1].astype(int)]
quadrants["phi02"][L_RUN][R_RUN] = phixx_array[grid_search_result_array[:,:,2].astype(int)]
quadrants["phi03"][L_RUN][R_RUN] = phixx_array[grid_search_result_array[:,:,3].astype(int)]
quadrants["potential"][L_RUN][R_RUN] = grid_search_result_array[:, :, 0]
quadrants["phi01"][L_RUN][R_RUN] = phixx_array[
grid_search_result_array[:, :, 1].astype(int)
]
quadrants["phi02"][L_RUN][R_RUN] = phixx_array[
grid_search_result_array[:, :, 2].astype(int)
]
quadrants["phi03"][L_RUN][R_RUN] = phixx_array[
grid_search_result_array[:, :, 3].astype(int)
]

result = {}
for key, value in quadrants.items():
Expand All @@ -100,9 +107,9 @@
im = ax.imshow(
# result["potential"],
result["phi01"],
extent = [LOWER, UPPER, LOWER, UPPER],
origin= 'lower',
cmap='cividis',
extent=[LOWER, UPPER, LOWER, UPPER],
origin="lower",
cmap="cividis",
# cmap='YlGnBu'
# interpolation='spline36'
)
Expand Down
318 changes: 0 additions & 318 deletions entrypoint_notebook.ipynb

This file was deleted.

6 changes: 2 additions & 4 deletions functions/potential.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ def potential_function(
):
"""Order of the flux array is [phi01, phi02, phi03]"""

alpha = float(alpha)
(L, R) = (float(L), float(R))
(phi01, phi02, phi03) = (
float(phi_array[0]),
float(phi_array[1]),
Expand All @@ -30,8 +28,8 @@ def potential_function(
alpha * cos(phi02)
+ cos(phi01)
+ cos(phi03)
+ cos(phi02 - phi01 - L)
+ cos(phi02 - phi03 + R)
+ cos(phi02 - phi01 + L)
+ cos(phi02 - phi03 - R)
)
)

Expand Down
9 changes: 3 additions & 6 deletions kernels/potential_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ def __init__(
self.gpu_info = gpu_check()

def allocate_max_threads(
self, user_defined_number: Optional[int] = None,
verbose=False
self, user_defined_number: Optional[int] = None, verbose=False
) -> Tuple[int, int, int]:
if verbose:
print(
Expand Down Expand Up @@ -104,8 +103,6 @@ def kernel(
while phi01_idx < NUMBER_OF_PHI_POINTS:
while phi02_idx < NUMBER_OF_PHI_POINTS:
while phi03_idx < NUMBER_OF_PHI_POINTS:
L_FIELD = lr_array[L_offset]
R_FIELD = lr_array[R_offset]
array_out[L][R][phi01_idx][phi02_idx][
phi03_idx
] = potential_function_cuda(
Expand All @@ -114,8 +111,8 @@ def kernel(
phixx_array[phi02_idx],
phixx_array[phi03_idx],
),
L_FIELD,
R_FIELD,
lr_array[L_offset],
lr_array[R_offset],
alpha,
)

Expand Down
13 changes: 9 additions & 4 deletions kernels/potential_minimum_searcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ def __init__(self, number_of_phi_points: int):
self.gpu_info = gpu_check()

def allocate_max_threads(
self, user_defined_number: Optional[int] = None,
verbose=False
self, user_defined_number: Optional[int] = None, verbose=False
) -> Tuple[int, int]:
if verbose:
print(
Expand Down Expand Up @@ -67,6 +66,7 @@ def kernel(

while phi01_idx < NUMBER_OF_PHI_POINTS:
while phi02_idx < NUMBER_OF_PHI_POINTS:
min_phi03_grid[phi01_idx][phi02_idx] = 0 # set default
for (phi03_idx, potential) in enumerate(
potential_grid[L][R][phi01_idx][phi02_idx]
):
Expand All @@ -86,6 +86,7 @@ def kernel(
cum = 0

while phi01_idx < NUMBER_OF_PHI_POINTS:
min_phi02_grid[phi01_idx] = 0 # set default
for (phi02_idx, potential) in enumerate(
potential_grid[L][R][phi01_idx][:, 0]
):
Expand All @@ -99,9 +100,13 @@ def kernel(
cuda.syncthreads()

# Project from line to points (go across the line) ################
array_out[L][R][0] = potential_grid[L][R][0][0][0]
array_out[L][R][1] = 0
array_out[L][R][2] = 0
array_out[L][R][3] = 0

for (phi01_idx, potential) in enumerate(potential_grid[L][R][:, 0, 0]):
array_out[L][R][0] = potential_grid[L][R][0][0][0]
if potential < potential_grid[L][R][0][0][0]:
if potential < array_out[L][R][0]:
array_out[L][R][0] = potential
array_out[L][R][1] = phi01_idx
array_out[L][R][2] = min_phi02_grid[phi01_idx]
Expand Down
8 changes: 6 additions & 2 deletions tests/test_potential_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def kernel_wrapped_func(
test_result = kernel_wrapped_func[1, 1](1, 2, 3, 4, 5, 6, store_result)
self.assertEqual(store_result.copy_to_host()[0], 612345)

def test_potential_function_cuda(self):
def test_potential_function__with_cuda(self):
store_result = cuda.device_array(shape=(1), dtype=np.float32)

test_result = self.sut[1, 1](0, 0, 0, 0, 0, 1, store_result)
Expand All @@ -55,6 +55,10 @@ def test_potential_function_cuda(self):
test_result = self.sut[1, 1](0, np.pi / 2, 0, 0, 0, 1, store_result)
self.assertAlmostEqual(store_result.copy_to_host()[0], 3, f"🐳 Expected a 3")

def test_potential_function(self):
def test_potential_function__withpi2(self):
test_result = potential_function((0, np.pi / 2, 0), 0, 0, 1)
self.assertAlmostEqual(test_result, 3)

def test_potential_function__with0(self):
test_result = potential_function((0, 0, 0), 0, 0, 1)
self.assertAlmostEqual(test_result, 0)
130 changes: 97 additions & 33 deletions tests/test_potential_minimum_searcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,120 @@


class TestPotentialMinimumSearcher(unittest.TestCase):
def setUp(self):

self.NUMBER_OF_PHI_POINTS = 2
self.potential_array = np.array(
[
[
[
[
[10, 11], # along phi03 - 2 values
[12, 13], # along phi03 - 2 values
], # along phi02 - 2 values
[[14, 15], [16, 17]], # along phi02 - 2 values
], # along phi01 - 2 values
[
[[22, 23], [24, 25]],
[[18, 19], [20, 21]],
], # along phi01 - 2 values
], # along R - 2 values
[
[[[38, 39], [40, 41]], [[35, 34], [36, 37]]],
[[[33, 32], [31, 30]], [[29, 28], [27, 26]]],
], # along R - 2 values
], # along L - 2 values
dtype=np.float32,
)

self.sut = PotentialMinimumSearcher(self.NUMBER_OF_PHI_POINTS)

def tearDown(self):
pass

def test(self):
NUMBER_OF_PHI_POINTS = 2
NUMBER_OF_FIELD_POINTS = 2
THREADS_PER_BLOCK = self.sut.allocate_max_threads()

sut = PotentialMinimumSearcher(NUMBER_OF_PHI_POINTS)
THREADS_PER_BLOCK = sut.allocate_max_threads()
BLOCKS_PER_GRID = (
NUMBER_OF_FIELD_POINTS,
NUMBER_OF_FIELD_POINTS,
)
DEVICE_potential_array = cuda.to_device(self.potential_array)
DEVICE_potential_array = cuda.to_device(
np.array(
[
[
[
[
[10, 11], # along phi03 - 2 values
[12, 13], # along phi03 - 2 values
], # along phi02 - 2 values
[[14, 15], [16, 17]], # along phi02 - 2 values
], # along phi01 - 2 values
[
[[22, 23], [24, 25]],
[[18, 19], [20, 21]],
], # along phi01 - 2 values
], # along R - 2 values
[
[[[38, 39], [40, 41]], [[35, 34], [36, 37]]],
[[[33, 32], [31, 30]], [[29, 28], [27, 26]]],
], # along R - 2 values
], # along L - 2 values
dtype=np.float32,
)
)
DEVICE_out_array = cuda.device_array(
shape=(NUMBER_OF_FIELD_POINTS, NUMBER_OF_FIELD_POINTS, 4), dtype=np.float32,
)

self.sut.kernel[BLOCKS_PER_GRID, THREADS_PER_BLOCK](
sut.kernel[BLOCKS_PER_GRID, THREADS_PER_BLOCK](
DEVICE_potential_array, DEVICE_out_array,
)

expected_array = np.array(
[[[10, 0, 0, 0], [18, 1, 0, 0]], [[34, 1, 0, 1], [26, 1, 1, 1]],]
)
np.testing.assert_almost_equal(
expected_array, DEVICE_out_array.copy_to_host(), decimal=0

self.assertTrue(np.all(DEVICE_out_array.copy_to_host() == expected_array))

def test_alternative(self):
NUMBER_OF_PHI_POINTS = 5
NUMBER_OF_FIELD_POINTS = 1

sut = PotentialMinimumSearcher(NUMBER_OF_PHI_POINTS)
THREADS_PER_BLOCK = (2, 2)
BLOCKS_PER_GRID = (
NUMBER_OF_FIELD_POINTS,
NUMBER_OF_FIELD_POINTS,
)
DEVICE_potential_array = cuda.to_device(
np.array(
[
[
[
[
[5.0, 5.0, 5.0, 5.0, 5.0],
[6.5, 4.5, 4.5, 6.5, 6.5],
[8.0, 6.0, 4.0, 6.0, 8.0],
[6.5, 6.5, 4.5, 4.5, 6.5],
[5.0, 5.0, 5.0, 5.0, 5.0],
],
[
[5.0, 5.0, 5.0, 5.0, 5.0],
[4.5, 2.5, 2.5, 4.5, 4.5],
[6.0, 4.0, 2.0, 4.0, 6.0],
[6.5, 6.5, 4.5, 4.5, 6.5],
[5.0, 5.0, 5.0, 5.0, 5.0],
],
[
[5.0, 5.0, 5.0, 5.0, 5.0],
[4.5, 2.5, 2.5, 4.5, 4.5],
[4.0, 2.0, 0.0, 2.0, 4.0],
[4.5, 4.5, 2.5, 2.5, 4.5],
[5.0, 5.0, 5.0, 5.0, 5.0],
],
[
[5.0, 5.0, 5.0, 5.0, 5.0],
[6.5, 4.5, 4.5, 6.5, 6.5],
[6.0, 4.0, 2.0, 4.0, 6.0],
[4.5, 4.5, 2.5, 2.5, 4.5],
[5.0, 5.0, 5.0, 5.0, 5.0],
],
[
[5.0, 5.0, 5.0, 5.0, 5.0],
[6.5, 4.5, 4.5, 6.5, 6.5],
[8.0, 6.0, 4.0, 6.0, 8.0],
[6.5, 6.5, 4.5, 4.5, 6.5],
[5.0, 5.0, 5.0, 5.0, 5.0],
],
]
]
]
)
)
DEVICE_out_array = cuda.device_array(
shape=(NUMBER_OF_FIELD_POINTS, NUMBER_OF_FIELD_POINTS, 4), dtype=np.float32,
)

sut.kernel[BLOCKS_PER_GRID, THREADS_PER_BLOCK](
DEVICE_potential_array, DEVICE_out_array,
)

expected_array = np.array([[[0, 2, 2, 2]]])

self.assertTrue(np.all(DEVICE_out_array.copy_to_host() == expected_array))

0 comments on commit 114ae94

Please sign in to comment.