Skip to content

Commit

Permalink
#16 Added guards to toggle the different implementations to test in t…
Browse files Browse the repository at this point in the history
…est_diffusion
  • Loading branch information
carljohnsen committed Sep 9, 2024
1 parent bf1b8e1 commit 019c2b9
Showing 1 changed file with 68 additions and 61 deletions.
129 changes: 68 additions & 61 deletions src/test/test_diffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
from lib.cpp.gpu.diffusion import diffusion as diffusion_gpu

n = 333
sigma = 4 # Radius has to be <= 16 for the GPU implementation
sigma = 3 # Radius has to be <= 16 for the GPU implementation
reps = 1
plot = False

run_py = True
run_cpp = True
run_gpu = True

# Generate a random 3d array and binarize it
np.random.seed(42)
a = np.random.rand(n, n, n)
Expand All @@ -38,77 +42,80 @@
#
# Python implementation
#
python_start = datetime.datetime.now()
buf0 = a.copy().astype(np.float32)
buf1 = np.zeros_like(buf0)
for _ in range(reps):
buf1[:] = ndi.convolve1d(buf0, kernel, axis=0, mode='constant')
buf0[:] = ndi.convolve1d(buf1, kernel, axis=1, mode='constant')
buf1[:] = ndi.convolve1d(buf0, kernel, axis=2, mode='constant')
buf1[a] = 1
buf0, buf1 = buf1, buf0
python_impl = np.floor(buf0 * 65535).astype(np.uint16)
del buf1
python_end = datetime.datetime.now()
print (f"Python implementation took {python_end - python_start}")
if run_py:
python_start = datetime.datetime.now()
buf0 = a.copy().astype(np.float32)
buf1 = np.zeros_like(buf0)
for _ in range(reps):
buf1[:] = ndi.convolve1d(buf0, kernel, axis=0, mode='constant')
buf0[:] = ndi.convolve1d(buf1, kernel, axis=1, mode='constant')
buf1[:] = ndi.convolve1d(buf0, kernel, axis=2, mode='constant')
buf1[a] = 1
buf0, buf1 = buf1, buf0
python_impl = np.floor(buf0 * 65535).astype(np.uint16)
del buf1
python_end = datetime.datetime.now()
print (f"Python implementation took {python_end - python_start}")

if plot:
plt.imshow(python_impl[n//2], cmap='gray')
plt.savefig('python_impl.png')
plt.close()
plt.imshow(a[n//2], cmap='gray')
plt.savefig('original.png')
plt.close()
if plot:
plt.imshow(python_impl[n//2], cmap='gray')
plt.savefig('python_impl.png')
plt.close()
plt.imshow(a[n//2], cmap='gray')
plt.savefig('original.png')
plt.close()

#
# Parallel CPU C++ implementation
#
cpp_start = datetime.datetime.now()
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 run_cpp:
cpp_start = datetime.datetime.now()
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 plot:
plt.imshow(cpp_impl[n//2], cmap='gray')
plt.savefig('cpp_impl.png')
plt.close()
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)
# 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
#
gpu_start = datetime.datetime.now()
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 run_gpu:
gpu_start = datetime.datetime.now()
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 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.imshow(gpu_impl[n//2], cmap='gray')
plt.savefig('gpu_impl.png')
plt.close()
checksum = np.sum(gpu_impl)
print (f"Checksum of GPU: {checksum != 0} ({checksum})")
assert np.all(diff <= 1)

# 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)

0 comments on commit 019c2b9

Please sign in to comment.