Skip to content

Commit

Permalink
[SYCLomatic #1969] Add test for cub::BlockReduce::Reduce
Browse files Browse the repository at this point in the history
Signed-off-by: Wang, Yihan <yihan.wang@intel.com>
  • Loading branch information
yihanwg committed May 16, 2024
1 parent a31e27a commit e698daf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
40 changes: 38 additions & 2 deletions features/feature_case/cub/cub_block.cu
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,43 @@ bool TestBlockReduceValidItem() {
return flag;
}

template <typename T>
__device__ __forceinline__ float reduce_topk_op_2(const float &a,
const float &b) {
return a > b ? a : b;
}

__global__ void reduce_kernel(float *da) {
typedef cub::BlockReduce<float, 32> BlockReduce;
__shared__ typename BlockReduce::TempStorage temp_storage;
int id = threadIdx.x;
BlockReduce rd(temp_storage);
float temp = rd.Reduce(da[id], reduce_topk_op_2<float>);
if (id == 0) {
da[id] = temp;
}
__syncthreads();
}

bool TestBlockReduceWithUserDefineReductions() {
int N = 32;
float *ha = (float *)malloc(N * sizeof(float));
float *da;
cudaMalloc(&da, N * sizeof(float));

for (int i = 0; i < N; i++) {
ha[i] = i * 1.0f;
}

cudaMemcpy(da, ha, N * sizeof(float), cudaMemcpyHostToDevice);
reduce_kernel<<<1, 32>>>(da);
cudaMemcpy(ha, da, 1 * sizeof(float), cudaMemcpyDeviceToHost);
float val = ha[0];
cudaFree(da);
free(ha);
return std::abs(val - 31.0) < 1e-6;
}

int main() {
bool Result = true;
int* dev_data = nullptr;
Expand Down Expand Up @@ -413,12 +450,11 @@ int main() {

Result = TestBlockReduceSumValidItem() && Result;
Result = TestBlockReduceValidItem() && Result;
Result = TestBlockReduceWithUserDefineReductions() && Result;

if(Result) {
std::cout << "passed" << std::endl;
return 0;
}
return 1;
}


2 changes: 2 additions & 0 deletions features/test_feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ def migrate_test():
src.append(' --enable-profiling ')
if test_config.current_test == 'asm_bar':
src.append(' --use-experimental-features=non-uniform-groups ')
if test_config.current_test == 'cub_block':
src.append(' --use-experimental-features=user-defined-reductions ')
if test_config.current_test == 'device_global':
src.append(' --use-experimental-features=device_global ')
if test_config.current_test == 'sync_warp_p2':
Expand Down

0 comments on commit e698daf

Please sign in to comment.