@@ -50,6 +50,29 @@ import scala.language.higherKinds
50
50
*/
51
51
object OpenCL {
52
52
53
+ final case class PlatformId [Owner <: Singleton with OpenCL ](handle : Long ) extends AnyVal {
54
+
55
+ final def deviceIdsByType (deviceType : Int ): Seq [DeviceId [Owner ]] = {
56
+ val Array (numberOfDevices) = {
57
+ val a = Array (0 )
58
+ checkErrorCode(clGetDeviceIDs(handle, deviceType, null , a))
59
+ a
60
+ }
61
+ val stack = stackPush()
62
+ try {
63
+ val deviceIdBuffer = stack.mallocPointer(numberOfDevices)
64
+ checkErrorCode(clGetDeviceIDs(handle, deviceType, deviceIdBuffer, null : IntBuffer ))
65
+ for (i <- 0 until numberOfDevices) yield {
66
+ val deviceId = deviceIdBuffer.get(i)
67
+ new DeviceId [Owner ](deviceId)
68
+ }
69
+ } finally {
70
+ stack.close()
71
+ }
72
+ }
73
+
74
+ }
75
+
53
76
/** Returns a [[String ]] for the C string `address`.
54
77
*
55
78
* @note We don't know the exact charset of the C string. Use [[memASCII ]] because lwjgl treats them as ASCII.
@@ -265,25 +288,18 @@ object OpenCL {
265
288
}
266
289
}
267
290
268
- trait UseFirstPlatform {
291
+ trait UseFirstPlatform extends OpenCL {
269
292
@ transient
270
- protected lazy val platformId : Long = {
271
- val stack = stackPush()
272
- try {
273
- val platformIdBuffer = stack.mallocPointer(1 )
274
- checkErrorCode(clGetPlatformIDs(platformIdBuffer, null : IntBuffer ))
275
- platformIdBuffer.get(0 )
276
- } finally {
277
- stack.close()
278
- }
293
+ protected lazy val platformId : PlatformId = {
294
+ platformIds.head
279
295
}
280
296
}
281
297
282
298
trait UseAllDevices extends OpenCL {
283
299
284
300
@ transient
285
301
protected lazy val deviceIds : Seq [DeviceId ] = {
286
- deviceIdsByType(CL_DEVICE_TYPE_ALL )
302
+ platformId. deviceIdsByType(CL_DEVICE_TYPE_ALL )
287
303
}
288
304
289
305
}
@@ -292,7 +308,7 @@ object OpenCL {
292
308
293
309
@ transient
294
310
protected lazy val deviceIds : Seq [DeviceId ] = {
295
- val allDeviceIds = deviceIdsByType(CL_DEVICE_TYPE_ALL )
311
+ val allDeviceIds = platformId. deviceIdsByType(CL_DEVICE_TYPE_ALL )
296
312
Seq (allDeviceIds.head)
297
313
}
298
314
@@ -302,23 +318,23 @@ object OpenCL {
302
318
303
319
@ transient
304
320
protected lazy val deviceIds : Seq [DeviceId ] = {
305
- deviceIdsByType(CL_DEVICE_TYPE_GPU )
321
+ platformId. deviceIdsByType(CL_DEVICE_TYPE_GPU )
306
322
}
307
323
}
308
324
309
325
trait UseFirstGpuDevice extends OpenCL {
310
326
311
327
@ transient
312
328
protected lazy val deviceIds : Seq [DeviceId ] = {
313
- val allDeviceIds = deviceIdsByType(CL_DEVICE_TYPE_GPU )
329
+ val allDeviceIds = platformId. deviceIdsByType(CL_DEVICE_TYPE_GPU )
314
330
Seq (allDeviceIds.head)
315
331
}
316
332
}
317
333
trait UseFirstCpuDevice extends OpenCL {
318
334
319
335
@ transient
320
336
protected lazy val deviceIds : Seq [DeviceId ] = {
321
- val allDeviceIds = deviceIdsByType(CL_DEVICE_TYPE_CPU )
337
+ val allDeviceIds = platformId. deviceIdsByType(CL_DEVICE_TYPE_CPU )
322
338
Seq (allDeviceIds.head)
323
339
}
324
340
}
@@ -327,7 +343,7 @@ object OpenCL {
327
343
328
344
@ transient
329
345
protected lazy val deviceIds : Seq [DeviceId ] = {
330
- deviceIdsByType(CL_DEVICE_TYPE_CPU )
346
+ platformId. deviceIdsByType(CL_DEVICE_TYPE_CPU )
331
347
}
332
348
}
333
349
@@ -1010,20 +1026,18 @@ trait OpenCL extends MonadicCloseable[UnitContinuation] with ImplicitsSingleton
1010
1026
type Event = OpenCL .Event [this .type ]
1011
1027
type CommandQueue = OpenCL .CommandQueue [this .type ]
1012
1028
type DeviceId = OpenCL .DeviceId [this .type ]
1029
+ type PlatformId = OpenCL .PlatformId [this .type ]
1013
1030
1014
- protected final def deviceIdsByType (deviceType : Int ): Seq [DeviceId ] = {
1015
- val Array (numberOfDevices) = {
1016
- val a = Array (0 )
1017
- checkErrorCode(clGetDeviceIDs(platformId, deviceType, null , a))
1018
- a
1019
- }
1031
+ def platformIds : Seq [PlatformId ] = {
1020
1032
val stack = stackPush()
1021
1033
try {
1022
- val deviceIdBuffer = stack.mallocPointer(numberOfDevices)
1023
- checkErrorCode(clGetDeviceIDs(platformId, deviceType, deviceIdBuffer, null : IntBuffer ))
1024
- for (i <- 0 until deviceIdBuffer.capacity()) yield {
1025
- val deviceId = deviceIdBuffer.get(i)
1026
- new DeviceId (deviceId)
1034
+ val numberOfPlatformsBuffer = stack.mallocInt(1 )
1035
+ checkErrorCode(clGetPlatformIDs(null , numberOfPlatformsBuffer))
1036
+ val numberOfPlatforms = numberOfPlatformsBuffer.get(0 )
1037
+ val platformIdBuffer = stack.mallocPointer(numberOfPlatforms)
1038
+ checkErrorCode(clGetPlatformIDs(platformIdBuffer, null : IntBuffer ))
1039
+ (0 until numberOfPlatforms).map { i =>
1040
+ new PlatformId (platformIdBuffer.get(i))
1027
1041
}
1028
1042
} finally {
1029
1043
stack.close()
@@ -1174,20 +1188,20 @@ trait OpenCL extends MonadicCloseable[UnitContinuation] with ImplicitsSingleton
1174
1188
1175
1189
import OpenCL ._
1176
1190
1177
- protected val platformId : Long
1191
+ protected val platformId : PlatformId
1178
1192
protected val deviceIds : Seq [DeviceId ]
1179
1193
1180
1194
@ transient
1181
1195
protected lazy val platformCapabilities : CLCapabilities = {
1182
- CL .createPlatformCapabilities(platformId)
1196
+ CL .createPlatformCapabilities(platformId.handle )
1183
1197
}
1184
1198
1185
1199
protected def createCommandQueue (deviceId : DeviceId , properties : Map [Int , Long ]): CommandQueue = new CommandQueue (
1186
1200
if (deviceCapabilities(deviceId).OpenCL20 ) {
1187
1201
val cl20Properties = (properties.view.flatMap { case (key, value) => Seq (key, value) } ++ Seq (0L )).toArray
1188
1202
val a = Array (0 )
1189
1203
val commandQueue =
1190
- clCreateCommandQueueWithProperties(platformId, deviceId.handle, cl20Properties, a)
1204
+ clCreateCommandQueueWithProperties(platformId.handle , deviceId.handle, cl20Properties, a)
1191
1205
checkErrorCode(a(0 ))
1192
1206
commandQueue
1193
1207
} else {
@@ -1211,7 +1225,7 @@ trait OpenCL extends MonadicCloseable[UnitContinuation] with ImplicitsSingleton
1211
1225
val stack = stackPush()
1212
1226
try {
1213
1227
val errorCodeBuffer = stack.ints(CL_SUCCESS )
1214
- val contextProperties = stack.pointers(CL_CONTEXT_PLATFORM , platformId, 0 )
1228
+ val contextProperties = stack.pointers(CL_CONTEXT_PLATFORM , platformId.handle , 0 )
1215
1229
val deviceIdBuffer = stack.pointers(deviceIds.view.map(_.handle): _* )
1216
1230
val context =
1217
1231
clCreateContext(contextProperties,
0 commit comments