Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

162
LINES

< > BotCompany Repo | #1006668 // JOCLSample.java (GPU calculation, waiting to be ported!)

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 101K of libraries. Click here for Pure Java version (4886L/31K).

1  
!7
2  
3  
// OpenCL Java binding by Marco Hutter of jocl.org
4  
// patched by Stefan to allow dynamic library loading
5  
lib 1006673
6  
7  
import org.jocl.*;
8  
import static org.jocl.CL.*;
9  
10  
/**
11  
 * A small JOCL sample.
12  
 */
13  
 
14  
  /**
15  
   * The source code of the OpenCL program to execute
16  
   */
17  
  static S programSource =
18  
      "__kernel void "+
19  
      "sampleKernel(__global const float *a,"+
20  
      "             __global const float *b,"+
21  
      "             __global float *c)"+
22  
      "{"+
23  
      "    int gid = get_global_id(0);"+
24  
      "    c[gid] = a[gid] * b[gid];"+
25  
      "}";
26  
  
27  
  // MAIN PROGRAM
28  
  p {
29  
    // Linux
30  
    // loadNative(1006670); // JOCL Native Parts
31  
    File libFile = prepareProgramFile("opencl.so");
32  
    copyFile(loadLibrary(#1006670), libFile);
33  
    System.load(libFile.getPath());
34  
    call(CL.class, "initNativeLibrary", libFile.getName());
35  
    
36  
    // Create input- and output data 
37  
    int n = 10;
38  
    float srcArrayA[] = new float[n];
39  
    float srcArrayB[] = new float[n];
40  
    float dstArray[] = new float[n];
41  
    for (int i=0; i<n; i++)
42  
    {
43  
        srcArrayA[i] = i;
44  
        srcArrayB[i] = i;
45  
    }
46  
    Pointer srcA = Pointer.to(srcArrayA);
47  
    Pointer srcB = Pointer.to(srcArrayB);
48  
    Pointer dst = Pointer.to(dstArray);
49  
50  
    // The platform, device type and device number
51  
    // that will be used
52  
    final int platformIndex = 0;
53  
    final long deviceType = CL_DEVICE_TYPE_ALL;
54  
    final int deviceIndex = 0;
55  
56  
    // Enable exceptions and subsequently omit error checks in this sample
57  
    CL.setExceptionsEnabled(true);
58  
59  
    // Obtain the number of platforms
60  
    int numPlatformsArray[] = new int[1];
61  
    clGetPlatformIDs(0, null, numPlatformsArray);
62  
    int numPlatforms = numPlatformsArray[0];
63  
64  
    // Obtain a platform ID
65  
    cl_platform_id platforms[] = new cl_platform_id[numPlatforms];
66  
    clGetPlatformIDs(platforms.length, platforms, null);
67  
    cl_platform_id platform = platforms[platformIndex];
68  
69  
    // Initialize the context properties
70  
    cl_context_properties contextProperties = new cl_context_properties();
71  
    contextProperties.addProperty(CL_CONTEXT_PLATFORM, platform);
72  
    
73  
    // Obtain the number of devices for the platform
74  
    int numDevicesArray[] = new int[1];
75  
    clGetDeviceIDs(platform, deviceType, 0, null, numDevicesArray);
76  
    int numDevices = numDevicesArray[0];
77  
    
78  
    // Obtain a device ID 
79  
    cl_device_id devices[] = new cl_device_id[numDevices];
80  
    clGetDeviceIDs(platform, deviceType, numDevices, devices, null);
81  
    cl_device_id device = devices[deviceIndex];
82  
83  
    // Create a context for the selected device
84  
    cl_context context = clCreateContext(
85  
        contextProperties, 1, new cl_device_id[]{device}, 
86  
        null, null, null);
87  
    
88  
    // Create a command-queue for the selected device
89  
    cl_command_queue commandQueue = 
90  
        clCreateCommandQueue(context, device, 0, null);
91  
92  
    // Allocate the memory objects for the input- and output data
93  
    cl_mem memObjects[] = new cl_mem[3];
94  
    memObjects[0] = clCreateBuffer(context, 
95  
        CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
96  
        Sizeof.cl_float * n, srcA, null);
97  
    memObjects[1] = clCreateBuffer(context, 
98  
        CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
99  
        Sizeof.cl_float * n, srcB, null);
100  
    memObjects[2] = clCreateBuffer(context, 
101  
        CL_MEM_READ_WRITE, 
102  
        Sizeof.cl_float * n, null, null);
103  
    
104  
    // Create the program from the source code
105  
    cl_program program = clCreateProgramWithSource(context,
106  
        1, new String[]{ programSource }, null, null);
107  
    
108  
    // Build the program
109  
    clBuildProgram(program, 0, null, null, null, null);
110  
    
111  
    // Create the kernel
112  
    cl_kernel kernel = clCreateKernel(program, "sampleKernel", null);
113  
    
114  
    // Set the arguments for the kernel
115  
    clSetKernelArg(kernel, 0, 
116  
        Sizeof.cl_mem, Pointer.to(memObjects[0]));
117  
    clSetKernelArg(kernel, 1, 
118  
        Sizeof.cl_mem, Pointer.to(memObjects[1]));
119  
    clSetKernelArg(kernel, 2, 
120  
        Sizeof.cl_mem, Pointer.to(memObjects[2]));
121  
    
122  
    // Set the work-item dimensions
123  
    long global_work_size[] = new long[]{n};
124  
    long local_work_size[] = new long[]{1};
125  
    
126  
    // Execute the kernel
127  
    clEnqueueNDRangeKernel(commandQueue, kernel, 1, null,
128  
        global_work_size, local_work_size, 0, null, null);
129  
    
130  
    // Read the output data
131  
    clEnqueueReadBuffer(commandQueue, memObjects[2], CL_TRUE, 0,
132  
        n * Sizeof.cl_float, dst, 0, null, null);
133  
    
134  
    // Release kernel, program, and memory objects
135  
    clReleaseMemObject(memObjects[0]);
136  
    clReleaseMemObject(memObjects[1]);
137  
    clReleaseMemObject(memObjects[2]);
138  
    clReleaseKernel(kernel);
139  
    clReleaseProgram(program);
140  
    clReleaseCommandQueue(commandQueue);
141  
    clReleaseContext(context);
142  
    
143  
    // Verify the result
144  
    boolean passed = true;
145  
    final float epsilon = 1e-7f;
146  
    for (int i=0; i<n; i++)
147  
    {
148  
        float x = dstArray[i];
149  
        float y = srcArrayA[i] * srcArrayB[i];
150  
        boolean epsilonEqual = Math.abs(x - y) <= epsilon * Math.abs(x);
151  
        if (!epsilonEqual)
152  
        {
153  
            passed = false;
154  
            break;
155  
        }
156  
    }
157  
    print("Test "+(passed?"PASSED":"FAILED"));
158  
    if (n <= 10)
159  
    {
160  
        print("Result: "+java.util.Arrays.toString(dstArray));
161  
    }
162  
  }

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1006668
Snippet name: JOCLSample.java (GPU calculation, waiting to be ported!)
Eternal ID of this version: #1006668/2
Text MD5: 07e4fc19bf305c052156d30791c75ff2
Transpilation MD5: 4ecead5a8b1ea2399d51ededbc9bd24a
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-01-05 05:57:43
Source code size: 5390 bytes / 162 lines
Pitched / IR pitched: No / No
Views / Downloads: 486 / 982
Version history: 1 change(s)
Referenced in: [show references]