Skip to content

Call setDevice on each thread at entry point. #3269

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

Merged
merged 1 commit into from
Jul 5, 2022

Conversation

umar456
Copy link
Member

@umar456 umar456 commented Jul 2, 2022

This PR improves the af_init function so that it calls cudaSetDevice for
each new thread when creating any ArrayFire objects.

Description

CUDA requires that cudaSetDevice be called in each thread before
any other calls are made to the CUDA API. This is done by default
on the main thread but it is not done on new threads created. This
commit changes the behavior or the af_init function so that it call
the cudaSetDevice when creating a new object in ArrayFire.

This commit also refactors the af_init function so that it calls a lower
overhead init function which initializes the device manager.

Changes to Users

Users may not need to call af::setDevice in new threads before they
can use ArrayFire functions.

Checklist

  • Rebased on latest master
  • Code compiles
  • Tests pass
  • [ ] Functions added to unified API
  • [ ] Functions documented

@umar456 umar456 added this to the 3.9.0 milestone Jul 2, 2022
CUDA requires that cudaSetDevice be called in each thread before
any other calls are made to the CUDA API. This is done by default
on the main thread but it is not done on new threads created. This
commit changes the behavior or the af_init function so that it call
the cudaSetDevice when creating a new object in ArrayFire.

This commit also refactors the af_init function so that it calls a lower
overhead init function which initializes the device manager.
@@ -348,6 +348,11 @@ int getDeviceCount() {
}
}

void init() {
thread_local auto err = cudaSetDevice(getDeviceNativeId(getActiveDeviceId()));
Copy link
Contributor

Choose a reason for hiding this comment

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

Errs??

@syurkevi syurkevi merged commit f199a5d into arrayfire:master Jul 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants