jstzwj 6 lat temu
rodzic
commit
f1fb690591

+ 11 - 11
CMakeLists.txt

@@ -22,10 +22,10 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
 set(CMAKE_CXX_EXTENSIONS OFF)
 
 
-find_package(Boost)
-if(Boost_FOUND)
-	message(STATUS Boost found: ${Boost_INCLUDE_DIRS})
-endif()
+# find_package(Boost)
+# if(Boost_FOUND)
+# 	message(STATUS Boost found: ${Boost_INCLUDE_DIRS})
+# endif()
 
 
 # languge feature
@@ -33,13 +33,13 @@ set(ENABLE_PYTHON False CACHE BOOL "Language: Python")
 
 
 # thread feature
-FIND_PACKAGE(OpenMP REQUIRED)
-if(OPENMP_FOUND)
-	message("OPENMP FOUND")
-	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
-	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
-	set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
-endif()
+# FIND_PACKAGE(OpenMP REQUIRED)
+# if(OPENMP_FOUND)
+# 	message("OPENMP FOUND")
+# 	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
+# 	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
+# 	set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
+# endif()
 
 # gpgpu feature
 set(ENABLE_CUDA FALSE CACHE BOOL "Feature: OPENCL")

+ 0 - 3
traph/include/traph/core/tensor.h

@@ -9,8 +9,6 @@
 #include <traph/core/index.h>
 #include <traph/core/slice.h>
 
-#include <traph/core/tensor_storage.h>
-
 namespace traph
 {
 	template<class T>
@@ -110,7 +108,6 @@ namespace traph
         virtual void sin_() = 0;
 		virtual DimVector size() const = 0;
 		virtual idx_type size(idx_type i) const = 0;
-        virtual std::shared_ptr<StorageBase<T>> storage() const = 0;
 		virtual DimVector stride() const = 0;
 		virtual idx_type stride(idx_type i) const = 0;
         virtual void sub_(std::shared_ptr<TensorInterface> other) = 0;

+ 0 - 52
traph/include/traph/core/tensor_storage.h

@@ -1,52 +0,0 @@
-#ifndef TRAPH_CORE_TENSOR_STORAGE_H_
-#define TRAPH_CORE_TENSOR_STORAGE_H_
-namespace traph
-{
-    template<class T>
-    class StorageBase
-    {
-    public:
-        using value_type = T;
-        using self_type = StorageBase<T>;
-
-        using raw_pointer = self_type*;
-        using raw_const_pointer = const self_type*;
-        using shared_pointer = std::shared_ptr<self_type>;
-        using reference = self_type&;
-        using const_reference = const self_type&;
-    public:
-        virtual std::shared_ptr<StorageBase<T>> clone() const = 0;
-        virtual T* data_ptr() = 0;
-        virtual const T* data_ptr() const = 0;
-        virtual size_type element_size() const = 0;
-        virtual void fill_(T v) = 0;
-        virtual void resize_(idx_type size) = 0;
-        virtual idx_type size() const = 0;
-    };
-
-    template<class T>
-    class ContiguousStorageBase: public StorageBase<T>
-    {
-    public:
-        using value_type = T;
-        using self_type = ContiguousStorageBase<T>;
-        using base_type = StorageBase<T>;
-
-        using raw_pointer = self_type*;
-        using raw_const_pointer = const self_type*;
-        using shared_pointer = std::shared_ptr<self_type>;
-        using reference = self_type&;
-        using const_reference = const self_type&;
-    public:
-        virtual std::shared_ptr<StorageBase<T>> clone() const = 0;
-        virtual T* data_ptr() = 0;
-        virtual const T* data_ptr() const = 0;
-        virtual size_type element_size() const = 0;
-        virtual void fill_(T v) = 0;
-        virtual void resize_(idx_type size) = 0;
-        virtual idx_type size() const = 0;
-    };
-}
-
-
-#endif

+ 0 - 1
traph/include/traph/core/variable.h

@@ -81,7 +81,6 @@ namespace traph
         virtual void reshape_(const DimVector& dims) = 0;
         virtual void resize_(const DimVector& dims) = 0;
 		virtual DimVector size() const = 0;
-        virtual std::shared_ptr<StorageBase<T>> storage() const = 0;
 		virtual DimVector stride() const = 0;
     };
 

+ 0 - 16
traph/include/traph/core/view.h

@@ -1,16 +0,0 @@
-#ifndef TRAPH_TENSOR_VIEW_H_
-#define TRAPH_TENSOR_VIEW_H_
-
-#include <utility>
-
-#include <traph/core/type.h>
-#include <traph/tensor/index.h>
-#include <traph/tensor/utils.h>
-#include <traph/tensor/tensor.h>
-
-namespace traph
-{
-    
-}
-
-#endif

