|
| 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 | + |
| 18 | +#include <sys/types.h> |
| 19 | +#include <stdio.h> |
| 20 | +#include <string.h> |
| 21 | + |
| 22 | +/* Include CLBLAS header. It automatically includes needed OpenCL header, |
| 23 | + * so we can drop out explicit inclusion of cl.h header. |
| 24 | + */ |
| 25 | +#include <clBLAS.h> |
| 26 | + |
| 27 | +/* This example uses predefined matrices and their characteristics for |
| 28 | + * simplicity purpose. |
| 29 | + */ |
| 30 | +static const clblasOrder order = clblasRowMajor; |
| 31 | +static const clblasSide side = clblasLeft; |
| 32 | + |
| 33 | +static const size_t M = 4; |
| 34 | +static const size_t N = 5; |
| 35 | + |
| 36 | +static const FloatComplex alpha = { 10, 0 }; |
| 37 | + |
| 38 | +static const clblasTranspose transA = clblasNoTrans; |
| 39 | +static const clblasUplo uploA = clblasUpper; |
| 40 | +static const clblasDiag diagA = clblasNonUnit; |
| 41 | +static const FloatComplex A[] = { |
| 42 | + { 11, 0 },{ 12, 0 },{ 13, 0 },{ 14, 0 }, |
| 43 | + { 0, 0 },{ 22, 0 },{ 23, 0 },{ 24, 0 }, |
| 44 | + { 0, 0 },{ 0, 0 },{ 33, 0 },{ 34, 0 }, |
| 45 | + { 0, 0 },{ 0, 0 },{ 0, 0 },{ 44, 0 } |
| 46 | +}; |
| 47 | +static const size_t lda = 4; /* i.e. lda = M */ |
| 48 | + |
| 49 | +static FloatComplex B[] = { |
| 50 | + { 11, 0 },{ 12, 0 },{ 13, 0 },{ 14, 0 },{ 15, 0 }, |
| 51 | + { 21, 0 },{ 22, 0 },{ 23, 0 },{ 24, 0 },{ 25, 0 }, |
| 52 | + { 31, 0 },{ 32, 0 },{ 33, 0 },{ 34, 0 },{ 35, 0 }, |
| 53 | + { 41, 0 },{ 42, 0 },{ 43, 0 },{ 44, 0 },{ 45, 0 }, |
| 54 | +}; |
| 55 | +static const size_t ldb = 5; /* i.e. ldb = N */ |
| 56 | + |
| 57 | + |
| 58 | +static FloatComplex result[20]; /* ldb*M */ |
| 59 | + |
| 60 | +static const size_t off = 1; |
| 61 | +static const size_t offA = 4 + 1; /* M + off */ |
| 62 | +static const size_t offB = 5 + 1; /* N + off */ |
| 63 | + |
| 64 | +static void |
| 65 | +printResult(const char* str) |
| 66 | +{ |
| 67 | + size_t i, j, nrows; |
| 68 | + |
| 69 | + printf("%s:\n", str); |
| 70 | + |
| 71 | + nrows = (sizeof(result) / sizeof(FloatComplex)) / ldb; |
| 72 | + for (i = 0; i < nrows; i++) { |
| 73 | + for (j = 0; j < ldb; j++) { |
| 74 | + printf("%.5f ", result[i * ldb + j].x); |
| 75 | + } |
| 76 | + printf("\n"); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +int |
| 81 | +main(void) |
| 82 | +{ |
| 83 | + cl_int err; |
| 84 | + cl_platform_id platform[] = { 0, 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, bufB; |
| 90 | + cl_event event = NULL; |
| 91 | + int ret = 0; |
| 92 | + |
| 93 | + /* Setup OpenCL environment. */ |
| 94 | + err = clGetPlatformIDs(sizeof( platform ), &platform, NULL); |
| 95 | + if (err != CL_SUCCESS) { |
| 96 | + printf( "clGetPlatformIDs() failed with %d\n", err ); |
| 97 | + return 1; |
| 98 | + } |
| 99 | + |
| 100 | + err = clGetDeviceIDs(platform[0], CL_DEVICE_TYPE_CPU, 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 * M * sizeof(*A), |
| 131 | + NULL, &err); |
| 132 | + bufB = clCreateBuffer(ctx, CL_MEM_READ_WRITE, M * N * sizeof(*B), |
| 133 | + NULL, &err); |
| 134 | + |
| 135 | + err = clEnqueueWriteBuffer(queue, bufA, CL_TRUE, 0, |
| 136 | + M * M * sizeof(*A), A, 0, NULL, NULL); |
| 137 | + err = clEnqueueWriteBuffer(queue, bufB, CL_TRUE, 0, |
| 138 | + M * N * sizeof(*B), B, 0, NULL, NULL); |
| 139 | + |
| 140 | + /* Call clblas function. Perform TRSM for the lower right sub-matrices */ |
| 141 | + err = clblasCtrsm(order, side, uploA, transA, diagA, M - off, N - off, |
| 142 | + alpha, bufA, offA, lda, bufB, offB, ldb, 1, &queue, 0, |
| 143 | + NULL, &event); |
| 144 | + if (err != CL_SUCCESS) { |
| 145 | + printf("clblasStrsmEx() failed with %d\n", err); |
| 146 | + ret = 1; |
| 147 | + } |
| 148 | + else { |
| 149 | + /* Wait for calculations to be finished. */ |
| 150 | + err = clWaitForEvents(1, &event); |
| 151 | + |
| 152 | + /* Fetch results of calculations from GPU memory. */ |
| 153 | + err = clEnqueueReadBuffer(queue, bufB, CL_TRUE, 0, |
| 154 | + M * N * sizeof(*result), |
| 155 | + result, 0, NULL, NULL); |
| 156 | + |
| 157 | + /* At this point you will get the result of STRSM placed in 'result' array. */ |
| 158 | + puts(""); |
| 159 | + printResult("clblasCtrsmEx result"); |
| 160 | + } |
| 161 | + |
| 162 | + /* Release OpenCL events. */ |
| 163 | + clReleaseEvent(event); |
| 164 | + |
| 165 | + /* Release OpenCL memory objects. */ |
| 166 | + clReleaseMemObject(bufB); |
| 167 | + clReleaseMemObject(bufA); |
| 168 | + |
| 169 | + /* Finalize work with clblas. */ |
| 170 | + clblasTeardown(); |
| 171 | + |
| 172 | + /* Release OpenCL working objects. */ |
| 173 | + clReleaseCommandQueue(queue); |
| 174 | + clReleaseContext(ctx); |
| 175 | + |
| 176 | + return ret; |
| 177 | +} |
0 commit comments