Skip to content

Commit

Permalink
[dtype] Added dtype
Browse files Browse the repository at this point in the history
  • Loading branch information
dmed256 committed Feb 7, 2019
1 parent 44d6900 commit 54f4003
Show file tree
Hide file tree
Showing 27 changed files with 939 additions and 32 deletions.
1 change: 1 addition & 0 deletions include/occa.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <occa/c/base.h>
#include <occa/c/cli.h>
#include <occa/c/device.h>
#include <occa/c/dtype.h>
#include <occa/c/json.h>
#include <occa/c/kernel.h>
#include <occa/c/memory.h>
Expand Down
2 changes: 2 additions & 0 deletions include/occa.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

#include <occa/array.hpp>
#include <occa/core.hpp>
#include <occa/dtype.hpp>
#include <occa/io.hpp>
#include <occa/mode.hpp>
#include <occa/tools.hpp>
#include <occa/types.hpp>

#endif
10 changes: 10 additions & 0 deletions include/occa/c/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,19 @@ OCCA_LFUNC occaMemory OCCA_RFUNC occaMalloc(const occaUDim_t bytes,
const void *src,
occaProperties props);

OCCA_LFUNC occaMemory OCCA_RFUNC occaTypedMalloc(const occaUDim_t entries,
const occaDtype type,
const void *src,
occaProperties props);

OCCA_LFUNC void* OCCA_RFUNC occaUMalloc(const occaUDim_t bytes,
const void *src,
occaProperties props);

OCCA_LFUNC void* OCCA_RFUNC occaTypedUMalloc(const occaUDim_t entries,
const occaDtype type,
const void *src,
occaProperties props);
//======================================

OCCA_END_EXTERN_C
Expand Down
12 changes: 12 additions & 0 deletions include/occa/c/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,22 @@ OCCA_LFUNC occaMemory OCCA_RFUNC occaDeviceMalloc(occaDevice device,
const void *src,
occaProperties props);

OCCA_LFUNC occaMemory OCCA_RFUNC occaDeviceTypedMalloc(occaDevice device,
const occaUDim_t entries,
const occaDtype type,
const void *src,
occaProperties props);

OCCA_LFUNC void* OCCA_RFUNC occaDeviceUMalloc(occaDevice device,
const occaUDim_t bytes,
const void *src,
occaProperties props);

OCCA_LFUNC void* OCCA_RFUNC occaDeviceTypedUMalloc(occaDevice device,
const occaUDim_t entries,
const occaDtype type,
const void *src,
occaProperties props);
//======================================

OCCA_END_EXTERN_C
Expand Down
57 changes: 57 additions & 0 deletions include/occa/c/dtype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#ifndef OCCA_C_DTYPE_HEADER
#define OCCA_C_DTYPE_HEADER

#include <occa/c/defines.h>
#include <occa/c/types.h>

OCCA_START_EXTERN_C

//-----[ Methods ]----------------------
OCCA_LFUNC occaDtype OCCA_RFUNC occaCreateDtype(const char *name,
const int bytes);

OCCA_LFUNC const char* OCCA_RFUNC occaDtypeGetName(occaDtype type);
OCCA_LFUNC int OCCA_RFUNC occaDtypeGetBytes(occaDtype type);

OCCA_LFUNC void OCCA_RFUNC occaDtypeAddField(occaDtype type,
const char *field,
occaDtype fieldType);

OCCA_LFUNC int OCCA_RFUNC occaDtypeIsEqual(occaDtype a,
occaDtype b);

OCCA_LFUNC occaDtype OCCA_RFUNC occaDtypeFromJson(occaJson json);
OCCA_LFUNC occaDtype OCCA_RFUNC occaDtypeFromJsonString(const char *str);

OCCA_LFUNC occaJson OCCA_RFUNC occaDtypeToJson(occaDtype type);
//======================================

//-----[ Builtins ]---------------------
extern occaDtype occaDtypeNone;

extern occaDtype occaDtypeVoid;
extern occaDtype occaDtypeByte;

extern occaDtype occaDtypeBool;
extern occaDtype occaDtypeChar;
extern occaDtype occaDtypeShort;
extern occaDtype occaDtypeInt;
extern occaDtype occaDtypeLong;
extern occaDtype occaDtypeFloat;
extern occaDtype occaDtypeDouble;

extern occaDtype occaDtypeInt8;
extern occaDtype occaDtypeUint8;
extern occaDtype occaDtypeInt16;
extern occaDtype occaDtypeUint16;
extern occaDtype occaDtypeInt32;
extern occaDtype occaDtypeUint32;
extern occaDtype occaDtypeInt64;
extern occaDtype occaDtypeUint64;
extern occaDtype occaDtypeFloat32;
extern occaDtype occaDtypeFloat64;
//======================================

OCCA_END_EXTERN_C

#endif
2 changes: 2 additions & 0 deletions include/occa/c/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ typedef occaType occaMemory;
typedef occaType occaStream;
typedef occaType occaStreamTag;

typedef occaType occaDtype;
typedef occaType occaJson;
typedef occaType occaProperties;

Expand Down Expand Up @@ -75,6 +76,7 @@ extern const int OCCA_MEMORY;
extern const int OCCA_STREAM;
extern const int OCCA_STREAMTAG;

extern const int OCCA_DTYPE;
extern const int OCCA_JSON;
extern const int OCCA_PROPERTIES;
//======================================
Expand Down
9 changes: 7 additions & 2 deletions include/occa/c/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ namespace occa {
static const int stream = 19;
static const int streamTag = 20;

static const int json = 21;
static const int properties = 22;
static const int dtype = 21;
static const int json = 22;
static const int properties = 23;
}

