Skip to content

Commit

Permalink
Merge pull request #296 from clEsperanto/remove-64-bit-type
Browse files Browse the repository at this point in the history
  • Loading branch information
StRigaud committed May 13, 2024
2 parents 414f193 + 561f305 commit 5d44cdb
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 146 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/clang-format-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ jobs:
- name: Run clang-format style check for C/C++ programs.
uses: jidicula/clang-format-action@v4.11.0
with:
clang-format-version: "13"
clang-format-version: "14"
214 changes: 99 additions & 115 deletions clic/include/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <cmath>
#include <fstream>
#include <limits>
#include <type_traits>
#include <unordered_map>
#ifndef M_PI
# define M_PI 3.14159265358979323846 /* pi */
#endif
Expand All @@ -30,15 +32,16 @@ enum class mType
*/
enum class dType
{
FLOAT,
INT32,
UINT32,
INT8,
UINT8,
INT16,
UINT16,
INT64,
UINT64,
INT32,
UINT32,
// INT64, // not supported by Metal
// UINT64, // not supported by Metal
FLOAT,
// DOUBLE, // not supported by GPUs

INT = INT32,
INDEX = UINT32,
Expand All @@ -54,67 +57,15 @@ enum class dType
inline auto
toString(const dType & dtype) -> std::string
{
switch (dtype)
{
case dType::FLOAT:
return "float";
case dType::INT32:
return "int";
case dType::UINT32:
return "uint";
case dType::INT8:
return "char";
case dType::UINT8:
return "uchar";
case dType::INT16:
return "short";
case dType::UINT16:
return "ushort";
case dType::INT64:
return "long";
case dType::UINT64:
return "ulong";
default:
return "unknown";
}
}
static const std::unordered_map<dType, std::string> dtypeToString = {
{ dType::FLOAT, "float" }, { dType::INT32, "int" }, { dType::UINT32, "uint" }, { dType::INT8, "char" },
{ dType::UINT8, "uchar" }, { dType::INT16, "short" }, { dType::UINT16, "ushort" }
};

/**
* @brief Operator << for cle::dType
*/
inline auto
operator<<(std::ostream & out, const dType & dtype) -> std::ostream &
{
return out << toString(dtype);
auto it = dtypeToString.find(dtype);
return it != dtypeToString.end() ? it->second : "unknown";
}

/**
* @brief Convert a cle::mType to a string
*/
inline auto
toString(const mType & mtype) -> std::string
{
switch (mtype)
{
case mType::BUFFER:
return "Buffer";
case mType::IMAGE:
return "Image";
default:
return "unknown";
}
}

/**
* @brief Operator << for cle::mType
*/
inline auto
operator<<(std::ostream & out, const mType & mtype) -> std::ostream &
{
return out << toString(mtype);
}


/**
* @brief Convert a template type T to a cle::dType
*/
Expand Down Expand Up @@ -150,14 +101,6 @@ toType() -> dType
{
return dType::UINT8;
}
else if constexpr (std::is_same_v<T, int64_t>)
{
return dType::INT64;
}
else if constexpr (std::is_same_v<T, uint64_t>)
{
return dType::UINT64;
}
else
{
throw std::invalid_argument("Error: Invalid type");
Expand All @@ -170,28 +113,20 @@ toType() -> dType
inline auto
toBytes(const dType & dtype) -> size_t
{
switch (dtype)
{
case dType::FLOAT:
return sizeof(float);
case dType::INT32:
return sizeof(int32_t);
case dType::UINT32:
return sizeof(uint32_t);
case dType::INT8:
return sizeof(int8_t);
case dType::UINT8:
return sizeof(uint8_t);
case dType::INT16:
return sizeof(int16_t);
case dType::UINT16:
return sizeof(uint16_t);
case dType::INT64:
return sizeof(int64_t);
case dType::UINT64:
return sizeof(uint64_t);
default:
throw std::invalid_argument("Invalid Array::Type value");
static const std::unordered_map<dType, size_t> dtypeToBytes = {
{ dType::FLOAT, sizeof(float) }, { dType::INT32, sizeof(int32_t) }, { dType::UINT32, sizeof(uint32_t) },
{ dType::INT8, sizeof(int8_t) }, { dType::UINT8, sizeof(uint8_t) }, { dType::INT16, sizeof(int16_t) },
{ dType::UINT16, sizeof(uint16_t) }
};

auto it = dtypeToBytes.find(dtype);
if (it != dtypeToBytes.end())
{
return it->second;
}
else
{
throw std::invalid_argument("Invalid Array::Type value");
}
}

Expand All @@ -203,31 +138,44 @@ inline auto
castTo(const T & value, const dType & dtype) ->
typename std::common_type<float, uint8_t, uint16_t, uint32_t, uint64_t, int8_t, int16_t, int32_t, int64_t>::type
{
switch (dtype)
{
case dType::FLOAT:
return static_cast<float>(value);
case dType::INT32:
return static_cast<int32_t>(value);
case dType::UINT32:
return static_cast<uint32_t>(value);
case dType::INT8:
return static_cast<int8_t>(value);
case dType::UINT8:
return static_cast<uint8_t>(value);
case dType::INT16:
return static_cast<int16_t>(value);
case dType::UINT16:
return static_cast<uint16_t>(value);
case dType::INT64:
return static_cast<int64_t>(value);
case dType::UINT64:
return static_cast<uint64_t>(value);
default:
throw std::invalid_argument("Invalid Array::Type value");
using CommonType =
typename std::common_type<float, uint8_t, uint16_t, uint32_t, uint64_t, int8_t, int16_t, int32_t, int64_t>::type;

if (dtype == dType::FLOAT)
{
return static_cast<CommonType>(value);
}
else if (dtype == dType::INT32)
{
return static_cast<CommonType>(value);
}
else if (dtype == dType::UINT32)
{
return static_cast<CommonType>(value);
}
else if (dtype == dType::INT8)
{
return static_cast<CommonType>(value);
}
else if (dtype == dType::UINT8)
{
return static_cast<CommonType>(value);
}
else if (dtype == dType::INT16)
{
return static_cast<CommonType>(value);
}
else if (dtype == dType::UINT16)
{
return static_cast<CommonType>(value);
}
else
{
throw std::invalid_argument("Invalid Array::Type value");
}
}


/**
* @brief compute a kernel size from a sigma value
*/
Expand Down Expand Up @@ -344,6 +292,42 @@ correct_range(int * start, int * stop, int * step, int size) -> void
}
}

/**
* @brief Convert a cle::mType to a string
*/
inline auto
toString(const mType & mtype) -> std::string
{
switch (mtype)
{
case mType::BUFFER:
return "Buffer";
case mType::IMAGE:
return "Image";
default:
return "unknown";
}
}

/**
* @brief Operator << for cle::dType
*/
inline auto
operator<<(std::ostream & out, const dType & dtype) -> std::ostream &
{
return out << toString(dtype);
}

/**
* @brief Operator << for cle::mType
*/
inline auto
operator<<(std::ostream & out, const mType & mtype) -> std::ostream &
{
return out << toString(mtype);
}


/**
* @brief convert a string to lower case
*/
Expand Down
26 changes: 0 additions & 26 deletions clic/src/openclbackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -885,32 +885,6 @@ OpenCLBackend::setBuffer(const Device::Pointer & device,
nullptr);
break;
}
case dType::INT64: {
auto cval = static_cast<int64_t>(value);
err = clEnqueueFillBuffer(opencl_device->getCLCommandQueue(),
*static_cast<cl_mem *>(*buffer_ptr),
&cval,
sizeof(cval),
0,
size,
0,
nullptr,
nullptr);
break;
}
case dType::UINT64: {
auto cval = static_cast<uint64_t>(value);
err = clEnqueueFillBuffer(opencl_device->getCLCommandQueue(),
*static_cast<cl_mem *>(*buffer_ptr),
&cval,
sizeof(cval),
0,
size,
0,
nullptr,
nullptr);
break;
}
default:
throw std::invalid_argument("Invalid Array::Type value");
}
Expand Down
2 changes: 1 addition & 1 deletion clic/src/tier1/median_box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ median_box_func(const Device::Pointer & device,
{ "scalar1", radius2kernelsize(radius_y) },
{ "scalar2", radius2kernelsize(radius_z) } };
const ConstantList constants = {
{ "MAX_ARRAY_SIZE", radius2kernelsize(radius_x) * radius2kernelsize(radius_y) * radius2kernelsize(radius_z) }
{ "MAX_ARRAY_SIZE", radius2kernelsize(radius_x) * radius2kernelsize(radius_y) * radius2kernelsize(radius_z) }
};
const RangeArray range = { dst->width(), dst->height(), dst->depth() };
execute(device, kernel, params, range, constants);
Expand Down
2 changes: 1 addition & 1 deletion clic/src/tier1/median_sphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ median_sphere_func(const Device::Pointer & device,
{ "scalar1", radius2kernelsize(radius_y) },
{ "scalar2", radius2kernelsize(radius_z) } };
const ConstantList constants = {
{ "MAX_ARRAY_SIZE", radius2kernelsize(radius_x) * radius2kernelsize(radius_y) * radius2kernelsize(radius_z) }
{ "MAX_ARRAY_SIZE", radius2kernelsize(radius_x) * radius2kernelsize(radius_y) * radius2kernelsize(radius_z) }
};
const RangeArray range = { dst->width(), dst->height(), dst->depth() };
execute(device, kernel, params, range, constants);
Expand Down
4 changes: 2 additions & 2 deletions tests/tier5/test_array_comparison.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class TestArrayComparisons : public ::testing::TestWithParam<std::string>
{
protected:
std::array<int64_t, 3 * 1 * 1> input1 = { 1, 2, 3 };
std::array<int32_t, 3 * 1 * 1> input1 = { 1, 2, 3 };
std::array<int16_t, 3 * 1 * 1> input2 = { 4, 5, 6 };
std::array<int8_t, 4 * 1 * 1> input3 = { 1, 2, 3, 3 };
std::array<float, 3 * 1 * 1> input4 = { 1.0F, 2.0F, 3.0F };
Expand All @@ -19,7 +19,7 @@ TEST_P(TestArrayComparisons, execute)
auto device = cle::BackendManager::getInstance().getBackend().getDevice("", "all");
device->setWaitToFinish(true);

auto gpu_input1 = cle::Array::create(3, 1, 1, 3, cle::dType::INT64, cle::mType::BUFFER, device);
auto gpu_input1 = cle::Array::create(3, 1, 1, 3, cle::dType::INT32, cle::mType::BUFFER, device);
auto gpu_input2 = cle::Array::create(3, 1, 1, 3, cle::dType::INT16, cle::mType::BUFFER, device);
auto gpu_input3 = cle::Array::create(4, 1, 1, 3, cle::dType::INT8, cle::mType::BUFFER, device);
auto gpu_input4 = cle::Array::create(3, 1, 1, 3, cle::dType::FLOAT, cle::mType::BUFFER, device);
Expand Down

0 comments on commit 5d44cdb

Please sign in to comment.