[processor-sdk/performance-audio-sr.git] / psdk_cust / libarch_k2g_1_0_1_0 / examples / arm+dsp / test_app_arm.cpp
1 /******************************************************************************
2 * Copyright (c) 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 ////////////////////////////////////////////////////////////////////////////////
30 // This file shows an example of the host code of an application that uses an
31 // ARM+DSP library.
32 ////////////////////////////////////////////////////////////////////////////////
33 #include <cstdlib>
34 #include <cmath>
35 #include <cstring>
36 #include <iostream>
37 #include <fstream>
38 #define __CL_ENABLE_EXCEPTIONS
39 #include <CL/cl.hpp>
40 using namespace std;
41 using namespace cl;
42 // Both cl and std namespace define size_t, so we must be explicit.
43 #define size_t ::size_t
44 #ifndef TEST_FAT_BINARY
45 #include "ocl_util.h"
46 #endif
47 #include <pthread.h>
50 #define TEST_DEBUG_PRINT(...) {fprintf(stdout,"TEST DEBUG: ");fprintf(stdout,__VA_ARGS__);}
52 extern void lib_init_arm();
53 extern void lib_find_l2_start();
54 extern void lib_kernel_arm(double *in1_ptr, double *in2_ptr, double *out_ptr, int data_size);
56 void app_fill_memory(double *buf1, double *buf2, int num_elements);
57 double app_err_check(double *in1, double *in2, double *out, int num_elements);
59 #define TEST_DATA_SIZE (1024) //1K data points
61 /*==============================================================================
62 * Application code on the host which calls library functions to process data.
63 *============================================================================*/
64 int main()
65 {
66 // Allocate input/output buffers using TI-OpenCL built-ins.
67 TEST_DEBUG_PRINT("Allocating memory for input/output buffers.\n");
68 double *in1_ptr = (double *)__malloc_ddr(TEST_DATA_SIZE*sizeof(double));
69 double *in2_ptr = (double *)__malloc_ddr(TEST_DATA_SIZE*sizeof(double));
70 double *out_ptr = (double *)__malloc_ddr(TEST_DATA_SIZE*sizeof(double));
72 TEST_DEBUG_PRINT("in1_ptr is: 0x%08x.\n", (unsigned int)in1_ptr);
73 TEST_DEBUG_PRINT("in2_ptr is: 0x%08x.\n", (unsigned int)in2_ptr);
74 TEST_DEBUG_PRINT("out_ptr is: 0x%08x.\n", (unsigned int)out_ptr);
76 if(in1_ptr==NULL || in2_ptr==NULL || out_ptr==NULL) {
77 TEST_DEBUG_PRINT("No memory!\n");
78 exit(0);
79 }
80 else {
81 TEST_DEBUG_PRINT("Memory allocated.\n");
82 }
84 // Fill memory with data for testing.
85 app_fill_memory(in1_ptr, in2_ptr, TEST_DATA_SIZE);
87 // OpenCL initialization
88 lib_init_arm();
90 // Testing - print L2 start address
91 lib_find_l2_start();
93 // Pass input/output buffers to library function which sums two input buffers
94 // and return the sum in the output buffer.
95 lib_kernel_arm(in1_ptr, in2_ptr, out_ptr, TEST_DATA_SIZE);
97 __free_ddr(in1_ptr);
98 __free_ddr(in2_ptr);
99 __free_ddr(out_ptr);
101 double err = app_err_check(in1_ptr, in2_ptr, out_ptr, TEST_DATA_SIZE);
103 TEST_DEBUG_PRINT("Error checking. Difference between ARM results and DSP results: %e\n", err);
105 if(err != 0) {
106 TEST_DEBUG_PRINT("Failed!\n");
107 }
108 else {
109 TEST_DEBUG_PRINT("Passed!\n");
110 }
112 return(0);
113 } /* main */
115 /*==============================================================================
116 * Filling memory with random data.
117 *============================================================================*/
118 void app_fill_memory(double *buf1, double *buf2, int num_elements)
119 {
120 for(int i=0; i<num_elements; i++)
121 {
122 buf1[i] = (double)rand()/RAND_MAX;
123 buf2[i] = (double)rand()/RAND_MAX;
124 }
126 for(int i=0;i<10;i++)
127 {
128 TEST_DEBUG_PRINT("ARM Input 1: %e, Input 2: %e\n", buf1[i],buf2[i]);
129 }
131 } /* app_fill_memory */
133 /*==============================================================================
134 * Error checking: check if buffer out is the sum of buffer in1 and buffer in2.
135 *============================================================================*/
136 double app_err_check(double *in1, double *in2, double *out, int num_elements)
137 {
138 double error = 0.0;
140 for(int i=0;i<10;i++)
141 {
142 TEST_DEBUG_PRINT("Output returned by DSP: %e\n", out[i]);
143 }
145 for(int i=0; i<num_elements; i++)
146 {
147 error += out[i] - (in1[i]+in2[i]);
148 }
150 return(error);
151 } /* app_err_check */
153 /* Nothing past this point */