1 // -------------------------------------------------------------
2 // cuDPP -- CUDA Data Parallel Primitives library
3 // -------------------------------------------------------------
4 // $Revision: 3572$
5 // $Date: 2007-11-19 13:58:06 +0000 (Mon, 19 Nov 2007) $
6 // -------------------------------------------------------------
7 // This source code is distributed under the terms of license.txt
8 // in the root directory of this source distribution.
9 // -------------------------------------------------------------
10 #include "cudpp.h"
11 #include "cudpp_plan.h"
12 #include "cudpp_plan_manager.h"
13 #include "cudpp_maximal_launch.h"
14
15 typedef void* KernelPointer;
16
getNumCTAs(KernelPointer kernel)17 extern "C" size_t getNumCTAs(KernelPointer kernel)
18 {
19 return CUDPPPlanManager::numCTAs(kernel);
20 }
compNumCTAs(KernelPointer kernel,size_t bytesDynamicSharedMem,size_t threadsPerBlock)21 extern "C" void compNumCTAs(KernelPointer kernel, size_t bytesDynamicSharedMem, size_t threadsPerBlock)
22 {
23 CUDPPPlanManager::computeNumCTAs(kernel, bytesDynamicSharedMem, threadsPerBlock);
24 }
25
26 //! @internal Instantiate the plan manager singleton object
Instantiate()27 void CUDPPPlanManager::Instantiate()
28 {
29 if (NULL == m_instance)
30 m_instance = new CUDPPPlanManager;
31 }
32
33 //! @internal Destroy the plan manager singleton object
Destroy()34 void CUDPPPlanManager::Destroy()
35 {
36 if (NULL != m_instance)
37 {
38 delete m_instance;
39 m_instance = NULL;
40 }
41 }
42
43 /** @brief Plan Manager destructor
44 * Destroys all plans as well as the plan manager.
45 */
~CUDPPPlanManager()46 CUDPPPlanManager::~CUDPPPlanManager()
47 {
48 std::map<CUDPPHandle,CUDPPPlan*>::iterator it;
49
50 for (it = m_instance->plans.begin(); it != m_instance->plans.end(); it++)
51 {
52 CUDPPPlan* plan = it->second;
53 delete plan;
54 plan = NULL;
55 }
56 m_instance->plans.clear();
57
58 m_instance->numCTAsTable.clear();
59 }
60
61 /** @brief Add a plan to the plan manager
62 *
63 * @returns a valid CUDPPHandle if the plan was successfully added, or
64 * CUDPP_INVALID_HANDLE otherwise
65 * @param[in] plan The plan to add
66 */
AddPlan(CUDPPPlan * plan)67 CUDPPHandle CUDPPPlanManager::AddPlan(CUDPPPlan* plan)
68 {
69 Instantiate();
70
71 std::pair<std::map<CUDPPHandle, CUDPPPlan*>::iterator, bool> ret;
72
73 CUDPPHandle handle = (CUDPPHandle)m_instance->plans.size();
74 ret = m_instance->plans.insert(std::pair<CUDPPHandle,CUDPPPlan*>(handle, plan));
75 if (ret.second == true)
76 return handle;
77 else
78 return CUDPP_INVALID_HANDLE;
79 }
80
81 /** @brief Remove a plan from the plan manager
82 *
83 * @returns true if the plan was successfully removed, false otherwise
84 * @param[in] handle The handle to the plan to remove
85 */
RemovePlan(CUDPPHandle handle)86 bool CUDPPPlanManager::RemovePlan(CUDPPHandle handle)
87 {
88 if (m_instance == NULL)
89 {
90 return false;
91 }
92
93 std::map<CUDPPHandle,CUDPPPlan*>::iterator it;
94 it = m_instance->plans.find(handle);
95
96 if (it != m_instance->plans.end())
97 {
98 CUDPPPlan* plan = it->second;
99 delete plan;
100 plan = NULL;
101 m_instance->plans.erase(it);
102
103 if (0 == m_instance->plans.size())
104 {
105 Destroy();
106 }
107
108 return true;
109 }
110 else
111 {
112 return false;
113 }
114 }
115
116 /** @brief Get a plan from the plan manager by handle
117 *
118 * @returns A pointer to the plan if found, or NULL otherwise
119 * @param handle The handle to the requested plan
120 */
GetPlan(CUDPPHandle handle)121 CUDPPPlan* CUDPPPlanManager::GetPlan(CUDPPHandle handle)
122 {
123 if (m_instance == NULL)
124 {
125 return NULL;
126 }
127
128 std::map<CUDPPHandle, CUDPPPlan*>::iterator it;
129 it = m_instance->plans.find(handle);
130 if (it != m_instance->plans.end())
131 {
132 return it->second;
133 }
134 else
135 {
136 return NULL;
137 }
138 }
139
numCTAs(KernelPointer kernel)140 size_t CUDPPPlanManager::numCTAs(KernelPointer kernel)
141 {
142 if (m_instance == NULL)
143 {
144 return 0;
145 }
146
147 return m_instance->numCTAsTable[kernel];
148 }
149
computeNumCTAs(KernelPointer kernel,size_t bytesDynamicSharedMem,size_t threadsPerBlock)150 void CUDPPPlanManager::computeNumCTAs(KernelPointer kernel, size_t bytesDynamicSharedMem, size_t threadsPerBlock)
151 {
152 Instantiate();
153
154 m_instance->numCTAsTable[kernel] = maxBlocks(kernel, bytesDynamicSharedMem, threadsPerBlock);
155 }
156