1 module dopt.core.types; 2 3 import std.algorithm; 4 5 enum DataType 6 { 7 float32, 8 int32 9 } 10 11 size_t sizeOf(DataType t) 12 { 13 switch(t) 14 { 15 case DataType.float32: 16 return float.sizeof; 17 18 case DataType.int32: 19 return int.sizeof; 20 21 default: 22 throw new Exception("Not implemented"); 23 } 24 } 25 26 struct TensorType 27 { 28 DataType elementType; 29 size_t[] shape; 30 31 this(DataType et, size_t[] s) 32 { 33 elementType = et; 34 shape = s.dup; 35 } 36 37 this(const TensorType t) 38 { 39 elementType = t.elementType; 40 shape = t.shape.dup; 41 } 42 43 bool opEquals(const TensorType t) const 44 { 45 return elementType == t.elementType && shape == t.shape; 46 } 47 48 @property size_t rank() const 49 { 50 return shape.length; 51 } 52 53 @property size_t volume() const 54 { 55 return shape.fold!((a, b) => a * b)(cast(size_t)1); 56 } 57 } 58 59 class DeviceBuffer 60 { 61 abstract size_t numBytes() const; 62 abstract void get(void[] buf) const; 63 abstract void set(const void[] buf); 64 65 T[] get(T)() const 66 { 67 auto ret = new T[numBytes / T.sizeof]; 68 get(ret); 69 70 return ret; 71 } 72 73 void set(const DeviceBuffer buf) 74 { 75 auto tmp = buf.get!ubyte(); 76 set(tmp); 77 } 78 }