Skip to content

Commit

Permalink
Switching to CFFI.
Browse files Browse the repository at this point in the history
  • Loading branch information
feiy committed May 9, 2020
1 parent 23cc74d commit 1b8cb1d
Show file tree
Hide file tree
Showing 108 changed files with 2,594 additions and 2,686 deletions.
5 changes: 0 additions & 5 deletions DVTuple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ DVTuple::DVTuple(const std::vector<CapturedDeviceViewable>& elem_map)
TRTC_Query_Struct(m_name_view_cls.c_str(), name_elems, m_offsets.data());
}

std::string DVTuple::name_view_cls() const
{
return m_name_view_cls;
}

ViewBuf DVTuple::view() const
{
ViewBuf ret(m_offsets[m_offsets.size() - 1]);
Expand Down
3 changes: 0 additions & 3 deletions DVTuple.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ class THRUST_RTC_API DVTuple : public DeviceViewable
{
public:
DVTuple(const std::vector<CapturedDeviceViewable>& elem_map);

virtual std::string name_view_cls() const;
virtual ViewBuf view() const;

private:
std::string m_name_view_cls;
std::vector<ViewBuf> m_view_elems;
std::vector<size_t> m_offsets;

Expand Down
19 changes: 8 additions & 11 deletions DVVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ DVVector::DVVector(const char* elem_cls, size_t size, void* hdata)
cuMemcpyHtoD(dptr, hdata, m_elem_size*m_size);
else
cuMemsetD8(dptr, 0, m_elem_size*m_size);

m_name_view_cls = std::string("VectorView<") + m_elem_cls + ">";
}

DVVector::~DVVector()
Expand All @@ -34,11 +36,6 @@ void DVVector::to_host(void* hdata, size_t begin, size_t end) const
cuMemcpyDtoH(hdata, (CUdeviceptr)((char*)m_data + begin* m_elem_size), m_elem_size*n);
}

std::string DVVector::name_view_cls() const
{
return std::string("VectorView<") + m_elem_cls + ">";
}

ViewBuf DVVector::view() const
{
ViewBuf buf(sizeof(VectorView<char>));
Expand All @@ -49,26 +46,26 @@ ViewBuf DVVector::view() const
}

DVVectorAdaptor::DVVectorAdaptor(const char* elem_cls, size_t size, void* ddata)
: DVVectorLike(elem_cls, (std::string(elem_cls) + "&").c_str(), size), m_data(ddata){}
: DVVectorLike(elem_cls, (std::string(elem_cls) + "&").c_str(), size), m_data(ddata)
{
m_name_view_cls = std::string("VectorView<") + m_elem_cls + ">";
}

DVVectorAdaptor::DVVectorAdaptor(const DVVector& vec, size_t begin, size_t end)
: DVVectorLike(vec.name_elem_cls().c_str(), vec.name_ref_type().c_str(), (end == (size_t)(-1)? vec.size() : end) - begin)
{
m_data = (char*)vec.data() + begin * vec.elem_size();
m_name_view_cls = std::string("VectorView<") + m_elem_cls + ">";
}

DVVectorAdaptor::DVVectorAdaptor(const DVVectorAdaptor& vec, size_t begin, size_t end)
: DVVectorLike(vec.name_elem_cls().c_str(), vec.name_ref_type().c_str(), (end == (size_t)(-1) ? vec.size() : end) - begin)
{
m_data = (char*)vec.data() + begin * vec.elem_size();
m_name_view_cls = std::string("VectorView<") + m_elem_cls + ">";
}


std::string DVVectorAdaptor::name_view_cls() const
{
return std::string("VectorView<") + m_elem_cls + ">";
}

