Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
inakleinbottle committed Oct 18, 2023
1 parent 37dafb1 commit 80eae50
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 187 deletions.
5 changes: 4 additions & 1 deletion device/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ add_roughpy_component(Device
src/kernel_launch_params.cpp
src/queue.cpp
src/queue_interface.cpp
DEPENDENCIES
PRIVATE
OpenCL::OpenCL
NEEDS
RoughPy::Core
RoughPy::Platform
)
)
4 changes: 2 additions & 2 deletions device/src/cpu/cpu_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ CPUDeviceHandle::CPUDeviceHandle() : p_ocl_handle(nullptr)
CPUDeviceHandle::~CPUDeviceHandle() = default;

CPUDevice CPUDeviceHandle::get() {
static const CPUDeviceHandle device;
return &device;
static boost::intrusive_ptr<CPUDeviceHandle> device(new CPUDeviceHandle);
return device;
}


Expand Down
4 changes: 2 additions & 2 deletions device/src/get_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ Device rpy::devices::get_device(const rpy::devices::DeviceSpecification& spec)
return CPUDeviceHandle::get();
}

Device get_cpu_device() { return CPUDeviceHandle::get(); }
Device get_default_device() { return CPUDeviceHandle::get(); }
Device rpy::devices::get_cpu_device() { return CPUDeviceHandle::get(); }
Device rpy::devices::get_default_device() { return CPUDeviceHandle::get(); }
107 changes: 52 additions & 55 deletions device/src/opencl/ocl_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,71 +111,68 @@ OCLDeviceHandle::OCLDeviceHandle(cl_device_id id) : m_device(id)
}

