Skip to content

Commit

Permalink
[UM] Added templated umalloc
Browse files Browse the repository at this point in the history
  • Loading branch information
dmed256 committed Jul 2, 2019
1 parent d6c6bdf commit 92ffb58
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 79 deletions.
2 changes: 1 addition & 1 deletion examples/c/1_add_vectors/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ int main(int argc, const char **argv) {
occaCopyMemToPtr(ab, o_ab, occaAllBytes, 0, occaDefault);

// Assert values
for (i = 0; i < 5; ++i) {
for (i = 0; i < entries; ++i) {
printf("%d = %f\n", i, ab[i]);
}
for (i = 0; i < entries; ++i) {
Expand Down
4 changes: 2 additions & 2 deletions examples/c/2_background_device/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ int main(int argc, const char **argv) {
// making it safe to use them again
occaFinish();

for (i = 0; i < 5; ++i)
for (i = 0; i < entries; ++i) {
printf("%d = %f\n", i, ab[i]);

}
for (i = 0; i < entries; ++i) {
if (ab[i] != (a[i] + b[i])) {
exit(1);
Expand Down
2 changes: 1 addition & 1 deletion examples/cpp/01_add_vectors/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ int main(int argc, const char **argv) {
o_ab.copyTo(ab);

// Assert values
for (int i = 0; i < 5; ++i) {
for (int i = 0; i < entries; ++i) {
std::cout << i << ": " << ab[i] << '\n';
}
for (int i = 0; i < entries; ++i) {
Expand Down
10 changes: 5 additions & 5 deletions examples/cpp/02_background_device/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ int main(int argc, const char **argv) {

int entries = 5;

float *a = (float*) occa::umalloc(entries, occa::dtype::float_);
float *b = (float*) occa::umalloc(entries, occa::dtype::float_);
float *ab = (float*) occa::umalloc(entries, occa::dtype::float_);
float *a = occa::umalloc<float>(entries);
float *b = occa::umalloc<float>(entries);
float *ab = occa::umalloc<float>(entries);

for (int i = 0; i < entries; ++i) {
a[i] = i;
Expand All @@ -47,9 +47,9 @@ int main(int argc, const char **argv) {
// making it safe to use them again
occa::finish();

for (int i = 0; i < 5; ++i)
for (int i = 0; i < entries; ++i) {
std::cout << i << ": " << ab[i] << '\n';

}
for (int i = 0; i < entries; ++i) {
if (ab[i] != (a[i] + b[i]))
throw 1;
Expand Down
29 changes: 11 additions & 18 deletions examples/cpp/03_inline_kernels/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,16 @@ int main(int argc, const char **argv) {

int entries = 5;

float *a = new float[entries];
float *b = new float[entries];
float *ab = new float[entries];
float *a = occa::umalloc<float>(entries);
float *b = occa::umalloc<float>(entries);
float *ab = occa::umalloc<float>(entries);

for (int i = 0; i < entries; ++i) {
a[i] = i;
b[i] = 1 - i;
ab[i] = 0;
}

occa::memory o_a = occa::malloc(entries, occa::dtype::float_, a);
occa::memory o_b = occa::malloc(entries, occa::dtype::float_, b);
occa::memory o_ab = occa::malloc(entries, occa::dtype::float_);

// Props are
occa::properties props;
props["defines/TILE_SIZE"] = 16;
Expand All @@ -49,35 +45,32 @@ int main(int argc, const char **argv) {
// - Resolved once 'auto' is supported. Function arguments of
// type 'auto' will act as templated typed variables
//
// - Cannot use unified memory from occa::umalloc
// - dtype::get<> needs to check a pointer type
//
// ~ Cannot use external functions
// - Potentially can happen with another macro OCCA_INLINED_FUNCTION
OCCA_INLINED_KERNEL(
(entries, o_a, o_b, o_ab),
(entries, a, b, ab),
props,
(
for (int i = 0; i < entries; ++i; @tile(TILE_SIZE, @outer, @inner)) {
o_ab[i] = o_a[i] + o_b[i];
ab[i] = a[i] + b[i];
}
)
);

// Copy result to the host
o_ab.copyTo(ab);
occa::finish();

for (int i = 0; i < 5; ++i)
for (int i = 0; i < entries; ++i) {
std::cout << i << ": " << ab[i] << '\n';

}
for (int i = 0; i < entries; ++i) {
if (ab[i] != (a[i] + b[i]))
throw 1;
}

delete [] a;
delete [] b;
delete [] ab;
occa::free(a);
occa::free(b);
occa::free(ab);

return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/cpp/05_building_kernels/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ int main(int argc, const char **argv) {

o_vec2.copyTo(vec2);

for (int i = 0; i < 5; ++i)
for (int i = 0; i < entries; ++i) {
std::cout << vec[i] << "^2 = " << vec2[i] << '\n';

}
for (int i = 0; i < entries; ++i) {
if (vec2[i] != (vec[i] * vec[i]))
throw 1;
Expand Down
2 changes: 1 addition & 1 deletion examples/cpp/06_unified_memory/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int main(int argc, const char **argv) {
// making it safe to use them again
occa::finish();

for (int i = 0; i < 5; ++i) {
for (int i = 0; i < entries; ++i) {
std::cout << i << ": " << ab[i] << '\n';
}
for (int i = 0; i < entries; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion examples/cpp/interop_examples/cuda/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ int main(int argc, char **argv) {

o_ab.copyTo(ab);

for (int i = 0; i < 5; ++i) {
for (int i = 0; i < entries; ++i) {
std::cout << i << ": " << ab[i] << '\n';
}

Expand Down
2 changes: 1 addition & 1 deletion examples/cpp/interop_examples/native/addVectors/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int main(int argc, char **argv) {

o_ab.copyTo(ab);

for (int i = 0; i < 5; ++i) {
for (int i = 0; i < entries; ++i) {
std::cout << i << ": " << ab[i] << '\n';
}
for (int i = 0; i < entries; ++i) {
Expand Down
2 changes: 1 addition & 1 deletion examples/cpp/interop_examples/opencl/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ int main(int argc, char **argv) {

o_ab.copyTo(ab);

for (int i = 0; i < 5; ++i) {
for (int i = 0; i < entries; ++i) {
std::cout << i << ": " << ab[i] << '\n';
}

Expand Down
2 changes: 1 addition & 1 deletion examples/cpp/interop_examples/openmp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int main(int argc, char **argv) {

o_ab.copyTo(ab);

for (int i = 0; i < 5; ++i) {
for (int i = 0; i < entries; ++i) {
std::cout << i << ": " << ab[i] << '\n';
}

Expand Down
9 changes: 6 additions & 3 deletions include/occa/core/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,12 @@ namespace occa {
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());
template <class TM>
TM* umalloc(const dim_t bytes,
const void *src = NULL,
const occa::properties &props = occa::properties()) {
return (TM*) umalloc(bytes, dtype::get<TM>(), src, props);
}

void memcpy(void *dest, const void *src,
const dim_t bytes,
Expand Down
29 changes: 19 additions & 10 deletions include/occa/core/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
#include <iostream>
#include <sstream>

#include <occa/defines.hpp>
#include <occa/core/kernel.hpp>
#include <occa/core/memory.hpp>
#include <occa/core/stream.hpp>
#include <occa/defines.hpp>
#include <occa/dtype.hpp>
#include <occa/io/output.hpp>
#include <occa/tools/gc.hpp>
#include <occa/tools/uva.hpp>

namespace occa {
class modeKernel_t; class kernel;
class modeMemory_t; class memory;
class modeDevice_t; class device;
class modeStreamTag_t; class streamTag;
class deviceInfo;
Expand Down Expand Up @@ -270,16 +270,25 @@ namespace occa {
const dtype_t &dtype,
const occa::properties &props);

void* umalloc(const dim_t bytes,
const void *src = NULL,
const occa::properties &props = occa::properties());
template <class TM>
TM* umalloc(const dim_t entries,
const void *src = NULL,
const occa::properties &props = occa::properties()) {
return (TM*) umalloc(entries, dtype::get<TM>(), src, props);
}

void* umalloc(const dim_t bytes,
const occa::memory src,
const occa::properties &props = occa::properties());
template <class TM>
TM* umalloc(const dim_t entries,
const occa::memory src,
const occa::properties &props = occa::properties()) {
return (TM*) umalloc(entries, dtype::get<TM>(), src, props);
}

void* umalloc(const dim_t bytes,
const occa::properties &props);
template <class TM>
TM* umalloc(const dim_t entries,
const occa::properties &props) {
return (TM*) umalloc(entries, dtype::get<TM>(), props);
}
// |===============================
};

Expand Down
8 changes: 4 additions & 4 deletions src/c/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@ void* OCCA_RFUNC occaUMalloc(const occaUDim_t bytes,
occaProperties props) {

if (occa::c::isDefault(props)) {
return occa::umalloc(bytes, src);
return occa::umalloc<void>(bytes, src);
}
return occa::umalloc(bytes,
src,
occa::c::properties(props));
return occa::umalloc<void>(bytes,
src,
occa::c::properties(props));
}

void* OCCA_RFUNC occaTypedUMalloc(const occaUDim_t entries,
Expand Down
9 changes: 4 additions & 5 deletions src/c/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,13 @@ void* OCCA_RFUNC occaDeviceUMalloc(occaDevice device,
occa::device device_ = occa::c::device(device);

if (occa::c::isDefault(props)) {
return device_.umalloc(bytes, src);
return device_.umalloc<void>(bytes, src);
}
return device_.umalloc(bytes,
src,
occa::c::properties(props));
return device_.umalloc<void>(bytes,
src,
occa::c::properties(props));
}


void* OCCA_RFUNC occaDeviceTypedUMalloc(occaDevice device,
const occaUDim_t entries,
const occaDtype dtype,
Expand Down
6 changes: 0 additions & 6 deletions src/core/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,6 @@ namespace occa {
return getDevice().umalloc(entries, dtype, src, props);
}

void* umalloc(const dim_t bytes,
const void *src,
const occa::properties &props) {
return getDevice().umalloc(bytes, src, props);
}

void memcpy(void *dest, const void *src,
const dim_t bytes,
const occa::properties &props) {
Expand Down
17 changes: 0 additions & 17 deletions src/core/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,23 +627,6 @@ namespace occa {
const occa::properties &props) {
return umalloc(entries, dtype, NULL, props);
}

void* device::umalloc(const dim_t bytes,
const void *src,
const occa::properties &props) {
return umalloc(bytes, dtype::byte, src, props);
}

void* device::umalloc(const dim_t bytes,
const occa::memory src,
const occa::properties &props) {
return umalloc(bytes, dtype::byte, src, props);
}

void* device::umalloc(const dim_t bytes,
const occa::properties &props) {
return umalloc(bytes, dtype::byte, NULL, props);
}
// |=================================

template <>
Expand Down

0 comments on commit 92ffb58

Please sign in to comment.