1 /* ************************************************************************
2  * Copyright 2013 Advanced Micro Devices, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  * ************************************************************************/
16 
17 #include <sys/types.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <math.h>
21 
22 /* Include CLBLAS header. It automatically includes needed OpenCL header,
23  * so we can drop out explicit inclusion of cl.h header.
24  */
25 #include <clBLAS.h>
26 
27 /* This example uses predefined matrices and their characteristics for
28  * simplicity purpose.
29  */
30 static const size_t N = 7;
31 static const cl_float alpha = 10;
32 static cl_float X[] = {
33     11,
34     21,
35     31,
36     41,
37     51,
38     61,
39     71,
40 };
41 static const int incx = 1;
42 
43 static cl_float Y[] = {
44     15,
45     11,
46     1,
47     2,
48     1,
49     8,
50     1,
51 };
52 static const int incy = 1;
53 
54 
55 static void
printResult(void)56 printResult(void)
57 {
58     size_t i;
59     printf("\nResult:\n");
60 
61     printf("Y\n");
62     for (i = 0; i < N; i++) {
63             printf("\t%f\n", Y[i]);
64     }
65 }
66 
67 int
main(void)68 main(void)
69 {
70     cl_int err;
71     cl_platform_id platform = 0;
72     cl_device_id device = 0;
73     cl_context_properties props[3] = { CL_CONTEXT_PLATFORM, 0, 0 };
74     cl_context ctx = 0;
75     cl_command_queue queue = 0;
76     cl_mem bufX, bufY;
77     cl_event event = NULL;
78     int ret = 0;
79 	int lenX = 1 + (N-1)*abs(incx);
80 	int lenY = 1 + (N-1)*abs(incy);
81 
82     /* Setup OpenCL environment. */
83     err = clGetPlatformIDs(1, &platform, NULL);
84     if (err != CL_SUCCESS) {
85         printf( "clGetPlatformIDs() failed with %d\n", err );
86         return 1;
87     }
88 
89     err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
90     if (err != CL_SUCCESS) {
91         printf( "clGetDeviceIDs() failed with %d\n", err );
92         return 1;
93     }
94 
95     props[1] = (cl_context_properties)platform;
96     ctx = clCreateContext(props, 1, &device, NULL, NULL, &err);
97     if (err != CL_SUCCESS) {
98         printf( "clCreateContext() failed with %d\n", err );
99         return 1;
100     }
101 
102     queue = clCreateCommandQueue(ctx, device, 0, &err);
103     if (err != CL_SUCCESS) {
104         printf( "clCreateCommandQueue() failed with %d\n", err );
105         clReleaseContext(ctx);
106         return 1;
107     }
108 
109     /* Setup clblas. */
110     err = clblasSetup();
111     if (err != CL_SUCCESS) {
112         printf("clblasSetup() failed with %d\n", err);
113         clReleaseCommandQueue(queue);
114         clReleaseContext(ctx);
115         return 1;
116     }
117 
118     /* Prepare OpenCL memory objects and place matrices inside them. */
119     bufX = clCreateBuffer(ctx, CL_MEM_READ_ONLY, (lenX*sizeof(cl_float)), NULL, &err);
120     bufY = clCreateBuffer(ctx, CL_MEM_READ_WRITE, (lenY*sizeof(cl_float)), NULL, &err);
121 
122     err = clEnqueueWriteBuffer(queue, bufX, CL_TRUE, 0, (lenX*sizeof(cl_float)), X, 0, NULL, NULL);
123     err = clEnqueueWriteBuffer(queue, bufY, CL_TRUE, 0, (lenY*sizeof(cl_float)), Y, 0, NULL, NULL);
124 
125     /* Call clblas function. */
126     err = clblasSaxpy( N, alpha, bufX, 0, incx, bufY, 0, incy, 1, &queue, 0, NULL, &event);
127     if (err != CL_SUCCESS) {
128         printf("clblasSaxpy() failed with %d\n", err);
129         ret = 1;
130     }
131     else {
132         /* Wait for calculations to be finished. */
133         err = clWaitForEvents(1, &event);
134 
135         /* Fetch results of calculations from GPU memory. */
136         err = clEnqueueReadBuffer(queue, bufY, CL_TRUE, 0, (lenY*sizeof(cl_float)),
137                                     Y, 0, NULL, NULL);
138 
139         /* At this point you will get the result of SAXPY placed in vector Y. */
140         printResult();
141     }
142 
143     /* Release OpenCL events. */
144     clReleaseEvent(event);
145 
146     /* Release OpenCL memory objects. */
147     clReleaseMemObject(bufY);
148     clReleaseMemObject(bufX);
149 
150     /* Finalize work with clblas. */
151     clblasTeardown();
152 
153     /* Release OpenCL working objects. */
154     clReleaseCommandQueue(queue);
155     clReleaseContext(ctx);
156 
157     return ret;
158 }
159