YR_INVOKE

Contents

YR_INVOKE#

YR_INVOKE(...)#

Register functions for Yuanrong distributed invocation.

In local mode, registered functions execute within the current process. In cluster mode, functions execute remotely.

All functions intended for remote execution must be registered using the YR_INVOKE interface. If a function is registered multiple times, the program will throw an exception and terminate during runtime.

// functions
int AddOne(int x)
{
    return x + 1;
}

YR_INVOKE(AddOne);

class Counter {
public:
    Counter() {}
    Counter(int init)
    {
        count = init;
    }

    static Counter *FactoryCreate(int init)
    {
        return new Counter(init);
    }

    int Add(int x)
    {
        count += x;
        return count;
    }

    int Get(void)
    {
        return count;
    }

    YR_STATE(count);

public:
    int count;
};

YR_INVOKE(Counter::FactoryCreate, &Counter::Add, &Counter::Get);

Note

Specifically, when using printf within remotely registered functions via YR_INVOKE, note that Yuanrong’s runtime kernel redirects standard output, switching it to full buffering mode. This means output will only be written to disk when the buffer is full or fflush is explicitly called, and newline characters will not be immediately printed. For example:

int HelloWorld()
{
   printf("helloworld\n"); // Not recommended; output may not immediately appear due to remote runtime buffering
   return 0;
}
YR_INVOKE(HelloWorld)
Therefore, it is recommended to use std::cout or explicitly call fflush:
int HelloWorld()
{
    std::cout << "helloworld" << std::endl; // Recommended
    // Or explicitly call fflush
    printf("helloworld\n");
    fflush(stdout);
    return 0;
}
YR_INVOKE(HelloWorld)

Throws Exception:

If a function is detected to be registered multiple times, the program will exit with an error message.