Skip to content

Commit

Permalink
[Kernel] Adds occa::null (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmed256 authored Dec 7, 2019
1 parent 711709e commit 7aff480
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
12 changes: 12 additions & 0 deletions include/occa/core/kernelArg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ namespace occa {
namespace kArgInfo {
static const char none = 0;
static const char usePointer = (1 << 0);
static const char isNull = (1 << 1);
}

class null_t {
public:
inline null_t() {}
};

extern const null_t null;

union kernelArgData_t {
uint8_t uint8_;
uint16_t uint16_;
Expand Down Expand Up @@ -54,6 +62,8 @@ namespace occa {

void* ptr() const;

bool isNull() const;

void setupForKernelCall(const bool isConst) const;
};

Expand All @@ -67,6 +77,8 @@ namespace occa {
kernelArg(const kernelArg &other);
kernelArg& operator = (const kernelArg &other);

kernelArg(const null_t arg);

kernelArg(const uint8_t arg);
kernelArg(const uint16_t arg);
kernelArg(const uint32_t arg);
Expand Down
5 changes: 3 additions & 2 deletions src/core/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ namespace occa {
lang::argMetadata_t &argInfo = metadata.arguments[i];

modeMemory_t *mem = arg.getModeMemory();
bool isPtr = (bool) mem;
const bool isNull = arg.isNull();
const bool isPtr = mem || isNull;
if (isPtr != argInfo.isPtr) {
if (argInfo.isPtr) {
OCCA_FORCE_ERROR("(" << name << ") Kernel expects an occa::memory for argument ["
Expand All @@ -118,7 +119,7 @@ namespace occa {
}
}

if (!isPtr) {
if (!isPtr || isNull) {
continue;
}

Expand Down
17 changes: 17 additions & 0 deletions src/core/kernelArg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace occa {
//---[ KernelArg ]--------------------
const null_t null;

kernelArgData::kernelArgData() :
modeMemory(NULL),
size(0),
Expand Down Expand Up @@ -45,6 +47,10 @@ namespace occa {
return ((info & kArgInfo::usePointer) ? data.void_ : (void*) &data);
}

bool kernelArgData::isNull() const {
return (info & kArgInfo::isNull);
}

void kernelArgData::setupForKernelCall(const bool isConst) const {
if (!modeMemory ||
!modeMemory->isManaged() ||
Expand Down Expand Up @@ -107,6 +113,17 @@ namespace occa {
return args[index];
}

kernelArg::kernelArg(const null_t arg) {
kernelArgData kArg;
kArg.data.void_ = NULL;
kArg.size = sizeof(void*);
kArg.info = (
kArgInfo::usePointer
| kArgInfo::isNull
);
args.push_back(kArg);
}

kernelArg::kernelArg(const uint8_t arg) {
kernelArgData kArg;
kArg.data.uint8_ = arg;
Expand Down
6 changes: 6 additions & 0 deletions tests/files/argKernel.okl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@kernel void argKernel(char *mem,
char *uvaPtr,
void *null,
int i8,
int u8,
int i16,
Expand All @@ -16,6 +17,7 @@
printf(
"mem: %d\n"
"uvaPtr: %d\n"
"null: %p\n"
"i8: %d\n"
"u8: %d\n"
"i16: %d\n"
Expand All @@ -30,6 +32,7 @@
"str: %s\n",
mem[0],
uvaPtr[0],
null,
(int) i8,
(int) u8,
(int) i16,
Expand All @@ -43,5 +46,8 @@
xy[0], xy[1],
str
);
if (null != NULL) {
throw 1;
}
}
}
1 change: 1 addition & 0 deletions tests/src/core/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ void testRun() {
argKernel(
mem,
uvaPtr,
occa::null,
(int8_t) 2,
(uint8_t) 3,
(int16_t) 4,
Expand Down

0 comments on commit 7aff480

Please sign in to comment.