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
21 /* Include CLBLAS header. It automatically includes needed OpenCL header,
22 * so we can drop out explicit inclusion of cl.h header.
23 */
24 #include <clBLAS.h>
25
26 /* This example uses predefined matrices and their characteristics for
27 * simplicity purpose.
28 */
29
30 static const clblasOrder order = clblasRowMajor;
31 static const clblasTranspose trans = clblasNoTrans;
32
33 static const size_t M = 5;
34 static const size_t N = 5;
35 static const size_t KL = 1;
36 static const size_t KU = 2;
37 static const size_t lda = 4; // lda = KL + KU + 1
38
39 static const cl_float A[] = {
40 00, 12, 13, 14,
41 21, 22, 23, 24,
42 31, 32, 33, 34,
43 41, 42, 43, 00,
44 51, 62, 00, 00
45 };
46
47 static const cl_float alpha = 10;
48 static const cl_float X[] = {
49 11,
50 21,
51 31,
52 41,
53 51,
54 };
55 static const int incx = 1;
56
57 static const cl_float beta = 20;
58 static cl_float Y[] = {
59 11,
60 21,
61 31,
62 41,
63 51,
64 };
65 static const int incy = 1;
66
67
68 static void
printResult(const char * str)69 printResult(const char* str)
70 {
71 size_t i;
72
73 printf("%s:\n", str);
74
75 for (i = 0; i < M; i++) {
76 printf("%f\n", Y[ i * incy ]);
77 }
78 }
79
80 int
main(void)81 main(void)
82 {
83 cl_int err;
84 cl_platform_id platform = 0;
85 cl_device_id device = 0;
86 cl_context_properties props[3] = { CL_CONTEXT_PLATFORM, 0, 0 };
87 cl_context ctx = 0;
88 cl_command_queue queue = 0;
89 cl_mem bufA, bufX, bufY;
90 cl_event event = NULL;
91 int ret = 0;
92
93 /* Setup OpenCL environment. */
94 err = clGetPlatformIDs(1, &platform, NULL);
95 if (err != CL_SUCCESS) {
96 printf( "clGetPlatformIDs() failed with %d\n", err );
97 return 1;
98 }
99
100 err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
101 if (err != CL_SUCCESS) {
102 printf( "clGetDeviceIDs() failed with %d\n", err );
103 return 1;
104 }
105
106 props[1] = (cl_context_properties)platform;
107 ctx = clCreateContext(props, 1, &device, NULL, NULL, &err);
108 if (err != CL_SUCCESS) {
109 printf( "clCreateContext() failed with %d\n", err );
110 return 1;
111 }
112
113 queue = clCreateCommandQueue(ctx, device, 0, &err);
114 if (err != CL_SUCCESS) {
115 printf( "clCreateCommandQueue() failed with %d\n", err );
116 clReleaseContext(ctx);
117 return 1;
118 }
119
120 /* Setup clblas. */
121 err = clblasSetup();
122 if (err != CL_SUCCESS) {
123 printf("clblasSetup() failed with %d\n", err);
124 clReleaseCommandQueue(queue);
125 clReleaseContext(ctx);
126 return 1;
127 }
128
129 /* Prepare OpenCL memory objects and place matrices inside them. */
130 bufA = clCreateBuffer(ctx, CL_MEM_READ_ONLY, M * lda * sizeof(cl_float),
131 NULL, &err);
132 bufX = clCreateBuffer(ctx, CL_MEM_READ_ONLY, N * sizeof(cl_float),
133 NULL, &err);
134 bufY = clCreateBuffer(ctx, CL_MEM_READ_WRITE, M * sizeof(cl_float),
135 NULL, &err);
136
137 err = clEnqueueWriteBuffer(queue, bufA, CL_TRUE, 0,
138 M * lda * sizeof(cl_float), A, 0, NULL, NULL);
139 err = clEnqueueWriteBuffer(queue, bufX, CL_TRUE, 0,
140 N * sizeof(cl_float), X, 0, NULL, NULL);
141 err = clEnqueueWriteBuffer(queue, bufY, CL_TRUE, 0,
142 M * sizeof(cl_float), Y, 0, NULL, NULL);
143
144 /* Call clblas function. */
145 err = clblasSgbmv(order, trans, M, N, KL, KU, alpha, bufA, 0, lda, bufX, 0, incx,
146 beta, bufY, 0, incy, 1, &queue, 0, NULL, &event);
147 if (err != CL_SUCCESS) {
148 printf("clblasSgbmv() failed with %d\n", err);
149 ret = 1;
150 }
151 else {
152 /* Wait for calculations to be finished. */
153 err = clWaitForEvents(1, &event);
154
155 /* Fetch results of calculations from GPU memory. */
156 err = clEnqueueReadBuffer(queue, bufY, CL_TRUE, 0, M * sizeof(cl_float),
157 Y, 0, NULL, NULL);
158 /* At this point you will get the result of SGBMV placed in Y array. */
159 printResult("clblasSgbmv result");
160 }
161
162 /* Release OpenCL events. */
163 clReleaseEvent(event);
164
165 /* Release OpenCL memory objects. */
166 clReleaseMemObject(bufY);
167 clReleaseMemObject(bufX);
168 clReleaseMemObject(bufA);
169
170 /* Finalize work with clblas. */
171 clblasTeardown();
172
173 /* Release OpenCL working objects. */
174 clReleaseCommandQueue(queue);
175 clReleaseContext(ctx);
176
177 return ret;
178 }
179