Skip to content

Commit

Permalink
Add alternate paths for the tuning file on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
lealgo committed Jun 4, 2019
1 parent 4173b92 commit c933b1a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cross-files/aarch64-linux-android
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ endian = 'little'

[properties]
needs_exe_wrapper = true
cpp_args = ['-Dandroid']
cpp_args = []
cpp_link_args = ['-llog', '-static-libstdc++']

[binaries]
Expand Down
2 changes: 1 addition & 1 deletion cross-files/armv7a-linux-androideabi
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ endian = 'little'

[properties]
needs_exe_wrapper = true
cpp_args = ['-Dandroid']
cpp_args = []
cpp_link_args = ['-llog', '-static-libstdc++']

[binaries]
Expand Down
36 changes: 27 additions & 9 deletions src/neural/opencl/OpenCLTuner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,16 @@
#include <random>
#include <sstream>
#include <string>
#include <unistd.h>

#include "neural/opencl/OpenCL.h"
#include "neural/opencl/OpenCLParams.h"
#include "neural/opencl/OpenCLTuner.h"

#include "utils/logging.h"
#include "utils/commandline.h"

// Use the temporary directory on Android as the engine may run with restricted file access
#ifdef android
const auto TUNER_FILE_LOCAL = std::string("/data/local/tmp/leelaz_opencl_tuning");
#else
const auto TUNER_FILE_LOCAL = std::string("leelaz_opencl_tuning");
#endif

constexpr auto MAX_ERROR = 1e-4f;

Expand Down Expand Up @@ -372,20 +369,41 @@ std::string Tuner::tune_sgemm(const int m, const int n, const int k,
return best_params;
}

const std::string leelaz_file(std::string file) {
// On Android, the engine may run with restricted file access. The ideal place to store
// the tuning file should be the Android Context.getFilesDir(). But such context is hard
// to get from here. As an alternative, the current directory is tried if it's writeable.
// Else the binary directory is used, hoping that the engine was installed to a writable
// directory.
#ifdef __ANDROID__
std::string path;
char buffer[PATH_MAX];
const char* current_directory = getcwd(buffer, sizeof(buffer));
if (access(current_directory, W_OK) == 0) {
path = current_directory;
} else {
path = lczero::CommandLine::BinaryDirectory();
}
return path + "/" + file;
#else
return file;
#endif
}

void Tuner::store_sgemm_tuners(const int m, const int n, const int k,
const int batch_size, std::string tuners) {
auto file_contents = std::vector<std::string>();
{
// Read the previous contents to string.
auto file = std::ifstream{TUNER_FILE_LOCAL};
auto file = std::ifstream{leelaz_file(TUNER_FILE_LOCAL)};
if (file.good()) {
auto line = std::string{};
while (std::getline(file, line)) {
file_contents.emplace_back(line);
}
}
}
auto file = std::ofstream{TUNER_FILE_LOCAL};
auto file = std::ofstream{leelaz_file(TUNER_FILE_LOCAL)};

auto device_name = m_opencl.get_device_name();
auto tuning_params = std::stringstream{};
Expand All @@ -409,7 +427,7 @@ void Tuner::store_sgemm_tuners(const int m, const int n, const int k,

if (file.fail()) {
CERR << "Could not save the tuning result.";
CERR << "Do I have write permissions on " << TUNER_FILE_LOCAL << "?";
CERR << "Do I have write permissions on " << leelaz_file(TUNER_FILE_LOCAL) << "?";
}
}

Expand Down Expand Up @@ -462,7 +480,7 @@ std::string Tuner::sgemm_tuners_from_line(std::string line, const int m,
std::string Tuner::load_sgemm_tuners(const int m, const int n, const int k,
const int batch_size) {
if (!m_params.force_tune) {
auto file = std::ifstream{TUNER_FILE_LOCAL};
auto file = std::ifstream{leelaz_file(TUNER_FILE_LOCAL)};
if (file.good()) {
auto line = std::string{};
while (std::getline(file, line)) {
Expand Down

0 comments on commit c933b1a

Please sign in to comment.