print "--------------- OPENCL ---------------"
print "--------------------------------------"

require "luaopencl"

cl = luaopencl
cl.utilInitExtensions()

--
function cl.utilGetDevice()
	local ids = cl.cl_platform_idArray(8)
	local err,idcnt = cl.clGetPlatformIDs(8,ids:cast())

	if (err ~= 0 or idcnt < 1) then return end

	local buf = sw:new(1024)

	local device = nil
	local units = 0
	for i=1,idcnt do
		local pf = ids[i-1]
		local err = cl.clGetPlatformInfo(pf,
			cl.CL_PLATFORM_PROFILE,
			buf:storage())

		local devids = cl.cl_device_idArray(16)

		buf:astype("string")
		if (err == 0 and buf:data() == "FULL_PROFILE") then
			-- iterate devices
			local err,dcnt = cl.clGetDeviceIDs(pf,cl.CL_DEVICE_TYPE_GPU,16,devids:cast())

			buf:astype("uint")
			for n=1,dcnt do
				local dev = devids[n-1]
				cl.clGetDeviceInfo(dev,cl.CL_DEVICE_GLOBAL_MEM_SIZE,buf:storage())
				print ("CL_DEVICE_GLOBAL_MEM_SIZE",buf:at(0))
				cl.clGetDeviceInfo(dev,cl.CL_DEVICE_MAX_COMPUTE_UNITS,buf:storage())
				print ("CL_DEVICE_MAX_COMPUTE_UNITS",buf:at(0))
				if (buf:at(0) > units) then
					units = buf:at(0)
					device = dev
				end
			end

		end

	end

	return device,units
end

print(GetDevice())