occaType defaultOccaType();
Expand Down Expand Up @@ -91,6 +92,8 @@ namespace occa {
occaType newOccaType(occa::stream stream);
occaType newOccaType(occa::streamTag streamTag);

occaType newOccaType(const dtype &type);

occaType newOccaType(const json &json,
const bool needsFree);

Expand All @@ -111,6 +114,8 @@ namespace occa {
occa::primitive primitive(occaType value,
const int type);

occa::dtype& dtype(occaType value);

occa::json& json(occaType value);
occa::json inferJson(occaType value);

Expand Down
10 changes: 10 additions & 0 deletions include/occa/core/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,20 @@ namespace occa {
//====================================

//---[ Memory Functions ]-------------
occa::memory malloc(const dim_t entries,
const dtype &type,
const void *src = NULL,
const occa::properties &props = occa::properties());

occa::memory malloc(const dim_t bytes,
const void *src = NULL,
const occa::properties &props = occa::properties());

void* umalloc(const dim_t entries,
const dtype &type,
const void *src = NULL,
const occa::properties &props = occa::properties());

void* umalloc(const dim_t bytes,
const void *src = NULL,
const occa::properties &props = occa::properties());
Expand Down
29 changes: 29 additions & 0 deletions include/occa/core/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <occa/defines.hpp>
#include <occa/core/kernel.hpp>
#include <occa/core/stream.hpp>
#include <occa/dtype.hpp>
#include <occa/tools/gc.hpp>
#include <occa/tools/uva.hpp>

Expand Down Expand Up @@ -228,6 +229,20 @@ namespace occa {
// |===============================

// |---[ Memory ]------------------
occa::memory malloc(const dim_t entries,
const dtype &type,
const void *src = NULL,
const occa::properties &props = occa::properties());

occa::memory malloc(const dim_t entries,
const dtype &type,
const occa::memory src,
const occa::properties &props = occa::properties());

occa::memory malloc(const dim_t entries,
const dtype &type,
const occa::properties &props);

occa::memory malloc(const dim_t bytes,
const void *src = NULL,
const occa::properties &props = occa::properties());
Expand All @@ -239,6 +254,20 @@ namespace occa {
occa::memory malloc(const dim_t bytes,
const occa::properties &props);

void* umalloc(const dim_t entries,
const dtype &type,
const void *src = NULL,
const occa::properties &props = occa::properties());

void* umalloc(const dim_t entries,
const dtype &type,
const occa::memory src,
const occa::properties &props = occa::properties());

void* umalloc(const dim_t entries,
const dtype &type,
const occa::properties &props);

void* umalloc(const dim_t bytes,
const void *src = NULL,
const occa::properties &props = occa::properties());
Expand Down
6 changes: 6 additions & 0 deletions include/occa/core/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <iostream>

#include <occa/defines.hpp>
#include <occa/dtype.hpp>
#include <occa/tools/gc.hpp>
#include <occa/tools/properties.hpp>

Expand Down Expand Up @@ -36,6 +37,7 @@ namespace occa {

occa::modeDevice_t *modeDevice;

const dtype *type;
udim_t size;
bool isOrigin;

Expand Down Expand Up @@ -147,7 +149,11 @@ namespace occa {
const std::string& mode() const;
const occa::properties& properties() const;

void setType(const dtype &type);
const dtype& getType();

udim_t size() const;
udim_t length() const;

template <class TM>
udim_t size() const {
Expand Down
7 changes: 7 additions & 0 deletions include/occa/dtype.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef OCCA_DTYPE_HEADER
#define OCCA_DTYPE_HEADER

#include <occa/dtype/dtype.hpp>
#include <occa/dtype/builtins.hpp>

#endif
34 changes: 34 additions & 0 deletions include/occa/dtype/builtins.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef OCCA_DTYPE_BUILTINS_HEADER
#define OCCA_DTYPE_BUILTINS_HEADER

#include <occa/dtype/dtype.hpp>

namespace occa {
namespace dtypes {
extern dtype none;

extern dtype void_;
extern dtype byte;

extern dtype bool_;
extern dtype char_;
extern dtype short_;
extern dtype int_;
extern dtype long_;
extern dtype float_;
extern dtype double_;

extern dtype int8;
extern dtype uint8;
extern dtype int16;
extern dtype uint16;
extern dtype int32;
extern dtype uint32;
extern dtype int64;
extern dtype uint64;
extern dtype float32;
extern dtype float64;
}
}

#endif
65 changes: 65 additions & 0 deletions include/occa/dtype/dtype.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#ifndef OCCA_DTYPE_DTYPE_HEADER
#define OCCA_DTYPE_DTYPE_HEADER

#include <iostream>
#include <map>
#include <vector>

#include <occa/tools/json.hpp>


namespace occa {
class dtype;
class dtypeField;

typedef std::map<std::string, dtype*> dtypeNameMap_t;

class dtype {
private:
std::string name;
int bytes;
std::vector<dtypeField> fields;

public:
dtype(const std::string &name_);
dtype(const std::string &name_,
const int bytes_);
dtype(const dtype &other);

const std::string& getName() const;
int getBytes() const;

dtype& addField(const std::string field,
const dtype &type);

bool operator == (const dtype &other) const;
bool operator != (const dtype &other) const;

static const dtype& byName(const std::string name);

static dtype fromJson(const std::string &str);
static dtype fromJson(const json &j);

json toJson() const;

friend std::ostream& operator << (std::ostream &out,
const dtype &type);
};

std::ostream& operator << (std::ostream &out,
const dtype &type);
class dtypeField {
friend class dtype;

private:
std::string name;
dtype type;

public:
dtypeField(const std::string &name_,
const dtype &type_);
dtypeField(const dtypeField &other);
};
}

#endif
Loading

0 comments on commit 54f4003

Please sign in to comment.