重明鸟计算工作室
重明鸟计算工作室
首页 算法 应用
OpenCL示例-01.设备遍历和向量加法
标签:异步计算,OpenCL发布日期:2020-08-28作者:重明鸟计算工作室

一、OpenCL遍历设备程序示例
import pyopencl as cl

#显示OPENCL版本号,cl.h的版本号
print("CL_VERSION:",cl.VERSION)
print("CL_HEADER_VERSION:",cl.get_cl_header_version())
print()
 
#获取当前所有计算平台
platforms  = cl.get_platforms()
print("Platform num:",len(platforms))
 
#遍历所有平台中的所有计算设备,并显示它们的信息
for plat in platforms:
    print("--Platform Name:",plat.get_info(cl.platform_info.NAME))
#   print("--Platform Extensions:",plat.get_info(cl.platform_info.EXTENSIONS))
    print("--Platform Profile:",plat.get_info(cl.platform_info.PROFILE))
    print("--Platform Vendor:",plat.get_info(cl.platform_info.VENDOR))
    print("--Platform Version:",plat.get_info(cl.platform_info.VERSION))
 
    devices = plat.get_devices(cl.device_type.ALL)
    print("--device num:",len(devices))
 
    for device in devices:
        print("----Name:",device.get_info(cl.device_info.NAME))
        print("----OpenCL_C_Version:",device.get_info(cl.device_info.OPENCL_C_VERSION))
        print("----Vendor:",device.get_info(cl.device_info.VENDOR))
        print("----Version:",device.get_info(cl.device_info.VERSION))
        print("----Driver Version:",device.get_info(cl.device_info.DRIVER_VERSION))
         
        print("----MAX_WORK_GROUP_SIZE:",device.get_info(cl.device_info.MAX_WORK_GROUP_SIZE))
        print("----MAX_COMPUTE_UNITS:",device.get_info(cl.device_info.MAX_COMPUTE_UNITS))
        print("----MAX_WORK_ITEM_SIZES:",device.get_info(cl.device_info.MAX_WORK_ITEM_SIZES))
        print("----LOCAL_MEM_SIZE:",device.get_info(cl.device_info.LOCAL_MEM_SIZE))
 
 

运行结果

二、OpenCL向量加法程序示例
1.main.py主程序
import pyopencl as cl
import numpy as np

if __name__ == '__main__':
    
    #step 1:选择并创建上下文
    context = cl.create_some_context() 
    
    #step 3:创建命令队列
    queue = cl.CommandQueue(context)
    
    #定义两个数组,每个数组包含4个元素
    buf_A = np.array([1.0,2.0,3.0,4.0]).astype(np.float32)
    buf_B = np.array([5.0,6.0,7.0,8.0]).astype(np.float32)
    
    #获得OpenCL内存标志集
    mf = cl.mem_flags
    a_buf = cl.Buffer(context, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=buf_A)
    b_buf = cl.Buffer(context, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=buf_B)
    c_buf = cl.Buffer(context, mf.WRITE_ONLY , buf_A.nbytes )
    
    #加载并创建CL程序
    f_cl = open("add.cl","r")
    f_buf = f_cl.read()
    prg = cl.Program(context,f_buf).build()
    
    #运行
    prg.sum(queue,buf_A.shape,None,a_buf,b_buf,c_buf)
    
    #克隆一块相同的缓冲区
    c_ret = np.empty_like(buf_A)
    #复制设备内存中的值
    cl.enqueue_copy(queue,c_ret,c_buf)
    #输出结果
    print(c_ret)
2.add.cl内核程序
__kernel void sum(__global const float *a,__global const float *b,__global float *c)
{  
    int cid = get_global_id(0);  
    c[cid] = a[cid] + b[cid];
}
运行结果

桂ICP备11003301号-1 公安备案号:45040302000027 Copyright @ 2011-2020 By Sun zi chao