From 287228453b8c26f5d59b520f5a1b4a964ee82246 Mon Sep 17 00:00:00 2001 From: Carl Johnsen Date: Tue, 10 Sep 2024 15:26:45 +0200 Subject: [PATCH] #16 Added verify guard to the diffusion test, to disable the slow python implementation (i.e. allow for faster, light benchmarking). --- src/test/test_diffusion.py | 59 ++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/src/test/test_diffusion.py b/src/test/test_diffusion.py index 0646505..bc35bae 100644 --- a/src/test/test_diffusion.py +++ b/src/test/test_diffusion.py @@ -19,6 +19,7 @@ sigma = 3 # Radius has to be <= 16 for the faster GPU implementation reps = 100 plot = False +verify = True run_py = True run_cpp = True @@ -73,21 +74,25 @@ cpp_impl = np.empty(a.shape, np.uint16) diffusion_cpu(a.astype(np.uint8), kernel, cpp_impl, reps) cpp_end = datetime.datetime.now() - print (f"Parallel CPU C++ implementation took {cpp_end - cpp_start} ({(python_end - python_start) / (cpp_end - cpp_start):.2f}x)") + if verify: + print (f"Parallel CPU C++ implementation took {cpp_end - cpp_start} ({(python_end - python_start) / (cpp_end - cpp_start):.2f}x)") + else: + print (f"Parallel CPU C++ implementation took {cpp_end - cpp_start}") if plot: plt.imshow(cpp_impl[n//2], cmap='gray') plt.savefig('cpp_impl.png') plt.close() - # Check if the results are the same - diff = np.abs(python_impl.astype(np.int32) - cpp_impl.astype(np.int32)) - divergers = np.sum(diff > 0) - divergers2 = np.sum(diff > 1) - if divergers2 > 0: - print (f"Found {divergers} diverging pixels out of ({n**3}) ({divergers / n**3 * 100:.2f}%)") - print (f"Found {divergers2} pixels with a difference greater than 1 ({divergers2 / n**3 * 100:.2f}%)") - assert np.all(diff <= 1) + if verify: + # Check if the results are the same + diff = np.abs(python_impl.astype(np.int32) - cpp_impl.astype(np.int32)) + divergers = np.sum(diff > 0) + divergers2 = np.sum(diff > 1) + if divergers2 > 0: + print (f"Found {divergers} diverging pixels out of ({n**3}) ({divergers / n**3 * 100:.2f}%)") + print (f"Found {divergers2} pixels with a difference greater than 1 ({divergers2 / n**3 * 100:.2f}%)") + assert np.all(diff <= 1) # # Parallel GPU C++ implementation @@ -97,25 +102,29 @@ gpu_impl = np.empty(a.shape, np.uint16) diffusion_gpu(a.astype(np.uint8), kernel, gpu_impl, reps) gpu_end = datetime.datetime.now() - print (f"Parallel GPU C++ implementation took {gpu_end - gpu_start} ({(python_end - python_start) / (gpu_end - gpu_start):.2f}x)") + if verify: + print (f"Parallel GPU C++ implementation took {gpu_end - gpu_start} ({(python_end - python_start) / (gpu_end - gpu_start):.2f}x)") + else: + print (f"Parallel GPU C++ implementation took {gpu_end - gpu_start}") if plot: plt.imshow(gpu_impl[n//2], cmap='gray') plt.savefig('gpu_impl.png') plt.close() - # Check if the results are the same - diff = np.abs(python_impl.astype(np.int32) - gpu_impl.astype(np.int32)) - divergers = np.sum(diff > 0) - divergers2 = np.sum(diff > 1) - if divergers2 > 0: - print (f"Found {divergers} diverging pixels out of ({n**3}) ({divergers / n**3 * 100:.2f}%)") - print (f"Found {divergers2} pixels with a difference greater than 1 ({divergers2 / n**3 * 100:.2f}%)") - if plot: - plt.imshow(diff[n//2]) - plt.colorbar() - plt.savefig('diff.png') - plt.close() - checksum = np.sum(gpu_impl) - print (f"Checksum of GPU: {checksum != 0} ({checksum})") - assert np.all(diff <= 1) + if verify: + # Check if the results are the same + diff = np.abs(python_impl.astype(np.int32) - gpu_impl.astype(np.int32)) + divergers = np.sum(diff > 0) + divergers2 = np.sum(diff > 1) + if divergers2 > 0: + print (f"Found {divergers} diverging pixels out of ({n**3}) ({divergers / n**3 * 100:.2f}%)") + print (f"Found {divergers2} pixels with a difference greater than 1 ({divergers2 / n**3 * 100:.2f}%)") + if plot: + plt.imshow(diff[n//2]) + plt.colorbar() + plt.savefig('diff.png') + plt.close() + checksum = np.sum(gpu_impl) + print (f"Checksum of GPU: {checksum != 0} ({checksum})") + assert np.all(diff <= 1)