Skip to content

Be able to share compiled OpenCL binary caches among identical GPUs #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Baikal/Controllers/clw_scene_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ namespace Baikal
}


Material::Ptr ClwSceneController::s_default_material = UberV2Material::Create();

ClwSceneController::ClwSceneController(CLWContext context, RadeonRays::IntersectionApi* api, const CLProgramManager *program_manager)
: m_context(context)
, m_api(api)
, m_default_material(UberV2Material::Create())
, m_program_manager(program_manager)
{
auto acc_type = "fatbvh";
Expand All @@ -72,7 +73,7 @@ namespace Baikal

Material::Ptr ClwSceneController::GetDefaultMaterial() const
{
return m_default_material;
return s_default_material;
}

ClwSceneController::~ClwSceneController()
Expand Down Expand Up @@ -1275,7 +1276,7 @@ namespace Baikal

int ClwSceneController::GetMaterialIndex(Collector const& collector, Material::Ptr material) const
{
auto m = material ? material : m_default_material;
auto m = material ? material : s_default_material;
return ResolveMaterialPtr(m);
}

Expand Down Expand Up @@ -1392,7 +1393,7 @@ namespace Baikal

int ClwSceneController::GetMaterialLayers(Material::Ptr material) const
{
auto m = material ? material : m_default_material;
auto m = material ? material : s_default_material;
return std::static_pointer_cast<UberV2Material>(m)->GetLayers();
}

Expand Down
2 changes: 1 addition & 1 deletion Baikal/Controllers/clw_scene_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ namespace Baikal
// Intersection API
RadeonRays::IntersectionApi* m_api;
// Default material
Material::Ptr m_default_material;
static Material::Ptr s_default_material;
// CL Program manager
const CLProgramManager *m_program_manager;
// Material to device material map
Expand Down
24 changes: 23 additions & 1 deletion Baikal/Utils/cl_program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ inline void SaveBinaries(std::string const& name, std::vector<std::uint8_t>& dat
}


std::unordered_map<std::string, std::shared_ptr<std::mutex>> CLProgram::s_binary_cache_names;
std::mutex CLProgram::s_binary_cache_map_mutex;

CLProgram::CLProgram(const CLProgramManager *program_manager, uint32_t id, CLWContext context,
const std::string &program_name, const std::string &cache_path) :
m_program_manager(program_manager),
Expand Down Expand Up @@ -246,8 +249,24 @@ CLWProgram CLProgram::GetCLWProgram(const std::string &opts)
cached_program_path.append(".bin");

std::vector<std::uint8_t> binary;
if (LoadBinaries(cached_program_path, binary))

std::unique_lock<std::mutex> map_lock(s_binary_cache_map_mutex);
auto iter = s_binary_cache_names.find(filename);
if (iter == s_binary_cache_names.end())
iter = s_binary_cache_names.insert({ filename, std::make_shared<std::mutex>() }).first;
auto cache_mutex = iter->second;
assert(cache_mutex != nullptr);
map_lock.unlock();

// Other workers with the same binary cache requirement will be blocked here
std::unique_lock<std::mutex> cache_lock(*cache_mutex);
bool loaded = LoadBinaries(cached_program_path, binary);

if (loaded)
{
// Binary cache existed, other workers can read it
cache_lock.unlock();

// Create from binary
std::size_t size = binary.size();
auto binaries = &binary[0];
Expand All @@ -262,6 +281,9 @@ CLWProgram CLProgram::GetCLWProgram(const std::string &opts)
// Save binaries
result.GetBinaries(0, binary);
SaveBinaries(cached_program_path, binary);

// Block other workers until binary cache generated
cache_lock.unlock();
Copy link
Contributor

@dtarakanov1 dtarakanov1 Aug 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unlock is excessive since the cache_lock goes out of scope right after the call.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

}
}

Expand Down
3 changes: 3 additions & 0 deletions Baikal/Utils/cl_program.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ THE SOFTWARE.
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <mutex>
#include "CLWProgram.h"
#include "CLWContext.h"

Expand Down Expand Up @@ -95,5 +96,7 @@ namespace Baikal
CLWContext m_context;
std::set<std::string> m_included_headers; ///< Set of included headers

static std::unordered_map<std::string, std::shared_ptr<std::mutex>> s_binary_cache_names;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a part of the class interface, so it might be moved into the .cpp file.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved into cl_program.cpp

static std::mutex s_binary_cache_map_mutex;
};
}