PyOpenCLハンズオン 課題解答例

課題1解答例

# -*- 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)) # 結果の検証

ダウンロード

課題2解答例

# -*- 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)
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)
dest_buf = pyopencl.Buffer(context, mem_flags.WRITE_ONLY, dest.nbytes)

program = pyopencl.Program(context, '''
__kernel void reverse(__global const float* src, __global float* dest)
{
    const int gid = get_global_id(0);
    dest[get_global_size(0) - 1 - gid] = src[gid];
}
''').build()

program.reverse(queue, (size,), None, a_buf, dest_buf)
pyopencl.enqueue_copy(queue, dest, dest_buf)

print linalg.norm(dest - a[::-1]) # 結果の検証

ダウンロード

Table Of Contents

Previous topic

PyOpenCLハンズオン in kyoto.py(2011年9月24日)

This Page