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.h"
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 void __real_cblas_cswap(const int N, void *X, const int incX, void *Y, const int incY) ;
36 #ifdef __cplusplus
37 }
38 extern "C"
39 #endif
40 void cblas_cswap(const int N, void *X, const int incX, void *Y, const int incY)
41 {
43 /* Do an init on first use */
44 if (!ti_cblas_init_done) ti_cblas_init();
45 TI_CBLAS_DEBUG_PRINT("Intercepted call to %s\n", "cblas_cswap");
47 /* OpenCL cannot deal with overlapping memory regions. This is an issue when you
48 * are trying to swap two rows of a matrix, where the matrix is column major. Hence,
49 * the offload of this routine to the DSP is disabled.
50 */
51 #ifndef TI_CBLAS_SWAP_ENABLE_OFFLOAD
52 TI_CBLAS_PROFILE_START();
53 TI_CBLAS_DEBUG_PRINT("Executing ARM %s\n", "cblas_cswap");
54 __real_cblas_cswap(N,X,incX,Y,incY);
55 TI_CBLAS_PROFILE_REPORT(" Entire %s call (ARM) took %8.2f us\n","cblas_cswap", (float) clock_diff);
56 return ;
57 #else
59 TI_CBLAS_PROFILE_START();
60 /* Dynamic condtional offload to ARM */
61 if ((TI_CBLAS_L1_OFFLOAD == TI_CBLAS_OFFLOAD_NONE)) {
62 TI_CBLAS_DEBUG_PRINT("Executing ARM %s\n", "cblas_cswap");
63 __real_cblas_cswap(N,X,incX,Y,incY);
64 TI_CBLAS_PROFILE_REPORT(" Entire %s call (ARM) took %8.2f us\n","cblas_cswap", (float) clock_diff);
65 return ;
66 }
67 /* End ARM offload */
69 /******************************************************************/
70 /* DSP offload WILL be done if control reaches here */
71 TI_CBLAS_DEBUG_PRINT("Offloading to DSP %s\n", "cblas_cswap");
73 /* Lookup kernel pointer from global table */
74 #ifdef __cplusplus
75 Event e;
76 Kernel* __K;
77 #else
78 cl_kernel __K;
79 #endif
80 __K = ti_cblas_get_kernel(TI_CBLAS_CBLAS_CSWAP_IDX, "ocl_cblas_cswap");
81 #ifdef __cplusplus
82 try
83 #else
84 cl_int err = CL_SUCCESS;
85 #endif
86 {
88 #ifdef __cplusplus
89 __K->setArg(0, N);
90 #else
91 err |= clSetKernelArg(__K, 0, sizeof(N), &N);
92 #endif
94 int size_bufX;
95 size_bufX = 2*(1+(N-1)*abs(incX))*sizeof(float);
96 size_bufX = MAX(size_bufX,1);
98 #ifdef __cplusplus
99 Buffer buf_X(*ti_cblas_ocl_context, CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR, size_bufX, (void *)X);
100 __K->setArg(1, buf_X);
101 #else
102 cl_mem buf_X = clCreateBuffer(ti_cblas_ocl_context, CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR, size_bufX, (void *)X, &err);
103 TI_CBLAS_OCL_CHKERROR("clCreateBuffer",err);
104 err |= clSetKernelArg(__K, 1, sizeof(buf_X), &buf_X);
105 TI_CBLAS_OCL_CHKERROR("clSetKernelArg",err);
106 #endif
108 #ifdef __cplusplus
109 __K->setArg(2, incX);
110 #else
111 err |= clSetKernelArg(__K, 2, sizeof(incX), &incX);
112 #endif
114 int size_bufY;
115 size_bufY = 2*(1+(N-1)*abs(incY))*sizeof(float);
116 size_bufY = MAX(size_bufY,1);
118 #ifdef __cplusplus
119 Buffer buf_Y(*ti_cblas_ocl_context, CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR, size_bufY, (void *)Y);
120 __K->setArg(3, buf_Y);
121 #else
122 cl_mem buf_Y = clCreateBuffer(ti_cblas_ocl_context, CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR, size_bufY, (void *)Y, &err);
123 TI_CBLAS_OCL_CHKERROR("clCreateBuffer",err);
124 err |= clSetKernelArg(__K, 3, sizeof(buf_Y), &buf_Y);
125 TI_CBLAS_OCL_CHKERROR("clSetKernelArg",err);
126 #endif
128 #ifdef __cplusplus
129 __K->setArg(4, incY);
130 #else
131 err |= clSetKernelArg(__K, 4, sizeof(incY), &incY);
132 #endif
134 #ifdef __cplusplus
135 ti_cblas_ocl_Q->enqueueTask(*__K, 0, &e);
136 e.wait();
137 #else
138 cl_event e;
139 err |= clEnqueueTask(ti_cblas_ocl_Q, __K, 0, 0, &e);
140 TI_CBLAS_OCL_CHKERROR("clEnqueueTask",err);
141 err |= clWaitForEvents(1, &e);
142 TI_CBLAS_OCL_CHKERROR("clWaitForEvents",err);
143 err |= clReleaseEvent(e);
144 TI_CBLAS_OCL_CHKERROR("clReleaseEvent",err);
146 #endif
148 ti_cblas_delete_kernel(__K);
150 TI_CBLAS_DEBUG_PRINT("Finished executing %s\n", "cblas_cswap");
151 TI_CBLAS_PROFILE_REPORT(" Entire %s call (DSP) took %8.2f us\n","cblas_cswap", (float) clock_diff);
152 return ;
153 }
155 #ifdef __cplusplus
156 catch (Error err)
157 {
158 ti_cblas_error(err.what(),err.err());
159 return ;
160 }
161 #endif
162 #endif //TI_CBLAS_SWAP_ENABLE_OFFLOAD
163 }