Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveBronder committed Jan 20, 2018
1 parent 7b27fe9 commit 65d755f
Showing 1 changed file with 33 additions and 32 deletions.
65 changes: 33 additions & 32 deletions stan/math/prim/mat/fun/ocl_gpu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

#define __CL_ENABLE_EXCEPTIONS

#include <stan/math/prim/arr/err/check_opencl.hpp>
#include <CL/cl.hpp>
#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
#include <iostream>
#include <map>
#include <stan/math/prim/arr/err/check_opencl.hpp>
#include <string>
#include <vector>

#ifdef STAN_DEVICE_CPU
Expand Down Expand Up @@ -77,20 +78,20 @@ inline void init_kernel_groups() {

kernel_strings["timing"] = dummy_kernel;
kernel_strings["check_gpu"] =
#include <stan/math/prim/mat/kern_gpu/check_gpu.cl> // NOLINT
; // NOLINT
#include <stan/math/prim/mat/kern_gpu/check_gpu.cl> // NOLINT
; // NOLINT
kernel_strings["cholesky_decomposition"] =
#include <stan/math/prim/mat/kern_gpu/cholesky_decomposition.cl> // NOLINT
; // NOLINT
#include <stan/math/prim/mat/kern_gpu/cholesky_decomposition.cl> // NOLINT
; // NOLINT
kernel_strings["matrix_inverse"] =
#include <stan/math/prim/mat/kern_gpu/matrix_inverse.cl> // NOLINT
; // NOLINT
#include <stan/math/prim/mat/kern_gpu/matrix_inverse.cl> // NOLINT
; // NOLINT
kernel_strings["matrix_multiply"] =
#include <stan/math/prim/mat/kern_gpu/matrix_multiply.cl> // NOLINT
; // NOLINT
#include <stan/math/prim/mat/kern_gpu/matrix_multiply.cl> // NOLINT
; // NOLINT
kernel_strings["basic_matrix"] =
#include <stan/math/prim/mat/kern_gpu/basic_matrix.cl> // NOLINT
; // NOLINT
#include <stan/math/prim/mat/kern_gpu/basic_matrix.cl> // NOLINT
; // NOLINT

// Check if the kernels were already compiled
compiled_kernels["basic_matrix"] = false;
Expand All @@ -112,7 +113,7 @@ inline void init_kernel_groups() {
*
*/
class ocl {
private:
private:
std::string description_;
cl::Context oclContext_;
cl::CommandQueue oclQueue_;
Expand All @@ -137,9 +138,9 @@ class ocl {
exit(1);
}
oclDevice_ = allDevices[0];
description_ = "Device " + oclDevice_.getInfo<CL_DEVICE_NAME>()
+ " on the platform "
+ oclPlatform_.getInfo<CL_PLATFORM_NAME>();
description_ = "Device " + oclDevice_.getInfo<CL_DEVICE_NAME>() +
" on the platform " +
oclPlatform_.getInfo<CL_PLATFORM_NAME>();
allDevices[0].getInfo<size_t>(CL_DEVICE_MAX_WORK_GROUP_SIZE,
&max_workgroup_size);
oclContext_ = cl::Context(allDevices);
Expand All @@ -155,33 +156,33 @@ class ocl {
program_.build(allDevices);
kernels["dummy"] = cl::Kernel(program_, "dummy", NULL);
compiled_kernels["timing"] = true;
} catch (const cl::Error& e) {
} catch (const cl::Error &e) {
std::cout << "Building failed, " << e.what() << "(" << e.err() << ")"
<< "\nRetrieving build log\n"
<< program_.getBuildInfo<CL_PROGRAM_BUILD_LOG>(allDevices[0]);
}
} catch (const cl::Error& e) {
} catch (const cl::Error &e) {
check_ocl_error("build", e);
}
}

public:
public:
inline std::string description() const {
if (initialized_ == 1) {
return description_;
}
return "No device selected yet.";
}

inline cl::Context& context() {
inline cl::Context &context() {
if (initialized_ == 0) {
init();
initialized_ = 1;
}
return oclContext_;
}

inline cl::CommandQueue& queue() {
inline cl::CommandQueue &queue() {
if (initialized_ == 0) {
init();
initialized_ = 1;
Expand All @@ -206,7 +207,7 @@ inline std::string get_description() { return ocl_context_queue.description(); }
* a new context is created.
*
*/
inline cl::Context& get_context() { return ocl_context_queue.context(); }
inline cl::Context &get_context() { return ocl_context_queue.context(); }
/**
* Returns the reference to the active
* OpenCL command queue. If no context
Expand All @@ -215,7 +216,7 @@ inline cl::Context& get_context() { return ocl_context_queue.context(); }
* the reference to the new queue is returned.
*
*/
inline cl::CommandQueue& get_queue() { return ocl_context_queue.queue(); }
inline cl::CommandQueue &get_queue() { return ocl_context_queue.queue(); }
/**
* Returns the reference to the active
* OpenCL command queue. If no context
Expand All @@ -235,7 +236,7 @@ inline int get_maximum_workgroup_size() {
*
*/
inline void compile_kernel_group(std::string group) {
cl::Context& ctx = get_context();
cl::Context &ctx = get_context();
std::vector<cl::Device> devices = ctx.getInfo<CL_CONTEXT_DEVICES>();
std::string kernel_source = kernel_strings[group];
cl::Program::Sources source(
Expand All @@ -257,15 +258,15 @@ inline void compile_kernel_group(std::string group) {
cl_int err = CL_SUCCESS;
// Iterate over the kernel list
// and get all the kernels from this group
for (std::map<std::string, std::string>::iterator it
= kernel_groups.begin();
for (std::map<std::string, std::string>::iterator it =
kernel_groups.begin();
it != kernel_groups.end(); ++it) {
if (group.compare((it->second).c_str()) == 0) {
kernels[(it->first).c_str()]
= cl::Kernel(program_, (it->first).c_str(), &err);
kernels[(it->first).c_str()] =
cl::Kernel(program_, (it->first).c_str(), &err);
}
}
} catch (const cl::Error& e) {
} catch (const cl::Error &e) {
std::cout << "Building failed, " << e.what() << "(" << e.err() << ")"
<< "\nRetrieving build log\n"
<< program_.getBuildInfo<CL_PROGRAM_BUILD_LOG>(devices[0]);
Expand All @@ -289,7 +290,7 @@ inline cl::Kernel get_kernel(std::string name) {
return kernels[name];
}

} // namespace math
} // namespace stan
} // namespace math
} // namespace stan

#endif

0 comments on commit 65d755f

Please sign in to comment.