14 import scala.math.BigInt
15 import scala.io.Source
35 val FLOAT_FIELD_SIZE: Int = 4
38 val INT_FIELD_SIZE: Int = 4
41 val LONG_FIELD_SIZE: Int = 8
48 private var assertsEnabled: Boolean =
false 53 case e: AssertionError => assertsEnabled =
true 62 def fileToString(filename: String): String =
63 "#line 1 \"" + filename +
"\"\n" + Source.fromFile(filename).mkString
70 def getDeviceId(platformI: Int, deviceI: Int): cl_device_id = {
75 val platformNbArray: Array[Int] = Array.ofDim[Int](1)
77 clGetPlatformIDs(0, null, platformNbArray)
79 val platformNb: Int = platformNbArray(0)
81 if (platformI >= platformNb) {
82 throw new Exception(
"Wrong platformI: " + platformI)
86 val platformIds: Array[cl_platform_id] = Array.ofDim[cl_platform_id](platformNb)
88 clGetPlatformIDs(platformIds.length, platformIds, null)
91 val platformId: cl_platform_id = platformIds(platformI)
94 val deviceNbArray: Array[Int] = Array.ofDim[Int](1)
96 clGetDeviceIDs(platformId, CL_DEVICE_TYPE_ALL, 0, null, deviceNbArray)
98 val deviceNb: Int = deviceNbArray(0)
100 if (deviceI >= deviceNb) {
101 throw new Exception(
"Wrong deviceI: " + deviceI)
105 val deviceIds: Array[cl_device_id] = Array.ofDim[cl_device_id](deviceNb)
107 clGetDeviceIDs(platformId, CL_DEVICE_TYPE_ALL, deviceNb, deviceIds, null)
117 def getDeviceInfoString(deviceId: cl_device_id, paramName: Int): String = {
119 val size: Array[Long] = Array.ofDim[Long](1)
121 clGetDeviceInfo(deviceId, paramName, 0, null, size)
124 val buffer: Array[Byte] = Array.ofDim[Byte](size(0).toInt)
126 clGetDeviceInfo(deviceId, paramName, buffer.length, Pointer.to(buffer), null)
128 new String(buffer, 0, buffer.length - 1)
139 def runExample(nbWorkGroup: Int, nbWorkItemsByWorkGroup: Int,
140 deviceId: cl_device_id, debug: Boolean) {
142 val hOuts: Array[Int] = Array.ofDim[Int](2)
143 val hOutsByteSize: Int = INT_FIELD_SIZE * 2
145 val hAsserts: Array[Long] = Array[Long](0, 0)
146 val hAssertsByteSize: Int = LONG_FIELD_SIZE * 2
148 val hAssertFloat: Array[Float] = Array[Float](0)
149 val hAssertFloatByteSize: Int = FLOAT_FIELD_SIZE
153 val devicesIds: Array[cl_device_id] = Array[cl_device_id](deviceId)
154 val context: cl_context = clCreateContext(null, 1, devicesIds, null, null, null)
158 var options: String =
"-I../../OpenCL/" 161 System.err.println(
"OpenCL in DEBUG mode!")
164 options +=
" -DNDEBUG" 167 val kernelFilename: String =
"../kernel/example.cl" 168 val kernelSrc: String = fileToString(kernelFilename)
169 val program: cl_program = clCreateProgramWithSource(context,
170 1, Array[String](kernelSrc),
173 clBuildProgram(program, 1, devicesIds, options, null, null)
175 val kernel: cl_kernel = clCreateKernel(program,
"example", null)
179 val queue:cl_command_queue = clCreateCommandQueue(context, deviceId,
180 CL_QUEUE_PROFILING_ENABLE, null)
184 val dOuts: cl_mem = clCreateBuffer(context, CL_MEM_WRITE_ONLY | CL_MEM_COPY_HOST_PTR,
185 hOutsByteSize, Pointer.to(hOuts), null)
187 var dAsserts: cl_mem = null
188 var dAssertFloat: cl_mem = null
191 dAsserts = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
192 hAssertsByteSize, Pointer.to(hAsserts), null)
193 dAssertFloat = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
194 hAssertFloatByteSize, Pointer.to(hAssertFloat), null)
199 clSetKernelArg(kernel, 0, Sizeof.cl_uint, Pointer.to(Array[Int](666)))
200 clSetKernelArg(kernel, 1, Sizeof.cl_mem, Pointer.to(dOuts))
205 clSetKernelArg(kernel, nbArgs, Sizeof.cl_mem, Pointer.to(dAsserts))
206 clSetKernelArg(kernel, nbArgs + 1, Sizeof.cl_mem, Pointer.to(dAssertFloat))
211 val globalSize: Int = nbWorkItemsByWorkGroup * nbWorkGroup
213 println(
"===== run kernel =====")
216 clEnqueueNDRangeKernel(queue, kernel,
219 Array[Long](globalSize),
220 Array[Long](nbWorkItemsByWorkGroup),
229 println("===== end kernel =====")
234 clEnqueueReadBuffer(queue, dOuts, CL_TRUE, 0,
235 hOutsByteSize, Pointer.to(hOuts), 0, null, null)
238 clEnqueueReadBuffer(queue, dAsserts, CL_TRUE, 0,
239 hAssertsByteSize, Pointer.to(hAsserts), 0, null, null)
240 clEnqueueReadBuffer(queue, dAssertFloat, CL_TRUE, 0,
241 hAssertFloatByteSize, Pointer.to(hAssertFloat), 0, null, null)
243 val line: Long = hAsserts(0)
246 val uint64Value: BigInt = uint64(hAsserts(1))
247 val sint64Value: Long = hAsserts(1)
248 val floatValue: Float = hAssertFloat(0)
250 System.err.println(
s"$kernelFilename:$line\tAssertion failed | Maybe\t$uint64Value\t$sint64Value | Maybe\t$floatValue")
259 println(
s"Results: (${uint32(hOuts(0))}, ${uint32(hOuts(1))})
") 262 // Free OpenCL resources 263 clReleaseMemObject(dOuts) 265 clReleaseMemObject(dAsserts) 266 clReleaseMemObject(dAssertFloat) 269 clReleaseCommandQueue(queue) 270 clReleaseProgram(program) 271 clReleaseKernel(kernel) 272 clReleaseContext(context) 279 def uint32(n: Int): Long =
281 else (4294967296L + n) // 2^32 + n
287 def uint64(n: Long): BigInt =
288 if (n >= 0) BigInt(n)
289 else BigInt("18446744073709551616
") + n // 2^64 + n 297 def main(args: Array[String]): Unit = {
298 var debug: Boolean = assertsEnabled
300 var platformI: Int = 0
306 while (i < args.length) {
307 val arg: String = args(i)
309 if ("--debug
".equals(arg) || "--ndebug
".equals(arg)) { 310 debug = "--debug
".equals(arg) 312 else if ("--device
".equals(arg)) { 315 if (i >= args.length) { 316 throw new Exception("Missing parameter
") 319 val bothI: Array[String] = args(i).split(":
") 321 platformI = Integer.parseInt(bothI(0)) 322 if (bothI.length >= 2) { 323 deviceI = Integer.parseInt(bothI(1)) 326 if ((platformI < 0) || (deviceI < 0)) { 327 throw new Exception("Wrong parameter:
" + args(i)) 335 val deviceId: cl_device_id = getDeviceId(platformI, deviceI) 337 // Get device name and print 338 println(s"Device $platformI:$deviceI ${getDeviceInfoString(deviceId, CL_DEVICE_NAME)}
") 342 runExample(3, 4, deviceId, debug) #define assert(test)
If test is true then do nothing. Else init (if they are still null) assert_result and assert_result_f...
Simple Java example to show how run a kernel with assert*() and PRINT*() macros and test them...