Changes

Jump to: navigation, search

BarraCUDA Boiz

1,623 bytes added, 01:20, 12 April 2017
Assignment 3
[[File:Set Samples.png]]
 
__global__ void setSamples(cv::cuda::PtrStepSz<float> samples, cv::cuda::PtrStepSz<uchar> img, int dimC) {
int i = blockIdx.y*blockDim.y + threadIdx.y;
int j = blockIdx.x*blockDim.x + threadIdx.x;
if (i >= img.rows || j >= img.cols)
return;
int index = i * img.cols + j;
int y_index = j * dimC;
for (int d = 0; d<dimC; d++) {
samples(index, d) = (float)img(i, y_index + d);
}
}
[[File:Calculate Distance Kernel.png]]
 
__global__ void calculateDistance(cv::cuda::PtrStepSz<float> centers, cv::cuda::PtrStepSz<float> samples, int k, int N, int dim, double* minval, float* D) {
// Compute distances between already sampled centers and other input samples.
// Update nearest distance if it is smaller than previous ones.
int col = blockIdx.x * blockDim.x + threadIdx.x;
int row = blockIdx.y * blockDim.y + threadIdx.y;
int i = col + row * N;
//int i = blockIdx.y*blockDim.y + threadIdx.y;
if (i >= N)
return;
double dist = 0.0;
int k_diff = k - 1;
for (int d = 0; d<dim; d++) {
double diff = centers(k_diff, d) - samples(i, d);
dist += diff * diff;
}
if (dist < minval[i]) {
minval[i] = dist;
}
*D += minval[i];
}
[[File:Generate Image Kernel.png]]
 
__global__ void generateImage(cv::cuda::PtrStepSz<uchar> out, cv::cuda::PtrStepSz<int> indices, cv::cuda::PtrStepSz<float> centers, int dim) {
// Generate output image
int i = blockIdx.y*blockDim.y + threadIdx.y;
int j = blockIdx.x*blockDim.x + threadIdx.x;
if (i >= out.rows || j >= out.cols)
return;
int index = i * out.cols + j;
int ci = indices(index, 0);
int y_index = j*dim;
for (int d = 0; d<dim; d++) {
out(i, y_index + d) = (uchar)centers(ci, d);
}
}
36
edits

Navigation menu