Posts

Showing posts with the label opencl

Dissecting OpenCL code

Image
In my previous post , I got sample OpenCL matrix multiplication kernel to run inside Python on Windows. That was one of the first time I got to work on openCL. The code itself is pretty self explanatory for the most part, but it is interesting to see the comparisons with CUDA, and also how the kernel gets invoked though python. import pyopencl as cl import numpy as np import os os.environ['PYOPENCL_CTX']='0' (n, m, p) = (3, 4, 5) a = np.random.randn(n, m).astype(np.float32) b = np.random.randn(m, p).astype(np.float32) c = np.zeros((n*p), dtype=np.float32) context = cl.create_some_context() queue = cl.CommandQueue(context) mf = cl.mem_flags a_buf = cl.Buffer(context, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=a) b_buf = cl.Buffer(context, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=b) c_buf = cl.Buffer(context, mf.WRITE_ONLY, c.nbytes) prg = cl.Program(context, """ __kernel void multiply(ushort n, ushort m, ushort p, __global float *a, __global fl...

Running pyopencl in Windows

Image
After my failed attempt at executing openCL code on my AMD APU mini PC within WSL, I'm now attempting to do that in Windows. I haven't coded in python on Windows before, so I had to look up on how I can create a virtual env, and activate it. It was fairly straighforward with the use of powershell, which allowed me to use some of the Linux commands familiar to me. After install pyopencl by running pip install pyopencl and then jumped into the python console import pyopencl from pyopencl.tools import get_test_platforms_and_devices get_test_platforms_and_devices() gave me a warning about not having siphash24 installed (which I installed later) but it was also showing the GPU as an opencl device [(<pyopencl.Platform 'AMD Accelerated Parallel Processing' at 0x7ff8f0e98000>, [<pyopencl.Device 'gfx902' on 'AMD Accelerated Parallel Processing' at 0x146c185c0b0>])] Getting back to the exercise at https://homepages.math.uic.edu/~jan/mcs...

Getting Started with pyOpenCL in WSL

Image
In the past, I experimented with GPU programming, using CUDA on Nvidia GPUs. Back then, the bandwidth of the PCIe interface between the CPU and the GPU was deemed to be one of it's Achilles heel. It was about the same time when AMD was putting out APUs post-ATI acquisition, but at that time, the company was just struggling compared to it's peer, Nvidia. Having highly capable and complex CPU cores with massively parallel  GPU cores share the same memory space and working together in a heterogeneous compute environment sounded like a dream. I recently had to look into buying a Windows PC for my wife and I found some nice used mini PCs with a  35 W AMD Ryzen 5 PRO 2400GE on ebay. I found them very interesting and decided to purchase one.  I'll attempt to use this GPU for general purpose compute and see how things go. I'll just install the necessary libraries, and run some sample code. This is also an opportunity for me to test this out in WSL and see if it works. I use...