Compiling a C++ function

C++ functions are built with CMake and held in the func directory.

A simple hello world function exists at hello.cpp.

From the Faasm CLI, you can compile, upload and invoke the hello.cppfunction with:

  1. inv compile demo hello
  2. inv upload demo hello
  3. inv invoke demo hello

You should then see the response Hello faasm!.

Writing functions

Faasm aims to be uninvasive, allowing code to run natively and in a serverless context.To do this in C/ C++ functions we use a set of Faasm macros. These macros allow code tobe compiled with a standard toolchain and run natively, and with the Faasm toolchain,and run in a serverless context.

The outline of this looks like:

  1. #include "faasm/faasm.h"
  2.  
  3. FAASM_MAIN_FUNC() {
  4. // Do something
  5.  
  6. return 0;
  7. }

Although for very simple functions, a standard int main() will also still work.

C++ API

Faasm provides a simple C++ wrapper library around the Faasm host interface.Some of the methods in this wrapper are:

  • faasmGetInput() - allows functions to retrieve their input data
  • faasmSetOutput() - this allows functions to return output data to the caller
  • faasmChainFunction() - this allows one function to invoke others
  • faasmAwaitCall() - waits for a chained function invocation to finish
  • faasmReadState() and writeState() - allows functions to read/ write key/value state
  • faasmReadStateOffset() and faasmWriteStateOffset() - allows functions to read/ write at specific points in existing state (e.g. updating a subsection of an array)

Chaining

"Chaining" is when one function makes a call to another function (which must be owned by the same user).There are two supported methods of chaining, one for invoking totally separate Faasm functions, theother for automatically parallelising functions in the same piece of code (useful for porting legacy applications).

Chaining a function

Multiple functions can be defined in the same file, invoke each other and await results. For example:

  1. #include "faasm/faasm.h"
  2. #include <vector>
  3.  
  4. // Define some function to be chained
  5. FAASM_FUNC(funcOne, 1) {
  6. return 0;
  7. }
  8.  
  9. // Define another function to be chained
  10. FAASM_FUNC(funcTwo, 2) {
  11. return 0;
  12. }
  13.  
  14. // Define the main function
  15. FAASM_MAIN_FUNC() {
  16. // Chain calls to the other functions
  17. int callOneId = faasmChainThis(1);
  18. int callTwoId = faasmChainThis(2);
  19.  
  20. // Await results
  21. faasmAwaitCall(callOneId);
  22. faasmAwaitCall(callTwoId);
  23.  
  24. return 0;
  25. }

Chaining can also be done across functions defined separately, e.g. in C++:

  1. #include "faasm/faasm.h"
  2.  
  3. FAASM_MAIN_FUNC() {
  4. // Chain a call to my other function named "other-func"
  5. int callId = faasmChainFunction("other-func");
  6. faasmAwaitCall(callId);
  7.  
  8. return 0;
  9. }

OpenMP and MPI

See the OpenMP and MPI docs for further C/C++ APIs.