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

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

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

  • Property mode set to 100644
File size: 3.4 KB
Line 
1#include <civlc.h>
2#include <stdio.h>
3#include "setup/openCL.cvl"
4
5$input int DATA_SIZE;
6
7//this is the kernel that gets loaded into the program
8// Simple compute kernel which computes the square of an input array
9//CIVL can't turn a string into a function at this time
10
11//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];}";
12
13// Simple compute kernel which computes the square of an input array
14//
15const char *KernelSource = "\n" \
16"__kernel void square( \n" \
17" __global float* input, \n" \
18" __global float* output, \n" \
19" const unsigned int count) \n" \
20"{ \n" \
21" int i = get_global_id(0); \n" \
22" if(i < count) \n" \
23" output[i] = input[i] * input[i]; \n" \
24"} \n" \
25"\n";
26/*
27void square(float* input, float* output, const unsigned int count)
28{
29 int i = get_global_id(0);
30 if (i < count)
31 {
32 output[i] = input[i] * input[i];
33 }
34}
35*/
36void main()
37{
38 int err;
39 int num_entries = 1;
40
41 cl_device_id device_id;
42 cl_context context;
43 cl_command_queue commands;
44 cl_program program;
45 cl_kernel kernel;
46
47 cl_mem input;
48 cl_mem output;
49
50 float data[DATA_SIZE];
51 float results[DATA_SIZE];
52 unsigned int correct;
53
54 int i = 0;
55 int count = DATA_SIZE;
56 for(i = 0; i < count; i++)
57 {
58 data[i] = rand() / (float)RAND_MAX;
59 }
60
61 //assumes you have hardware that works, does not check that part
62 //fills the device_id with device ids, returns CL_SUCCESS if it works
63
64 err = clGetDeviceIDs(NULL,
65 CL_DEVICE_TYPE_GPU,
66 num_entries,
67 &device_id,
68 NULL);
69 //for non arrays, print using device_id[0], otherwise use *device_id[0];
70 printf("device's id is %d\n", device_id[0]);
71 $assert (err == CL_SUCCESS);
72
73 //if not using an array, use &device_id
74 //Returns a context, which has device information
75 context = clCreateContext(0, 1, &device_id, NULL, NULL, &err);
76 $assert (context != NULL);
77
78 commands = clCreateCommandQueue(context, device_id, 0, &err);
79 $assert (commands != NULL);
80 //if not using an array, use device_id, no []
81 program = clCreateProgramWithSource(context, 1, (const char **) & KernelSource, NULL, &err);
82 $assert (program != NULL);
83
84 err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
85 $assert (err == CL_SUCCESS);
86
87 kernel = clCreateKernel(program, "square", &err);
88 $assert (kernel != NULL || err == CL_SUCCESS);
89 //is sizeof(int), was float before bug
90 input = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(int) * count, NULL, NULL);
91 output = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(int) * count, NULL, NULL);
92 $assert (input || output);
93 //printf("\nsizeof of input is %zu ", (size_t)sizeof(input));
94 //a reminder that c can't get sizeof a pointer pointing to something else so easily
95 //Run just input, get num_devices is 1 twice, just output, get 1 twice, both is five times?
96
97
98}
Note: See TracBrowser for help on using the repository browser.