Skip to content

Commit

Permalink
[#475][Refactor] Remove UVA + umalloc (#507)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmed256 authored Sep 7, 2021
1 parent 9446605 commit 845c524
Show file tree
Hide file tree
Showing 122 changed files with 139 additions and 2,139 deletions.
1 change: 0 additions & 1 deletion docs/guide/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
- [Introduction](/guide/occa/introduction)
- [Device Streams](/guide/occa/device-streams)
- [Background Device](/guide/occa/background-device)
- [Unified Memory](/guide/occa/unified-memory)

- **OKL**
- [Introduction](/guide/okl/introduction)
Expand Down
51 changes: 0 additions & 51 deletions docs/guide/occa/unified-memory.md

This file was deleted.

13 changes: 5 additions & 8 deletions examples/c/01_add_vectors/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,13 @@ int main(int argc, const char **argv) {
//========================================================

// Allocate memory on the device
o_a = occaDeviceTypedMalloc(device, entries, occaDtypeFloat, NULL, occaDefault);
o_b = occaDeviceTypedMalloc(device, entries, occaDtypeFloat, NULL, occaDefault);
o_a = occaDeviceTypedMalloc(device, entries, occaDtypeFloat, a, occaDefault);
o_b = occaDeviceTypedMalloc(device, entries, occaDtypeFloat, b, occaDefault);

// We can also allocate memory without a dtype
// We can also allocate memory without a dtype and manually copy the data over
// WARNING: This will disable runtime type checking
o_ab = occaDeviceMalloc(device, entries * sizeof(float), NULL, occaDefault);
occaCopyPtrToMem(o_ab, ab, occaAllBytes, 0, occaDefault);

// Setup properties that can be passed to the kernel
occaJson props = occaCreateJson();
Expand All @@ -86,10 +87,6 @@ int main(int argc, const char **argv) {
"addVectors",
props);

// Copy memory to the device
occaCopyPtrToMem(o_a, a, entries*sizeof(float), 0, occaDefault);
occaCopyPtrToMem(o_b, b, occaAllBytes , 0, occaDefault);

// Launch device kernel
occaKernelRun(addVectors,
occaInt(entries), o_a, o_b, o_ab);
Expand Down Expand Up @@ -132,7 +129,7 @@ occaJson parseArgs(int argc, const char **argv) {
" shortname: 'd',"
" description: 'Device properties (default: \"{mode: \\'Serial\\'}\")',"
" with_arg: true,"
" default_value: { mode: 'Serial' },"
" default_value: \"{ mode: 'Serial' }\","
" },"
" {"
" name: 'verbose',"
Expand Down
39 changes: 21 additions & 18 deletions examples/c/02_background_device/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,30 @@ int main(int argc, const char **argv) {
int entries = 5;
int i;

float *a = (float*) occaTypedUMalloc(entries, occaDtypeFloat, NULL, occaDefault);
float *b = (float*) occaTypedUMalloc(entries, occaDtypeFloat, NULL, occaDefault);
float *ab = (float*) occaTypedUMalloc(entries, occaDtypeFloat, NULL, occaDefault);
float *a = (float*) malloc(entries*sizeof(float));
float *b = (float*) malloc(entries*sizeof(float));
float *ab = (float*) malloc(entries*sizeof(float));

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

// Allocate memory on the background device
occaMemory o_a = occaTypedMalloc(entries, occaDtypeFloat, a, occaDefault);
occaMemory o_b = occaTypedMalloc(entries, occaDtypeFloat, b, occaDefault);
occaMemory o_ab = occaTypedMalloc(entries, occaDtypeFloat, ab, occaDefault);

occaKernel addVectors = occaBuildKernel("addVectors.okl",
"addVectors",
occaDefault);

// Arrays a, b, and ab are now resident
// on [device]
occaKernelRun(addVectors,
occaInt(entries), occaPtr(a), occaPtr(b), occaPtr(ab));

// b is not const in the kernel, so we can use
// dontSync(b) to manually force b to not sync
occaDontSync(b);
occaInt(entries), o_a, o_b, o_ab);

// Finish work queued up in [device],
// synchronizing a, b, and ab and
// making it safe to use them again
occaFinish();
// Copy result to the host
occaCopyMemToPtr(ab, o_ab, occaAllBytes, 0, occaDefault);

for (i = 0; i < entries; ++i) {
printf("%d = %f\n", i, ab[i]);
Expand All @@ -74,11 +71,17 @@ int main(int argc, const char **argv) {
}
}

// Free host memory
free(a);
free(b);
free(ab);

// Free device memory and occa objects
occaFree(&args);
occaFree(&addVectors);
occaFreeUvaPtr(a);
occaFreeUvaPtr(b);
occaFreeUvaPtr(ab);
occaFree(&o_a);
occaFree(&o_b);
occaFree(&o_ab);

return 0;
}
Expand All @@ -94,7 +97,7 @@ occaJson parseArgs(int argc, const char **argv) {
" shortname: 'd',"
" description: 'Device properties (default: \"{ mode: \\'Serial\\' }\")',"
" with_arg: true,"
" default_value: { mode: 'Serial' },"
" default_value: \"{ mode: 'Serial' }\","
" },"
" {"
" name: 'verbose',"
Expand Down
34 changes: 23 additions & 11 deletions examples/c/03_generic_inline_kernel/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,21 @@ int main(int argc, const char **argv) {
int entries = 5;
int i;

float *a = (float*) occaTypedUMalloc(entries, occaDtypeFloat, NULL, occaDefault);
float *b = (float*) occaTypedUMalloc(entries, occaDtypeFloat, NULL, occaDefault);
float *ab = (float*) occaTypedUMalloc(entries, occaDtypeFloat, NULL, occaDefault);
float *a = (float*) malloc(entries*sizeof(float));
float *b = (float*) malloc(entries*sizeof(float));
float *ab = (float*) malloc(entries*sizeof(float));

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

// Allocate memory on the background device
occaMemory o_a = occaTypedMalloc(entries, occaDtypeFloat, a, occaDefault);
occaMemory o_b = occaTypedMalloc(entries, occaDtypeFloat, b, occaDefault);
occaMemory o_ab = occaTypedMalloc(entries, occaDtypeFloat, ab, occaDefault);

occaJson props = occaCreateJson();
occaJsonObjectSet(props,
"defines/TILE_SIZE",
Expand All @@ -46,10 +51,10 @@ int main(int argc, const char **argv) {

// Build the variable scope used inside the inlined OKL code
occaScopeAddConst(scope, "entries", occaInt(entries));
occaScopeAddConst(scope, "a", occaPtr(a));
occaScopeAddConst(scope, "b", occaPtr(b));
occaScopeAddConst(scope, "a", o_a);
occaScopeAddConst(scope, "b", o_b);
// We can name our scoped variales anything
occaScopeAdd(scope, "output", occaPtr(ab));
occaScopeAdd(scope, "output", o_ab);
// We can also add unused variables to the scope which could be
// useful while debugging
occaScopeAdd(scope, "debugValue", occaInt(42));
Expand All @@ -66,7 +71,8 @@ int main(int argc, const char **argv) {
)
);

occaFinish();
// Copy result to the host
occaCopyMemToPtr(ab, o_ab, occaAllBytes, 0, occaDefault);

for (i = 0; i < entries; ++i) {
printf("%d = %f\n", i, ab[i]);
Expand All @@ -77,12 +83,18 @@ int main(int argc, const char **argv) {
}
}

// Free host memory
free(a);
free(b);
free(ab);

// Free device memory and occa objects
occaFree(&args);
occaFree(&props);
occaFree(&scope);
occaFreeUvaPtr(a);
occaFreeUvaPtr(b);
occaFreeUvaPtr(ab);
occaFree(&o_a);
occaFree(&o_b);
occaFree(&o_ab);

return 0;
}
Expand All @@ -98,7 +110,7 @@ occaJson parseArgs(int argc, const char **argv) {
" shortname: 'd',"
" description: 'Device properties (default: \"{ mode: \\'Serial\\' }\")',"
" with_arg: true,"
" default_value: { mode: 'Serial' },"
" default_value: \"{ mode: 'Serial' }\","
" },"
" {"
" name: 'verbose',"
Expand Down
2 changes: 1 addition & 1 deletion examples/c/04_reduction/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ occaJson parseArgs(int argc, const char **argv) {
" shortname: 'd',"
" description: 'Device properties (default: \"{ mode: \\'Serial\\' }\")',"
" with_arg: true,"
" default_value: { mode: 'Serial' },"
" default_value: \"{ mode: 'Serial' }\","
" },"
" {"
" name: 'verbose',"
Expand Down
32 changes: 21 additions & 11 deletions examples/cpp/04_generic_inline_kernel/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,31 @@ int main(int argc, const char **argv) {

int entries = 5;

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

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

// Uses the background device
occa::array<float> array_a(entries);
occa::array<float> array_b(entries);
occa::array<float> array_ab(entries);

// Copy over host data
array_a.copyFrom(a);
array_b.copyFrom(b);
array_ab.fill(0);

occa::scope scope({
{"entries", entries},
// Const-ness of variables is passed through, which can be useful for the compiler
{"a", (const float*) a},
{"b", (const float*) b},
{"ab", ab}
{"a", array_a},
{"b", array_b},
{"ab", array_ab}
}, {
// Define TILE_SIZE at compile-time
{"defines/TILE_SIZE", 16}
Expand All @@ -46,7 +55,7 @@ int main(int argc, const char **argv) {
}
));

occa::finish();
array_ab.copyTo(ab);

for (int i = 0; i < entries; ++i) {
std::cout << i << ": " << ab[i] << '\n';
Expand All @@ -57,9 +66,10 @@ int main(int argc, const char **argv) {
}
}

occa::free(a);
occa::free(b);
occa::free(ab);
// Free host memory
delete [] a;
delete [] b;
delete [] ab;

return 0;
}
Expand Down
9 changes: 0 additions & 9 deletions examples/cpp/07_memory_management/.gitignore

This file was deleted.

4 changes: 0 additions & 4 deletions examples/cpp/07_memory_management/CMakeLists.txt

This file was deleted.

26 changes: 0 additions & 26 deletions examples/cpp/07_memory_management/README.md

This file was deleted.

Loading

0 comments on commit 845c524

Please sign in to comment.