File: cudpp_plan_manager.cpp

package info (click to toggle)
lammps 20220106.git7586adbb6a%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 348,064 kB
  • sloc: cpp: 831,421; python: 24,896; xml: 14,949; f90: 10,845; ansic: 7,967; sh: 4,226; perl: 4,064; fortran: 2,424; makefile: 1,501; objc: 238; lisp: 163; csh: 16; awk: 14; tcl: 6
file content (155 lines) | stat: -rw-r--r-- 3,957 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// -------------------------------------------------------------
// cuDPP -- CUDA Data Parallel Primitives library
// -------------------------------------------------------------
// $Revision: 3572$
// $Date: 2007-11-19 13:58:06 +0000 (Mon, 19 Nov 2007) $
// -------------------------------------------------------------
// This source code is distributed under the terms of license.txt
// in the root directory of this source distribution.
// -------------------------------------------------------------
#include "cudpp.h"
#include "cudpp_plan.h"
#include "cudpp_plan_manager.h"
#include "cudpp_maximal_launch.h"

typedef void* KernelPointer;

extern "C" size_t getNumCTAs(KernelPointer kernel)
{
    return CUDPPPlanManager::numCTAs(kernel);
}
extern "C" void compNumCTAs(KernelPointer kernel, size_t bytesDynamicSharedMem, size_t threadsPerBlock)
{
    CUDPPPlanManager::computeNumCTAs(kernel, bytesDynamicSharedMem, threadsPerBlock);
}

//! @internal Instantiate the plan manager singleton object
void CUDPPPlanManager::Instantiate()
{
    if (nullptr == m_instance)
        m_instance = new CUDPPPlanManager;
}

//! @internal Destroy the plan manager singleton object
void CUDPPPlanManager::Destroy()
{
    if (nullptr != m_instance)
    {
        delete m_instance;
        m_instance = nullptr;
    }
}

/** @brief Plan Manager destructor
* Destroys all plans as well as the plan manager.
*/
CUDPPPlanManager::~CUDPPPlanManager()
{
    std::map<CUDPPHandle,CUDPPPlan*>::iterator it;

    for (it = m_instance->plans.begin(); it != m_instance->plans.end(); it++)
    {
        CUDPPPlan* plan = it->second;
        delete plan;
        plan = nullptr;
    }
    m_instance->plans.clear();

    m_instance->numCTAsTable.clear();
}

/** @brief Add a plan to the plan manager
*
* @returns a valid CUDPPHandle if the plan was successfully added, or
* CUDPP_INVALID_HANDLE otherwise
* @param[in] plan The plan to add
*/
CUDPPHandle CUDPPPlanManager::AddPlan(CUDPPPlan* plan)
{
    Instantiate();

    std::pair<std::map<CUDPPHandle, CUDPPPlan*>::iterator, bool> ret;

    CUDPPHandle handle = (CUDPPHandle)m_instance->plans.size();
    ret = m_instance->plans.insert(std::pair<CUDPPHandle,CUDPPPlan*>(handle, plan));
    if (ret.second == true)
        return handle;
    else
        return CUDPP_INVALID_HANDLE;
}

/** @brief Remove a plan from the plan manager
*
* @returns true if the plan was successfully removed, false otherwise
* @param[in] handle The handle to the plan to remove
*/
bool CUDPPPlanManager::RemovePlan(CUDPPHandle handle)
{
    if (m_instance == nullptr)
    {
        return false;
    }

    std::map<CUDPPHandle,CUDPPPlan*>::iterator it;
    it = m_instance->plans.find(handle);

    if (it != m_instance->plans.end())
    {
        CUDPPPlan* plan = it->second;
        delete plan;
        plan = nullptr;
        m_instance->plans.erase(it);

        if (0 == m_instance->plans.size())
        {
            Destroy();
        }

        return true;
    }
    else
    {
        return false;
    }
}

/** @brief Get a plan from the plan manager by handle
*
* @returns A pointer to the plan if found, or nullptr otherwise
* @param handle The handle to the requested plan
*/
CUDPPPlan* CUDPPPlanManager::GetPlan(CUDPPHandle handle)
{
    if (m_instance == nullptr)
    {
        return nullptr;
    }

    std::map<CUDPPHandle, CUDPPPlan*>::iterator it;
    it = m_instance->plans.find(handle);
    if (it != m_instance->plans.end())
    {
        return it->second;
    }
    else
    {
        return nullptr;
    }
}

size_t CUDPPPlanManager::numCTAs(KernelPointer kernel)
{
    if (m_instance == nullptr)
    {
        return 0;
    }

    return m_instance->numCTAsTable[kernel];
}

void CUDPPPlanManager::computeNumCTAs(KernelPointer kernel, size_t bytesDynamicSharedMem, size_t threadsPerBlock)
{
    Instantiate();

    m_instance->numCTAsTable[kernel] = maxBlocks(kernel, bytesDynamicSharedMem, threadsPerBlock);
}