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