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
RevLine 
[9c7b25c]1#include <civlc.h>
2#include <stdio.h>
[6777880]3#include "setup/openCL.cvl"
[8472eb5]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
[2259218]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];}";
[8472eb5]10
[2259218]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";
[8472eb5]24/*
[4b7d11e]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/*
[8472eb5]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*/
[e01b89d]44void main()
45{
46 int err;
[4b7d11e]47 int num_entries = 1;
[9c7b25c]48
49 cl_device_id device_id;
[0744ab0]50 cl_context context;
51 cl_command_queue commands;
[8472eb5]52 cl_program program;
53 cl_kernel kernel;
[4b7d11e]54
[9c7b25c]55
[0744ab0]56 //assumes you have hardware that works, does not check that part
[8472eb5]57 //fills the device_id with device ids, returns CL_SUCCESS if it works
[4b7d11e]58
[9c7b25c]59 err = clGetDeviceIDs(NULL,
60 CL_DEVICE_TYPE_GPU,
61 num_entries,
62 &device_id,
63 NULL);
[4b7d11e]64 //for non arrays, print using device_id[0], otherwise use *device_id[0];
[9c7b25c]65 printf("devices is %d", device_id[0]);
66 $assert (err == CL_SUCCESS);
[0744ab0]67
[4b7d11e]68 //if not using an array, use &device_id
[8472eb5]69 //Returns a context, which has device information
[0744ab0]70 context = clCreateContext(0, 1, &device_id, NULL, NULL, &err);
71 $assert (context != NULL);
72
[8472eb5]73 commands = clCreateCommandQueue(context, device_id, 0, &err);
74 $assert (commands != NULL);
[4b7d11e]75 //if not using an array, use device_id, no []
[8472eb5]76 program = clCreateProgramWithSource(context, 1, (const char **) & KernelSource, NULL, &err);
[1a54e12]77 $assert (program != NULL);
78
[4b7d11e]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);
[9c7b25c]84}
Note: See TracBrowser for help on using the repository browser.