"vscode:/vscode.git/clone" did not exist on "983837ff860367690d0d50ab222bc5023f18550e"
handlehip.cpp 7.93 KB
Newer Older
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
/*******************************************************************************
 *
 * MIT License
 *
 * Copyright (c) 2017-2020 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 *******************************************************************************/

#include <handle.hpp>

#include <binary_cache.hpp>
#include <env.hpp>
#include <kernel_cache.hpp>
#include <stringutils.hpp>
#include <target_properties.hpp>

#include <hipCheck.hpp>

#include <write_file.hpp>

#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>

#ifndef _WIN32
#include <unistd.h>
#endif

#include <algorithm>
#include <cassert>
#include <chrono>
#include <thread>

OLC_DECLARE_ENV_VAR(OLC_DEVICE_CU)

Chao Liu's avatar
Chao Liu committed
53
namespace online_compile {
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

std::size_t GetAvailableMemory()
{
    size_t free, total;
    MY_HIP_CHECK(hipMemGetInfo(&free, &total));
    return free;
}

int get_device_id() // Get random device
{
    int device;

    MY_HIP_CHECK(hipGetDevice(&device));

    return device;
}

void set_device(int id) { MY_HIP_CHECK(hipSetDevice(id)); }

int set_default_device()
{
    int n;

    MY_HIP_CHECK(hipGetDeviceCount(&n));

    // Pick device based on process id
    auto pid = ::getpid();
    assert(pid > 0);
    set_device(pid % n);
    return (pid % n);
}

struct HandleImpl
{
    using StreamPtr = std::shared_ptr<typename std::remove_pointer<hipStream_t>::type>;

    HandleImpl() {}

    StreamPtr create_stream()
    {
        hipStream_t result;

        MY_HIP_CHECK(hipStreamCreate(&result));

        return StreamPtr{result, &hipStreamDestroy};
    }

    static StreamPtr reference_stream(hipStream_t s) { return StreamPtr{s, null_deleter{}}; }

    std::string get_device_name() const
    {
        hipDeviceProp_t props;

        MY_HIP_CHECK(hipGetDeviceProperties(&props, device));

        const std::string name(props.gcnArchName);
        return name;
    }

