Ver Fonte

opencl create context refactor

JasonWang há 7 anos atrás
pai
commit
4ecfac6403

+ 1 - 0
README.md

@@ -1 +1,2 @@
 # traph
+Traph is a open-source machine learning platform.

+ 44 - 5
traph/include/traph/cltensor/clcontext.h

@@ -2,6 +2,7 @@
 #define TRAPH_CLTENSOR_CLCONTEXT_H_
 
 #include <stdexcept>
+#include <cstring>
 #include <vector>
 
 #if defined(__APPLE__) || defined(__MACOSX)
@@ -17,12 +18,8 @@ namespace traph
     private:
         cl_context _context;
     public:
-        CLContext()
+        CLContext(cl_context_properties* cprops)
             :_context(nullptr)
-        {
-        }
-
-        void create_context(cl_context_properties* cprops)
         {
             cl_int status = 0;
             _context = clCreateContextFromType(
@@ -75,6 +72,48 @@ namespace traph
 
             return devices;
         }
+
+        cl_program create_program(const char* kernelSourceCode)
+        {
+            if(_context == nullptr)
+                return {};
+
+            cl_int status = 0;
+
+            size_t sourceSize[] = {strlen(kernelSourceCode)};
+            cl_program program = clCreateProgramWithSource(_context,
+                                1,
+                                &kernelSourceCode,
+                                sourceSize,
+                                &status);
+            if (status != CL_SUCCESS)
+            {
+                throw std::runtime_error("Error: Loading Binary into cl_program (clCreateProgramWithBinary)\n");
+            }
+
+            return program;
+        }
+
+        cl_mem create_buffer(std::size_t size)
+        {
+            if(_context == nullptr)
+                return {};
+
+            cl_int status = 0;
+
+            cl_mem outputBuffer = clCreateBuffer(
+									_context,
+									CL_MEM_ALLOC_HOST_PTR,
+									size,
+									NULL,
+									&status);
+            if (status != CL_SUCCESS)
+            {
+                throw std::runtime_error("Error: Create Buffer, outputBuffer. (clCreateBuffer)\n");
+            }
+
+            return outputBuffer;
+        }
     };
 }
 

+ 12 - 9
traph/include/traph/cltensor/clplatform.h

@@ -4,6 +4,7 @@
 #include <stdexcept>
 #include <vector>
 
+#include <traph/cltensor/clcontext.h>
 
 #if defined(__APPLE__) || defined(__MACOSX)
 #include <OpenCL/cl.hpp>
@@ -16,12 +17,7 @@ namespace traph
     class CLPlatform
     {
     public:
-        CLPlatform()
-        {
-
-        }
-
-        int platform_num()
+        static int platform_num()
         {
             cl_int status = 0;
             cl_uint numPlatforms;
@@ -35,7 +31,7 @@ namespace traph
             return numPlatforms;
         }
 
-        std::vector<cl_platform_id> get_all_platforms()
+        static std::vector<cl_platform_id> get_all_platforms()
         {
             cl_int status = 0;
 			cl_uint numPlatforms = platform_num();
@@ -54,7 +50,7 @@ namespace traph
             return {};
         }
 
-        cl_platform_id select_first_platform(const std::vector<cl_platform_id>& platforms)
+        static cl_platform_id select_first_platform(const std::vector<cl_platform_id>& platforms)
         {
             cl_int status = 0;
             cl_platform_id platform = nullptr;
@@ -72,7 +68,7 @@ namespace traph
             }
         }
 
-        cl_context_properties* get_context_property(cl_platform_id platform)
+        static cl_context_properties* get_context_property(cl_platform_id platform)
         {
             cl_context_properties cps[3] = {
                 CL_CONTEXT_PLATFORM,
@@ -83,6 +79,13 @@ namespace traph
             cl_context_properties *cprops = (nullptr == platform) ? nullptr : cps;
             return cps;
         }
+
+        static CLContext create_context()
+        {
+            auto platforms = get_all_platforms();
+            auto selected = select_first_platform(platforms);
+            return CLContext(get_context_property(selected));
+        }
     };
 }
 

+ 39 - 7
traph/include/traph/cltensor/cltensor.h

@@ -1,6 +1,7 @@
 #ifndef TRAPH_CLTENSOR_CLTENSOR_H_
 #define TRAPH_CLTENSOR_CLTENSOR_H_
 
+#include <traph/core/type.h>
 #include <traph/core/tensor.h>
 
 #if defined(__APPLE__) || defined(__MACOSX)