+ 0 - 7
traph/include/traph/nn/variable.h

@@ -64,7 +64,6 @@ namespace traph
         virtual void reshape_(const DimVector& dims) override;
         virtual void resize_(const DimVector& dims) override;
 		virtual DimVector size() const override;
-        virtual std::shared_ptr<StorageBase<T>> storage() const override;
 		virtual DimVector stride() const override;
     };
 
@@ -296,12 +295,6 @@ namespace traph
 		return _data->size();
 	}
 
-	template<typename T>
-	std::shared_ptr<StorageBase<T>> Variable<T>::storage() const
-	{
-		return _data->storage();
-	}
-
 	template<typename T>
 	DimVector Variable<T>::stride() const
 	{

+ 2 - 15
traph/include/traph/tensor/tensor.h

@@ -16,8 +16,8 @@
 #include<traph/core/utils.h>
 #include<traph/core/tensor.h>
 
-#include<traph/tensor/tensor_storage.h>
 #include<traph/tensor/arithmetic.h>
+#include <xtensor/xarray.hpp>
 
 namespace traph
 {
@@ -29,7 +29,6 @@ namespace traph
         using value_type = T;
         using self_type = Tensor<T>;
         using base_type = TensorBase<T>;
-        using storage_type = TensorStorage<value_type>;
 
         using raw_pointer = self_type*;
         using raw_const_pointer = const self_type*;
@@ -37,18 +36,7 @@ namespace traph
         using reference = self_type&;
         using const_reference = const self_type&;
     private:
-        std::shared_ptr<TensorStorage<T>> _rep;
-        DimVector _dimensions;
-        idx_type _offset;
-		DimVector _strides;
-
-    private:
-        void auto_strides();
-        void reduce_impl(T& result, idx_type dim, idx_type idx, std::function<T(T,T)> f) const;
-		void Tensor<T>::reduce_dim_impl(Tensor<T>& result, idx_type dim, idx_type reduce_dim,
-			idx_type this_idx, idx_type result_idx,
-			std::function<T(T, T)> f) const;
-        T reduce_dim_kernel(idx_type begin, idx_type step_len, idx_type step_num, std::function<T(T,T)> f) const;
+        xt::xarray<T> tensor_impl;
     public:
         Tensor();
         explicit Tensor(const DimVector& dimensions);
@@ -91,7 +79,6 @@ namespace traph
         virtual void sin_() override;
 		virtual DimVector size() const override;
 		virtual idx_type size(idx_type i) const override;
-        virtual std::shared_ptr<StorageBase<T>> storage() const override;
 		virtual DimVector stride() const override;
 		virtual idx_type stride(idx_type i) const override;
         virtual void sub_(std::shared_ptr<TensorInterface> other) override;

+ 0 - 105
traph/include/traph/tensor/tensor_storage.h

@@ -1,105 +0,0 @@
-#ifndef TRAPH_TENSOR_TENSOR_STORAGE_H_
-#define TRAPH_TENSOR_TENSOR_STORAGE_H_
-
-#include<traph/core/type.h>
-#include<traph/core/tensor_storage.h>
-
-namespace traph
-{
-    // The real representation of all tensors.
-    template<typename T>
-    class TensorStorage: public ContiguousStorageBase<T>
-    {
-    public:
-        using value_type = T;
-        using self_type = TensorStorage<T>;
-        using base_type = ContiguousStorageBase<T>;
-
-        using raw_pointer = self_type*;
-        using raw_const_pointer = const self_type*;
-        using shared_pointer = std::shared_ptr<self_type>;
-        using reference = self_type&;
-        using const_reference = const self_type&;
-
-    public:
-        std::unique_ptr<T[]> data;
-        idx_type len;
-        TensorStorage()
-            :data(nullptr), len(0)
-        {
-        }
-
-        TensorStorage(const TensorStorage& other)
-            :data(new T[other.len]), len(other.len)
-        {
-            std::memcpy(data.get(), other.data.get(), other.len * sizeof(T));
-        }
-
-        TensorStorage(TensorStorage&& other)
-            :data(std::move(other.data)), len(other.len)
-        {
-        }
-
-        TensorStorage& operator=(const TensorStorage& other)
-        {
-            data = std::make_unique(new T[other.len]);
-            std::memcpy(data.get(), other.data.get(), other.len * sizeof(T));
-            len = other.len;
-
-            return *this;
-        }
-
-        TensorStorage& operator=(TensorStorage&& other)
-        {
-            data = std::move(other.data);
-            len = other.len;
-
-            return *this;
-        }
-
-        virtual std::shared_ptr<StorageBase<T>> clone() const override
-        {
-            std::shared_ptr<TensorStorage<T>> cloned_storage(new TensorStorage<T>);
-            cloned_storage->data = std::unique_ptr<T[]>(new T[len]);
-            std::memcpy(cloned_storage->data.get(), data.get(), len * sizeof(T));
-            cloned_storage->len = len;
-
-            return std::dynamic_pointer_cast<StorageBase<T>>(cloned_storage);
-        }
-        virtual T* data_ptr() override {return data.get();}
-        virtual const T* data_ptr() const override {return data.get();}
-        virtual idx_type size() const override {return len;}
-        virtual size_type element_size() const override {return sizeof(T);}
-
-        virtual void resize_(idx_type size) override
-        {
-            if(size < 0 || size == len)
-                return;
-            idx_type move_size = (size > len ? len: size);
-            std::unique_ptr<T[]> temp(new T[size]);
-            std::memcpy(temp.get(), data.get(), move_size * sizeof(T));
-            data = std::move(temp);
-
-            len = size;
-        }
-
-        // fill
-        virtual void fill_(T v) override
-        {
-            for(idx_type i = 0; i < size(); ++i)
-            {
-                data[i] = v;
-            }
-        }
-    };
-
-    using DoubleStorage = TensorStorage<f64>;
-    using FloatStorage = TensorStorage<f32>;
-    using LongStorage = TensorStorage<i64>;
-    using IntStorage = TensorStorage<i32>;
-    using ShortStorage = TensorStorage<i16>;
-    using CharStorage = TensorStorage<i8>;
-    using ByteStorage = TensorStorage<u8>;
-}
-
-#endif

+ 2 - 0
traph/install/CMakeLists.txt

@@ -50,4 +50,6 @@ ExternalProject_Add(
         -DXTENSOR_USE_XSIMD=ON -DXTENSOR_ENABLE_ASSERT=ON -DXTENSOR_CHECK_DIMENSION=ON -DXTENSOR_USE_TBB=ON
 )
 
+add_dependencies(xtensor xsimd xtl tbb)
+
 INCLUDE_DIRECTORIES(${TRAPH_PATH_INSTALL}/include)

+ 0 - 4
traph/source/CMakeLists.txt

@@ -2,10 +2,6 @@ INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/traph/include)
 INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/traph/contrib)
 INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/traph/install/include)
 
