Skip to content

Commit 95503a6

Browse files
committed
Decouple shared memory from thread size.
1 parent 0a74da5 commit 95503a6

1 file changed

Lines changed: 58 additions & 51 deletions

File tree

‎graph_framework/cuda_context.hpp‎

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "timing.hpp"
1919

2020
/// Maximum number of registers to use.
21-
#define MAX_REG 128
21+
#define MAX_REG 256
2222

2323
namespace gpu {
2424
//------------------------------------------------------------------------------
@@ -90,8 +90,6 @@ namespace gpu {
9090
CUdeviceptr offset_buffer;
9191
/// Cuda stream.
9292
CUstream stream;
93-
/// Assumed thread sizes.
94-
std::map<std::string, int> assumed_thread_size;
9593

9694
//------------------------------------------------------------------------------
9795
/// @brief Check results of async cuda functions.
@@ -229,6 +227,19 @@ namespace gpu {
229227
if (jit::verbose) {
230228
std::cout << "CUDA GPU info." << std::endl;
231229
std::cout << " Major compute capability : " << compute_version << std::endl;
230+
231+
int value;
232+
check_error(cuDeviceGetAttribute(&value,
233+
CU_DEVICE_ATTRIBUTE_MAX_SHARED_MEMORY_PER_BLOCK,
234+
device), "cuDeviceGetAttribute");
235+
236+
std::cout << " Max shared memory : " << value << std::endl;
237+
238+
check_error(cuDeviceGetAttribute(&value,
239+
CU_DEVICE_ATTRIBUTE_WARP_SIZE,
240+
device), "cuDeviceGetAttribute");
241+
242+
std::cout << " Warp size : " << value << std::endl;
232243
}
233244

234245
check_error(cuDeviceGetAttribute(&compute_version,
@@ -493,12 +504,13 @@ namespace gpu {
493504
int value;
494505
check_error(cuFuncGetAttribute(&value, CU_FUNC_ATTRIBUTE_MAX_THREADS_PER_BLOCK,
495506
function), "cuFuncGetAttribute");
496-
if (assumed_thread_size[kernel_name] != -1) {
497-
value = assumed_thread_size[kernel_name];
498-
}
507+
int warp_size;
508+
check_error(cuDeviceGetAttribute(&warp_size,
509+
CU_DEVICE_ATTRIBUTE_WARP_SIZE,
510+
device), "cuDeviceGetAttribute");
499511

500512
unsigned int total_parallel = state.get() ? random_state_size : num_rays;
501-
unsigned int threads_per_group = total_parallel < 1024 ? 32 : value;
513+
unsigned int threads_per_group = total_parallel < 1024 ? warp_size : value;
502514
unsigned int thread_groups = total_parallel/threads_per_group + (total_parallel%threads_per_group ? 1 : 0);
503515

504516
int min_grid;
@@ -513,12 +525,6 @@ namespace gpu {
513525
std::cout << " Total parallel : " << total_parallel << std::endl;
514526
std::cout << " Min grid size : " << min_grid << std::endl;
515527
std::cout << " Suggested Block size : " << value << std::endl;
516-
517-
check_error(cuDeviceGetAttribute(&value,
518-
CU_DEVICE_ATTRIBUTE_MAX_SHARED_MEMORY_PER_BLOCK,
519-
device), "cuDeviceGetAttribute");
520-
521-
std::cout << " Max shared memory : " << value << std::endl;
522528
}
523529
#ifdef PROFILE_KERNELS
524530
timing::measure_diagnostic timer(kernel_name);
@@ -934,23 +940,27 @@ namespace gpu {
934940
source_buffer << std::endl;
935941
source_buffer << "extern \"C\" __global__ void "
936942
<< name << "(" << std::endl;
937-
943+
938944
int used_thread_mem = 0;
939945
int max_shared_mem;
940946
check_error(cuDeviceGetAttribute(&max_shared_mem,
941947
CU_DEVICE_ATTRIBUTE_MAX_SHARED_MEMORY_PER_BLOCK,
942948
device), "cuDeviceGetAttribute");
943949

950+
int warp_size;
951+
check_error(cuDeviceGetAttribute(&warp_size,
952+
CU_DEVICE_ATTRIBUTE_WARP_SIZE,
953+
device), "cuDeviceGetAttribute");
954+
944955
jit::argument_set used_args;
945956
if (inputs.size()) {
946957
if (!is_constant[0] && iterations > 1) {
947-
const size_t needed_mem = inputs[0]->size() > 1024 ? 1024*sizeof(T) : 32*sizeof(T);
958+
const size_t needed_mem = inputs[0]->size() > 1024 ?
959+
1024*sizeof(T) :
960+
warp_size*sizeof(T);
948961
if (used_thread_mem + needed_mem < max_shared_mem) {
949962
used_thread_mem += needed_mem;
950963
thread_shared.insert(inputs[0].get());
951-
if (!assumed_thread_size.contains(name)) {
952-
assumed_thread_size[name] = inputs[i]->size() > 1024 ? 1024 : 32;
953-
}
954964
}
955965
} else if (is_constant[0] &&
956966
inputs[0]->size() < size &&
@@ -960,9 +970,6 @@ namespace gpu {
960970
used_thread_mem += needed_mem;
961971
thread_shared.insert(inputs[0].get());
962972
thread_mem[inputs[0].get()] = jit::to_string('t', inputs[0].get());
963-
if (!assumed_thread_size.contains(name)) {
964-
assumed_thread_size[name] = 1024;
965-
}
966973
}
967974
}
968975
source_buffer << " ";
@@ -976,13 +983,12 @@ namespace gpu {
976983
}
977984
for (size_t i = 1, ie = inputs.size(); i < ie; i++) {
978985
if (!is_constant[i] && iterations > 1) {
979-
const size_t needed_mem = inputs[i]->size() > 1024 ? 1024*sizeof(T) : 32*sizeof(T);
986+
const size_t needed_mem = inputs[i]->size() > 1024 ?
987+
1024*sizeof(T) :
988+
warp_size*sizeof(T);
980989
if (used_thread_mem + needed_mem < max_shared_mem) {
981990
used_thread_mem += needed_mem;
982991
thread_shared.insert(inputs[i].get());
983-
if (!assumed_thread_size.contains(name)) {
984-
assumed_thread_size[name] = inputs[i]->size() > 1024 ? 1024 : 32;
985-
}
986992
}
987993
} else if (is_constant[i] &&
988994
inputs[i]->size() < size &&
@@ -991,10 +997,7 @@ namespace gpu {
991997
if (used_thread_mem + needed_mem < max_shared_mem) {
992998
used_thread_mem += needed_mem;
993999
thread_shared.insert(inputs[i].get());
994-
thread_mem[inputs[i].get()] = jit::to_string('t', inputs[0].get());
995-
if (!assumed_thread_size.contains(name)) {
996-
assumed_thread_size[name] = 1024;
997-
}
1000+
thread_mem[inputs[i].get()] = jit::to_string('t', inputs[i].get());
9981001
}
9991002
}
10001003
if (!used_args.contains(inputs[i].get())) {
@@ -1094,33 +1097,28 @@ namespace gpu {
10941097
if (thread_shared.contains(inputs[i].get()) && is_constant[i]) {
10951098
source_buffer << " __shared__ ";
10961099
jit::add_type<T> (source_buffer);
1097-
source_buffer << jit::to_string('t', inputs[i].get())
1100+
source_buffer << " " << jit::to_string('t', inputs[i].get())
10981101
<< "[" << inputs[i]->size() << "]";
10991102
inputs[i]->endline(source_buffer, usage);
11001103
}
11011104
}
11021105
for (size_t i = 0, ie = inputs.size(); i < ie; i++) {
11031106
if (thread_shared.contains(inputs[i].get()) && is_constant[i]) {
1104-
source_buffer << " if (t_index < "
1107+
source_buffer << " for(int j = t_index; j < "
11051108
<< inputs[i]->size()
1106-
<< ") {" << std::endl;
1107-
break;
1108-
}
1109-
}
1110-
for (size_t i = 0, ie = inputs.size(); i < ie; i++) {
1111-
if (thread_shared.contains(inputs[i].get()) && is_constant[i]) {
1112-
source_buffer << " "
1109+
<< "; j += blockDim.x) {" << std::endl
1110+
<< " "
11131111
<< jit::to_string('t', inputs[i].get())
1114-
<< "[t_index] = "
1112+
<< "[j] = "
11151113
<< jit::to_string('v', inputs[i].get())
1116-
<< "[t_index]";
1114+
<< "[j]";
11171115
inputs[i]->endline(source_buffer, usage);
1116+
source_buffer << " }" << std::endl;
11181117
}
11191118
}
11201119
for (size_t i = 0, ie = inputs.size(); i < ie; i++) {
11211120
if (thread_shared.contains(inputs[i].get()) && is_constant[i]) {
1122-
source_buffer << " }" << std::endl
1123-
<< " __syncthreads();"
1121+
source_buffer << " __syncthreads();"
11241122
<< std::endl;
11251123
break;
11261124
}
@@ -1134,9 +1132,9 @@ namespace gpu {
11341132
if (thread_shared.contains(inputs[i].get()) && !is_constant[i]) {
11351133
source_buffer << " __shared__ ";
11361134
jit::add_type<T> (source_buffer);
1137-
source_buffer << jit::to_string('t', inputs[i].get())
1135+
source_buffer << " " << jit::to_string('t', inputs[i].get())
11381136
<< "["
1139-
<< (inputs[i]->size() > 1024 ? 1024 : 32)
1137+
<< (inputs[i]->size() > 1024 ? 1024 : warp_size)
11401138
<< "]";
11411139
inputs[i]->endline(source_buffer, usage);
11421140
source_buffer << " "
@@ -1208,13 +1206,18 @@ namespace gpu {
12081206
if (!out->is_match(in)) {
12091207
auto a = out->compile(source_buffer, registers,
12101208
thread_mem, usage);
1211-
source_buffer << " "
1212-
<< jit::to_string('v', in.get())
1213-
<< "[";
1214-
if (state.get()) {
1215-
source_buffer << "offset[0] + ";
1209+
source_buffer << " ";
1210+
if (thread_shared.contains(in.get())) {
1211+
source_buffer << jit::to_string('t', in.get())
1212+
<< "[t_index] = ";
1213+
} else {
1214+
source_buffer << jit::to_string('v', in.get())
1215+
<< "[";
1216+
if (state.get()) {
1217+
source_buffer << "offset[0] + ";
1218+
}
1219+
source_buffer << "index] = ";
12161220
}
1217-
source_buffer << "index] = ";
12181221
if constexpr (SAFE_MATH) {
12191222
if constexpr (jit::complex_scalar<T>) {
12201223
jit::add_type<T> (source_buffer);
@@ -1282,7 +1285,11 @@ namespace gpu {
12821285
if (thread_shared.contains(in.get())) {
12831286
source_buffer << " "
12841287
<< jit::to_string('v', in.get())
1285-
<< "[index] = "
1288+
<< "[";
1289+
if (state.get()) {
1290+
source_buffer << "offset[0] + ";
1291+
}
1292+
source_buffer << "index] = "
12861293
<< jit::to_string('t', in.get())
12871294
<< "[t_index];" << std::endl;
12881295
}

0 commit comments

Comments
 (0)