Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Allow passing general objects by value to kernels #676

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions include/occa/core/kernelArg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ namespace occa {
addPointer((void*) const_cast<type4<T>*>(&arg), sizeof(type4<T>));
}

template <class T>
kernelArg(const T &arg) {
addPointer((void*) const_cast<T*>(&arg), sizeof(T));
}

int size() const;

device getDevice() const;
Expand All @@ -104,6 +109,15 @@ namespace occa {
static int argumentCount(const std::vector<kernelArg> &arguments);
};

template <>
kernelArg::kernelArg(const char &arg);

template <>
kernelArg::kernelArg(const unsigned char &arg);

template <>
kernelArg::kernelArg(const memory &arg);

template <>
kernelArg::kernelArg(modeMemory_t *arg);

Expand Down Expand Up @@ -181,6 +195,9 @@ namespace occa {
template <>
hash_t hash(const occa::scopeKernelArg &arg);
//====================================

template <>
kernelArg::kernelArg(const scopeKernelArg &arg);
}

#endif
22 changes: 21 additions & 1 deletion src/core/kernelArg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace occa {
}

bool kernelArgData::isPointer() const {
return value.isPointer();
return ptrSize ? false : value.isPointer();
}

//====================================
Expand All @@ -75,6 +75,21 @@ namespace occa {

kernelArg::~kernelArg() {}

template <>
kernelArg::kernelArg(const char &arg) {
primitiveConstructor(static_cast<int8_t>(arg));
}

template <>
kernelArg::kernelArg(const unsigned char &arg) {
primitiveConstructor(static_cast<uint8_t>(arg));
}

template <>
kernelArg::kernelArg(const memory &arg) {
addMemory(arg.getModeMemory());
}

template <>
kernelArg::kernelArg(modeMemory_t *arg) {
addMemory(arg);
Expand All @@ -85,6 +100,11 @@ namespace occa {
addMemory(const_cast<modeMemory_t*>(arg));
}

template <>
kernelArg::kernelArg(const scopeKernelArg &arg) {
args = arg.args;
}

int kernelArg::size() const {
return (int) args.size();
}
Expand Down
18 changes: 11 additions & 7 deletions tests/files/argKernel.okl
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
typedef struct {
double x, y;
} mystruct;

@kernel void argKernel(void *nullPtr,
int *mem,
char i8,
char u8,
unsigned char u8,
short i16,
short u16,
unsigned short u16,
int i32,
int u32,
unsigned int u32,
long i64,
long u64,
unsigned long u64,
float f,
double d,
int *xy,
mystruct xy,
const char *str) {
for (int i = 0; i < 1; ++i; @tile(1, @outer, @inner)) {
// Note: NULL is not 0 because some backends don't handle NULL arguments properly
Expand All @@ -27,7 +31,7 @@
"u64: %d\n"
"f: %f\n"
"d: %f\n"
"xy: [%d, %d]\n"
"xy: [%f, %f]\n"
"str: %s\n",
nullPtr,
(int) mem[0],
Expand All @@ -41,7 +45,7 @@
(int) u64,
f,
d,
xy[0], xy[1],
xy.x, xy.y,
str
);
if (i8 != 3) {
Expand Down
13 changes: 9 additions & 4 deletions tests/src/c/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ void testRun() {
occaMemory mem = occaMalloc(1 * sizeof(int), &value, occaDefault);
value = 2;

int xy[2] = {13, 14};
struct {
double x,y;
} xy;
xy.x = 13.0;
xy.y = 14.0;

std::string str = "fifteen";

// Good argument types
Expand All @@ -128,7 +133,7 @@ void testRun() {
occaUInt64(10),
occaFloat(11.0),
occaDouble(12.0),
occaStruct(xy, sizeof(xy)),
occaStruct(&xy, sizeof(xy)),
occaString(str.c_str())
);

Expand All @@ -146,7 +151,7 @@ void testRun() {
occaKernelPushArg(argKernel, occaUInt64(10));
occaKernelPushArg(argKernel, occaFloat(11.0));
occaKernelPushArg(argKernel, occaDouble(12.0));
occaKernelPushArg(argKernel, occaStruct(xy, sizeof(xy)));
occaKernelPushArg(argKernel, occaStruct(&xy, sizeof(xy)));
occaKernelPushArg(argKernel, occaString(str.c_str()));
occaKernelRunFromArgs(argKernel);

Expand All @@ -164,7 +169,7 @@ void testRun() {
occaUInt64(10),
occaFloat(11.0),
occaDouble(12.0),
occaStruct(xy, sizeof(xy)),
occaStruct(&xy, sizeof(xy)),
occaString(str.c_str())
};

Expand Down
8 changes: 7 additions & 1 deletion tests/src/core/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,13 @@ void testRun() {
int value = 1;
occa::memory mem = occa::malloc<int>(1, &value);

int xy[2] = {13, 14};
struct {
double x;
double y;
} xy;
xy.x = 13.0;
xy.y = 14.0;

std::string str = "fifteen";

argKernel(
Expand Down