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

[Functional] Adds occa::function and occa::array #442

Merged
merged 20 commits into from
Jan 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
[Array] Adds copy methods
  • Loading branch information
dmed256 committed Jan 17, 2021
commit 2bec6ae7cb5c61cf9a52443b53a9322543e7309c
102 changes: 83 additions & 19 deletions include/occa/experimental/functional/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,20 @@ namespace occa {
tileSize(-1),
tileIterations(-1) {}

array(const dim_t size) :
tileSize(-1),
tileIterations(-1) {

memory_ = occa::malloc<TM>(size);
}

array(occa::device device, const dim_t size) :
tileSize(-1),
tileIterations(-1) {

memory_ = device.malloc<TM>(size);
}

array(occa::memory mem) :
memory_(mem),
tileSize(-1),
Expand All @@ -331,24 +345,37 @@ namespace occa {
return *this;
}

bool isInitialized() const {
return memory_.isInitialized();
bool usingNativeCpuMode() const {
const std::string &mode = getDevice().mode();
return (mode == "Serial" || mode == "OpenMP");
}

array clone() const {
return array(memory_.clone());
void setTileSize(const int tileSize_) {
if (tileSize_ >= 0) {
tileSize = tileSize_;
}
}

void setTileIterations(const int tileIterations_) {
if (tileIterations_ >= 0) {
tileIterations = tileIterations_;
}
}

//---[ Memory methods ]-------------
bool isInitialized() const {
return memory_.isInitialized();
}

occa::device getDevice() const {
return memory_.getDevice();
}

bool usingNativeCpuMode() const {
const std::string &mode = getDevice().mode();
return (mode == "Serial" || mode == "OpenMP");
occa::memory memory() const {
return memory_;
}

occa::memory memory() const {
operator occa::memory () const {
return memory_;
}

Expand All @@ -360,21 +387,58 @@ namespace occa {
return memory_;
}

void setTileSize(const int tileSize_) {
if (tileSize_ >= 0) {
tileSize = tileSize_;
}
udim_t length() const {
return memory_.length<TM>();
}

void setTileIterations(const int tileIterations_) {
if (tileIterations_ >= 0) {
tileIterations = tileIterations_;
}
array clone() const {
return array(memory_.clone());
}

udim_t length() const {
return memory_.length<TM>();
void copyFrom(const TM *src,
const dim_t entries = -1) {
const dim_t safeEntries = (
entries <= 0
? length()
: entries
);

memory_.copyFrom(src, safeEntries * sizeof(TM));
}

void copyFrom(const occa::memory src,
const dim_t entries = -1) {
const dim_t safeEntries = (
entries <= 0
? length()
: entries
);

memory_.copyFrom(src, safeEntries * sizeof(TM));
}

void copyTo(TM *dest,
const dim_t entries = -1) const {
const dim_t safeEntries = (
entries <= 0
? length()
: entries
);

memory_.copyTo(dest, safeEntries * sizeof(TM));
}

void copyTo(occa::memory dest,
const dim_t entries = -1) const {
const dim_t safeEntries = (
entries <= 0
? length()
: entries
);

memory_.copyTo(dest, safeEntries * sizeof(TM));
}
//==================================

private:
//---[ Lambda methods ]-------------
Expand Down Expand Up @@ -783,7 +847,7 @@ namespace occa {
array slice(const dim_t offset,
const dim_t count = -1) const {
return array(
memory_.slice(offset, count).clone()
memory_.slice(offset, count)
);
}

Expand Down
18 changes: 9 additions & 9 deletions tests/src/experimental/functional/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -705,8 +705,8 @@ void testConcat(occa::device device) {
h_three[i] = -1;
}

two.memory().copyTo(h_two);
three.memory().copyTo(h_three);
two.copyTo(h_two);
three.copyTo(h_three);

for (int i = 0; i < (2 * ctx.length); ++i) {
ASSERT_EQ(i % ctx.length, h_two[i]);
Expand All @@ -728,14 +728,14 @@ void testFill(occa::device device) {
}

ctx.array.fill(5);
ctx.array.memory().copyTo(ctx.values);
ctx.array.copyTo(ctx.values);

for (int i = 0; i < ctx.length; ++i) {
ASSERT_EQ(5, ctx.values[i]);
}

ctx.array.fill(6);
ctx.array.memory().copyTo(ctx.values);
ctx.array.copyTo(ctx.values);

for (int i = 0; i < ctx.length; ++i) {
ASSERT_EQ(6, ctx.values[i]);
Expand Down Expand Up @@ -768,7 +768,7 @@ void testIndexOf(occa::device device) {
for (int i = 0; i < ctx.length; ++i) {
ctx.values[i] = i % 2;
}
ctx.array.memory().copyFrom(ctx.values);
ctx.array.copyFrom(ctx.values);

ASSERT_EQ(
0,
Expand All @@ -792,7 +792,7 @@ void testLastIndexOf(occa::device device) {
for (int i = 0; i < ctx.length; ++i) {
ctx.values[i] = i % 2;
}
ctx.array.memory().copyFrom(ctx.values);
ctx.array.copyFrom(ctx.values);

ASSERT_EQ(
ctx.length - 2,
Expand Down Expand Up @@ -846,7 +846,7 @@ void testReverse(occa::device device) {

int *reversedValues = new int[ctx.length];

reversedArray.memory().copyTo(reversedValues);
reversedArray.copyTo(reversedValues);

for (int i = 0; i < ctx.length; ++i) {
ASSERT_EQ(
Expand All @@ -868,7 +868,7 @@ void testShiftLeft(occa::device device) {
ASSERT_EQ(ctx.maxValue, ctx.array.max());

int *shiftedValues = new int[ctx.length];
shiftedArray.memory().copyTo(shiftedValues);
shiftedArray.copyTo(shiftedValues);

for (int i = 0; i < (ctx.length - 3); ++i) {
ASSERT_EQ(
Expand All @@ -894,7 +894,7 @@ void testShiftRight(occa::device device) {
ASSERT_EQ(ctx.maxValue, ctx.array.max());

int *shiftedValues = new int[ctx.length];
shiftedArray.memory().copyTo(shiftedValues);
shiftedArray.copyTo(shiftedValues);

for (int i = 0; i < 3; ++i) {
ASSERT_EQ(-1, shiftedValues[i]);
Expand Down