-
-
Notifications
You must be signed in to change notification settings - Fork 425
/
Copy pathProgram.cs
330 lines (287 loc) · 12.4 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Silk.NET.OpenCL;
using System;
using System.IO;
namespace HelloWorld
{
internal class Program
{
const int ARRAY_SIZE = 1000;
static unsafe void Main(string[] args)
{
var cl = CL.GetApi();
nint context = 0;
nint commandQueue = 0;
nint program = 0;
nint kernel = 0;
nint device = 0;
nint[] memObjects = new nint[3];
// Create an OpenCL context on first available platform
context = CreateContext(cl);
if (context == IntPtr.Zero)
{
Console.WriteLine("Failed to create OpenCL context.");
return;
}
// Create a command-queue on the first device available
// on the created context
commandQueue = CreateCommandQueue(cl, context, ref device);
if (commandQueue == IntPtr.Zero)
{
Cleanup(cl, context, commandQueue, program, kernel, memObjects);
return;
}
// Create OpenCL program from HelloWorld.cl kernel source
program = CreateProgram(cl, context, device, "HelloWorld.cl");
if (program == IntPtr.Zero)
{
Cleanup(cl, context, commandQueue, program, kernel, memObjects);
return;
}
// Create OpenCL kernel
kernel = cl.CreateKernel(program, "hello_kernel", null);
if (kernel == IntPtr.Zero)
{
Console.WriteLine("Failed to create kernel");
Cleanup(cl, context, commandQueue, program, kernel, memObjects);
return;
}
// Create memory objects that will be used as arguments to
// kernel. First create host memory arrays that will be
// used to store the arguments to the kernel
float[] result = new float[ARRAY_SIZE];
float[] a = new float[ARRAY_SIZE];
float[] b = new float[ARRAY_SIZE];
for (int i = 0; i < ARRAY_SIZE; i++)
{
a[i] = (float) i;
b[i] = (float) (i * 2);
}
if (!CreateMemObjects(cl, context, memObjects, a, b))
{
Cleanup(cl, context, commandQueue, program, kernel, memObjects);
return;
}
// Set the kernel arguments (result, a, b)
int errNum = cl.SetKernelArg(kernel, 0, (nuint) sizeof(nint), memObjects[0]);
errNum |= cl.SetKernelArg(kernel, 1, (nuint) sizeof(nint), memObjects[1]);
errNum |= cl.SetKernelArg(kernel, 2, (nuint) sizeof(nint), memObjects[2]);
if (errNum != (int) ErrorCodes.Success)
{
Console.WriteLine("Error setting kernel arguments.");
Cleanup(cl, context, commandQueue, program, kernel, memObjects);
return;
}
nuint[] globalWorkSize = new nuint[1] { ARRAY_SIZE };
nuint[] localWorkSize = new nuint[1] { 1 };
// Queue the kernel up for execution across the array
errNum = cl.EnqueueNdrangeKernel(commandQueue, kernel, 1, (nuint*) null, globalWorkSize, localWorkSize, 0, (nint*) null, (nint*) null);
if (errNum != (int) ErrorCodes.Success)
{
Console.WriteLine("Error queuing kernel for execution.");
Cleanup(cl, context, commandQueue, program, kernel, memObjects);
return;
}
fixed (void* pValue = result)
{
// Read the output buffer back to the Host
errNum = cl.EnqueueReadBuffer(commandQueue, memObjects[2], true, 0, ARRAY_SIZE * sizeof(float), pValue, 0, null, null);
if (errNum != (int) ErrorCodes.Success)
{
Console.WriteLine("Error reading result buffer.");
Cleanup(cl, context, commandQueue, program, kernel, memObjects);
return;
}
}
// Output the result buffer
for (int i = 0; i < ARRAY_SIZE; i++)
{
Console.WriteLine(result[i]);
}
Console.WriteLine("Executed program succesfully.");
Cleanup(cl, context, commandQueue, program, kernel, memObjects);
}
/// <summary>
/// Create memory objects used as the arguments to the kernel
/// The kernel takes three arguments: result (output), a (input),
/// and b (input)
/// </summary>
/// <param name="context"></param>
/// <param name="memObjects"></param>
/// <param name=""></param>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
static unsafe bool CreateMemObjects(CL cl, nint context, nint[] memObjects, float[] a, float[] b)
{
fixed (void* pa = a)
{
memObjects[0] = cl.CreateBuffer(context, MemFlags.ReadOnly | MemFlags.CopyHostPtr, sizeof(float) * ARRAY_SIZE, pa, null);
}
fixed (void* pb = b)
{
memObjects[1] = cl.CreateBuffer(context, MemFlags.ReadOnly | MemFlags.CopyHostPtr, sizeof(float) * ARRAY_SIZE, pb, null);
}
memObjects[2] = cl.CreateBuffer(context, MemFlags.ReadWrite, sizeof(float) * ARRAY_SIZE, null, null);
if (memObjects[0] == IntPtr.Zero || memObjects[1] == IntPtr.Zero || memObjects[2] == IntPtr.Zero)
{
Console.WriteLine("Error creating memory objects.");
return false;
}
return true;
}
/// <summary>
/// Create an OpenCL program from the kernel source file
/// </summary>
/// <param name="cl"></param>
/// <param name="context"></param>
/// <param name="device"></param>
/// <param name="fileName"></param>
/// <returns></returns>
static unsafe nint CreateProgram(CL cl, nint context, nint device, string fileName)
{
if (!File.Exists(fileName))
{
Console.WriteLine($"File does not exist: {fileName}");
return IntPtr.Zero;
}
using StreamReader sr = new StreamReader(fileName);
string clStr = sr.ReadToEnd();
var program = cl.CreateProgramWithSource(context, 1, new string[] { clStr }, null, null);
if (program == IntPtr.Zero)
{
Console.WriteLine("Failed to create CL program from source.");
return IntPtr.Zero;
}
var errNum = cl.BuildProgram(program, 0, null, (byte*) null, null, null);
if (errNum != (int) ErrorCodes.Success)
{
_ = cl.GetProgramBuildInfo(program, device, ProgramBuildInfo.BuildLog, 0, null, out nuint buildLogSize);
byte[] log = new byte[buildLogSize / (nuint) sizeof(byte)];
fixed (void* pValue = log)
{
cl.GetProgramBuildInfo(program, device, ProgramBuildInfo.BuildLog, buildLogSize, pValue, null);
}
string? build_log = System.Text.Encoding.UTF8.GetString(log);
//Console.WriteLine("Error in kernel: ");
Console.WriteLine("=============== OpenCL Program Build Info ================");
Console.WriteLine(build_log);
Console.WriteLine("==========================================================");
cl.ReleaseProgram(program);
return IntPtr.Zero;
}
return program;
}
/// <summary>
/// Cleanup any created OpenCL resources
/// </summary>
/// <param name="cl"></param>
/// <param name="context"></param>
/// <param name="commandQueue"></param>
/// <param name="program"></param>
/// <param name="kernel"></param>
/// <param name="memObjects"></param>
static void Cleanup(CL cl, nint context, nint commandQueue,
nint program, nint kernel, nint[] memObjects)
{
for (int i = 0; i < memObjects.Length; i++)
{
if (memObjects[i] != 0)
cl.ReleaseMemObject(memObjects[i]);
}
if (commandQueue != 0)
cl.ReleaseCommandQueue(commandQueue);
if (kernel != 0)
cl.ReleaseKernel(kernel);
if (program != 0)
cl.ReleaseProgram(program);
if (context != 0)
cl.ReleaseContext(context);
}
/// <summary>
/// Create a command queue on the first device available on the
/// context
/// </summary>
/// <param name="cL"></param>
/// <param name="context"></param>
/// <param name="device"></param>
/// <returns></returns>
static unsafe nint CreateCommandQueue(CL cL, nint context, ref nint device)
{
int errNum = cL.GetContextInfo(context, ContextInfo.Devices, 0, null, out nuint deviceBufferSize);
if (errNum != (int) ErrorCodes.Success)
{
Console.WriteLine("Failed call to clGetContextInfo(...,GL_CONTEXT_DEVICES,...)");
return IntPtr.Zero;
}
if (deviceBufferSize <= 0)
{
Console.WriteLine("No devices available.");
return IntPtr.Zero;
}
nint[] devices = new nint[deviceBufferSize / (nuint) sizeof(nuint)];
fixed (void* pValue = devices)
{
int er = cL.GetContextInfo(context, ContextInfo.Devices, deviceBufferSize, pValue, null);
}
if (errNum != (int) ErrorCodes.Success)
{
devices = null;
Console.WriteLine("Failed to get device IDs");
return IntPtr.Zero;
}
// In this example, we just choose the first available device. In a
// real program, you would likely use all available devices or choose
// the highest performance device based on OpenCL device queries
var commandQueue = cL.CreateCommandQueue(context, devices[0], CommandQueueProperties.None, null);
if (commandQueue == IntPtr.Zero)
{
Console.WriteLine("Failed to create commandQueue for device 0");
return IntPtr.Zero;
}
device = devices[0];
return commandQueue;
}
/// <summary>
/// Create an OpenCL context on the first available platform using
/// either a GPU or CPU depending on what is available.
/// </summary>
/// <param name="cL"></param>
/// <returns></returns>
static unsafe nint CreateContext(CL cL)
{
var errNum = cL.GetPlatformIDs(1, out nint firstPlatformId, out uint numPlatforms);
if (errNum != (int) ErrorCodes.Success || numPlatforms <= 0)
{
Console.WriteLine("Failed to find any OpenCL platforms.");
return IntPtr.Zero;
}
// Next, create an OpenCL context on the platform. Attempt to
// create a GPU-based context, and if that fails, try to create
// a CPU-based context.
nint[] contextProperties = new nint[]
{
(nint)ContextProperties.Platform,
firstPlatformId,
0
};
fixed (nint* p = contextProperties)
{
var context = cL.CreateContextFromType(p, DeviceType.Gpu, null, null, out errNum);
if (errNum != (int) ErrorCodes.Success)
{
Console.WriteLine("Could not create GPU context, trying CPU...");
context = cL.CreateContextFromType(p, DeviceType.Cpu, null, null, out errNum);
if (errNum != (int) ErrorCodes.Success)
{
Console.WriteLine("Failed to create an OpenCL GPU or CPU context.");
return IntPtr.Zero;
}
return context;
}
return context;
}
}
}
}