const auto& config = get_config();
fs::directory_iterator iter(config.get_builtin_kernel_dir());
auto kernel_dir = config.get_builtin_kernel_dir();
if (exists(kernel_dir)) {
fs::directory_iterator iter(config.get_builtin_kernel_dir());

std::vector<string> sources;
for (auto&& dir : iter) {
std::ifstream istr(dir.path());
sources.emplace_back();
}
auto count = static_cast<cl_uint>(sources.size());

std::vector<const char*> strings;
std::vector<size_t> sizes;
strings.reserve(count);
sizes.reserve(count);
for (auto&& src : sources) {
strings.push_back(src.data());
sizes.push_back(src.size());
}

std::vector<string> sources;
for (auto&& dir : iter) {
std::ifstream istr(dir.path());
sources.emplace_back();
}
auto count = static_cast<cl_uint>(sources.size());

std::vector<const char*> strings;
std::vector<size_t> sizes;
strings.reserve(count);
sizes.reserve(count);
for (auto&& src : sources) {
strings.push_back(src.data());
sizes.push_back(src.size());
}
auto program = clCreateProgramWithSource(
m_ctx,
count,
strings.data(),
sizes.data(),
&ecode
);

auto program = clCreateProgramWithSource(
m_ctx,
count,
strings.data(),
sizes.data(),
&ecode
);
if (RPY_UNLIKELY(program == nullptr)) { RPY_HANDLE_OCL_ERROR(ecode); }

if (RPY_UNLIKELY(program == nullptr)) { RPY_HANDLE_OCL_ERROR(ecode); }
m_programs.push_back(program);

m_programs.push_back(program);

cl_uint num_kernels;
ecode = clGetProgramInfo(
program,
CL_PROGRAM_NUM_KERNELS,
sizeof(cl_uint),
&num_kernels,
nullptr
);

if (RPY_UNLIKELY(ecode != CL_SUCCESS)) { RPY_HANDLE_OCL_ERROR(ecode); }

std::vector<cl_kernel> kernels(num_kernels);
ecode = clCreateKernelsInProgram(
program,
num_kernels,
kernels.data(),
nullptr
);
cl_uint num_kernels;
ecode = clGetProgramInfo(
program,
CL_PROGRAM_NUM_KERNELS,
sizeof(cl_uint),
&num_kernels,
nullptr
);

if (RPY_UNLIKELY(ecode != CL_SUCCESS)) { RPY_HANDLE_OCL_ERROR(ecode); }
if (RPY_UNLIKELY(ecode != CL_SUCCESS)) { RPY_HANDLE_OCL_ERROR(ecode); }

char* kname;
dimn_t kname_len;
for (auto&& kernel : kernels) {
ecode = clGetKernelInfo(
kernel,
CL_KERNEL_FUNCTION_NAME,
sizeof(kname),
&kname,
&kname_len
std::vector<cl_kernel> kernels(num_kernels);
ecode = clCreateKernelsInProgram(
program,
num_kernels,
kernels.data(),
nullptr
);

if (RPY_UNLIKELY(ecode != CL_SUCCESS)) { RPY_HANDLE_OCL_ERROR(ecode); }

m_kernels[string(kname, kname_len)] = kernel;

for (auto&& kernel : kernels) {
auto kname = cl::string_info(clGetKernelInfo, kernel,
CL_KERNEL_FUNCTION_NAME);
m_kernels[kname] = kernel;
}
}


}

OCLDeviceHandle::~OCLDeviceHandle()
Expand Down
6 changes: 4 additions & 2 deletions scalars/include/roughpy/scalars/scalar_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ class RPY_EXPORT ScalarType
ScalarTypeInfo m_info;
RingCharacteristics m_characteristics;

devices::Device m_device;

protected:
/**
* @brief Constructor for Scalar types, must be called by derived types
* @param info Scalar type info
*/
explicit ScalarType(ScalarTypeInfo info);
explicit ScalarType(ScalarTypeInfo info, devices::Device&& device);

public:
template <typename T>
Expand Down Expand Up @@ -144,7 +146,7 @@ class RPY_EXPORT ScalarType
* @brief Get the underlying device for this type.
* @return Pointer to device handle
*/
RPY_NO_DISCARD virtual devices::Device device() const noexcept = 0;
RPY_NO_DISCARD devices::Device device() const noexcept;


/**
Expand Down
6 changes: 4 additions & 2 deletions scalars/src/scalar_implementations/rational/RationalType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

#include "RationalType.h"

#include <roughpy/device/device_handle.h>

#include <algorithm>
#include <cmath>
#include <ostream>
Expand Down Expand Up @@ -73,8 +75,8 @@ RationalType::RationalType()
ScalarTypeCode::OpaqueHandle,
0, 0,
},
{ DeviceType::CPU, 0 }
})
{ devices::DeviceType::CPU, 0 }
}, devices::get_cpu_device())
{}
ScalarPointer RationalType::allocate(std::size_t count) const
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include "scalar_type_helper.h"

#include "roughpy/core/alloc.h"
#include <roughpy/device/core.h>
#include <roughpy/device/device_handle.h>

static inline rpy::scalars::rational_poly_scalar try_convert(
rpy::scalars::ScalarPointer arg,
Expand Down Expand Up @@ -348,5 +350,6 @@ void RationalPolyScalarType::uminus_into(
dst, arg, count, mask, [](auto s) { return -s; }
);
}

}// namespace scalars
}// namespace rpy
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#define ROUGHPY_SCALARS_SRC_RATIONAL_POLY_SCALAR_TYPE_H

#include <roughpy/device/core.h>

#include <roughpy/device/device_handle.h>
#include <roughpy/scalars/types.h>

#include "conversion.h"
Expand All @@ -59,12 +59,13 @@ class RationalPolyScalarType
sizeof(rational_poly_scalar),
alignof(rational_poly_scalar),
{ScalarTypeCode::OpaqueHandle, 0, 0},
{DeviceType::CPU, 0},
})
{devices::DeviceType::CPU, 0},
}, devices::get_cpu_device())
{}

const ScalarType* rational_type() const noexcept override;
const ScalarType* host_type() const noexcept override;

Scalar
from(long long int numerator, long long int denominator) const override;
ScalarPointer allocate(std::size_t count) const override;
Expand Down
17 changes: 13 additions & 4 deletions scalars/src/scalar_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include <ostream>
#include <unordered_map>


#include <roughpy/device/device_handle.h>
#include <roughpy/scalars/scalar.h>
#include <roughpy/scalars/scalar_pointer.h>
#include <roughpy/scalars/types.h>
Expand All @@ -52,8 +54,19 @@ using namespace scalars;
using rpy::devices::DeviceInfo;
using rpy::devices::DeviceType;

ScalarType::ScalarType(ScalarTypeInfo info, devices::Device&& device)
: m_info(std::move(info)), m_device(std::move(device))
{

}

ScalarType::~ScalarType() = default;

devices::Device ScalarType::device() const noexcept
{
return m_device;
}

const ScalarType* ScalarType::rational_type() const noexcept { return this; }

const ScalarType* ScalarType::host_type() const noexcept { return this; }
Expand Down Expand Up @@ -434,10 +447,6 @@ void rpy::scalars::register_conversion(
}
}

ScalarType::ScalarType(ScalarTypeInfo info)
: m_info(std::move(info))
{
}

std::unique_ptr<RandomGenerator>
ScalarType::get_rng(const string& bit_generator, Slice<uint64_t> seed) const
Expand Down
Loading

0 comments on commit 80eae50

Please sign in to comment.