Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from clEsperanto/add_custom_kernel
Add custom kernel
- Loading branch information
Showing
4 changed files
with
244 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,39 @@ | ||
#ifndef __TIER1_CLECUSTOMKERNEL_HPP | ||
#define __TIER1_CLECUSTOMKERNEL_HPP | ||
|
||
#include "cleOperation.hpp" | ||
|
||
namespace cle | ||
{ | ||
|
||
class CustomKernel : public Operation | ||
{ | ||
public: | ||
explicit CustomKernel(const ProcessorPointer & device, | ||
const std::string & filename, | ||
const std::string & name, | ||
const size_t & nb_parameters); | ||
|
||
auto | ||
AddImages(const std::map<std::string, Image> & images) -> void; | ||
|
||
auto | ||
AddScalars(const std::map<std::string, float> & scalars) -> void; | ||
}; | ||
|
||
inline auto | ||
CustomKernel_Call(const std::shared_ptr<cle::Processor> & device, | ||
const std::string & filename, | ||
const std::string & name, | ||
const std::map<std::string, Image> & images, | ||
const std::map<std::string, float> & scalars) -> void | ||
{ | ||
CustomKernel kernel(device, filename, name, images.size() + scalars.size()); | ||
kernel.AddImages(images); | ||
kernel.AddScalars(scalars); | ||
kernel.Execute(); | ||
} | ||
|
||
} // namespace cle | ||
|
||
#endif // __TIER1_CLECUSTOMKERNEL_HPP |
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,42 @@ | ||
#include "cleCustomKernel.hpp" | ||
|
||
#include <fstream> | ||
#include <string> | ||
|
||
namespace cle | ||
{ | ||
|
||
CustomKernel::CustomKernel(const ProcessorPointer & device, | ||
const std::string & filename, | ||
const std::string & name, | ||
const size_t & nb_parameters) | ||
: Operation(device, nb_parameters) | ||
{ | ||
std::ifstream ifs(filename); | ||
if (ifs.fail()) | ||
{ | ||
std::cout << "Error in loading custom kernel, could not find file " << filename << std::endl; | ||
} | ||
std::string ocl_source((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>())); | ||
this->SetSource(name, ocl_source); | ||
} | ||
|
||
auto | ||
CustomKernel::AddImages(const std::map<std::string, Image> & images) -> void | ||
{ | ||
for (auto ite = images.begin(); ite != images.end(); ite++) | ||
{ | ||
this->AddParameter(ite->first, ite->second); | ||
} | ||
} | ||
|
||
auto | ||
CustomKernel::AddScalars(const std::map<std::string, float> & scalars) -> void | ||
{ | ||
for (auto ite = scalars.begin(); ite != scalars.end(); ite++) | ||
{ | ||
this->AddParameter(ite->first, ite->second); | ||
} | ||
} | ||
|
||
} // namespace cle |
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
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,155 @@ | ||
|
||
|
||
#include <random> | ||
|
||
#include "cleCustomKernel.hpp" | ||
#include "clesperanto.hpp" | ||
|
||
template <class type> | ||
auto | ||
run_test(const std::array<size_t, 3> & shape, const cle::MemoryType & mem_type, const std::string & path) -> bool | ||
{ | ||
type value = static_cast<type>(rand() % 10); | ||
std::vector<type> input(shape[0] * shape[1] * shape[2]); | ||
std::vector<type> valid(shape[0] * shape[1] * shape[2]); | ||
std::fill(input.begin(), input.end(), -value); | ||
std::fill(valid.begin(), valid.end(), value); | ||
|
||
cle::Clesperanto cle; | ||
cle.GetDevice()->WaitForKernelToFinish(); | ||
|
||
auto gpu_input = cle.Push<type>(input, shape, mem_type); | ||
auto gpu_output = cle.Create<type>(shape, mem_type); | ||
|
||
cle::CustomKernel_Call(cle.GetDevice(), path, "absolute", { { "src", gpu_input }, { "dst", gpu_output } }, {}); | ||
|
||
auto output = cle.Pull<type>(gpu_output); | ||
|
||
return std::equal(output.begin(), output.end(), valid.begin()); | ||
} | ||
|
||
auto | ||
main(int argc, char ** argv) -> int | ||
{ | ||
if (!run_test<float>({ 10, 1, 1 }, cle::BUFFER, argv[1])) | ||
{ | ||
return EXIT_FAILURE; | ||
} | ||
|
||
// if (!run_test<signed int>({ 10, 1, 1 }, cle::BUFFER)) | ||
// { | ||
// return EXIT_FAILURE; | ||
// } | ||
|
||
// if (!run_test<signed short>({ 10, 1, 1 }, cle::BUFFER)) | ||
// { | ||
// return EXIT_FAILURE; | ||
// } | ||
|
||
// if (!run_test<signed char>({ 10, 1, 1 }, cle::BUFFER)) | ||
// { | ||
// return EXIT_FAILURE; | ||
// } | ||
|
||
// if (!run_test<float>({ 10, 5, 1 }, cle::BUFFER)) | ||
// { | ||
// return EXIT_FAILURE; | ||
// } | ||
|
||
// if (!run_test<signed int>({ 10, 5, 1 }, cle::BUFFER)) | ||
// { | ||
// return EXIT_FAILURE; | ||
// } | ||
|
||
// if (!run_test<signed short>({ 10, 5, 1 }, cle::BUFFER)) | ||
// { | ||
// return EXIT_FAILURE; | ||
// } | ||
|
||
// if (!run_test<signed char>({ 10, 5, 1 }, cle::BUFFER)) | ||
// { | ||
// return EXIT_FAILURE; | ||
// } | ||
|
||
// if (!run_test<float>({ 10, 5, 3 }, cle::BUFFER)) | ||
// { | ||
// return EXIT_FAILURE; | ||
// } | ||
|
||
// if (!run_test<signed int>({ 10, 5, 3 }, cle::BUFFER)) | ||
// { | ||
// return EXIT_FAILURE; | ||
// } | ||
|
||
// if (!run_test<signed short>({ 10, 5, 3 }, cle::BUFFER)) | ||
// { | ||
// return EXIT_FAILURE; | ||
// } | ||
|
||
// if (!run_test<signed char>({ 10, 5, 3 }, cle::BUFFER)) | ||
// { | ||
// return EXIT_FAILURE; | ||
// } | ||
|
||
// if (!run_test<float>({ 10, 1, 1 }, cle::IMAGE)) | ||
// { | ||
// return EXIT_FAILURE; | ||
// } | ||
|
||
// if (!run_test<signed int>({ 10, 1, 1 }, cle::IMAGE)) | ||
// { | ||
// return EXIT_FAILURE; | ||
// } | ||
|
||
// if (!run_test<signed short>({ 10, 1, 1 }, cle::IMAGE)) | ||
// { | ||
// return EXIT_FAILURE; | ||
// } | ||
|
||
// if (!run_test<signed char>({ 10, 1, 1 }, cle::IMAGE)) | ||
// { | ||
// return EXIT_FAILURE; | ||
// } | ||
|
||
// if (!run_test<float>({ 10, 5, 1 }, cle::IMAGE)) | ||
// { | ||
// return EXIT_FAILURE; | ||
// } | ||
|
||
// if (!run_test<signed int>({ 10, 5, 1 }, cle::IMAGE)) | ||
// { | ||
// return EXIT_FAILURE; | ||
// } | ||
|
||
// if (!run_test<signed short>({ 10, 5, 1 }, cle::IMAGE)) | ||
// { | ||
// return EXIT_FAILURE; | ||
// } | ||
|
||
// if (!run_test<signed char>({ 10, 5, 1 }, cle::IMAGE)) | ||
// { | ||
// return EXIT_FAILURE; | ||
// } | ||
|
||
// if (!run_test<float>({ 10, 5, 3 }, cle::IMAGE)) | ||
// { | ||
// return EXIT_FAILURE; | ||
// } | ||
|
||
// if (!run_test<signed int>({ 10, 5, 3 }, cle::IMAGE)) | ||
// { | ||
// return EXIT_FAILURE; | ||
// } | ||
|
||
// if (!run_test<signed short>({ 10, 5, 3 }, cle::IMAGE)) | ||
// { | ||
// return EXIT_FAILURE; | ||
// } | ||
|
||
// if (!run_test<signed char>({ 10, 5, 3 }, cle::IMAGE)) | ||
// { | ||
// return EXIT_FAILURE; | ||
// } | ||
|
||
return EXIT_SUCCESS; | ||
} |