Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion stan/math/opencl/opencl_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,8 @@ class opencl_context {
kernel_caches_.push_back(cache);
}
};
static opencl_context opencl_context;
// Share the context and its kernel-cache registry across translation units.
inline opencl_context opencl_context;
} // namespace math
} // namespace stan

Expand Down
10 changes: 10 additions & 0 deletions test/unit/multiple_translation_units1.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
#include <test/unit/multiple_translation_units_includes.hpp>

inline stan::math::var function1() { return 0; }

#ifdef STAN_OPENCL
const void* opencl_context_address1() { return &stan::math::opencl_context; }

double opencl_cached_kernel1() {
stan::math::matrix_cl<double> matrix = stan::math::constant(1.0, 2, 2);
stan::math::matrix_cl<double> result = matrix * matrix;
return stan::math::from_matrix_cl(result).sum();
}
#endif
15 changes: 15 additions & 0 deletions test/unit/multiple_translation_units2.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
#include <test/unit/multiple_translation_units_includes.hpp>

inline stan::math::var function2() { return 0; }

#ifdef STAN_OPENCL
const void* opencl_context_address2() { return &stan::math::opencl_context; }

double opencl_cached_kernel2() {
stan::math::matrix_cl<double> matrix = stan::math::constant(2.0, 2, 2);
stan::math::matrix_cl<double> result = matrix + matrix;
return stan::math::from_matrix_cl(result).sum();
}

void select_opencl_device2() {
stan::math::opencl_context.select_device(OPENCL_PLATFORM_ID,
OPENCL_DEVICE_ID);
}
#endif
23 changes: 23 additions & 0 deletions test/unit/multiple_translation_units_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,26 @@ TEST(multiple_translation_units, compile) {
SUCCEED() << "this test compiling indicates that compiling the math library "
<< "public umbrella headers with multiple translation units is ok.";
}

#ifdef STAN_OPENCL
const void* opencl_context_address1();
const void* opencl_context_address2();
double opencl_cached_kernel1();
double opencl_cached_kernel2();
void select_opencl_device2();

TEST(multiple_translation_units, shares_opencl_context) {
EXPECT_EQ(opencl_context_address1(), opencl_context_address2());
}

TEST(multiple_translation_units, opencl_cached_kernels_after_device_switch) {
EXPECT_DOUBLE_EQ(8.0, opencl_cached_kernel1());
EXPECT_DOUBLE_EQ(16.0, opencl_cached_kernel2());

// Switching in another translation unit must invalidate both kernel caches.
select_opencl_device2();

EXPECT_DOUBLE_EQ(8.0, opencl_cached_kernel1());
EXPECT_DOUBLE_EQ(16.0, opencl_cached_kernel2());
}
#endif
Loading