File: GB_jit_cache.cu

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 (276 lines) | stat: -rw-r--r-- 9,025 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
/*
 * 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.
 */

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <pwd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <filesystem>

#include "GB_jit_cache.h"
#include "GraphBLAS.h"
// from GraphBLAS.h (for example):
// #define GxB_IMPLEMENTATION_MAJOR 6
// #define GxB_IMPLEMENTATION_MINOR 0
// #define GxB_IMPLEMENTATION_SUB   3

namespace jit {

// Get the directory in home to use for storing the cache
    std::string get_user_home_cache_dir() {
        auto home_dir = std::getenv("HOME");
        if (home_dir != nullptr) {
            std::string Major_ver = GB_XSTR (GxB_IMPLEMENTATION_MAJOR) ;
            std::string Minor_ver = GB_XSTR (GxB_IMPLEMENTATION_MINOR) ;
            std::string Imple_sub = GB_XSTR (GxB_IMPLEMENTATION_SUB) ;
            return std::string(home_dir) + "/.SuiteSparse/GraphBLAS/"
                   + Major_ver+"."+Minor_ver+"."+Imple_sub;
        } else {
            return std::string();
        }
    }

// Get the directory in home to use for storing the cache
    std::string get_user_graphblas_source_path() {
        auto gb_home = std::getenv("GRAPHBLAS_SOURCE_PATH");
        if (gb_home != nullptr) return std::string(gb_home);
        else return std::string();
    }


// Default `GRAPHBLAS_CACHE_PATH` to `$HOME/.GraphBLAS`.
// This definition can be overridden at compile time by specifying a
// `-DGRAPHBLAS_CACHE_PATH=/kernel/cache/path` CMake argument.
// This path is used in the `getCacheDir()` function below.
#if !defined(GRAPHBLAS_CACHE_PATH)
#define GRAPHBLAS_CACHE_PATH  get_user_home_cache_dir()
#endif

/**
 * @brief Get the string path to the JITIFY kernel cache directory.
 *
 * This path can be overridden at runtime by defining an environment variable
 * named `GRAPHBLAS_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 is `$HOME/.GraphBLAS`. If no overrides
 * are used and if $HOME is not defined, returns an empty path and file 
 * caching is not used.
 **/
std::string getCacheDir() {
  // The environment variable always overrides the
  // default/compile-time value of `GRAPHBLAS_CACHE_PATH`
  auto kernel_cache_path_env = std::getenv("GRAPHBLAS_CACHE_PATH");
  auto kernel_cache_path = (kernel_cache_path_env != nullptr ? kernel_cache_path_env
                                       : GRAPHBLAS_CACHE_PATH);

  struct stat st;
  if ( (stat( kernel_cache_path.c_str(), &st) != 0) ) {
    // `mkdir -p` the kernel cache path if it doesn't exist
//    printf("cache is going to path %s\n", kernel_cache_path.c_str());
    int status;
    status = std::filesystem::create_directories(kernel_cache_path.c_str());
//    if (status != 0 ) return std::string();
    //boost::filesystem::create_directories(kernel_cache_path);
  }
  return std::string(kernel_cache_path);
}

GBJitCache::GBJitCache() { }

GBJitCache::~GBJitCache() { }


//void GBJitCache::macrofy() {
//    printf("GOT HERE and shouldn't have!\n");
//}


std::mutex GBJitCache::_kernel_cache_mutex;
std::mutex GBJitCache::_program_cache_mutex;

std::string GBJitCache::getFile(
    File_Desc &file_object )
{
    // Lock for thread safety
    std::lock_guard<std::mutex> lock(_program_cache_mutex);

    // Macrofied version
    auto cached_file = getCachedFile( file_object, file_map );
    return *std::get<1>( cached_file ).get();
}

named_prog<jitify::experimental::Program> GBJitCache::getProgram(
    std::string const& prog_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)
{
    // Lock for thread safety
    std::lock_guard<std::mutex> lock(_program_cache_mutex);
//    printf(" jit_cache get program %s\n", prog_name.c_str());

    return getCached(prog_name, program_map, 
        [&](){
            return jitify::experimental::Program(cuda_source,
                                        given_headers,
                                        given_options,
                                        file_callback);
        }
    );
}

named_prog<jitify::experimental::KernelInstantiation> GBJitCache::getKernelInstantiation(
    std::string const& kern_name,
    named_prog<jitify::experimental::Program> const& named_program,
    std::vector<std::string> const& arguments)
{
    // Lock for thread safety
    std::lock_guard<std::mutex> lock(_kernel_cache_mutex);

    std::string prog_name = std::get<0>(named_program);
    jitify::experimental::Program& program = *std::get<1>(named_program);

    // Make instance name e.g. "prog_binop.kernel_v_v_int_int_long int_Add"
    std::string kern_inst_name = kern_name;
    for ( auto&& arg : arguments ) kern_inst_name += '_' + arg;

//    printf(" got kernel instance %s\n",kern_inst_name.c_str());

    return getCached(kern_inst_name, kernel_inst_map, 
        [&](){return program.kernel(kern_name)
                            .instantiate(arguments);
        }
    );
}

// Another overload for getKernelInstantiation which might be useful to get
// kernel instantiations in one step
// ------------------------------------------------------------------------
/*
jitify::experimental::KernelInstantiation GBJitCache::getKernelInstantiation(
    std::string const& kern_name,
    std::string const& prog_name,
    std::string const& cuda_source = "",
    std::vector<std::string> const& given_headers = {},
    std::vector<std::string> const& given_options = {},
    file_callback_type file_callback = nullptr)
{
    auto program = getProgram(prog_name,
                              cuda_source,
                              given_headers,
                              given_options,
                              file_callback);
    return getKernelInstantiation(kern_name, program);
}
*/

GBJitCache::cacheFile::cacheFile(std::string file_name)
 : _file_name{file_name}
{ }

GBJitCache::cacheFile::~cacheFile() { }

std::string GBJitCache::cacheFile::read_file()
{
    // Open file (duh)
    int fd = open ( _file_name.c_str(), O_RDWR );
    if ( fd == -1 ) {
        // TODO: connect errors to GrB_error result
//        printf(" failed to open cache file %s\n",_file_name.c_str());
        successful_read = false;
        return std::string();
    }

    // Lock the file descriptor. we the only ones now
    if ( lockf(fd, F_LOCK, 0) == -1 ) {
        successful_read = false;
        return std::string();
    }

    // Get file descriptor from file pointer
    FILE *fp = fdopen( fd, "rb" );

    // Get file length
    fseek( fp , 0L , SEEK_END);
    size_t file_size = ftell( fp );
    rewind( fp );

    // Allocate memory of file length size
    std::string content;
    content.resize(file_size);

    char *buffer = content.data();

    // Copy file into buffer
    if( fread(buffer, file_size, 1, fp) != 1 ) {
        //printf(" failed to read cache file %s\n",_file_name.c_str());
        successful_read = false;
        fclose(fp);
//        free(buffer); FIXME: Shouldn't need to free buffer since it's RAII
        return content; // FIXME: use unique_ptr here
    }

//    printf("about to close\n");
    fclose(fp);
    successful_read = true;
//    printf(" read cache file %s\n",_file_name.c_str());

    return content;
}

void GBJitCache::cacheFile::write(std::string content)
{
    // Open file and create if it doesn't exist, with access 0600
    int fd = open ( _file_name.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR );
    if ( fd == -1 ) {
//        printf(" failed to open cache file for write %s\n",_file_name.c_str());
        successful_write = false;
        return;
    }

    // Lock the file descriptor. we the only ones now
    if ( lockf(fd, F_LOCK, 0) == -1 ) {
        successful_write = false;
        return;
    }

    // Get file descriptor from file pointer
    FILE *fp = fdopen( fd, "wb" );

    // Copy string into file
    if( fwrite(content.c_str(), content.length(), 1, fp) != 1 ) {
//        printf(" failed to write cache file %s\n",_file_name.c_str());
        successful_write = false;
        fclose(fp);
        return;
    }
    fclose(fp);

    successful_write = true;
    //printf(" wrote cache file %s\n",_file_name.c_str());
    
    return;
}

} // namespace jit