Hi.

I've just finished writing my first pyopencl program. While the OpenCL C
part build()s, calling it fails with a TypeError in pyopencl/__init__.py
I'm trying to run this on an Ubuntu 13.10 saucy with the python-pyopencl
2013.1~git20130613-1 provided in the Ubuntu repo (I've tried installing the
2013.2 version via pip, but run into compile errors due to it not finding
CL/cl.h in /opt/AMDAPP/include/; how can I tell pip that it should make the
compiler look there?).

Here are call, traceback and error:

    program.simulate(queue, instances, None,
                     np.int32(num_particles),
                     position_buf,
                     dest_buf)

Traceback (most recent call last):
  File "./particle_sim.py", line 88, in <module>
    simulate()
  File "./particle_sim.py", line 65, in simulate
    dest_buf)
  File "/usr/lib/python2.7/dist-packages/pyopencl/__init__.py", line 567,
in kernel_call
    global_offset, wait_for, g_times_l=g_times_l)
TypeError: object of type 'int' has no len()

The full code is in the attachment for error-reproduction (and, as I've
learned in the meantime, as an example on how not to do pairwise
computation of particle influences). Thanks in advance.

Liebe Grüße,
Sebastian Hoffmann
#!/usr/bin/env python

import pyopencl as cl
import numpy as np
import random
from math import sqrt

# Functions for creating the initial particle data
def initial_position():
    x = random.gauss(0, 1)
    y = random.gauss(0, 1)
    z = random.gauss(0, 1)
    distance = sqrt(x**2 + y**2 + z**2)
    return [x/distance, y/distance, z/distance]

# Main program
def simulate(num_particles = 10):
    # OpenCL basics
    context = cl.create_some_context()
    queue = cl.CommandQueue(context)
    # Setting up particles (data and buffers)
    position = np.array([initial_position()
                         for i in range(0, num_particles)]).astype(np.float32)
    interactions = np.zeros((num_particles)).astype(np.int32)
    # OpenCL buffers
    memflag = cl.mem_flags
    position_buf = cl.Buffer(context,
                             memflag.READ_ONLY | memflag.COPY_HOST_PTR,
                             hostbuf = position)
    dest_buf = cl.Buffer(context,
                         memflag.WRITE_ONLY,
                         interactions.nbytes)
    # The actual invocation
    program = cl.Program(context, """
        int2 particle_pair (long i, int num_particles) {
            int line = i / num_particles;
            int pair_in_line = i % num_particles;
            int x = line; // superfluous
            int y = pair_in_line + line + 1;
            int2 pairing;
            if (y < num_particles) {
                pairing.s0 = x;
                pairing.s1 = y;
            }
            else {
                pairing.s0 = num_particles - 2 - x;
                pairing.s1 = (num_particles - 1) - (y - num_particles);
            }
            return pairing;
        }

        __kernel void simulate(int num_particles,
                               __global const float *position,
                               __global int *interactions) {
          int i = get_global_id(0);
          int2 pair = particle_pair(i, num_particles);          
          interactions[pair.s0] += 1;
          interactions[pair.s1] += 1;
        }
        """).build()
    instances = ((num_particles-1) * num_particles) / 2
    program.simulate(queue, instances, None,
                     np.int32(num_particles),
                     position_buf,
                     dest_buf)
    # Copying the results back into the host
    cl.enqueue_copy(queue, interactions, dest_buf)
    print(interactions)
    # FIXME: This is debugging code to test the pairwise combination algorithm.
    # Delete this once it's translated to OpenCL C.
    matrix = np.zeros((num_particles, num_particles))
    for i in range(0, instances):
        line = int(i / num_particles)
        pair_in_line = i % num_particles
        x = line
        y = pair_in_line + line + 1
        if y < num_particles:
            matrix[x, y] = 1
            #print(x,y)
        else:
            x = num_particles - 2 - x
            y = (num_particles - 1) - (y - num_particles)
            matrix[x, y] = 1
            #print(x,y)
    #print(matrix)

if __name__ == '__main__':
    simulate()

_______________________________________________
PyOpenCL mailing list
[email protected]
http://lists.tiker.net/listinfo/pyopencl

Reply via email to