ViewBuf DVVectorAdaptor::view() const
{
ViewBuf buf(sizeof(VectorView<char>));
Expand Down
7 changes: 2 additions & 5 deletions DVVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
class THRUST_RTC_API DVVectorLike : public DeviceViewable
{
public:
std::string name_elem_cls() const { return m_elem_cls; }
std::string name_ref_type() const { return m_ref_type; }
const std::string& name_elem_cls() const { return m_elem_cls; }
const std::string& name_ref_type() const { return m_ref_type; }
size_t elem_size() const { return m_elem_size; }
size_t size() const { return m_size; }

Expand All @@ -35,7 +35,6 @@ class THRUST_RTC_API DVVector : public DVVectorLike
virtual bool is_writable() const { return true; }

void to_host(void* hdata, size_t begin=0, size_t end = (size_t)(-1)) const;
virtual std::string name_view_cls() const;
virtual ViewBuf view() const;

private:
Expand All @@ -53,8 +52,6 @@ class THRUST_RTC_API DVVectorAdaptor : public DVVectorLike
DVVectorAdaptor(const DVVectorAdaptor& vec, size_t begin = 0, size_t end = (size_t)(-1));

virtual bool is_writable() const { return true; }

virtual std::string name_view_cls() const;
virtual ViewBuf view() const;

private:
Expand Down
58 changes: 51 additions & 7 deletions DeviceViewable.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ class DeviceViewable
public:
DeviceViewable(){}
virtual ~DeviceViewable(){}
virtual std::string name_view_cls() const = 0;
virtual ViewBuf view() const = 0;
const std::string& name_view_cls() const { return m_name_view_cls; }

protected:
std::string m_name_view_cls;
};


Expand All @@ -36,18 +39,12 @@ class BuiltIn : public DeviceViewable
memcpy(m_view_buf.data(), data_view, size_view);
}

virtual std::string name_view_cls() const
{
return m_name_view_cls;
}

virtual ViewBuf view() const
{
return m_view_buf;
}

private:
std::string m_name_view_cls;
ViewBuf m_view_buf;
};

Expand Down Expand Up @@ -84,4 +81,51 @@ DECLAR_DV_BASIC(DVUInt64, uint64_t)

DECLAR_DV_BASIC(DVSizeT, size_t)


class SomeDeviceViewable : public DeviceViewable
{
public:
SomeDeviceViewable(const ViewBuf& buf, const char* type)
{
m_buf = buf;
m_name_view_cls = type;

}
virtual ViewBuf view() const
{
return m_buf;
}
private:
ViewBuf m_buf;
};

inline DeviceViewable* dv_from_viewbuf(const ViewBuf& buf, const char* type)
{
std::string s_type = type;
if (s_type == "int8_t")
return new DVInt8(*(int8_t*)buf.data());
else if (s_type == "uint8_t")
return new DVUInt8(*(uint8_t*)buf.data());
else if (s_type == "int16_t")
return new DVInt16(*(int16_t*)buf.data());
else if (s_type == "uint16_t")
return new DVUInt16(*(uint16_t*)buf.data());
else if (s_type == "int32_t")
return new DVInt32(*(int32_t*)buf.data());
else if (s_type == "uint32_t")
return new DVUInt32(*(uint32_t*)buf.data());
else if (s_type == "int64_t")
return new DVInt64(*(int64_t*)buf.data());
else if (s_type == "uint64_t")
return new DVUInt64(*(uint64_t*)buf.data());
else if (s_type == "float")
return new DVFloat(*(float*)buf.data());
else if (s_type == "double")
return new DVDouble(*(double*)buf.data());
else if (s_type == "bool")
return new DVBool(*(bool*)buf.data());
else
return new SomeDeviceViewable(buf, type);
}

#endif
4 changes: 2 additions & 2 deletions docs/QuickStartGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ At build time, you will only need:

* UnQLite source code, as submodule: thirdparty/unqlite
* CMake 3.x
* C libraries of Python 3 is required to build the Python binding part of the code.

After cloning the repo from github and resolving the submodules, you can build it with CMake.
Libraries for different lauguages are now built separately. The C++ library is used as a
Expand Down Expand Up @@ -57,7 +56,7 @@ You will get the library headers, binaries and examples in the "install" directo

#### Install ThrustRTC for Python from Pypi

