# Check that the results match cp_result = cp_data.get() assert np.allclose(np_data, cp_result) del cp_data del padded_data free_memory_pool() for use_pinned_memory in pinned_memory_mode: # Create empty lists for storing results median_filter_results = [] if use_pinned_memory: memory_string = "with pinned memory" else: memory_string = "without pinned memory" for size in ARRAY_SIZES[:SIZES_SUBSET]: imaging_obj = CupyImplementation(size, DTYPE, use_pinned_memory) avg_mf = imaging_obj.timed_image_stack_median_filter( N_RUNS, FILTER_SIZE) if avg_mf > 0: median_filter_results.append(avg_mf) write_results_to_file([LIB_NAME, memory_string], "median filter", median_filter_results)
size=(N_IMAGES, N, N)).astype(DTYPE) np_data = cp_data.get() gpu_result = np.empty_like(np_data) np_padded_data = create_padded_array(np_data, FILTER_SIZE // 2, "symmetric") cp_padded_data = cp.array(np_padded_data) diff = 0.5 cpu_result = tomopy.misc.corr.median_filter(np_data, FILTER_SIZE, axis=0) for i in range(N_IMAGES): cp.cuda.runtime.deviceSynchronize() cupy_two_dim_median_filter(cp_data[i], cp_padded_data[i], FILTER_SIZE) cp.cuda.runtime.deviceSynchronize() gpu_result[i][:] = cp_data[i].get() assert np.allclose(gpu_result, cpu_result) tomopy_median_results = [] for size in ARRAY_SIZES[:SIZES_SUBSET]: arr = np.random.uniform(low=0, high=20, size=size).astype(DTYPE) start = time.time() tomopy.misc.corr.median_filter(arr, FILTER_SIZE, axis=0) end = time.time() tomopy_median_results.append(end - start) write_results_to_file(["tomopy", str(FILTER_SIZE)], "median filter", tomopy_median_results)
mode="mirror") # Check that the results match gpu_result = cp_data.get() try: assert np.allclose(cpu_result, gpu_result) except AssertionError: print("Original data:") print(np_data) print("GPU Result data:") print(gpu_result) print("CPU Result data:") print(cpu_result) exit() del cp_data del padded_data free_memory_pool() for size in ARRAY_SIZES[:SIZES_SUBSET]: imaging_obj = CupyImplementation(size, DTYPE) avg_mf = imaging_obj.timed_async_median_filter(N_RUNS, FILTER_SIZE) if avg_mf > 0: median_filter_results.append(avg_mf) write_results_to_file( [LIB_NAME, "async", str(FILTER_SIZE)], "median filter", median_filter_results)
numpy_background_correction(np_data, np_dark, np_flat) assert np.allclose(np_data, cp_data.get()) free_memory_pool(random_test_arrays) for use_pinned_memory in pinned_memory_mode: # Create empty lists for storing results background_correction_results = [] if use_pinned_memory: memory_string = "with pinned memory" else: memory_string = "without pinned memory" for size in ARRAY_SIZES[:SIZES_SUBSET]: imaging_obj = CupyImplementation(size, DTYPE, use_pinned_memory) avg_bc = imaging_obj.timed_imaging_operation( N_RUNS, cupy_background_correction, "background correction", 3) if avg_bc > 0: background_correction_results.append(avg_bc) write_results_to_file( [LIB_NAME, memory_string], BACKGROUND_CORRECTION, background_correction_results, )
background_correction(numba_data, numba_dark, numba_flat, MINIMUM_PIXEL_VALUE, MAXIMUM_PIXEL_VALUE) numpy_background_correction(np_data, np_dark, np_flat, MINIMUM_PIXEL_VALUE, MAXIMUM_PIXEL_VALUE) assert np.allclose(np_data, numba_data) add_arrays_results = [] background_correction_results = [] def background_correction_fixed_clips(dark, data, flat): return background_correction(data, dark, flat, MINIMUM_PIXEL_VALUE, MAXIMUM_PIXEL_VALUE) for size in ARRAY_SIZES[:SIZES_SUBSET]: imaging_obj = NumbaParallelImplementation(size, DTYPE) avg_add = imaging_obj.timed_imaging_operation(N_RUNS, add_arrays, "adding", 2) avg_bc = imaging_obj.timed_imaging_operation( N_RUNS, background_correction_fixed_clips, "background correction", 3) add_arrays_results.append(avg_add) background_correction_results.append(avg_bc) write_results_to_file([LIB_NAME, mode], ADD_ARRAYS, add_arrays_results) write_results_to_file([LIB_NAME, mode], BACKGROUND_CORRECTION, background_correction_results)
data = self.cpu_arrays[0] total_time = 0 for _ in range(reps): total_time += time_function( lambda: scipy_median_filter(data, FILTER_SIZE)) operation_time = total_time / reps self.print_operation_times(total_time=total_time, operation_name="median filter", runs=reps) return operation_time # Create empty lists for storing results add_arrays_results = [] background_correction_results = [] median_filter_results = [] for size in ARRAY_SIZES[:SIZES_SUBSET]: imaging_obj = CPUImplementation(size, DTYPE) add_arrays_results.append(imaging_obj.timed_add_arrays(N_RUNS)) background_correction_results.append( imaging_obj.timed_background_correction(N_RUNS)) median_filter_results.append(imaging_obj.timed_median_filter(N_RUNS)) write_results_to_file([LIB_NAME], ADD_ARRAYS, add_arrays_results) write_results_to_file([LIB_NAME], BACKGROUND_CORRECTION, background_correction_results) write_results_to_file(["scipy"], "median filter", median_filter_results)
np_padded_data = create_padded_array(np_data, size // 2, "symmetric") cp_padded_data = cp.array(np_padded_data) diff = 0.5 cpu_result = tomopy.misc.corr.remove_outlier(np_data, diff, size) for i in range(IMAGES): cp.cuda.runtime.deviceSynchronize() cupy_two_dim_remove_outliers(cp_data[i], cp_padded_data[i], diff, size, "light") cp.cuda.runtime.deviceSynchronize() gpu_result[i][:] = cp_data[i].get() cp.cuda.runtime.deviceSynchronize() assert np.allclose(gpu_result, cpu_result) remove_outlier_results = [] for size in ARRAY_SIZES[:SIZES_SUBSET]: imaging_obj = CupyImplementation(size, DTYPE) avg_ro = imaging_obj.timed_async_remove_outlier(N_RUNS, FILTER_SIZE) if avg_ro > 0: remove_outlier_results.append(avg_ro) write_results_to_file( [LIB_NAME, "async", str(FILTER_SIZE)], "remove outlier", remove_outlier_results )
dtype=padded_data.dtype) gpu_padded_data.set_async(padded_data) # Run the median filter on the GPU print(gpu_data[0]) pycuda_median_filter(gpu_data, gpu_padded_data, filter_height, filter_width) print(gpu_data[0]) # Run the scipy median filter scipy_median_filter(np_data, size=filter_size) # Check that the results match print(np_data[0]) print(np.isclose(np_data, gpu_data.get())) assert np.allclose(np_data, gpu_data.get()) median_filter_results = [] free_memory_pool([gpu_data, gpu_padded_data]) for size in ARRAY_SIZES[:SIZES_SUBSET]: obj = PyCudaSourceModuleImplementation(size, DTYPE) avg_median = obj.timed_median_filter(N_RUNS, FILTER_SIZE) print("Free bytes after run:", get_free_bytes()) if avg_median > 0: median_filter_results.append(avg_median) write_results_to_file([LIB_NAME, mode], "median filter", median_filter_results)
# Checking that cupy will change the value of the array all_one = cp.ones((1, 1, 1)) cupy_add_arrays(all_one, all_one) assert cp.all(all_one == 2) free_memory_pool([all_one]) for use_pinned_memory in pinned_memory_mode: # Create empty lists for storing results add_arrays_results = [] if use_pinned_memory: memory_string = "with pinned memory" else: memory_string = "without pinned memory" for size in ARRAY_SIZES[:SIZES_SUBSET]: imaging_obj = CupyImplementation(size, DTYPE, use_pinned_memory) avg_add = imaging_obj.timed_imaging_operation(N_RUNS, cupy_add_arrays, "adding", 2) if avg_add > 0: add_arrays_results.append(avg_add) write_results_to_file([LIB_NAME, memory_string], ADD_ARRAYS, add_arrays_results)