1 /******************************************************************************
2 * Copyright (c) 2019 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 //! @file subgraph_runtime.h
31 #pragma once
32 #include <vector>
33 #include <mutex>
34 #include <condition_variable>
35 #include "execution_object_pipeline.h"
36 #include "subgraph_data_conv.h"
38 extern "C" {
40 void TidlRunSubgraph(int total_subgraphs,
41 int subgraph_id,
42 int num_inputs,
43 int num_outputs,
44 float **inputTensors,
45 float **outputTensors
46 );
48 } // extern "C"
50 namespace tidl {
52 #if 0
53 // Auto-generated code from Relay/TVM compilation step after
54 // partitioning and lowering to backend implementation
56 void TVM_TidlFunction(int total_subgraphs, int subgraph_id,
57 int num_input_tensors, int num_output_tensors,
58 PackedArgs args)
59 {
60 float** in_data = new float*[num_input_tensors];
61 float** out_data = new float*[num_output_tensors];
63 for (int i = 0; i < num_input_tensors + num_output_tensors; i++)
64 if (i < num_input_tensors)
65 in_data[i] = args.data[i];
66 else
67 out_data[i - num_input_tensors] = args.data[i];
69 // call into this function in libtidl.so
70 // dlopen("libtidl.so")
71 // TidlFunc = dlsym("TidlRunSubgraph");
72 (*TidlFunc)(total_subgraphs, subgraph_id,
73 num_input_tensors, num_output_tensors,
74 in_data, out_data);
76 delete [] in_data;
77 delete [] out_data;
78 }
79 #endif
82 // Singleton ResM .h file
83 // Resource manager for available EVE and DSP devices,
84 // - Allocates EVEs and DSPs
85 // - Constructs Executors (tidl_setup) and ExecutionObjects (tid_init)
86 // - Creates set of ExecutionPipelines (with or without DSP)
87 // - Allocating EOP on demand (acquire and free semantics)
88 // - Allocates input/output buffers
89 class ResM {
90 public:
91 ResM();
92 ~ResM();
93 static ResM& Instance(uint32_t total_num_subgraphs = 1);
95 // how to ge
96 ExecutionObjectPipeline* GetEOP(uint32_t subgraph_id);
97 void FreeEOP(uint32_t subgraph_id,
98 ExecutionObjectPipeline* eop);
99 Configuration& GetConfiguration(uint32_t subgraph_id);
100 const SubgraphDataConv& GetInConv(uint32_t subgraph_id);
101 const SubgraphDataConv& GetOutConv(uint32_t subgraph_id);
104 private:
105 void Init(uint32_t num_subgraphs);
107 bool enable_trace_m;
108 uint32_t num_subgraphs_m;
109 uint32_t num_es_per_subgraph_m;
110 uint32_t num_eves_m;
111 uint32_t num_dsps_m;
112 uint32_t num_lg2_dsps_used_m; // in partitioned execution case
113 std::mutex mutex_init_m;
115 // indexed by subgraph_id for resources
116 struct ResEOP {
117 ResEOP() : free_eop_index(0), is_used(), eops(nullptr) {}
119 uint32_t free_eop_index;
120 std::mutex mutex_eops;
121 std::condition_variable cv_eops;
122 std::vector<bool> is_used;
123 std::vector<ExecutionObjectPipeline*>* eops;
124 };
125 std::vector<Configuration> cs_m;
126 std::vector<Executor*> es_m;
127 std::vector<Executor*> e2s_m;
128 std::vector<ResEOP> *eops_m;
129 std::vector<SubgraphDataConv*> in_conv_m;
130 std::vector<SubgraphDataConv*> out_conv_m;
131 };
133 } // namespace tidl