2010-10-18 37 views
4

我想获得一个简单的Cloo程序来运行,但它不工作,谁能告诉我为什么?Cloo OpenCL c#问题

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using Cloo; 
using System.Runtime.InteropServices; 

namespace HelloWorld 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 


ComputeContextPropertyList cpl = new ComputeContextPropertyList(ComputePlatform.Platforms[0]); 
ComputeContext context = new ComputeContext(ComputeDeviceTypes.Gpu, cpl, null, IntPtr.Zero); 

string kernelSource = @" 
kernel void VectorAdd(
    global read_only float* a, 
    global read_only float* b, 
    global write_only float* c) 
{ 
    int index = get_global_id(0); 
    c[index] = a[index] + b[index]; 
} 
"; 
long count = 20; 
float[] arrA = new float[count]; 
float[] arrB = new float[count]; 
float[] arrC = new float[count]; 

Random rand = new Random(); 

for (int i = 0; i < count; i++) 
{ 
arrA[i] = (float)(rand.NextDouble() * 100); 
arrB[i] = (float)(rand.NextDouble() * 100); 
} 

ComputeBuffer<float> a = new ComputeBuffer<float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, arrA); 
ComputeBuffer<float> b = new ComputeBuffer<float>(context, ComputeMemoryFlags.ReadOnly | ComputeMemoryFlags.CopyHostPointer, arrB); 
ComputeBuffer<float> c = new ComputeBuffer<float>(context, ComputeMemoryFlags.WriteOnly, arrC.Length); 

ComputeProgram program = new ComputeProgram(context, new string[] { kernelSource }); 
program.Build(null, null, null, IntPtr.Zero); 

ComputeKernel kernel = program.CreateKernel("VectorAdd"); 
kernel.SetMemoryArgument(0, a); 
kernel.SetMemoryArgument(1, b); 
kernel.SetMemoryArgument(2, c); 

ComputeCommandQueue commands = new ComputeCommandQueue(context, context.Devices[0], ComputeCommandQueueFlags.None); 

ComputeEventList events = new ComputeEventList(); 

commands.Execute(kernel, null, new long[] { count }, null, events); 

arrC = new float[count]; 
GCHandle arrCHandle = GCHandle.Alloc(arrC, GCHandleType.Pinned); 

commands.Read(c, false, 0, count, arrCHandle.AddrOfPinnedObject(), events); 
commands.Finish(); 

arrCHandle.Free(); 

for (int i = 0; i < count; i++) 
richTextBox1.Text += "{" + arrA[i] + "} + {" + arrB[i] + "} = {" + arrC[i] + "} \n"; 

} 
     } 
    } 

这是程序给我

类型的未处理的异常 'Cloo.InvalidBinaryComputeException' 发生在Cloo.dll

附加信息的错误:检测到错误的OpenCL代码 :InvalidBinary。

+0

你看过程序buildlog吗?它是否说出任何有趣的事情?并从哪里抛出异常(我假设的命令.Execute,但只是为了确定)? – Grizzly 2010-10-21 12:19:51

+0

最后,你解决了这个问题吗? – sunside 2015-06-26 12:50:40

回答

2

更改内核名称 - 我认为名称VectorAdd与某些东西冲突。