Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 11af307

Browse filesBrowse files
willybornumar456
authored andcommitted
Random after device change
1 parent 8e3d1fa commit 11af307
Copy full SHA for 11af307

File tree

Expand file treeCollapse file tree

1 file changed

+16
-2
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+16
-2
lines changed

‎src/api/c/random.cpp

Copy file name to clipboardExpand all lines: src/api/c/random.cpp
+16-2Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
#include <af/array.h>
2020
#include <af/data.h>
2121
#include <af/defines.h>
22+
#include <af/device.h>
2223
#include <af/dim4.hpp>
24+
#include <map>
2325
#include <memory>
2426

2527
using af::dim4;
@@ -128,8 +130,20 @@ af_err af_get_default_random_engine(af_random_engine *r) {
128130
try {
129131
AF_CHECK(af_init());
130132

131-
thread_local auto *re = new RandomEngine;
132-
*r = static_cast<af_random_engine>(re);
133+
// RandomEngine contains device buffers which are dependent on
134+
// context|stream/device. Since nor context or stream are available at
135+
// this level, we will only use the deviceId.
136+
thread_local std::map<int /*deviceId*/, RandomEngine *>
137+
cachedDefaultRandomEngines;
138+
const int dependent = af::getDevice();
139+
auto it = cachedDefaultRandomEngines.find(dependent);
140+
if (it == cachedDefaultRandomEngines.end()) {
141+
RandomEngine *defaultRandomEngine = new RandomEngine;
142+
cachedDefaultRandomEngines[dependent] = defaultRandomEngine;
143+
*r = static_cast<af_random_engine>(defaultRandomEngine);
144+
} else {
145+
*r = static_cast<af_random_engine>(it->second);
146+
}
133147
return AF_SUCCESS;
134148
}
135149
CATCHALL;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.