source: CIVL/examples/translation/opencl/square.cvl@ cd8d07a

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since cd8d07a was 4b7d11e, checked in by Jacob Trieu <fuufusuu@…>, 12 years ago

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1120 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 2.8 KB
Line 
1#include <civlc.h>
2#include <stdio.h>
3#include "setup/openCL.cvl"
4
5//this is the kernel that gets loaded into the program
6// Simple compute kernel which computes the square of an input array
7//CIVL can't turn a string into a function at this time
8
9//const char *KernelSource = "__kernel void square( __global float* input, __global float* output, const unsigned int count){ int i = get_global_id(0); if(i < count) output[i] = input[i] * input[i];}";
10
11// Simple compute kernel which computes the square of an input array
12//
13const char *KernelSource = "\n" \
14"__kernel void square( \n" \
15" __global float* input, \n" \
16" __global float* output, \n" \
17" const unsigned int count) \n" \
18"{ \n" \
19" int i = get_global_id(0); \n" \
20" if(i < count) \n" \
21" output[i] = input[i] * input[i]; \n" \
22"} \n" \
23"\n";
24/*
25void square(float* input, float* output, int count)
26{
27 int i = get_global_id(0);
28 if(i < count)
29 {
30 output[i] = input[i] * input[i];
31 }
32}
33*/
34/*
35void square(float* input, float* output, const unsigned int count)
36{
37 int i = get_global_id(0);
38 if (i < count)
39 {
40 output[i] = input[i] * input[i];
41 }
42}
43*/
44void main()
45{
46 int err;
47 int num_entries = 1;
48
49 cl_device_id device_id;
50 cl_context context;
51 cl_command_queue commands;
52 cl_program program;
53 cl_kernel kernel;
54
55
56 //assumes you have hardware that works, does not check that part
57 //fills the device_id with device ids, returns CL_SUCCESS if it works
58
59 err = clGetDeviceIDs(NULL,
60 CL_DEVICE_TYPE_GPU,
61 num_entries,
62 &device_id,
63 NULL);
64 //for non arrays, print using device_id[0], otherwise use *device_id[0];
65 printf("devices is %d", device_id[0]);
66 $assert (err == CL_SUCCESS);
67
68 //if not using an array, use &device_id
69 //Returns a context, which has device information
70 context = clCreateContext(0, 1, &device_id, NULL, NULL, &err);
71 $assert (context != NULL);
72
73 commands = clCreateCommandQueue(context, device_id, 0, &err);
74 $assert (commands != NULL);
75 //if not using an array, use device_id, no []
76 program = clCreateProgramWithSource(context, 1, (const char **) & KernelSource, NULL, &err);
77 $assert (program != NULL);
78
79 err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
80 $assert (err == CL_SUCCESS);
81
82 kernel = clCreateKernel(program, "square", &err);
83 $assert (kernel != NULL || err == CL_SUCCESS);
84}
Note: See TracBrowser for help on using the repository browser.