File: GB_jit_cache.h

package info (click to toggle)
suitesparse-graphblas 7.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,112 kB
  • sloc: ansic: 1,072,243; cpp: 8,081; sh: 512; makefile: 506; asm: 369; python: 125; awk: 10
file content (320 lines) | stat: -rw-r--r-- 12,848 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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
248
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * Copyright (c) 2019,2020 NVIDIA CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef GB_JIT_CACHE_H_
#define GB_JIT_CACHE_H_

#include <jitify.hpp>
#include <unordered_map>
#include <string>
#include <memory>
#include <mutex>
#include <iostream>
#include <fstream>


#define JITIFY_USE_CACHE 1

namespace jit {

std::string get_user_home_cache_dir();
std::string get_user_graphblas_source_path();
std::string getCacheDir(void);

template <typename Tv>
using named_prog = std::pair<std::string, std::shared_ptr<Tv>>;

// Basic file descriptor to enable file manipulation with caching 
class File_Desc
{
public:
   virtual void open( const char *path_and_file, const char *mode) {}
   virtual void close() {}
   virtual void macrofy() {

       printf("Uh oh. this isn't good\n");

   }
   std::string filename;
};

/**
 * @brief Get the string path to the JITIFY kernel cache directory.
 *
 * This path can be overridden at runtime by defining an environment variable
 * named `GB_CUDA_KERNEL_CACHE_PATH`. The value of this variable must be a path
 * under which the process' user has read/write priveleges.
 *
 * This function returns a path to the cache directory, creating it if it
 * doesn't exist.
 *
 * The default cache directory `~/.GraphBLAS_kernel_cache`.
 **/

class GBJitCache
{
public:

    /**---------------------------------------------------------------------------*
     * @brief Get a process wide singleton cache object
     * 
     *---------------------------------------------------------------------------**/
    static GBJitCache& Instance() {
        // Meyers' singleton is thread safe in C++11
        // Link: https://stackoverflow.com/a/1661564
        static GBJitCache cache;
        return cache;
    }

    GBJitCache();
    ~GBJitCache();

    /**---------------------------------------------------------------------------*
     * @brief Get the file object
     * 
     * Searches an internal in-memory cache and file based cache for the file 
     * and if not found, opens the file, calls macrofy, closes the file 
     * 
     * @param file_desc [in] object representing file:  open, macrofy, close 
     * @return  string name of file, or 'error' if not able to create file  
     *---------------------------------------------------------------------------**/
    std::string getFile( File_Desc & file_obj );

    /**---------------------------------------------------------------------------*
     * @brief Get the Kernel Instantiation object
     * 
     * Searches an internal in-memory cache and file based cache for the kernel
     * and if not found, JIT compiles and returns the kernel
     * 
     * @param kern_name [in] name of kernel to return
     * @param program   [in] Jitify preprocessed program to get the kernel from
     * @param arguments [in] template arguments for kernel in vector of strings
     * @return  Pair of string kernel identifier and compiled kernel object
     *---------------------------------------------------------------------------**/
    named_prog<jitify::experimental::KernelInstantiation> getKernelInstantiation(
        std::string const& kern_name,
        named_prog<jitify::experimental::Program> const& program,
        std::vector<std::string> const& arguments);

    /**---------------------------------------------------------------------------*
     * @brief Get the Jitify preprocessed Program object
     * 
     * Searches an internal in-memory cache and file based cache for the Jitify
     * pre-processed program and if not found, JIT processes and returns it
     * 
     * @param prog_file_name [in] name of program to return
     * @param cuda_source    [in] string source code of program to compile
     * @param given_headers  [in] vector of strings representing source or names of
     *  each header included in cuda_source
     * @param given_options  [in] vector of strings options to pass to NVRTC
     * @param file_callback  [in] pointer to callback function to call whenever a
     *  header needs to be loaded
     * @return named_prog<jitify::experimental::Program> 
     *---------------------------------------------------------------------------**/
    named_prog<jitify::experimental::Program> getProgram(
        std::string const& prog_file_name, 
        std::string const& cuda_source = "",
        std::vector<std::string> const& given_headers = {},
        std::vector<std::string> const& given_options = {},
        jitify::experimental::file_callback_type file_callback = nullptr);

private:
    template <typename Tv>
    using umap_str_shptr = std::unordered_map<std::string, std::shared_ptr<Tv>>;

    umap_str_shptr<std::string>                                file_map;
    umap_str_shptr<jitify::experimental::KernelInstantiation>  kernel_inst_map;
    umap_str_shptr<jitify::experimental::Program>              program_map;

    /*
    Even though this class can be used as a non-singleton, the file cache
    access should remain limited to one thread per process. The lockf locks can
    prevent multiple processes from accessing the file but are ineffective in
    preventing multiple threads from doing so as the lock is shared by the
    entire process.
    Therefore the mutexes are static.
    */
    static std::mutex _file_cache_mutex;
    static std::mutex _kernel_cache_mutex;
    static std::mutex _program_cache_mutex;

private:
    /**---------------------------------------------------------------------------*
     * @brief Class to allow process wise exclusive access to cache files
     *
     *---------------------------------------------------------------------------**/
    class cacheFile
    {
    private:
        std::string _file_name ;
        std::string _dir_name = "~/.GraphBLAS_kernel_cache/";
        bool successful_read = false;
        bool successful_write = false;
    public:
        cacheFile(std::string file_name);
        ~cacheFile();

