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