@@ -25,18 +26,49 @@ namespace traph
     class CLTensor: public TensorBase<T>
     {
     public:
-        virtual void reshape(const DimVector& dims) = 0;
+        virtual platform_type platform() override
+        {
+            return platform_type::opencl;
+        }
 
-		virtual idx_type offset() const = 0;
+        virtual device_id device() override
+        {
+            return 0;
+        }
 
-		virtual layout_type layout() const = 0;
+        virtual void reshape(const DimVector& dims) override
+        {
 
-		virtual DimVector size() const = 0;
+        }
 
-		virtual const T* data() const = 0;
-		virtual T* data() = 0;
+		virtual idx_type offset() const override
+        {
 
-		virtual DimVector strides() const = 0;
+        }
+
+		virtual layout_type layout() const override
+        {
+
+        }
+
+		virtual DimVector size() const override
+        {
+
+        }
+
+		virtual const T* data() const override
+        {
+
+        }
+		virtual T* data() override
+        {
+
+        }
+
+		virtual DimVector strides() const override
+        {
+            
+        }
 
     };
 }

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

@@ -23,6 +23,10 @@ namespace traph
     class TensorBase
     {
     public:
+        virtual platform_type platform() = 0;
+
+        virtual device_id device() = 0;
+
         virtual void reshape(const DimVector& dims) = 0;
 
         virtual void resize(const DimVector& dims) = 0;

+ 10 - 0
traph/include/traph/core/type.h

@@ -17,12 +17,22 @@ namespace traph
     using u64 = std::uint64_t;
     using idx_type = i32;
     using size_type = i32;
+    using device_id = i32;
 
     enum layout_type
     {
         row_major,
         column_major
     };
+
+    enum platform_type
+    {
+        none,
+        cuda,
+        opencl,
+        vulkan,
+        opengl
+    };
 }
 
 #endif

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

@@ -0,0 +1,32 @@
+#ifndef TRAPH_CORE_VARIABLE_H_
+#define TRAPH_CORE_VARIABLE_H_
+
+
+namespace traph
+{
+    template<class T>
+    class VariableBase
+    {
+    public:
+        virtual platform_type platform() = 0;
+
+        virtual device_id device() = 0;
+        
+        virtual void reshape(const DimVector& dims) = 0;
+
+        virtual void resize(const DimVector& dims) = 0;
+
+		virtual idx_type offset() const = 0;
+
+		virtual layout_type layout() const = 0;
+
+		virtual DimVector size() const = 0;
+
+		virtual const T* data() const = 0;
+		virtual T* data() = 0;
+
+		virtual DimVector strides() const = 0;
+    };
+}
+
+#endif

+ 18 - 0
traph/include/traph/nn/arithmetic.h

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

+ 44 - 3
traph/include/traph/nn/variable.h

@@ -6,16 +6,18 @@
 
 #include <traph/core/index.h>
 #include <traph/core/tensor.h>
+#include <traph/core/variable.h>
 #include <traph/tensor/tensor.h>
 
 namespace traph
 {
     template<class T>
-    class Variable
+    class Variable: public VariableBase<T>
     {
     private:
         std::unique_ptr<TensorBase<T>> _data;
         std::unique_ptr<TensorBase<T>> _grad;
+        bool _requires_grad;
     public:
         Variable()
         {
@@ -23,12 +25,12 @@ namespace traph
         }
 
         Variable(const DimVector& dim)
-            :_data(new Tensor<T>(dim)), _grad(new Tensor<T>(dim))
+            :_data(new Tensor<T>(dim)), _grad(new Tensor<T>(dim)), _requires_grad(false)
         {
         }
 
         Variable(std::initializer_list<idx_type> l)
-            :_data(new Tensor<T>()), _grad(new Tensor<T>())
+            :_data(new Tensor<T>()), _grad(new Tensor<T>()), _requires_grad(false)
         {
             DimVector dim;
             for (auto i : l)
@@ -42,6 +44,45 @@ namespace traph
         {
 
         }
+
+        virtual void reshape(const DimVector& dims) override
+        {
+
+        }
+
+        virtual void resize(const DimVector& dims) override
+        {
+
+        }
+
+		virtual idx_type offset() const override
+        {
+
+        }
+
+		virtual layout_type layout() const override
+        {
+
+        }
+
+		virtual DimVector size() const override
+        {
+
+        }
+
+		virtual const T* data() const override
+        {
+
+        }
+		virtual T* data() override
+        {
+
+        }
+
+		virtual DimVector strides() const override
+        {
+
+        }
     };
 }
 

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

@@ -243,12 +243,22 @@ namespace traph
         {
         }
 
-        void reshape(const DimVector& dims)
+        virtual platform_type platform() override
+        {
+            return platform_type::none;
+        }
+
+        virtual device_id device() override
+        {
+            return 0;
+        }
+
+        virtual void reshape(const DimVector& dims) override
         {
 
         }
 
-        void resize(const DimVector& dims)
+        virtual void resize(const DimVector& dims) override
         {
             _dimensions = dims;
             _rep->resize_(dims.flat_size());