I have a piece of code that is in flagrant violation of DRY. However, I can't figure out a way to refactor it. I'm just wondering what the SO community would suggest. Here's the code:
bool reduce_equal(const Matrix *m, float x) {
unsigned int *dev_bool = safe_cuda_malloc<unsigned int>(1);
unsigned int t = 1;
cudaError_t cudaStat = host2device(1, &t, dev_bool);
check(cudaStat != cudaSuccess, "host2device failed in reduce_eq");
_reduce_equal<<<blockcount(size(m)), BLOCKSIZE>>>
(size(m), m->dev_array, dev_bool, x);
cudaStat = device2host(1, dev_bool, &t);
check(cudaStat != cudaSuccess, "device2host failed in reduce_sum");
cudaFree(dev_bool);
return t == 1;
}
float reduce_sum(const Matrix *m) {
float *dev_sum = safe_cuda_malloc<float>(1);
float sum = 0;
cudaError_t cudaStat = host2device(1, &sum, dev_sum);
check(cudaStat != cudaSuccess, "host2device failed in reduce_sum");
_reduce_sum<<<blockcount(size(m)), BLOCKSIZE>>>
(size(m), m->dev_array, dev_sum);
cudaStat = device2host(1, dev_sum, &sum);
check(cudaStat != cudaSuccess, "device2host failed in reduce_sum");
cudaFree(dev_sum);
return sum;
}
As you can see, there are slight differences in types and function signatures that make any of the typical solutions with macros or helper functions very difficult to implement. Thanks for your help!
Aucun commentaire:
Enregistrer un commentaire