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

Shared memory allocation without the use of an ID #81

Open
erikzenker opened this issue Oct 26, 2015 · 6 comments
Open

Shared memory allocation without the use of an ID #81

erikzenker opened this issue Oct 26, 2015 · 6 comments
Labels
Backend:CUDA Release:Optional Use this to mark optional features for the next release Type:Enhancement

Comments

@erikzenker
Copy link
Member

There is/was a bug in shared memory allocation when multiple shared variables/arrays with same size and type was defined. These variables/arrays pointed to the same memory.
this pull request here, solved the problem by introducing an ID for each shared memory call, which can be made unique by __COUNTER__ macro.

I opened this issue such that we do not forget that using this macro is not the most beautiful solution to this problem. I hope the stackoverflow community has some answer on this problem.

One solution is shown by the bulk project with bulk::malloc.

@erikzenker
Copy link
Member Author

The community does not really has a clue to solve the problem directly and I think there is no perfect way, but managing the shared memory directly is the most promising solution for me. Maybe alpaka could provide varying solutions to the problem (ID and self managed)? So we can leave the decision by the user. But what interface can we provide for this ?

@BenjaminW3
Copy link
Member

This should not get lost: We tried this some time ago by misusing a language shortcoming see #85.
However, it worked for C++ only but not with nvcc 7.0 or 7.5.

@j-stephan j-stephan added this to To do in Release 0.7 via automation Jan 26, 2021
@j-stephan j-stephan added this to the Version 0.7.0 milestone Jan 26, 2021
@j-stephan
Copy link
Member

We would like to implement this for alpaka 0.7.0. Due to time constraints this is marked as an OPTIONAL feature for the next release.

@j-stephan j-stephan added the Release:Optional Use this to mark optional features for the next release label Jan 26, 2021
@j-stephan j-stephan removed this from To do in Release 0.7 May 11, 2021
@j-stephan j-stephan added this to To do in Release 0.8 via automation May 11, 2021
@j-stephan j-stephan removed this from To do in Release 0.8 Nov 10, 2021
@j-stephan j-stephan added this to To do in Release 0.9 via automation Nov 10, 2021
@bernhardmgruber
Copy link
Member

bernhardmgruber commented Dec 26, 2021

Just stumbled over a trick in C++20 we could try: https://youtu.be/z5AnYgyjQ-o?t=2567

template <auto Lambda = []{}>
auto declareSharedVar() {
    static constexpr std::source_location loc = std::source_location::current();
    static constexpr const void* counter = static_cast<const void*>(&loc);
    return counter;
}

int main() {
    std::cout << declareSharedVar() << "\n";
    std::cout << declareSharedVar() << "\n";
    for (int i = 0; i < 3; i++)
        std::cout << declareSharedVar() << "\n";
}

https://godbolt.org/z/nzr7vMcGc

The value of counter will be unique for each location the user used declareSharedVar().

@psychocoderHPC
Copy link
Member

Just stumbled over a trick in C++20 we could try: https://youtu.be/z5AnYgyjQ-o?t=2567

I like it, the only problem will be the loop and if someone encapsulates the allocation within a helper, but if declareSharedVar will be used as default and provide still the way to use user id's all will be fine.

@bernhardmgruber
Copy link
Member

bernhardmgruber commented Jan 5, 2022

The loop is not the problem. The program I described prints:

0x402040
0x402038
0x402030
0x402030
0x402030

So the counter in the loop is the same for each loop iteration. The counter value changes for each source location.

If you wrap the call to declareSharedVar, this is indeed a problem:

auto wrap() {
    return declareSharedVar();
}

int main() {
    std::cout << declareSharedVar() << "\n";
    std::cout << declareSharedVar() << "\n";
    for (int i = 0; i < 3; i++)
        std::cout << declareSharedVar() << "\n";

    std::cout << wrap() << "\n";
    std::cout << wrap() << "\n"; // problem, prints the same as previous call to wrap()
}

https://godbolt.org/z/q3crYqPTs

@j-stephan j-stephan removed this from the Version 0.9.0 (I/2022) milestone Mar 29, 2022
@j-stephan j-stephan removed this from To do in Release 0.9 Mar 29, 2022
@j-stephan j-stephan added this to To do in Release 1.0 via automation Mar 29, 2022
@bernhardmgruber bernhardmgruber removed this from To do in Release 1.0 Dec 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend:CUDA Release:Optional Use this to mark optional features for the next release Type:Enhancement
Projects
None yet
Development

No branches or pull requests

5 participants