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 clblasOrder order = clblasRowMajor;
31 
32 static const size_t N = 5;
33 static const cl_float alpha = 10;
34 static cl_float X[] = {
35     11,
36     21,
37     31,
38     41,
39     51,
40 };
41 static const int incx = 1;
42 
43 static void
printResult(void)44 printResult(void)
45 {
46     size_t i;
47     printf("\nResult:\n");
48 
49     for (i = 0; i < N; i++) {
50 			printf("\t%f", X[i] );
51 		printf("\n");
52     }
53 }
54 
55 int
main(void)56 main(void)
57 {
58     cl_int err;
59     cl_platform_id platform = 0;
60     cl_device_id device = 0;
61     cl_context_properties props[3] = { CL_CONTEXT_PLATFORM, 0, 0 };
62     cl_context ctx = 0;
63     cl_command_queue queue = 0;
64     cl_mem bufX;
65     cl_event event = NULL;
66     int ret = 0;
67     int lenX = 1 + (N-1)*abs(incx);
68 
69     /* Setup OpenCL environment. */
70     err = clGetPlatformIDs(1, &platform, NULL);
71     if (err != CL_SUCCESS) {
72         printf( "clGetPlatformIDs() failed with %d\n", err );
73         return 1;
74     }
75 
76     err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
77     if (err != CL_SUCCESS) {
78         printf( "clGetDeviceIDs() failed with %d\n", err );
79         return 1;
80     }
81 
82     props[1] = (cl_context_properties)platform;
83     ctx = clCreateContext(props, 1, &device, NULL, NULL, &err);
84     if (err != CL_SUCCESS) {
85         printf( "clCreateContext() failed with %d\n", err );
86         return 1;
87     }
88 
89     queue = clCreateCommandQueue(ctx, device, 0, &err);
90     if (err != CL_SUCCESS) {
91         printf( "clCreateCommandQueue() failed with %d\n", err );
92         clReleaseContext(ctx);
93         return 1;
94     }
95 
96     /* Setup clblas. */
97     err = clblasSetup();
98     if (err != CL_SUCCESS) {
99         printf("clblasSetup() failed with %d\n", err);
100         clReleaseCommandQueue(queue);
101         clReleaseContext(ctx);
102         return 1;
103     }
104 
105     /* Prepare OpenCL memory objects and place vectors inside them. */
106     bufX = clCreateBuffer(ctx, CL_MEM_READ_WRITE, ( lenX * sizeof(cl_float)),
107                           NULL, &err);
108 
109     err = clEnqueueWriteBuffer(queue, bufX, CL_TRUE, 0,
110         ( lenX * sizeof(cl_float)), X, 0, NULL, NULL);
111 
112     /* Call clblas function. */
113     err = clblasSscal( N, alpha, bufX, 0, incx, 1, &queue, 0, NULL, &event);
114 
115     if (err != CL_SUCCESS) {
116         printf("clblasSscal() failed with %d\n", err);
117         ret = 1;
118     }
119     else {
120         /* Wait for calculations to be finished. */
121         err = clWaitForEvents(1, &event);
122 
123         /* Fetch results of calculations from GPU memory. */
124         err = clEnqueueReadBuffer(queue, bufX, CL_TRUE, 0, (lenX * sizeof(cl_float)),
125                                   X, 0, NULL, NULL);
126         /* At this point you will get the result of SSCAL placed in X vector. */
127         printResult();
128     }
129 
130     /* Release OpenCL events. */
131     clReleaseEvent(event);
132 
133     /* Release OpenCL memory objects. */
134     clReleaseMemObject(bufX);
135 
136     /* Finalize work with clblas. */
137     clblasTeardown();
138 
139     /* Release OpenCL working objects. */
140     clReleaseCommandQueue(queue);
141     clReleaseContext(ctx);
142 
143     return ret;
144 }
145