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_double2 alpha = {{10.0f, 2.0f}};
32 static const clblasUplo uplo = clblasUpper;
33 
34 static cl_double2 AP[] = {
35     {{11.0f, 00.0f}}, {{12.0f, 02.0f}}, {{13.0f, 05.0f}}, {{14.0f, 12.0f}}, {{15.0f, 06.0f}},
36                       {{22.0f, 00.0f}}, {{23.0f, 25.0f}}, {{24.0f, 23.0f}}, {{25.0f, 61.0f}},
37                                         {{33.0f, 00.0f}}, {{34.0f, 23.0f}}, {{35.0f, 21.0f}},
38                                                           {{44.0f, 00.0f}}, {{45.0f, 23.0f}},
39                                                                             {{55.0f, 00.0f}}
40 };
41 
42 static const cl_double2 X[] = {
43     {{11.0f, 03.0f}},
44     {{01.0f, 15.0f}},
45     {{30.0f, 20.0f}},
46     {{01.0f, 02.0f}},
47     {{11.0f, 10.0f}}
48 };
49 static const int incx = 1;
50 
51 static const cl_double2 Y[] = {
52     {{11.0f, 03.0f}},
53     {{03.0f, 05.0f}},
54     {{09.0f, 00.0f}},
55     {{01.0f, 02.0f}},
56     {{11.0f, 00.0f}}
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\t");
72                 continue;
73             }
74 
75 			printf("(%9.2lf, %-9.2lf)\t", CREAL( AP[ off ] ), CIMAG( 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     /* Prepare OpenCL memory objects and place matrices inside them. */
133     bufAP = clCreateBuffer(ctx, CL_MEM_READ_WRITE, (numElementsAP * sizeof(cl_double2)),
134                             NULL, &err);
135     bufX = clCreateBuffer(ctx, CL_MEM_READ_ONLY, N * sizeof(cl_double2),
136                             NULL, &err);
137 	bufY = clCreateBuffer(ctx, CL_MEM_READ_ONLY, N * sizeof(cl_double2),
138 						    NULL, &err);
139 
140     err = clEnqueueWriteBuffer(queue, bufAP, CL_TRUE, 0,
141 					                numElementsAP * sizeof(cl_double2), AP, 0, NULL, NULL);
142     err = clEnqueueWriteBuffer(queue, bufX, CL_TRUE, 0,
143 					                N * sizeof(cl_double2), X, 0, NULL, NULL);
144 	err = clEnqueueWriteBuffer(queue, bufY, CL_TRUE, 0,
145 					                N * sizeof(cl_double2), Y, 0, NULL, NULL);
146 
147     err = clblasZhpr2(order, uplo, N, alpha, bufX, 0 /*offx */, incx, bufY, 0 /*offy*/, incy,
148 						            bufAP, 0 /*offa */, 1, &queue, 0, NULL, &event);
149 
150    	if (err != CL_SUCCESS) {
151         printf("clblasZhpr2() failed with %d\n", err);
152         ret = 1;
153     }
154     else {
155         /* Wait for calculations to be finished. */
156         err = clWaitForEvents(1, &event);
157 
158         /* Fetch results of calculations from GPU memory. */
159         err = clEnqueueReadBuffer(queue, bufAP, CL_TRUE, 0, (numElementsAP * sizeof(cl_double2)),
160                                   AP, 0, NULL, NULL);
161         /* At this point you will get the result of ZHPR2 placed in A array. */
162         printResult();
163     }
164 
165     /* Release OpenCL events. */
166     clReleaseEvent(event);
167 
168     /* Release OpenCL memory objects. */
169     clReleaseMemObject(bufX);
170     clReleaseMemObject(bufAP);
171 	clReleaseMemObject(bufY);
172 
173     /* Finalize work with clblas. */
174     clblasTeardown();
175 
176     /* Release OpenCL working objects. */
177     clReleaseCommandQueue(queue);
178     clReleaseContext(ctx);
179 
180     return ret;
181 }
182