问题
EDIT
I did some tests on the char array inputs to the kernel. I noticed a rather odd behaviour: consider the Kernel program and accompanying PyOpenCL code:
#!/usr/bin/env python3
import pyopencl as cl
import numpy as np
import seq
# Write down our kernel as a multiline string.
kernel = """
__kernel void dragon(
const int N,
__global char *AplusB,
__global char *AminusB,
__global char *plusMinus,
__global char *minusMinus,
__global char *output
)
{
int idx = get_global_id(0);
if (idx < N){
char b =AplusB[12];
printf("\\ %c \\n",b);
}
}
"""
#declare constants
number_of_expansions = 4
total_probelem_size =7
resulting_str_size=62
# Step 1: Create a context.
# This will ask the user to select the device to be used.
context = cl.create_some_context()
# Create a queue to the device.
queue = cl.CommandQueue(context)
# Create the program.
program = cl.Program(context, kernel).build()
# Create the input string
AplusB =np.array(('FX+YF++-FX-YF+'))
AminusB= np.array(('FX+YF+--FX-YF+'))
plusMinus= np.array(('+-'))
minusMinus = np.array(('--'))
# Send the data to the guest memory.
mf = cl.mem_flags
AplusBBuf = cl.Buffer(context, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=AplusB)
AminusBBuf= cl.Buffer(context, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=AminusB)
plusMinusBuf = cl.Buffer(context, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=plusMinus)
minusMinusBuf = cl.Buffer(context, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=minusMinus)
# Create the memory on the device to put the result into.
out_buf = cl.Buffer(context, mf.WRITE_ONLY, size=resulting_str_size)
copied_str = np.zeros(resulting_str_size)
# Initiate the kernel.
dragon = program.dragon
dragon.set_scalar_arg_dtypes([np.int32, None, None,None,None,None])
global_work_size = total_probelem_size
# Execute C = A * B.
dragon(queue, (global_work_size,), None,total_probelem_size,AplusBBuf, AminusBBuf,plusMinusBuf,minusMinusBuf,out_buf)
# Wait for the queue to be completely processed.
queue.finish()
# Read the array from the device.
cl.enqueue_copy(queue, copied_str, out_buf).wait()
print (copied_str)
"https://stackoverflow.com/questions/17603740/how-to-pass-a-list-of-strings-to-an-opencl-kernel-using-pyopencl"
Notice that in the kernel I try to print out characters inside the buffer AplusB
. It appears I can only print out characters at indices 0, 4, 8,and 12. AplusB
is of size 14. What could be the explanation of this behavior.
来源:https://stackoverflow.com/questions/51506971/string-operations-on-opencl