cac7e276f6a73a63abaa363d2392ceab355a9693
[dense-linear-algebra-libraries/linalg.git] / src / ti / linalg / blasblisacc / src / ti_cblas_cblas_dtrmm.c
1 /******************************************************************************
2 * Copyright (c) 2013-2015, Texas Instruments Incorporated - http://www.ti.com/
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Texas Instruments Incorporated nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
27 *****************************************************************************/
29 #include "ti_cblas_acc.h"
30 #include "../../ticblas/ticblas.h"
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 void __real_cblas_dtrmm(const enum CBLAS_ORDER Order, const enum CBLAS_SIDE Side, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, const int M, const int N, const double alpha, const double *A, const int lda, double *B, const int ldb) ;
37 #ifdef __cplusplus
38 }
39 extern "C"
40 #endif
41 void cblas_dtrmm(const enum CBLAS_ORDER Order, const enum CBLAS_SIDE Side, const enum CBLAS_UPLO Uplo, const enum CBLAS_TRANSPOSE TransA, const enum CBLAS_DIAG Diag, const int M, const int N, const double alpha, const double *A, const int lda, double *B, const int ldb)
42 {
44 /* Do an init on first use */
45 if (!ti_cblas_init_done) ti_cblas_init();
46 TI_CBLAS_DEBUG_PRINT("Intercepted call to %s\n", "cblas_dtrmm");
48 TI_CBLAS_PROFILE_START();
49 /* Dynamic condtional offload to ARM */
50 if ((TI_CBLAS_L3_OFFLOAD == TI_CBLAS_OFFLOAD_NONE) || ((TI_CBLAS_L3_OFFLOAD == TI_CBLAS_OFFLOAD_SIZE) && (!dtrmm_offload_dsp(Order,Side,M,N)))) {
51 TI_CBLAS_DEBUG_PRINT("Executing ARM %s\n", "cblas_dtrmm");
52 __real_cblas_dtrmm(Order,Side,Uplo,TransA,Diag,M,N,alpha,A,lda,B,ldb);
53 TI_CBLAS_PROFILE_REPORT(" Entire %s call (ARM) took %8.2f us\n","cblas_dtrmm", (float) clock_diff);
54 return ;
55 }
56 /* End ARM offload */
58 /******************************************************************/
59 /* DSP offload WILL be done if control reaches here */
60 TI_CBLAS_DEBUG_PRINT("Offloading to DSP %s\n", "cblas_dtrmm");
62 /* Lookup kernel pointer from global table */
63 #ifdef __cplusplus
64 Event e;
65 Kernel* __K;
66 #else
67 cl_kernel __K;
68 #endif
69 __K = ti_cblas_get_kernel(TI_CBLAS_CBLAS_DTRMM_IDX, "ocl_cblas_dtrmm");
71 #ifdef __cplusplus
72 try
73 #else
74 cl_int err = CL_SUCCESS;
75 #endif
76 {
78 #ifdef __cplusplus
79 __K->setArg(0, Order);
80 #else
81 err |= clSetKernelArg(__K, 0, sizeof(Order), &Order);
82 #endif
84 #ifdef __cplusplus
85 __K->setArg(1, Side);
86 #else
87 err |= clSetKernelArg(__K, 1, sizeof(Side), &Side);
88 #endif
90 #ifdef __cplusplus
91 __K->setArg(2, Uplo);
92 #else
93 err |= clSetKernelArg(__K, 2, sizeof(Uplo), &Uplo);
94 #endif
96 #ifdef __cplusplus
97 __K->setArg(3, TransA);
98 #else
99 err |= clSetKernelArg(__K, 3, sizeof(TransA), &TransA);
100 #endif
102 #ifdef __cplusplus
103 __K->setArg(4, Diag);
104 #else
105 err |= clSetKernelArg(__K, 4, sizeof(Diag), &Diag);
106 #endif
108 #ifdef __cplusplus
109 __K->setArg(5, M);
110 #else
111 err |= clSetKernelArg(__K, 5, sizeof(M), &M);
112 #endif
114 #ifdef __cplusplus
115 __K->setArg(6, N);
116 #else
117 err |= clSetKernelArg(__K, 6, sizeof(N), &N);
118 #endif
120 #ifdef __cplusplus
121 __K->setArg(7, alpha);
122 #else
123 err |= clSetKernelArg(__K, 7, sizeof(alpha), &alpha);
124 #endif
125 /* For const arguments we use CL_MEM_USE_READ_ONLY */
127 int size_bufA;
128 size_bufA = BLAS_SIDE(Side,M,N)*lda*sizeof(double);
129 size_bufA = MAX(size_bufA,1);
131 #ifdef __cplusplus
132 Buffer buf_A(*ti_cblas_ocl_context, CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR, size_bufA, (void *)A);
133 __K->setArg(8, buf_A);
134 #else
135 cl_mem buf_A = clCreateBuffer(ti_cblas_ocl_context, CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR, size_bufA, (void *)A, &err);
136 TI_CBLAS_OCL_CHKERROR("clCreateBuffer",err);
137 err |= clSetKernelArg(__K, 8, sizeof(buf_A), &buf_A);
138 TI_CBLAS_OCL_CHKERROR("clSetKernelArg",err);
139 #endif
141 #ifdef __cplusplus
142 __K->setArg(9, lda);
143 #else
144 err |= clSetKernelArg(__K, 9, sizeof(lda), &lda);
145 #endif
147 int size_bufB;
148 size_bufB = BLAS_ORD(Order,M,N)*ldb*sizeof(double);
149 size_bufB = MAX(size_bufB,1);
151 #ifdef __cplusplus
152 Buffer buf_B(*ti_cblas_ocl_context, CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR, size_bufB, (void *)B);
153 __K->setArg(10, buf_B);
154 #else
155 cl_mem buf_B = clCreateBuffer(ti_cblas_ocl_context, CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR, size_bufB, (void *)B, &err);
156 TI_CBLAS_OCL_CHKERROR("clCreateBuffer",err);
157 err |= clSetKernelArg(__K, 10, sizeof(buf_B), &buf_B);
158 TI_CBLAS_OCL_CHKERROR("clSetKernelArg",err);
159 #endif
161 #ifdef __cplusplus
162 __K->setArg(11, ldb);
163 #else
164 err |= clSetKernelArg(__K, 11, sizeof(ldb), &ldb);
165 #endif
167 void *msmc_ptr;
168 size_t msmc_size = MSMC_BUF_SIZE;
169 msmc_ptr = ti_cblas_mem_alloc(MSMC_BUF_SIZE);
170 #ifdef __cplusplus
171 Buffer buf_MSMC(*ti_cblas_ocl_context, CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR, MSMC_BUF_SIZE, (void *)msmc_ptr);
172 //Buffer buf_MSMC(ti_cblas_ocl_context, CL_MEM_READ_WRITE|CL_MEM_USE_MSMC_TI, MSMC_BUF_SIZE);
173 __K->setArg(12, buf_MSMC);
175 #else
176 //cl_mem buf_MSMC = clCreateBuffer(ti_cblas_ocl_context, CL_MEM_READ_WRITE|CL_MEM_USE_MSMC_TI, MSMC_BUF_SIZE, NULL, &err);
177 cl_mem buf_MSMC = clCreateBuffer(ti_cblas_ocl_context, CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR, MSMC_BUF_SIZE, (void *)msmc_ptr, &err);
178 TI_CBLAS_OCL_CHKERROR("clCreateBuffer",err);
179 err |= clSetKernelArg(__K, 12, sizeof(buf_MSMC), &buf_MSMC);
180 TI_CBLAS_OCL_CHKERROR("clSetKernelArg",err);
181 #endif
183 #ifdef __cplusplus
184 __K->setArg(13, msmc_size);
185 #else
186 err |= clSetKernelArg(__K, 13, sizeof(msmc_size), &msmc_size);
187 #endif
189 void *ddr_ptr;
190 size_t ddr_size = DDR_BUF_SIZE;
191 ddr_ptr = __malloc_ddr(DDR_BUF_SIZE);
192 Buffer buf_DDR(*ti_cblas_ocl_context, CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR, DDR_BUF_SIZE, ddr_ptr);
193 __K->setArg(14, buf_DDR);
194 __K->setArg(15, ddr_size);
196 /* create a buffer argument to get the return error code from the DSP */
197 int err_code;
198 #ifdef __cplusplus
199 Buffer buf_err(*ti_cblas_ocl_context, CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR, sizeof(int), &err_code);
200 __K->setArg(16, buf_err);
201 #else
202 cl_mem buf_err = clCreateBuffer(*ti_cblas_ocl_context, CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR, sizeof(int), &err_code, &err);
203 TI_CBLAS_OCL_CHKERROR("clCreateBuffer",err);
204 err |= clSetKernelArg(__K, 16, sizeof(buf_err), &buf_err);
205 TI_CBLAS_OCL_CHKERROR("clSetKernelArg",err);
206 #endif
208 #ifdef __cplusplus
209 ti_cblas_ocl_Q->enqueueTask(*__K, 0, &e);
210 e.wait();
211 #else
212 cl_event e;
213 err |= clEnqueueTask(ti_cblas_ocl_Q, __K, 0, 0, &e);
214 TI_CBLAS_OCL_CHKERROR("clEnqueueTask",err);
215 err |= clWaitForEvents(1, &e);
216 TI_CBLAS_OCL_CHKERROR("clWaitForEvents",err);
217 err |= clReleaseEvent(e);
218 TI_CBLAS_OCL_CHKERROR("clReleaseEvent",err);
219 #endif
221 if(err_code != TICBLAS_SUCCESS) {
222 printf("Error in offloaded cblas_dtrmm with error code %d!\n", err_code);
223 }
225 ti_cblas_mem_free(msmc_ptr);
226 __free_ddr(ddr_ptr);
228 ti_cblas_delete_kernel(__K);
230 TI_CBLAS_DEBUG_PRINT("Finished executing %s\n", "cblas_dtrmm");
231 TI_CBLAS_PROFILE_REPORT(" Entire %s call (DSP) took %8.2f us\n","cblas_dtrmm", (float) clock_diff);
232 return ;
233 }
235 #ifdef __cplusplus
236 catch (Error err)
237 {
238 /* delete __K if it has not been deleted */
239 if(__K!=NULL) {
240 ti_cblas_delete_kernel(__K);
241 }
243 ti_cblas_error(err.what(),err.err());
244 return ;
245 }
246 #endif
247 }