Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache backend symbols in the unified backend #1770

Merged
merged 1 commit into from
May 17, 2017

Conversation

umar456
Copy link
Member

@umar456 umar456 commented May 2, 2017

unified

Test code:

#include <arrayfire.h>
#include <cstdio>
#include <cstdlib>
#include <chrono>

using namespace af;

int main(int argc, char *argv[])
{
    try {
      int backends = af::getAvailableBackends();
      af::setBackend(AF_BACKEND_CUDA);
      af::info();

      printf("\nRunning %s...\n", argv[0]);

      for(int i = 500; i < 100000; i+= 500) {
        auto start = std::chrono::high_resolution_clock::now();
        for(int iter = 0; iter < 10000; iter++) {
          af::array blah = af::randu(i);
          af::array constant_val = af::constant(1, i);
          af::array output = dot(blah, constant_val);
        }
        af::sync();
        auto end = std::chrono::high_resolution_clock::now();
        af::deviceGC();
        printf("| %7d | %5llu us |\n", i, std::chrono::duration_cast<std::chrono::microseconds>(end-start).count());
      }
    } catch (af::exception& e) {

        fprintf(stderr, "%s\n", e.what());
        throw;
    }

    return 0;
}

@umar456 umar456 requested a review from pavanky May 2, 2017 19:21
typedef af_err(*af_func)(CalleeArgs...);
af_func funcHandle;
static std::unordered_map<const char*, std::array<af_func, NUM_BACKENDS>> funcHandles;
af_func& funcHandle = funcHandles[symbolName][getActiveBackend()];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it always return NULL when it is not found ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am 96.3% sure it does.

typedef af_err(*af_func)(CalleeArgs...);
af_func funcHandle;
static std::unordered_map<const char*, std::array<af_func, NUM_BACKENDS>> funcHandles;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the array outside ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

static std::unordered_map<const char*, std::array<af_func, NUM_BACKENDS>> funcHandles;
af_func& funcHandle = funcHandles[symbolName][getActiveBackend()];
static std::array<std::unordered_map<const char*, af_func>, NUM_BACKENDS> funcHandles;
af_func& funcHandle = funcHandles[getActiveBackend()][symbolName];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is getActiveBackend returning the enum or is it returning the index ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops. It's a flag. Fixing.

case AF_BACKEND_CPU: return 0;
case AF_BACKEND_CUDA: return 1;
case AF_BACKEND_OPENCL: return 2;
default: return -1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, we have something called AF_BACKEND_DEFAULT. Please check if it works with that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SymbolManager and setBackend function sets the activeBackend to one of those backends.

@pavanky
Copy link
Member

pavanky commented May 2, 2017

@umar456 Alternative suggestion: Instead of querying from an unordered_map each time, can't the function pointer be cached inside the caller function ?

This reduces the lookup time from the map. You will need change the definition of CALL macros.

@umar456
Copy link
Member Author

umar456 commented May 2, 2017

I tried that first but the call function's template parameters are based on the type of arguments and not the function name itself. I could change the macro so that we only have an array instead of a map but I didn't want to break anything.

I can change it if you think its worthwhile.

@pavanky
Copy link
Member

pavanky commented May 2, 2017

We can still pass the arguments, but instead of calling the function, it will just return the function pointer. The call can be made externally. No reason to cache the symbol inside the call function.

@umar456
Copy link
Member Author

umar456 commented May 2, 2017

build arrayfire windows ci

@umar456 umar456 merged commit 9cb4e0b into arrayfire:devel May 17, 2017
@umar456 umar456 deleted the cache_unified branch May 18, 2017 23:28
@mlloreda mlloreda added this to the v3.5.0 milestone May 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants