forked from lochotzke/OCL-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample1.cpp
More file actions
54 lines (42 loc) · 1.32 KB
/
Copy pathexample1.cpp
File metadata and controls
54 lines (42 loc) · 1.32 KB
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
#include <iostream>
#include "ocl.h"
using namespace std;
int main(){
// Find all devices
ocl_device device = ocl::displayDevices();
// Create a kernel using the device above from vectoradd.cl
ocl_kernel kernel(&device,"vectoradd.cl");
// N is the work-group size in this example
int N = device.getGroupSize(0);
// Create host variables
float* a = new float[N];
float* b = new float[N]();
float* c = new float[N];
// Setup the values of a
for(int i=0;i<N;i++)
a[i] = i;
// Allocate memory on the device
ocl_mem cl_a = device.malloc(N*sizeof(float),CL_MEM_READ_ONLY);
ocl_mem cl_b = device.malloc(N*sizeof(float),CL_MEM_READ_ONLY);
ocl_mem cl_c = device.malloc(N*sizeof(float),CL_MEM_WRITE_ONLY);
// Copy host variables a and b to the device variables cl_a and cl_b
cl_a.copyFrom(a);
cl_b.copyFrom(b);
// Set the arguments required for the kernel
kernel.setArgs(&N,cl_a.mem(),cl_b.mem(),cl_c.mem());
// Execute using N-sized work-groups with a total of N work-items
kernel.run(N,N);
// Wait until the kernel is done executing
device.finish();
// Copy device variable cl_c to host variable c
cl_c.copyTo(c);
// Output c
// Should be 0,1,...,N-1
for(int i=0;i<N;i++)
cout << c[i] << ',';
cout << endl;
// Free host variables
delete[] a;
delete[] b;
delete[] c;
}