        /**---------------------------------------------------------------------------*
         * @brief Read this file and return the contents as a std::string
         * 
         *---------------------------------------------------------------------------**/
        std::string read_file();

        /**---------------------------------------------------------------------------*
         * @brief Write the passed string to this file
         * 
         *---------------------------------------------------------------------------**/
        void write(std::string);

        /**---------------------------------------------------------------------------*
         * @brief Check whether the read() operation on the file completed successfully
         * 
         * @return true Read was successful. String returned by `read()` is valid
         * @return false Read was unsuccessful. String returned by `read()` is empty
         *---------------------------------------------------------------------------**/
        bool is_read_successful() { return successful_read; }

        /**---------------------------------------------------------------------------*
         * @brief Check whether the write() operation on the file completed successfully
         * 
         * @return true Write was successful.
         * @return false Write was unsuccessful. File state is undefined
         *---------------------------------------------------------------------------**/
        bool is_write_successful() { return successful_write; }
    };

private:

    template <typename T, typename FileDescType>
    named_prog<T> getCachedFile(
            FileDescType &file_object,
        umap_str_shptr<T>& map )
    {

//        printf("INside get cached file\n");
        std::string name = file_object.filename;

        // Find memory cached T object
        auto it = map.find(name);
        if ( it != map.end()) {
//            std::cout<<"found memory-cached file "<<name<<std::endl;
            return std::make_pair(name, it->second);
        }
        else { // Find file cached T object
            bool successful_read = false;
            std::string serialized;
            std::string cache_dir = getCacheDir();
            std::string file_name = cache_dir + "/" + name;
            if (not cache_dir.empty() ) {
                // TODO: Use OS-agnostic path separator here
//                std::cout<<"looking for prog in file "<<file_name<<std::endl;
                file_object.open(file_name.c_str(), "r");
                cacheFile file{file_name};
                serialized = file.read_file();
                successful_read = file.is_read_successful();
//                std::cout << "successful_read: " << successful_read << std::endl;
                if(successful_read) {
                    file_object.close();
//                    std::cout << "Just closed" << std::endl;
                }
            }
            if (not successful_read) {
                // JIT compile and write to file if possible
//                std::cout << "not successful read. macrofying" << std::endl;
                file_object.open(file_name.c_str(), "w");
                file_object.macrofy();
//                std::cout<<" got fresh content for "<<name<<std::endl;
                file_object.close();

                if (not cache_dir.empty()) {
//                    std::cout<<"writing in file "<<file_name<<std::endl;
                    cacheFile file{file_name};

                    cacheFile macrofied{name};
                    serialized = macrofied.read_file();
                    file.write(serialized);
                }
            }
            // Add deserialized T to cache and return
            map[name] = std::make_shared<std::string>(serialized);
            //std::cout<<"storing file in memory "<<name<<std::endl;
            return std::make_pair(name, map[name]);
        }
    }


    // GetCached 
    // This does a lot. 1) it checks if the file named is in the memory cache 
    //                  2) checks the disk cache 
    //                  3) compiles and caches the result and returns the program
    template <typename T, typename FallbackFunc>
    named_prog<T> getCached(
        std::string const& name,
        umap_str_shptr<T>& map,
        FallbackFunc func) {

        // Find memory cached T object
        auto it = map.find(name);
        if ( it != map.end()) {
//            std::cout<<"found memory-cached prog "<<name<<std::endl;
            return std::make_pair(name, it->second);
        }
        else { // Find file cached T object
            bool successful_read = false;
            std::string serialized;
            #if defined(JITIFY_USE_CACHE)
                std::string cache_dir = getCacheDir();
                if (not cache_dir.empty() ) {
                    // TODO: Use OS-agnostic path separator
                    std::string file_name = cache_dir + "/" + name;
                    //std::cout<<"looking for prog in file "<<file_name<<std::endl;

                    cacheFile file{file_name};
                    serialized = file.read_file();
                    successful_read = file.is_read_successful();
                }
            #endif
            if (not successful_read) {
                // JIT compile and write to file if possible
//                    std::cout << "compiling now" << std::endl;
                auto f = func();

//                    std::cout << "completed func()" << std::endl;
                serialized = f.serialize();
//                std::cout<<" compiled serialized prog "<<name<<std::endl;

                #if defined(JITIFY_USE_CACHE)
                    if (not cache_dir.empty()) {
                        std::string file_name = cache_dir + "/" + name;
//                        std::cout<<"writing prog in file "<<file_name<<std::endl;
                        cacheFile file{file_name};
                        file.write(serialized);
                    }
                #endif
            }
            // Add deserialized T to cache and return
            auto program = std::make_shared<T>(T::deserialize(serialized));
            map[name] = program;
            //std::cout<<"storing prog in memory "<<name<<std::endl;
            return std::make_pair(name, program);
        }
    }
};

} // namespace jit


#endif // GB_JIT_CACHE_H_