Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <cmath>
  5. #include <cuda.h>
  6.  
  7. using namespace std;
  8.  
  9. void FilterCreation(double* GKernel, const int &n) {
  10.     int t = n / 2;
  11.     double sigma = 10.0;
  12.     double r, s = 2.0 * sigma * sigma;
  13.     // sum is for normalization
  14.     double sum = 0.0;
  15.     // generating 5x5 kernel
  16.     for (int x = -t; x <= t; x++) {
  17.         for (int y = -t; y <= t; y++) {
  18.             r = sqrt(x * x + y * y);
  19.             GKernel[(x + t) * n + (y + t)] = (exp(-(r * r) / s)) / (M_PI * s);
  20.             sum += GKernel[(x + t) * n + (y + t)];
  21.         }
  22.     }
  23.  
  24.     // normalising the Kernel
  25.     for (int i = 0; i < n; ++i)
  26.         for (int j = 0; j < n; ++j)
  27.             GKernel[i * n + j] /= sum;
  28. }
  29.  
  30. __global__ void
  31. blured(
  32.         int n,
  33.         int *img,
  34.         int *img_blured,
  35.         double *GKernel) {
  36.     double pixel = 0.;
  37.     int t = n / 2, h = gridDim.x, w = gridDim.y;
  38.     if (blockIdx.x >= t && blockIdx.x < h - t && blockIdx.y >= t && blockIdx.y < w - t) {
  39.         int x = blockIdx.z * gridDim.x * gridDim.y + blockIdx.y * gridDim.x + blockIdx.x;
  40.         for (int i = -t; i <= t; ++i) {
  41.             for (int j = -t; j <= t; ++j) {
  42.                 int y = blockIdx.z * gridDim.x * gridDim.y + (blockIdx.x + i) * gridDim.y + (blockIdx.y + j);
  43.                 pixel +=  img[y] * GKernel[(t + i) * n + (t + j)];
  44.             }
  45.         }
  46.         img_blured[x] = static_cast<int> (pixel);
  47.     }
  48. //    img_blured[blockIdx.z * gridDim.x * gridDim.y + blockIdx.x * gridDim.y + blockIdx.y] = 1;
  49. }
  50.  
  51. int main() {
  52.     int n = 15;
  53.     int t = n / 2;
  54.     double *GKernel, *d_GKernel;
  55.     GKernel = (double*)malloc(n * n * sizeof(double));
  56.     FilterCreation(GKernel, n);
  57.     int h, w;
  58.     ifstream in("test.txt");
  59.     in >> h >> w;
  60.     int N = 3 * h * w;
  61.     int  *img, *d_img, *img_blured, *d_img_blured;
  62.     img = (int*)malloc(N*sizeof(int));
  63.     img_blured = (int*)malloc(N*sizeof(int));
  64.     for (int i = 0; i != N; ++i)
  65.         img_blured[i] = 0.;
  66.     for (int k = 0; k != 3; ++k) {
  67.         for (int i = 0; i != h; ++i) {
  68.             for (int j = 0; j != w; ++j) {
  69.                 in >> img[k * h * w + i * w + j];
  70.             }
  71.         }
  72.     }
  73.     in.close();
  74.     dim3 grid(h, w, 3);
  75.     dim3 block(1, 1, 1);
  76.     cudaMalloc(&d_img, N*sizeof(int));
  77.     cudaMalloc(&d_img_blured, N*sizeof(int));
  78.     cudaMalloc(&d_GKernel, N*sizeof(double));
  79.     cudaMemcpy(d_img, img, N*sizeof(int), cudaMemcpyHostToDevice);
  80.     cudaMemcpy(d_img_blured, img_blured, N*sizeof(int), cudaMemcpyHostToDevice);
  81.     cudaMemcpy(d_GKernel, GKernel, N*sizeof(double), cudaMemcpyHostToDevice);
  82.     blured<<<grid, block>>>(n, d_img, d_img_blured, d_GKernel);
  83.     cudaMemcpy(img_blured, d_img_blured, N*sizeof(int), cudaMemcpyDeviceToHost);
  84.     cout << h << " " << w << "\n";
  85.     for (int k = 0; k != 3; ++k) {
  86.         for (int i = 0; i != h; ++i) {
  87.             for (int j = 0; j != w; ++j) {
  88.                 cout << img_blured[k * h * w + i * w + j] << " ";
  89.             }
  90.             cout << "\n";
  91.         }
  92.     }
  93.     cudaFree(d_img);
  94.     cudaFree(d_img_blured);
  95.     cudaFree(d_GKernel);
  96.     free(GKernel);
  97.     free(img);
  98.     free(img_blured);
  99.     // out.close();
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement