d1aa6dc7a5d403538e5cdbbedb9f5ae135847d48
1 /******************************************************************************
2 * Copyright (c) 2017-2018 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 example illustrates using multiple EOs to process a single frame
31 // For details, refer http://downloads.ti.com/mctools/esd/docs/tidl-api/
32 //
33 #include <signal.h>
34 #include <iostream>
35 #include <fstream>
36 #include <cassert>
37 #include <string>
39 #include "executor.h"
40 #include "execution_object.h"
41 #include "execution_object_pipeline.h"
42 #include "configuration.h"
43 #include "utils.h"
45 using namespace tidl;
46 using std::string;
47 using std::unique_ptr;
48 using std::vector;
50 using EOP = tidl::ExecutionObjectPipeline;
52 bool Run(int num_eve,int num_dsp, const char* ref_output);
54 int main(int argc, char *argv[])
55 {
56 // Catch ctrl-c to ensure a clean exit
57 signal(SIGABRT, exit);
58 signal(SIGTERM, exit);
60 // This example requires both EVE and C66x
61 uint32_t num_eve = Executor::GetNumDevices(DeviceType::EVE);
62 uint32_t num_dsp = Executor::GetNumDevices(DeviceType::DSP);
63 if (num_eve == 0 || num_dsp == 0)
64 {
65 std::cout << "TI DL not supported on this SoC." << std::endl;
66 return EXIT_SUCCESS;
67 }
69 string ref_file ="../test/testvecs/reference/j11_v2_ref.bin";
70 unique_ptr<const char> reference_output(ReadReferenceOutput(ref_file));
72 bool status = Run(num_eve, num_dsp, reference_output.get());
74 if (!status)
75 {
76 std::cout << "FAILED" << std::endl;
77 return EXIT_FAILURE;
78 }
80 std::cout << "PASSED" << std::endl;
81 return EXIT_SUCCESS;
82 }
84 bool Run(int num_eve, int num_dsp, const char* ref_output)
85 {
86 string config_file ="../test/testvecs/config/infer/tidl_config_j11_v2.txt";
88 Configuration c;
89 if (!c.ReadFromFile(config_file))
90 return false;
92 // Heap sizes for this network determined using Configuration::showHeapStats
93 c.PARAM_HEAP_SIZE = (3 << 20); // 3MB
94 c.NETWORK_HEAP_SIZE = (20 << 20); // 20MB
96 // Run this example for 16 input frames
97 c.numFrames = 16;
99 // Assign layers 12, 13 and 14 to the DSP layer group
100 const int EVE_LG = 1;
101 const int DSP_LG = 2;
102 c.layerIndex2LayerGroupId = { {12, DSP_LG}, {13, DSP_LG}, {14, DSP_LG} };
104 // Open input file for reading
105 std::ifstream input(c.inData, std::ios::binary);
107 bool status = true;
108 try
109 {
110 // Create Executors - use all the DSP and EVE cores available
111 // Specify layer group id for each Executor
112 unique_ptr<Executor> eve(CreateExecutor(DeviceType::EVE,
113 num_eve, c, EVE_LG));
114 unique_ptr<Executor> dsp(CreateExecutor(DeviceType::DSP,
115 num_dsp, c, DSP_LG));
117 // Create pipelines. Each pipeline has 1 EVE and 1 DSP. If there are
118 // more EVEs than DSPs, the DSPs are shared across multiple
119 // pipelines. E.g.
120 // 2 EVE, 2 DSP: EVE1 -> DSP1, EVE2 -> DSP2
121 // 4 EVE, 2 DSP: EVE1 -> DSP1, EVE2 -> DSP2, EVE3 -> DSP1, EVE4 ->DSP2
122 std::vector<EOP *> EOPs;
123 uint32_t num_pipe = std::max(num_eve, num_dsp);
124 for (uint32_t i = 0; i < num_pipe; i++)
125 EOPs.push_back(new EOP( { (*eve)[i % num_eve],
126 (*dsp)[i % num_dsp] } ));
128 AllocateMemory(EOPs);
130 // Process frames with EOs in a pipelined manner
131 // additional num_eos iterations to flush the pipeline (epilogue)
132 int num_eops = EOPs.size();
133 for (int frame_idx = 0; frame_idx < c.numFrames + num_eops; frame_idx++)
134 {
135 EOP* eop = EOPs[frame_idx % num_eops];
137 // Wait for previous frame on the same EOP to finish processing
138 if (eop->ProcessFrameWait())
139 {
140 // The reference output is valid only for the first frame
141 // processed on each EOP
142 if (frame_idx < num_eops && !CheckFrame(eop, ref_output))
143 status = false;
144 }
146 // Read a frame and start processing it with current eo
147 if (ReadFrame(eop, frame_idx, c, input))
148 eop->ProcessFrameStartAsync();
149 }
151 FreeMemory(EOPs);
153 }
154 catch (tidl::Exception &e)
155 {
156 std::cerr << e.what() << std::endl;
157 status = false;
158 }
160 input.close();
162 return status;
163 }