randu (special engine) fails when called after changing device
Description
Code to reproduce:
#include <arrayfire.h>
int main(int, char**) {
// Initialize the kernel array just once
af::info();
af::setDevice(0);
af::setDefaultRandomEngineType(AF_RANDOM_ENGINE_MERSENNE);
af::array d0_in{af::randu(af::dim4(10,10))}; // success
af::setDevice(1);
af::setDefaultRandomEngineType(AF_RANDOM_ENGINE_MERSENNE);
af::array d1_in{af::randu(af::dim4(10, 10))}; // <- exception
af::print("dev 1", d1_in);
}
Output:
ArrayFire v3.9.0 (OpenCL, 64-bit Windows, build 64586e0 )
[0] AMD: Spectre, 6571 MB -- OpenCL 2.0 AMD-APP (3224.5) -- Device driver 3224.5 -- FP64 Support: True
-1- NVIDIA: NVIDIA GeForce GTX 750 Ti, 2047 MB -- OpenCL 3.0 CUDA -- Device driver 531.61 -- FP64 Support:
OpenCL Error (-38): Invalid Memory Object when calling clSetKernelArg
Cause:
The defaultRandomEngine is cached for each thread.
The MERSENNE type engine, uses device buffers to store his state.
By changing device, the buffers on the previous device are no longer usable.
Can this PR be backported to older versions? yes
Changes to Users
1 bug less
Checklist
- Rebased on latest master
- Code compiles
- Tests pass
- Functions added to unified API
- Functions documented