    StreamPtr stream = nullptr;
    int device       = -1;
    KernelCache cache;
    TargetProperties target_properties;
};

Handle::Handle(hipStream_t stream) : impl(new HandleImpl())
{
    this->impl->device = get_device_id();

    if(stream == nullptr)
        this->impl->stream = HandleImpl::reference_stream(nullptr);
    else
        this->impl->stream = HandleImpl::reference_stream(stream);

    this->impl->target_properties.Init(this);
}

Handle::Handle() : impl(new HandleImpl())
{
    this->impl->device = get_device_id();
    this->impl->stream = HandleImpl::reference_stream(nullptr);

    this->impl->target_properties.Init(this);
}

Handle::~Handle() {}

void Handle::SetStream(hipStream_t streamID) const
{
    this->impl->stream = HandleImpl::reference_stream(streamID);

    this->impl->target_properties.Init(this);
}

hipStream_t Handle::GetStream() const { return impl->stream.get(); }

KernelInvoke Handle::AddKernel(const std::string& algorithm,
                               const std::string& network_config,
                               const std::string& program_name,
                               const std::string& kernel_name,
                               const std::vector<size_t>& vld,
                               const std::vector<size_t>& vgd,
                               const std::string& params,
                               std::size_t cache_index) const
{

    auto obj = this->impl->cache.AddKernel(
        *this, algorithm, network_config, program_name, kernel_name, vld, vgd, params, cache_index);
    return this->Run(obj);
}

void Handle::ClearKernels(const std::string& algorithm, const std::string& network_config) const
{
    this->impl->cache.ClearKernels(algorithm, network_config);
}

const std::vector<Kernel>& Handle::GetKernelsImpl(const std::string& algorithm,
                                                  const std::string& network_config) const
{
    return this->impl->cache.GetKernels(algorithm, network_config);
}

bool Handle::HasKernel(const std::string& algorithm, const std::string& network_config) const
{
    return this->impl->cache.HasKernels(algorithm, network_config);
}

KernelInvoke Handle::Run(Kernel k) const { return k.Invoke(this->GetStream()); }

Program Handle::LoadProgram(const std::string& program_name, std::string params) const
{
Chao Liu's avatar
Chao Liu committed
185
186
    if((!online_compile::EndsWith(program_name, ".mlir-cpp")) &&
       (!online_compile::EndsWith(program_name, ".mlir")))
187
188
189
190
    {
        params += " -mcpu=" + this->GetTargetProperties().Name();
    }

Chao Liu's avatar
Chao Liu committed
191
    auto hsaco = online_compile::LoadBinary(
192
193
194
195
196
        this->GetTargetProperties(), this->GetMaxComputeUnits(), program_name, params);
    if(hsaco.empty())
    {
        auto p = HIPOCProgram{program_name, params, this->GetTargetProperties()};

Chao Liu's avatar
Chao Liu committed
197
        auto path = online_compile::GetCachePath() / boost::filesystem::unique_path();
198
        if(p.IsCodeObjectInMemory())
Chao Liu's avatar
Chao Liu committed
199
            online_compile::WriteFile(p.GetCodeObjectBlob(), path);
200
201
        else
            boost::filesystem::copy_file(p.GetCodeObjectPathname(), path);
Chao Liu's avatar
Chao Liu committed
202
        online_compile::SaveBinary(path, this->GetTargetProperties(), program_name, params);
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247

        return p;
    }
    else
    {
        return HIPOCProgram{program_name, hsaco};
    }
}

bool Handle::HasProgram(const std::string& program_name, const std::string& params) const
{
    return this->impl->cache.HasProgram(program_name, params);
}

void Handle::AddProgram(Program prog,
                        const std::string& program_name,
                        const std::string& params) const
{
    this->impl->cache.AddProgram(prog, program_name, params);
}

void Handle::Finish() const { MY_HIP_CHECK(hipStreamSynchronize(this->GetStream())); }

std::size_t Handle::GetLocalMemorySize() const
{
    int result;

    MY_HIP_CHECK(hipDeviceGetAttribute(
        &result, hipDeviceAttributeMaxSharedMemoryPerBlock, this->impl->device));

    return result;
}

std::size_t Handle::GetGlobalMemorySize() const
{
    size_t result;

    MY_HIP_CHECK(hipDeviceTotalMem(&result, this->impl->device));

    return result;
}

std::size_t Handle::GetMaxComputeUnits() const
{
    int result;
Chao Liu's avatar
Chao Liu committed
248
    const char* const num_cu = online_compile::GetStringEnv(OLC_DEVICE_CU{});
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
    if(num_cu != nullptr && strlen(num_cu) > 0)
    {
        return boost::lexical_cast<std::size_t>(num_cu);
    }

    MY_HIP_CHECK(
        hipDeviceGetAttribute(&result, hipDeviceAttributeMultiprocessorCount, this->impl->device));

    return result;
}

std::size_t Handle::GetWavefrontWidth() const
{
    hipDeviceProp_t props{};

    MY_HIP_CHECK(hipGetDeviceProperties(&props, this->impl->device));

    auto result = static_cast<size_t>(props.warpSize);
    return result;
}

std::string Handle::GetDeviceNameImpl() const { return this->impl->get_device_name(); }

std::string Handle::GetDeviceName() const { return this->impl->target_properties.Name(); }

const TargetProperties& Handle::GetTargetProperties() const
{
    return this->impl->target_properties;
}

std::ostream& Handle::Print(std::ostream& os) const
{
    os << "stream: " << this->impl->stream << ", device_id: " << this->impl->device;
    return os;
}

Chao Liu's avatar
Chao Liu committed
285
} // namespace online_compile