Package: pytocl
pytoclA python library to seamlessly convert python functions to functions making use of OpenCLSetuppython setup.py installDependenciesPython 3Python librariesnumpypyopenclOpenCL compatible hardware (version 1 is enough)An OpenCL runtime (eg. AMDAPPSDK for AMD CPUs/GPUs)UsageThe complete example can be found in examples/usage.py. For more examples check tests/cltest.py and examples/benchmarks.py.1. Creating a function to be convertedCreate a function (or convert an existing one) to be parallelized. In this example we calculateoutput = a + bfor vectors, so we will be using one dimension. You can get the current index / global work id in the first dimension with get_global_id(0).frompytoclimport*defparallel_add(a,b,output):i=get_global_id(0)output[i]=a[i]+b[i]2. Converting the functionFirst we need to create the information for each argument of our function excluding the dimension parameters. For thisCLArgDescis used which holds information about the type of the argument as well as thearray_size(0 for scalars) and whether the argument is used as an output (but not necessarily copied back to the host).# Our vectors will have 16 elementsarray_size=16dim_shape=(array_size,)# Create the descriptors for the arguments of the function, excluding the dimensionarg_desc_a=CLArgDesc(CLArgType.float32_array,array_size=array_size)# aarg_desc_b=CLArgDesc(CLArgType.float32_array,array_size=array_size)# barg_desc_output=CLArgDesc(CLArgType.float32_array,array_size=array_size)# outputNext we need to create the descriptor for the function itself which has the gloabl id / dimension shape information and more information about its arguments (ie. whether they’re copied from host to device before execution and whether theyre copied from device to host after execution)"""
Create the function descriptor with the global id / dimension shape information.

Arguments can be added by chaining .arg() calls (the argument order has to match the original
function's argument order (ie. arg_desc_a -> a, arg_desc_b -> b, arg_desc_output -> output).
is_readonly has to be set to False for arguments that are assigned to in the function.

copy_in() or copy_out() can be called to copy the last added argument from host to
device before execution or from device to host after execution.
"""func_desc=(CLFuncDesc(parallel_add,global_size).arg(arg_desc_a).copy_in().arg(arg_desc_b).copy_in().arg(arg_desc_output,is_readonly=False).copy_out())Now we can compile the function which gives us a normal python function we can call# Compile the actual function, you can also pass an argument to compile with a CL context.# By default it uses cl.create_some_context()parallel_add_cl=CLFunc(func_desc).compile()3. Calling the functionYou can call the function like a normal python function now, all the copying will be done for you. The passed scalars can be normal python types but all arrays have to be numpy ndarrays.importnumpyasnp[...]# Create the host buffers / vectors, the dtype needs to match the arg type of the arg desca=np.array([1]*array_size,dtype=np.float32)b=np.array([2]*array_size,dtype=np.float32)output=np.array([0]*array_size,dtype=np.float32)# Now we can execute the compiled function, we need to provide buffers for all output copies.# For input copies we could also pass None to not copy themparallel_add_cl(a,b,output)# output should now be a + bprint("A:",a)print("B:",b)print("Output:",output)You can also pass None for arguments only used as copy-inputs meaning no data will be copied and the current content will be retained.Limitations / Todo listOnly simple python functions are supported.All mathematical and logical expressionsAll literals except for stringsIf statements and IfElse constructsWhile loopsFor loops currently only support range()Function calls get converted to use the same name in the kernel, but the called functions themselves aren’t converted if they aren’t available yet (eg. currently you canfrom math import expand use exp(x))Type inference for local variables is currently limited. Defaults tofloat, variables namedi,j,kor starting withi_becomeint, variables starting withb_becomeboolreturn valueis not supported, outputs have to be passed as an argumentArray slices are not supportedList comprehensions and other python-specific constructs are not supportedThe source code of the function has to be available for conversion (which is often the case)CUDA support?Clean up the converter codeBenchmarksinexamples/benchmarks.pyTest hardware was an AMD Phenom II 1090T and an AMD 6970 The original functions and OpenCL with CPU are orders of magnitude slower (not shown here, you can uncomment the line inbenchmarks.pythough). Numpy versions are compared to the clified GPU versions.Matrix Multiply 100 times for matrices of same sizeMatrix sizeRuntime NumpyRuntime OpenCL GPURelative NumpyRelative OpenCL GPU(128, 128)0.02s0.08s100.00%372.00%(256, 256)0.08s0.16s100.00%200.18%(512, 512)0.40s0.60s100.00%148.81%(1024, 1024)2.64s3.64s100.00%137.71%(2048, 2048)17.76s28.07s100.00%158.03%Neural Network sigmoid layer forward pass 100 times for input and weight matrices of same sizeMatrix sizeRuntime NumpyRuntime OpenCL GPURelative NumpyRelative OpenCL GPU(128, 128)0.05s0.05s100.00%114.13%(256, 256)0.19s0.13s100.00%70.13%(512, 512)2.62s0.54s100.00%20.62%(1024, 1024)11.52s3.65s100.00%31.66%(2048, 2048)60.01s27.40s100.00%45.66%2 layer MLP forward pass with 128 batch size and input vectors / weight matrices of same size 100 timesMatrix sizeRuntime NumpyRuntime OpenCL GPURelative NumpyRelative OpenCL GPU(128, 128)0.09s0.07s100.00%77.65%(256, 256)0.43s0.11s100.00%26.72%(512, 512)1.35s0.32s100.00%23.80%(1024, 1024)3.08s1.08s100.00%35.09%(2048, 2048)7.71s3.56s100.00%46.17%ContributorsToraxxx (Developer)