Builds for Win64/Linux64 + Python 3.7 are available from [Pypi](https://pypi.org/project/ThrustRTC/)
Builds for Win64/Linux64 + Python 3.x are available from [Pypi](https://pypi.org/project/ThrustRTC/)
If your environment matches, you can try:

```
Expand Down Expand Up @@ -90,6 +89,7 @@ Zip packages are available at:

For Python
* Python 3
* cffi
* numpy
* numba (optional)

Expand Down
12 changes: 3 additions & 9 deletions fake_vectors/DVConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@
DVConstant::DVConstant(const DeviceViewable& dvobj, size_t size) :
DVVectorLike(dvobj.name_view_cls().c_str(), (std::string("const ") + dvobj.name_view_cls() + "&").c_str(), size)
{
m_value = dvobj.view();

std::string name_struct = name_view_cls();
TRTC_Query_Struct(name_struct.c_str(), { "_size", "_value" }, m_offsets);
}

std::string DVConstant::name_view_cls() const
{
return std::string("ConstantView<") + m_elem_cls + ">";
m_value = dvobj.view();
m_name_view_cls = std::string("ConstantView<") + m_elem_cls + ">";
TRTC_Query_Struct(m_name_view_cls.c_str(), { "_size", "_value" }, m_offsets);
}

ViewBuf DVConstant::view() const
Expand Down
1 change: 0 additions & 1 deletion fake_vectors/DVConstant.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class THRUST_RTC_API DVConstant : public DVVectorLike
public:
ViewBuf value() const { return m_value; }
DVConstant(const DeviceViewable& dvobj, size_t size = (size_t)(-1));
virtual std::string name_view_cls() const;
virtual ViewBuf view() const;

private:
Expand Down
10 changes: 2 additions & 8 deletions fake_vectors/DVCounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@ DVCounter::DVCounter(const DeviceViewable& dvobj_init, size_t size) :
DVVectorLike(dvobj_init.name_view_cls().c_str(), dvobj_init.name_view_cls().c_str(), size)
{
m_value_init = dvobj_init.view();

std::string name_struct = name_view_cls();
TRTC_Query_Struct(name_struct.c_str(), { "_size", "_value_init" }, m_offsets);
}

std::string DVCounter::name_view_cls() const
{
return std::string("CounterView<") + m_elem_cls + ">";
m_name_view_cls = std::string("CounterView<") + m_elem_cls + ">";
TRTC_Query_Struct(m_name_view_cls.c_str(), { "_size", "_value_init" }, m_offsets);
}

ViewBuf DVCounter::view() const
Expand Down
1 change: 0 additions & 1 deletion fake_vectors/DVCounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class THRUST_RTC_API DVCounter : public DVVectorLike
public:
ViewBuf value_init() const { return m_value_init; }
DVCounter(const DeviceViewable& dvobj_init, size_t size = (size_t)(-1));
virtual std::string name_view_cls() const;
virtual ViewBuf view() const;

private:
Expand Down
6 changes: 0 additions & 6 deletions fake_vectors/DVCustomVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ DVCustomVector::DVCustomVector(const std::vector<CapturedDeviceViewable>& arg_ma
TRTC_Query_Struct(m_name_view_cls.c_str(), { "_size", "_op" }, m_offsets);
}


std::string DVCustomVector::name_view_cls() const
{
return m_name_view_cls;
}

ViewBuf DVCustomVector::view() const
{
ViewBuf view_op(m_arg_offsets[m_arg_offsets.size() - 1]);
Expand Down
2 changes: 0 additions & 2 deletions fake_vectors/DVCustomVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ class THRUST_RTC_API DVCustomVector : public DVVectorLike
DVCustomVector(const std::vector<CapturedDeviceViewable>& arg_map, const char* name_idx, const char* code_body,
const char* elem_cls, size_t size = (size_t)(-1), bool read_only = true);

virtual std::string name_view_cls() const;
virtual ViewBuf view() const;

virtual bool is_writable() const { return !m_read_only; }

private:
std::string m_name_view_cls;
size_t m_size;
bool m_read_only;
std::vector<ViewBuf> m_view_args;
Expand Down
6 changes: 2 additions & 4 deletions fake_vectors/DVDiscard.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#include "DVDiscard.h"

DVDiscard::DVDiscard(const char* elem_cls, size_t size)
:DVVectorLike(elem_cls, (std::string("_Sink<")+ elem_cls +">&").c_str(), size){}

std::string DVDiscard::name_view_cls() const
:DVVectorLike(elem_cls, (std::string("_Sink<")+ elem_cls +">&").c_str(), size)
{
return std::string("DiscardView<") + m_elem_cls + ">";
m_name_view_cls = std::string("DiscardView<") + m_elem_cls + ">";
}

ViewBuf DVDiscard::view() const
Expand Down
1 change: 0 additions & 1 deletion fake_vectors/DVDiscard.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class THRUST_RTC_API DVDiscard : public DVVectorLike
{
public:
DVDiscard(const char* elem_cls, size_t size = (size_t)(-1));
virtual std::string name_view_cls() const;
virtual ViewBuf view() const;
virtual bool is_readable() const { return false; }
virtual bool is_writable() const { return true; }
Expand Down
8 changes: 2 additions & 6 deletions fake_vectors/DVPermutation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ DVPermutation::DVPermutation(const DVVectorLike& vec_value, const DVVectorLike&
m_cls_index = vec_index.name_view_cls();
m_view_index = vec_index.view();

std::string name_struct = name_view_cls();
TRTC_Query_Struct(name_struct.c_str(), { "_view_vec_value", "_view_vec_index" }, m_offsets);
}
m_name_view_cls = std::string("PermutationView<") + m_cls_value + ", " + m_cls_index + ">";
TRTC_Query_Struct(m_name_view_cls.c_str(), { "_view_vec_value", "_view_vec_index" }, m_offsets);

std::string DVPermutation::name_view_cls() const
{
return std::string("PermutationView<") + m_cls_value + ", "+ m_cls_index+">";
}

ViewBuf DVPermutation::view() const
Expand Down
1 change: 0 additions & 1 deletion fake_vectors/DVPermutation.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class THRUST_RTC_API DVPermutation : public DVVectorLike
ViewBuf view_index() const { return m_view_index; }

DVPermutation(const DVVectorLike& vec_value, const DVVectorLike& vec_index );
virtual std::string name_view_cls() const;
virtual ViewBuf view() const;
virtual bool is_readable() const { return m_readable; }
virtual bool is_writable() const { return m_writable; }
Expand Down
8 changes: 2 additions & 6 deletions fake_vectors/DVRange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ DVRange::DVRange(const DVVectorLike& vec_value, size_t begin, size_t end)
m_begin = begin;
m_end = end;

std::string name_struct = name_view_cls();
TRTC_Query_Struct(name_struct.c_str(), { "_view_vec_value", "_begin", "_end" }, m_offsets);
m_name_view_cls = std::string("RangeView<") + m_cls_value + ">";
TRTC_Query_Struct(m_name_view_cls.c_str(), { "_view_vec_value", "_begin", "_end" }, m_offsets);
}

std::string DVRange::name_view_cls() const
{
return std::string("RangeView<") + m_cls_value + ">";
}

ViewBuf DVRange::view() const
{
Expand Down
1 change: 0 additions & 1 deletion fake_vectors/DVRange.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class THRUST_RTC_API DVRange : public DVVectorLike
ViewBuf view_value() const { return m_view_value; }

DVRange(const DVVectorLike& vec_value, size_t begin = 0, size_t end = (size_t)(-1));
virtual std::string name_view_cls() const;
virtual ViewBuf view() const;
virtual bool is_readable() const { return m_readable; }
virtual bool is_writable() const { return m_writable; }
Expand Down
7 changes: 1 addition & 6 deletions fake_vectors/DVReverse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,10 @@ DVReverse::DVReverse(const DVVectorLike& vec_value)
m_writable = vec_value.is_writable();
m_cls_value = vec_value.name_view_cls();
m_view_value = vec_value.view();
}


std::string DVReverse::name_view_cls() const
{
return std::string("ReverseView<") + m_cls_value + ">";
m_name_view_cls = std::string("ReverseView<") + m_cls_value + ">";
}


ViewBuf DVReverse::view() const
{
ViewBuf buf(m_view_value.size());
Expand Down
1 change: 0 additions & 1 deletion fake_vectors/DVReverse.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class THRUST_RTC_API DVReverse : public DVVectorLike
ViewBuf view_value() const { return m_view_value; }

DVReverse(const DVVectorLike& vec_value);
virtual std::string name_view_cls() const;
virtual ViewBuf view() const;
virtual bool is_readable() const { return m_readable; }
virtual bool is_writable() const { return m_writable; }
Expand Down
Loading

0 comments on commit 1b8cb1d

Please sign in to comment.