-if(Boost_FOUND)
-	INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
-endif()
-
 ADD_SUBDIRECTORY(core)
 ADD_SUBDIRECTORY(tensor)
 if(ENABLE_OPENCL)

+ 0 - 3
traph/source/cltensor/CMakeLists.txt

@@ -17,9 +17,6 @@ SET(CORE_LIST
 
 ADD_LIBRARY(${LIB_OUTNAME} ${CORE_LIST})
 
-IF(Boost_FOUND)
-	target_link_libraries(${LIB_OUTNAME} ${Boost_LIBRARIES})
-ENDIF()
 
 IF(OpenCL_FOUND)
 	target_link_libraries(${LIB_OUTNAME} ${OpenCL_LIBRARIES})

+ 0 - 4
traph/source/cutensor/CMakeLists.txt

@@ -14,10 +14,6 @@ SET(CUTENSOR_LIST
 ADD_LIBRARY(${LIB_OUTNAME} ${CUTENSOR_LIST})
 target_link_libraries(${LIB_OUTNAME} traph-cutensor-cuda)
 
-IF(Boost_FOUND)
-	target_link_libraries(${LIB_OUTNAME} ${Boost_LIBRARIES})
-ENDIF()
-
 
 IF(TRAPH_ACCELERATE EQUAL 0)
 	# do something

+ 3 - 1
traph/source/demo/main.cpp

@@ -151,8 +151,10 @@ int main()
 	{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 
 	arr.reshape({ 3, 3 });
+	auto brr = xt::strided_view(arr, { xt::range(0, 1), xt::all() });
 
-	std::cout << arr;
+	brr.fill(1);
+	std::cout << arr <<std::endl << brr;
 	
     return 0;
 }

+ 0 - 4
traph/source/nn/CMakeLists.txt

@@ -18,10 +18,6 @@ SET(NN_LIST
 
 ADD_LIBRARY(${LIB_OUTNAME} ${NN_LIST})
 
-IF(Boost_FOUND)
-	target_link_libraries(${LIB_OUTNAME} ${Boost_LIBRARIES})
-ENDIF()
-
 IF(OpenCL_FOUND)
 	target_link_libraries(${LIB_OUTNAME} ${OpenCL_LIBRARIES})
 ENDIF()

+ 2 - 3
traph/source/tensor/CMakeLists.txt

@@ -13,9 +13,8 @@ SET(TENSOR_LIST
 
 ADD_LIBRARY(${LIB_OUTNAME} ${TENSOR_LIST})
 
-IF(Boost_FOUND)
-	target_link_libraries(${LIB_OUTNAME} ${Boost_LIBRARIES})
-ENDIF()
+
+add_dependencies(${LIB_OUTNAME} xtensor)
 
 IF(TRAPH_ACCELERATE EQUAL 0)
 	# link eigen?

+ 4 - 85
traph/source/tensor/tensor.cpp

@@ -3,101 +3,23 @@
 namespace traph
 {
 	// definition
-    // private
-    template<typename T>
-    void Tensor<T>::auto_strides()
-    {
-        idx_type dim_num = _dimensions.size();
-        _strides.resize(dim_num);
-        idx_type stride = 1;
-        for (idx_type i = dim_num - 1; i >= 0; --i)
-        {
-            _strides[i] = stride;
-            stride *= _dimensions[i];
-        }
-    }
-
-    template<typename T>
-    void Tensor<T>::reduce_impl(T& result, idx_type dim, idx_type idx, std::function<T(T,T)> f) const
-    {
-        idx_type dim_size = _dimensions.size();
-
-        idx_type step_len = _strides[dim];
-        idx_type step_num = _dimensions[dim];
-
-        for(idx_type i = 0; i < step_num; ++i)
-        {
-            if(dim == dim_size - 1)
-                result = f(result, _rep->data[idx]);
-            else
-                reduce_impl(result, dim + 1, idx, f);
-            idx += step_len;
-        }
-    }
-
-    template<typename T>
-    T Tensor<T>::reduce_dim_kernel(idx_type begin, idx_type step_len, idx_type step_num, std::function<T(T,T)> f) const
-    {
-        T result{};
-        for(idx_type i = 0; i < step_num; ++i)
-        {
-            result = f(result, _rep->data[begin]);
-            begin += step_len;
-        }
-        return result;
-    }
-
-    template<typename T>
-    void Tensor<T>::reduce_dim_impl(Tensor<T>& result, idx_type dim, idx_type reduce_dim,
-        idx_type this_idx, idx_type result_idx,
-        std::function<T(T,T)> f) const
-    {
-        idx_type dim_size = _dimensions.size();
-
-        if(dim == dim_size)
-        {
-            result._rep->data[result_idx] = 
-                reduce_dim_kernel(this_idx, _strides[reduce_dim], _dimensions[reduce_dim], f);
-            return;
-        }
-
-        if(dim == reduce_dim)
-        {
-            reduce_dim_impl(result, dim + 1, reduce_dim, this_idx,result_idx, f);
-        }
-        else
-        {
-            for(idx_type i = 0; i < _dimensions[dim]; ++i)
-            {
-                reduce_dim_impl(result, dim + 1, reduce_dim, this_idx,result_idx, f);
-                    
-                this_idx += _strides[dim];
-                result_idx += result._strides[dim];
-            }
-        }
-    }
     // public
     template<typename T>
     Tensor<T>::Tensor()
-        :_rep(new TensorStorage<T>),
-        _dimensions(), _offset(0), _strides()
+        :tensor_impl()
     {
     }
 
     template<typename T>
     Tensor<T>::Tensor(const DimVector& dimensions)
-        :_rep(new TensorStorage<T>),
-        _dimensions(dimensions), _offset(0), _strides()
+        : tensor_impl()
     {
-        auto_strides();
-        
-        _rep->resize_(_dimensions.flat_size());
+		tensor_impl.resize(dimensions);
     }
 
     template<typename T>
     Tensor<T>::Tensor(const DimVector& dimensions, const DimVector& strides)
-        :_rep(new TensorStorage<T>),
-        _dimensions(dimensions), _offset(0), _strides(strides)
+        : tensor_impl()
     {
         auto_strides();
 
@@ -524,9 +446,6 @@ namespace traph
 			throw std::runtime_error("Dimension out of range");
 	}
 
-    template<typename T>
-	std::shared_ptr<StorageBase<T>>  Tensor<T>::storage() const { return _rep; }
-
     template<typename T>
     DimVector Tensor<T>::stride() const { return _strides; }