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