-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
cmake_minimum_required(VERSION 3.9) | ||
project(singleton) | ||
|
||
set(CMAKE_CXX_STANDARD 11) | ||
|
||
add_executable(singleton main.cpp ComputeSingleton.h ComputeSingleton.cpp CPUCompute.h GPUCompute.h CPUCompute.cpp GPUCompute.cpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#include "CPUCompute.h" | ||
|
||
CPUCompute::CPUCompute() { | ||
} | ||
|
||
std::shared_ptr<CPUCompute> CPUCompute::get_shared_ptr() { | ||
return std::static_pointer_cast<CPUCompute>(shared_from_this()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#pragma once | ||
|
||
#include "ComputeSingleton.h" | ||
|
||
class CPUCompute : public ComputeSingleton { | ||
public: | ||
CPUCompute(); | ||
std::shared_ptr<CPUCompute> get_shared_ptr(); | ||
private: | ||
//memory or other operations | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <cstdlib> | ||
#include "ComputeSingleton.h" | ||
|
||
std::shared_ptr<ComputeSingleton> ComputeSingleton::instance_; | ||
|
||
std::map<std::string, std::shared_ptr<ComputeSingleton>> ComputeSingleton::registry_; | ||
|
||
void ComputeSingleton::Register(const std::string &name, std::shared_ptr<ComputeSingleton> compute) { | ||
if (registry_.find(name) == registry_.end()) { | ||
registry_.emplace(std::make_pair(name, compute)); | ||
} | ||
} | ||
|
||
std::shared_ptr<ComputeSingleton> ComputeSingleton::Instance() { | ||
if (instance_ == nullptr) { | ||
// std::string compute_library_name = std::getenv("ComputeLibrary"); | ||
std::string compute_library_name = "CPU"; | ||
instance_ = Lookup(compute_library_name); | ||
} | ||
return instance_; | ||
} | ||
|
||
std::shared_ptr<ComputeSingleton> ComputeSingleton::Lookup(const std::string &name) { | ||
auto search = registry_.find(name); | ||
if (search != registry_.end()) { | ||
return search->second; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#pragma once | ||
|
||
#include <map> | ||
#include <memory> | ||
#include <string> | ||
|
||
class ComputeSingleton : public std::enable_shared_from_this<ComputeSingleton> { | ||
public: | ||
static void Register(const std::string& name, std::shared_ptr<ComputeSingleton>); | ||
static std::shared_ptr<ComputeSingleton> Instance(); | ||
protected: | ||
static std::shared_ptr<ComputeSingleton> Lookup(const std::string& name); | ||
private: | ||
static std::shared_ptr<ComputeSingleton> instance_; | ||
static std::map<std::string, std::shared_ptr<ComputeSingleton>> registry_; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include "GPUCompute.h" | ||
|
||
GPUCompute::GPUCompute() { | ||
} | ||
|
||
std::shared_ptr<GPUCompute> GPUCompute::get_shared_ptr() { | ||
return std::static_pointer_cast<GPUCompute>(shared_from_this()); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#pragma once | ||
|
||
#include "ComputeSingleton.h" | ||
|
||
class GPUCompute : public ComputeSingleton { | ||
public: | ||
GPUCompute(); | ||
std::shared_ptr<GPUCompute> get_shared_ptr(); | ||
private: | ||
//memory or other operations | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <cstdlib> | ||
#include <iostream> | ||
#include "CPUCompute.h" | ||
|
||
int main() { | ||
|
||
// std::setenv(); | ||
std::shared_ptr<CPUCompute> cpu_compute(new CPUCompute()); | ||
ComputeSingleton::Register("CPU" ,cpu_compute->get_shared_ptr()); | ||
|
||
std::shared_ptr<CPUCompute> gpu_compute(new CPUCompute()); | ||
ComputeSingleton::Register("GPU" ,cpu_compute->get_shared_ptr()); | ||
|
||
auto compute_library = ComputeSingleton::Instance(); | ||
|
||
return 0; | ||
} |