From 217a264a4230a281f5a070da77e705e0ae529167 Mon Sep 17 00:00:00 2001 From: Brian Tuomanen Date: Tue, 18 Feb 2020 10:58:55 -0800 Subject: [PATCH] Create simple_element_kernel_example0.py --- Chapter03/simple_element_kernel_example0.py | 31 +++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Chapter03/simple_element_kernel_example0.py diff --git a/Chapter03/simple_element_kernel_example0.py b/Chapter03/simple_element_kernel_example0.py new file mode 100644 index 0000000..90a69fa --- /dev/null +++ b/Chapter03/simple_element_kernel_example0.py @@ -0,0 +1,31 @@ +import numpy as np +import pycuda.autoinit +from pycuda import gpuarray +from time import time +from pycuda.elementwise import ElementwiseKernel + +host_data = np.float32( np.random.random(50000000) ) + +gpu_2x_ker = ElementwiseKernel( +"float *in, float *out", +"out[i] = 2*in[i];", +"gpu_2x_ker") + +def speedcomparison(): + t1 = time() + host_data_2x = host_data * np.float32(2) + t2 = time() + print('total time to compute on CPU: %f' % (t2 - t1)) + device_data = gpuarray.to_gpu(host_data) + # allocate memory for output + device_data_2x = gpuarray.empty_like(device_data) + t1 = time() + gpu_2x_ker(device_data, device_data_2x) + t2 = time() + from_device = device_data_2x.get() + print('total time to compute on GPU: %f' % (t2 - t1)) + print('Is the host computation the same as the GPU computation? : {}'.format(np.allclose(from_device, host_data_2x) )) + + +if __name__ == '__main__': + speedcomparison()