<jupyter_start><jupyter_text>Let's Start<jupyter_code>import pyopencl as cl
import numpy as np 
opencl_platforms = [p.name for p in cl.get_platforms()]
print(opencl_platforms)
nvidia_platform = [p for p in cl.get_platforms() if p.name == "NVIDIA CUDA"][0]
nvidia_devices = nvidia_platform.get_devices()
nvidia_devices<jupyter_output><empty_output><jupyter_text>Building Programs Using the PyOpenCL to create a OpenCL context, then declaring OPENCL kernel code and compiling it  The code is for a simple vector sum c = a + b<jupyter_code>nvidia_context = cl.Context(devices = nvidia_devices)

program_source = """
    kernel void sum(global float *a, global float *b, global float *c){
        int gid = get_global_id(0);
        c[gid] = a[gid] + b[gid];
    }
"""

nvidia_program_source = cl.Program(nvidia_context, program_source)
nvidia_program = nvidia_program_source.build()
program_kernel_names = nvidia_program.get_info(cl.program_info.KERNEL_NAMES)
print("Kernel Names:", program_kernel_names)
def run_ocl_kernel(queue, kernel, global_size, input_tuples, output_tuples, local_size = (32,)):
    
    # Copying data onto the device 
    for (array, buffer) in input_tuples:
        cl.enqueue_copy(queue, src = array, dest = buffer)
    
    # Running program on the device 
    kernel_arguments = [buffer for (_, buffer) in input_tuples]
    kernel_arguments += [buffer for (_, buffer) in output_tuples]
    
    kernel(queue, global_size, local_size, *kernel_arguments)
    
    # Copying data off the device
    for (arr, buffer) in output_tuples:
        cl.enqueue_copy(queue, src = buffer, dest = arr)
        
    # waiting for everything to finish
    queue.finish()
def check_sum_results(a,b,c):
    c_ref = a + b
    err = np.abs(c - c_ref)
    if (err.sum() > 0.0).any() :
        print("Result does not match")
    else:
        print("Result Matches")<jupyter_output><empty_output><jupyter_text>Preparing Synthetic Data<jupyter_code>N = int(2**20)
a = np.random.rand(N).astype(np.float32)
b = np.random.rand(N).astype(np.float32)
c = np.empty_like(a)<jupyter_output><empty_output><jupyter_text>Device Memory Setup<jupyter_code>a_nvidia_buffer = cl.Buffer(nvidia_context, flags = cl.mem_flags.READ_ONLY, size = a.nbytes)
b_nvidia_buffer = cl.Buffer(nvidia_context, flags = cl.mem_flags.READ_ONLY, size = b.nbytes)
c_nvidia_buffer = cl.Buffer(nvidia_context, flags = cl.mem_flags.WRITE_ONLY, size = c.nbytes)
nvidia_queue = cl.CommandQueue(nvidia_context)
input_tuples = ((a, a_nvidia_buffer), (b, b_nvidia_buffer))
output_tuples = ((c, c_nvidia_buffer),)
run_ocl_kernel(nvidia_queue, nvidia_program.sum, (N,), input_tuples, output_tuples)
check_sum_results(a, b ,c)
%timeit run_ocl_kernel(nvidia_queue, nvidia_program.sum, (N,), input_tuples, output_tuples)
# LAPTOP CHARGE ON XD

%timeit run_ocl_kernel(nvidia_queue, nvidia_program.sum, (N,), input_tuples, output_tuples)<jupyter_output>4.48 ms ± 209 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)<jupyter_text>Using GLOBAL MEMORY<jupyter_code>def create_input_memory(context, input_array):
    return [(array, cl.Buffer(context, flags = cl.mem_flags.READ_ONLY, size = array.nbytes)) for array in input_array]
def create_output_memory(context, output_array):
    return [(array, cl.Buffer(context, flags = cl.mem_flags.WRITE_ONLY, size = array.nbytes)) for array in output_array]
N = int(2**20)
a = np.random.rand(N).astype(np.float32)
b = np.random.rand(N).astype(np.float32)
c = np.empty_like(a)
input_tuples = create_input_memory(nvidia_context, (a, b, ))
output_tuples = create_output_memory(nvidia_context, (c,))
run_ocl_kernel(nvidia_queue, nvidia_program.sum, (N,), input_tuples, output_tuples)
check_sum_results(a,b,c)<jupyter_output>Result Matches
{'fraction_non_alphanumeric': 0.11643468950749465, 'fraction_numerical': 0.008029978586723769, 'mean_word_length': 5.388034188034188, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '540858', 'license': 'MIT', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1340, 'n_words': 299, 'path': '01 - Getting Started.ipynb', 'repo_name': 'ayushmankumar7/learning_OPENCL_PYTHON', 'size': '3736'}