Skip to content

Commit

Permalink
[C] Updated the C API
Browse files Browse the repository at this point in the history
  • Loading branch information
dmed256 committed Mar 25, 2018
1 parent 5249d4c commit 1026ff6
Show file tree
Hide file tree
Showing 23 changed files with 1,751 additions and 5,380 deletions.
19 changes: 13 additions & 6 deletions examples/1_add_vectors/c/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,24 @@ int main(int argc, char **argv) {
occaProperties props = occaCreateProperties();
occaPropertiesSet(props, "defines/TILE_SIZE", occaInt(10));

// Compile the kernel at run-time
addVectors = occaDeviceBuildKernel(device,
"addVectors.okl",
"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);

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

// Assert values
for (i = 0; i < 5; ++i) {
printf("%d = %f\n", i, ab[i]);
}
Expand All @@ -96,14 +101,16 @@ int main(int argc, char **argv) {
exit(1);
}

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

occaFreeProperties(props);
occaFreeKernel(addVectors);
occaFreeMemory(o_a);
occaFreeMemory(o_b);
occaFreeMemory(o_ab);
occaFreeDevice(device);
// Free device memory and occa objects
occaFree(props);
occaFree(addVectors);
occaFree(o_a);
occaFree(o_b);
occaFree(o_ab);
occaFree(device);
}
8 changes: 8 additions & 0 deletions examples/1_add_vectors/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ int main(int argc, char **argv) {
//---[ Device setup with string flags ]-------------------
device.setup("mode: 'Serial'");

std::cout << "device.properties = " << device.properties() << '\n';

// device.setup("mode : 'OpenMP', "
// "schedule : 'compact', "
// "chunk : 10");
Expand All @@ -67,16 +69,21 @@ int main(int argc, char **argv) {
o_b = device.malloc(entries*sizeof(float));
o_ab = device.malloc(entries*sizeof(float));

// Compile the kernel at run-time
addVectors = device.buildKernel("addVectors.okl",
"addVectors");

// Copy memory to the device
o_a.copyFrom(a);
o_b.copyFrom(b);

// Launch device kernel
addVectors(entries, o_a, o_b, o_ab);

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

// Assert values
for (int i = 0; i < 5; ++i) {
std::cout << i << ": " << ab[i] << '\n';
}
Expand All @@ -86,6 +93,7 @@ int main(int argc, char **argv) {
}
}

// Free host memory
delete [] a;
delete [] b;
delete [] ab;
Expand Down
38 changes: 34 additions & 4 deletions include/occa.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
#include "occa/c_wrapper.hpp"
/* The MIT License (MIT)
*
* Copyright (c) 2014-2018 David Medina and Tim Warburton
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*/
#include "occa/c/defines.h"
#include "occa/c/device.h"
#include "occa/c/kernel.h"
#include "occa/c/memory.h"
#include "occa/c/properties.h"

#define OCCA_ARG_COUNT(...) OCCA_ARG_COUNT2(__VA_ARGS__, \
// Just in case someone wants to run with an older format than C99
#ifndef OCCA_DISABLE_VARIADIC_MACROS

#define OCCA_ARG_COUNT(...) OCCA_ARG_COUNT2(__VA_ARGS__, \
50,49,48,47,46,45,44,43,42,41, \
40,39,38,37,36,35,34,33,32,31, \
30,29,28,27,26,25,24,23,22,21, \
Expand All @@ -14,8 +42,10 @@
_31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
_41,_42,_43,_44,_45,_46,_47,_48,_49,_50, N, ...) N

#define OCCA_C_RUN_KERNEL1(...) OCCA_C_RUN_KERNEL2(__VA_ARGS__)
#define OCCA_C_RUN_KERNEL3(N, kernel, ...) occaKernelRunN(kernel, N, __VA_ARGS__)
#define OCCA_C_RUN_KERNEL2(...) OCCA_C_RUN_KERNEL3(__VA_ARGS__)
#define OCCA_C_RUN_KERNEL3(N, kernel, ...) occaKernelRun##N(kernel, __VA_ARGS__)
#define OCCA_C_RUN_KERNEL1(...) OCCA_C_RUN_KERNEL2(__VA_ARGS__)

#define occaKernelRun(...) OCCA_C_RUN_KERNEL1(OCCA_ARG_COUNT(__VA_ARGS__), __VA_ARGS__)

#endif
41 changes: 41 additions & 0 deletions include/occa/c/defines.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* The MIT License (MIT)
*
* Copyright (c) 2014-2018 David Medina and Tim Warburton
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*/
#ifndef OCCA_C_DEFINES_HEADER
#define OCCA_C_DEFINES_HEADER

#include "occa/defines.hpp"

#if (OCCA_OS & (OCCA_LINUX_OS | OCCA_OSX_OS))
# define OCCA_RFUNC
# define OCCA_LFUNC
#else
# define OCCA_RFUNC __stdcall
# ifdef OCCA_C_EXPORTS
// #define OCCA_LFUNC __declspec(dllexport)
# define OCCA_LFUNC
# else
// #define OCCA_LFUNC __declspec(dllimport)
# define OCCA_LFUNC
# endif
#endif

#endif
113 changes: 113 additions & 0 deletions include/occa/c/device.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/* The MIT License (MIT)
*
* Copyright (c) 2014-2018 David Medina and Tim Warburton
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*/

#ifndef OCCA_C_DEVICE_HEADER
#define OCCA_C_DEVICE_HEADER

#include "occa/c/defines.h"
#include "occa/c/types.h"

OCCA_START_EXTERN_C

//---[ Background ]---------------------
OCCA_LFUNC void OCCA_RFUNC occaSetDevice(occaDevice device);

OCCA_LFUNC void OCCA_RFUNC occaSetDeviceFromInfo(const char *infos);

OCCA_LFUNC void OCCA_RFUNC occaFinish();

OCCA_LFUNC void OCCA_RFUNC occaWaitFor(occaStreamTag tag);

OCCA_LFUNC occaStream OCCA_RFUNC occaCreateStream();

OCCA_LFUNC occaStream OCCA_RFUNC occaGetStream();

OCCA_LFUNC void OCCA_RFUNC occaSetStream(occaStream stream);

OCCA_LFUNC occaStream OCCA_RFUNC occaWrapStream(void *handle_,
const occaProperties props);

OCCA_LFUNC occaStreamTag OCCA_RFUNC occaTagStream();
//======================================

OCCA_LFUNC void OCCA_RFUNC occaPrintModeInfo();

OCCA_LFUNC occaDevice OCCA_RFUNC occaCreateDevice(occaType info);

OCCA_LFUNC const char* OCCA_RFUNC occaDeviceMode(occaDevice device);

OCCA_LFUNC occaUDim_t OCCA_RFUNC occaDeviceMemorySize(occaDevice device);

OCCA_LFUNC occaUDim_t OCCA_RFUNC occaDeviceMemoryAllocated(occaDevice device);

OCCA_LFUNC occaUDim_t OCCA_RFUNC occaDeviceBytesAllocated(occaDevice device);

OCCA_LFUNC occaKernel OCCA_RFUNC occaDeviceBuildKernel(occaDevice device,
const char *filename,
const char *kernelName,
const occaProperties props);

OCCA_LFUNC occaKernel OCCA_RFUNC occaDeviceBuildKernelFromString(occaDevice device,
const char *str,
const char *kernelName,
const occaProperties props);

OCCA_LFUNC occaKernel OCCA_RFUNC occaDeviceBuildKernelFromBinary(occaDevice device,
const char *filename,
const char *kernelName,
const occaProperties props);

OCCA_LFUNC occaMemory OCCA_RFUNC occaDeviceMalloc(occaDevice device,
const occaUDim_t bytes,
const void *src,
occaProperties props);

OCCA_LFUNC void* OCCA_RFUNC occaDeviceUmalloc(occaDevice device,
const occaUDim_t bytes,
const void *src,
occaProperties props);

OCCA_LFUNC void OCCA_RFUNC occaDeviceFinish(occaDevice device);

OCCA_LFUNC occaStream OCCA_RFUNC occaDeviceCreateStream(occaDevice device);

OCCA_LFUNC occaStream OCCA_RFUNC occaDeviceGetStream(occaDevice device);

OCCA_LFUNC void OCCA_RFUNC occaDeviceSetStream(occaDevice device,
occaStream stream);

OCCA_LFUNC occaStream OCCA_RFUNC occaDeviceWrapStream(occaDevice device,
void *handle_,
const occaProperties props);

OCCA_LFUNC occaStreamTag OCCA_RFUNC occaDeviceTagStream(occaDevice device);

OCCA_LFUNC void OCCA_RFUNC occaDeviceWaitForTag(occaDevice device,
occaStreamTag tag);

OCCA_LFUNC double OCCA_RFUNC occaDeviceTimeBetweenTags(occaDevice device,
occaStreamTag startTag,
occaStreamTag endTag);

OCCA_END_EXTERN_C

#endif
60 changes: 60 additions & 0 deletions include/occa/c/kernel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* The MIT License (MIT)
*
* Copyright (c) 2014-2018 David Medina and Tim Warburton
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*/

#ifndef OCCA_C_KERNEL_HEADER
#define OCCA_C_KERNEL_HEADER

#include "occa/c/defines.h"
#include "occa/c/types.h"

OCCA_START_EXTERN_C

//---[ Background ]---------------------
OCCA_LFUNC occaKernel OCCA_RFUNC occaBuildKernel(const char *filename,
const char *kernelName,
const occaProperties props);

OCCA_LFUNC occaKernel OCCA_RFUNC occaBuildKernelFromString(const char *str,
const char *kernelName,
const occaProperties props);

OCCA_LFUNC occaKernel OCCA_RFUNC occaBuildKernelFromBinary(const char *filename,
const char *kernelName,
const occaProperties props);
//======================================

OCCA_LFUNC const char* OCCA_RFUNC occaKernelMode(occaKernel kernel);
OCCA_LFUNC const char* OCCA_RFUNC occaKernelName(occaKernel kernel);

OCCA_LFUNC occaDevice OCCA_RFUNC occaKernelGetDevice(occaKernel kernel);

OCCA_LFUNC void OCCA_RFUNC occaKernelSetRunDims(occaKernel kernel,
occaDim groups,
occaDim items);

OCCA_LFUNC void OCCA_RFUNC occaKernelRunN(occaKernel kernel,
const int argc,
...);

OCCA_END_EXTERN_C

#endif
Loading

0 comments on commit 1026ff6

Please sign in to comment.