Skip to content

Commit 365b43d

Browse files
committed
Use x64 numbers
1 parent 28e7aac commit 365b43d

File tree

17 files changed

+55
-96
lines changed

17 files changed

+55
-96
lines changed

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,8 @@ See [TypeScript declarations](/index.d.ts) for more details.
3737
```
3838
2. Fetch the CL control objects:
3939
```ts
40-
const platform = cl.getPlatformIDs()[0];
41-
const devices = cl.getDeviceIDs(platform, cl.DEVICE_TYPE_ALL);
42-
const context = cl.createContext([cl.CONTEXT_PLATFORM, platform], devices);
43-
const device = cl.getContextInfo(context, cl.CONTEXT_DEVICES)[0];
44-
const queue = cl.createCommandQueue(context, device, null);
40+
const { context, device } = cl.quickStart(); // see /index.js
41+
const queue = cl.createCommandQueue(context, device);
4542
```
4643
3. Prepare the data input/output buffers:
4744
```ts
@@ -67,7 +64,8 @@ See [TypeScript declarations](/index.d.ts) for more details.
6764
4. Create a valid CL program, e.g. from source:
6865
```ts
6966
const program = cl.createProgramWithSource(context, `
70-
__kernel void vadd(__global int *a, __global int *b, __global int *c, uint num) {
67+
__kernel
68+
void vadd(__global int *a, __global int *b, __global int *c, uint num) {
7169
size_t i = get_global_id(0);
7270
if (i < num) {
7371
c[i] = a[i] + b[i];
@@ -92,7 +90,7 @@ See [TypeScript declarations](/index.d.ts) for more details.
9290
// Do the work
9391
cl.enqueueWriteBuffer(queue, bufferA, true, 0, BYTE_SIZE, arrayA);
9492
cl.enqueueWriteBuffer(queue, bufferB, true, 0, BYTE_SIZE, arrayB);
95-
cl.enqueueNDRangeKernel(queue, kernel, 1, null, [BUFFER_SIZE], null);
93+
cl.enqueueNDRangeKernel(queue, kernel, 1, null, [BUFFER_SIZE]);
9694
cl.enqueueReadBuffer(queue, bufferC, true, 0, BYTE_SIZE, arrayC);
9795
```
9896
7. See if it worked:

examples/saxpy.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import cl from '../index.js';
22

33
const getEventExecTime = (event: cl.TClEvent): number => {
4-
// times are 64-bit values in nanoseconds. They are returned as [hi, lo] a 2-integer array
5-
// here we use the lo parts since this example is unlikely to go beyond 2^31 nanseconds per event.
64
const startTime = cl.getEventProfilingInfo(event, cl.PROFILING_COMMAND_START);
75
const endTime = cl.getEventProfilingInfo(event, cl.PROFILING_COMMAND_END);
8-
9-
return (endTime[1] - startTime[1]) * 1e-6; // report in millisecond (from nanoseconds)
6+
return (endTime - startTime) * 1e-6; // nanoseconds to milliseconds
107
};
118

129
const VECTOR_SIZE = 512 * 1024;

examples/simple.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ cl.setKernelArg(kernel, 3, 'uint', BUFFER_SIZE);
4646
// Do the work
4747
cl.enqueueWriteBuffer(queue, bufferA, true, 0, BYTE_SIZE, arrayA);
4848
cl.enqueueWriteBuffer(queue, bufferB, true, 0, BYTE_SIZE, arrayB);
49-
cl.enqueueNDRangeKernel(queue, kernel, 1, null, [BUFFER_SIZE], null);
49+
cl.enqueueNDRangeKernel(queue, kernel, 1, null, [BUFFER_SIZE]);
5050
cl.enqueueReadBuffer(queue, bufferC, true, 0, BYTE_SIZE, arrayC);
5151

5252
// print results

examples/square-events.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ cl.setKernelArg(kern, 0, 'uint*', inputsMem);
2727
cl.setKernelArg(kern, 1, 'uint*', outputsMem);
2828
cl.setKernelArg(kern, 2, 'uint', NVALUES);
2929

30-
const cq = cl.createCommandQueue(context, device, null);
30+
const cq = cl.createCommandQueue(context, device);
3131

32-
cl.enqueueNDRangeKernel(cq, kern, 1, null, [NVALUES], null);
32+
cl.enqueueNDRangeKernel(cq, kern, 1, null, [NVALUES]);
3333

3434
// here we use the returned user event to associate a callback that will be called from OpenCL
3535
// once read buffer is complete.

examples/square.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ cl.setKernelArg(kern2, 0, 'uint*', inputsMem);
3636
cl.setKernelArg(kern2, 1, 'uint*', outputsMem);
3737
cl.setKernelArg(kern2, 2, 'uint', NVALUES);
3838

39-
cl.enqueueNDRangeKernel(cq, kern2, 1, null, [NVALUES], null);
39+
cl.enqueueNDRangeKernel(cq, kern2, 1, null, [NVALUES]);
4040
cl.enqueueReadBuffer(cq, outputsMem, true, 0, NVALUES * BYTES_PER_ELEMENT, outputs);
4141
cl.finish(cq);
4242

examples/vector-add.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ cl.enqueueWriteBuffer (queue, aBuffer, true, 0, A.length * Uint32Array.BYTES_PER
5858
cl.enqueueWriteBuffer (queue, bBuffer, true, 0, B.length * Uint32Array.BYTES_PER_ELEMENT, B);
5959

6060
// Execute (enqueue) kernel
61-
cl.enqueueNDRangeKernel(
62-
queue, kernel, 1, null, [BUFFER_SIZE], null,
63-
);
61+
cl.enqueueNDRangeKernel(queue, kernel, 1, null, [BUFFER_SIZE]);
6462

6563
// get results and block while getting them
6664
const C = new Uint32Array(BUFFER_SIZE);

index.d.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ declare namespace cl {
6868
const INVALID_COMPILER_OPTIONS: Error;
6969
const INVALID_LINKER_OPTIONS: Error;
7070
const INVALID_DEVICE_PARTITION_COUNT: Error;
71+
// Additional Error Codes
72+
const PLATFORM_NOT_FOUND_KHR: Error;
73+
const INVALID_GL_SHAREGROUP_REFERENCE_KHR: Error;
7174

7275
// OpenCL Version
7376
const VERSION_1_0: number;
@@ -457,13 +460,13 @@ declare namespace cl {
457460
const WGL_HDC_KHR: number;
458461
const GLX_DISPLAY_KHR: number;
459462
const CGL_SHAREGROUP_KHR: number;
463+
const CURRENT_DEVICE_FOR_GL_CONTEXT_KHR: number;
464+
const DEVICES_FOR_GL_CONTEXT_KHR: number;
460465

461466
// ---- Not available on Apple:
462467
// cl_khr_icd extension:
463468
// cl_platform_info
464469
const PLATFORM_ICD_SUFFIX_KHR: number;
465-
// Additional Error Codes
466-
const PLATFORM_NOT_FOUND_KHR: number;
467470

468471
// cl_nv_device_attribute_query extension:
469472
// no extension exports since it has no functions
@@ -567,7 +570,7 @@ declare namespace cl {
567570
kernel: TClKernel,
568571
device: TClDevice,
569572
param_name: number,
570-
) => TClStatus;
573+
) => (number | number[]);
571574

572575

573576
/**
@@ -1068,9 +1071,9 @@ declare namespace cl {
10681071
queue: TClQueue,
10691072
kernel: TClKernel,
10701073
work_dim: number,
1071-
work_offset: number[] | null,
1072-
work_global: number[] | null,
1073-
work_local: number[] | null,
1074+
work_offset?: number[] | null,
1075+
work_global?: number[] | null,
1076+
work_local?: number[] | null,
10741077
event_wait_list?: TClEvent[] | null,
10751078
hasEvent?: boolean,
10761079
) => TClEventOrVoid;
@@ -1300,8 +1303,14 @@ declare namespace cl {
13001303
const getEventProfilingInfo: (
13011304
event: TClEvent,
13021305
param_name: number,
1303-
) => [number, number];
1306+
) => number;
13041307

1308+
/**
1309+
* A small helper to quickly grab a good CL context.
1310+
*
1311+
* Multiple calls to this function will return the same `context`.
1312+
* Pass `true` to see the intermediate device list (from where we choose).
1313+
*/
13051314
const quickStart: (isLoggingDevices?: boolean) => Readonly<{
13061315
platform: TClPlatform,
13071316
device: TClDevice,

src/cpp/bindings.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ Napi::Object initModule(Napi::Env env, Napi::Object exports) {
206206
JS_CL_ERROR(INVALID_DEVICE_PARTITION_COUNT);
207207
// Additional Error Codes
208208
JS_CL_ERROR(PLATFORM_NOT_FOUND_KHR);
209+
JS_CL_ERROR(INVALID_GL_SHAREGROUP_REFERENCE_KHR);
209210

210211
JS_CL_CONSTANT(SUCCESS);
211212

@@ -597,6 +598,8 @@ Napi::Object initModule(Napi::Env env, Napi::Object exports) {
597598
JS_CL_CONSTANT(WGL_HDC_KHR);
598599
JS_CL_CONSTANT(GLX_DISPLAY_KHR);
599600
JS_CL_CONSTANT(CGL_SHAREGROUP_KHR);
601+
JS_CL_CONSTANT(CURRENT_DEVICE_FOR_GL_CONTEXT_KHR);
602+
JS_CL_CONSTANT(DEVICES_FOR_GL_CONTEXT_KHR);
600603

601604
// cl_platform_info
602605
JS_CL_CONSTANT(PLATFORM_ICD_SUFFIX_KHR);

src/cpp/common.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ const char* getExceptionMessage(const cl_int code) {
140140
return "Invalid device partition count";
141141
case CL_PLATFORM_NOT_FOUND_KHR:
142142
return "Platform not found (ICD)";
143+
case CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR:
144+
return "Invalid GL sharegroup reference";
143145
default:
144146
printf("OpenCL Unknown error: %d\n", code);
145147
return "Unknown error";

src/cpp/common.hpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@
1717
#define strncasecmp _strnicmp
1818
#endif
1919

20-
#ifndef CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR
21-
#define CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR 0x2006
22-
#endif
23-
#ifndef CL_DEVICES_FOR_GL_CONTEXT_KHR
24-
#define CL_DEVICES_FOR_GL_CONTEXT_KHR 0x2007
25-
#endif
26-
2720
#define CHECK_ERR(code) { \
2821
cl_int _err = (code); \
2922
if (_err != CL_SUCCESS) { \
@@ -41,6 +34,15 @@ namespace opencl {
4134
void getPtrAndLen(Napi::Object obj, void** ptr, size_t *len);
4235
const char* getExceptionMessage(cl_int code);
4336

37+
inline Napi::Number NewInt64(napi_env env, int64_t val) {
38+
napi_value value;
39+
napi_status status = napi_create_int64(env, val, &value);
40+
NAPI_THROW_IF_FAILED(env, status, Napi::Number());
41+
return Napi::Number(env, value);
42+
}
43+
44+
#define RET_X64(VAL) return NewInt64(env, static_cast<int64_t>(VAL))
45+
4446
JS_METHOD(createKernel);
4547
JS_METHOD(createKernelsInProgram);
4648
JS_METHOD(retainKernel);

0 commit comments

Comments
 (0)