binary_cache.cpp 3.55 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
/*******************************************************************************
 *
 * MIT License
 *
 * Copyright (c) 2017 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 <binary_cache.hpp>
#include <handle.hpp>
#include <md5.hpp>
#include <env.hpp>
#include <stringutils.hpp>
#include <logger.hpp>
#include <target_properties.hpp>
#include <boost/filesystem.hpp>
#include <fstream>
#include <iostream>

Chao Liu's avatar
Chao Liu committed
38
namespace online_compile {
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

OLC_DECLARE_ENV_VAR(OLC_DISABLE_CACHE)
OLC_DECLARE_ENV_VAR(HOME)

static boost::filesystem::path ComputeCachePath()
{
    const char* home_dir = GetStringEnv(HOME{});
    if(home_dir == nullptr || home_dir == std::string("/") || home_dir == std::string(""))
    {
        home_dir = "/tmp";
    }

    auto p = boost::filesystem::path{home_dir} / "_hip_binary_kernels_";

    if(!boost::filesystem::exists(p))
        boost::filesystem::create_directories(p);
    return p;
}

boost::filesystem::path GetCachePath()
{
    static const boost::filesystem::path user_path = ComputeCachePath();

    return user_path;
}

Chao Liu's avatar
Chao Liu committed
65
static bool IsCacheDisabled() { return online_compile::IsEnabled(OLC_DISABLE_CACHE{}); }
66
67
68
69

boost::filesystem::path
GetCacheFile(const std::string& device, const std::string& name, const std::string& args)
{
Chao Liu's avatar
Chao Liu committed
70
    // std::string filename = (is_kernel_str ? online_compile::md5(name) : name) + ".o";
71
    std::string filename = name + ".o";
Chao Liu's avatar
Chao Liu committed
72
    return GetCachePath() / online_compile::md5(device + ":" + args) / filename;
73
74
75
76
77
78
79
}

boost::filesystem::path LoadBinary(const TargetProperties& target,
                                   const size_t num_cu,
                                   const std::string& name,
                                   const std::string& args)
{
Chao Liu's avatar
Chao Liu committed
80
    if(online_compile::IsCacheDisabled())
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
        return {};

    (void)num_cu;
    auto f = GetCacheFile(target.DbId(), name, args);
    if(boost::filesystem::exists(f))
    {
        return f.string();
    }
    else
    {
        return {};
    }
}

void SaveBinary(const boost::filesystem::path& binary_path,
                const TargetProperties& target,
                const std::string& name,
                const std::string& args)
{
Chao Liu's avatar
Chao Liu committed
100
    if(online_compile::IsCacheDisabled())
101
102
103
104
105
106
107
108
109
110
111
    {
        boost::filesystem::remove(binary_path);
    }
    else
    {
        auto p = GetCacheFile(target.DbId(), name, args);
        boost::filesystem::create_directories(p.parent_path());
        boost::filesystem::rename(binary_path, p);
    }
}

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