# -*- coding: utf-8 -*-
import pyopencl
from pyopencl import mem_flags
import numpy
from numpy import linalg
size = 50000
a = numpy.random.rand(size).astype(numpy.float32)
b = numpy.random.rand(size).astype(numpy.float32)
dest = numpy.empty_like(a)
context = pyopencl.create_some_context()
queue = pyopencl.CommandQueue(context)
a_buf = pyopencl.Buffer(context, mem_flags.READ_ONLY | mem_flags.COPY_HOST_PTR, hostbuf=a)
b_buf = pyopencl.Buffer(context, mem_flags.READ_ONLY | mem_flags.COPY_HOST_PTR, hostbuf=b)
dest_buf = pyopencl.Buffer(context, mem_flags.WRITE_ONLY, dest.nbytes)
program = pyopencl.Program(context, '''
__kernel void sub(
__global const float* a,
__global const float* b,
__global float* dest
)
{
const int gid = get_global_id(0);
dest[gid] = a[gid] - b[gid];
}
''').build()
program.sub(queue, (size,), None, a_buf, b_buf, dest_buf)
pyopencl.enqueue_copy(queue, dest, dest_buf)
print linalg.norm(dest - (a - b)) # 結果の検証