-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcube.cu
160 lines (131 loc) · 4.72 KB
/
cube.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <cuda_runtime_api.h>
#include <device_functions.hpp>
#include <device_launch_parameters.h>
#include <driver_types.h>
#include <host_defines.h>
#include <opencv2/core/core.hpp>
#include <opencv2/core/cuda_devptrs.hpp>
#include <opencv2/core/gpumat.hpp>
#include <opencv2/gpu/device/common.hpp>
#include <stdio.h>
#include <vector_types.h>
#include <ostream>
#include <string>
#include <iostream>
#include <typeinfo>
using namespace std;
using std::cout;
__shared__ int edgesValues[240];
__global__ void funcKernel(const float* srcptr, float* dstptr, size_t srcstep,
const size_t dststep, int cols, int rows, int* inputArray_d,
int* outputArray_d) {
int rowInd = blockIdx.y * blockDim.y + threadIdx.y;
int colInd = blockIdx.x * blockDim.x + threadIdx.x;
// printf("test=%d", edgesValues[0]);
if (rowInd >= rows || colInd >= cols)
return;
const float* rowsrcptr = (const float *) (((char *) srcptr)
+ rowInd * srcstep);
// float* rowdstPtr = (float *) (((char *) dstptr) + rowInd * dststep);
float val = rowsrcptr[colInd];
// printf("test");
// printf("\nat row=%d col=%d inp array=%d ", rowInd, colInd,
// inputArray_d[rowInd * cols + colInd]);
if ((rowInd > 2 && rowInd < (rows - 2))
&& (colInd > 2 && colInd < (cols - 2))) {
if (val == 255) {
const float* rowsrcptrNxt = (const float *) (((char *) srcptr)
+ (rowInd + 1) * srcstep);
const float* rowsrcptrPrev = (const float *) (((char *) srcptr)
+ (rowInd - 1) * srcstep);
if (rowsrcptrPrev[colInd - 1] == 0 || rowsrcptrPrev[colInd] == 0
|| rowsrcptrPrev[colInd + 1] == 0
|| rowsrcptr[colInd - 1] == 0 || rowsrcptr[colInd - 1] == 0
|| rowsrcptrNxt[colInd - 1] == 0
|| rowsrcptrNxt[colInd] == 0
|| rowsrcptrNxt[colInd + 1] == 0) {
//outputArray_d[rowInd * cols + colInd] = 1;
edgesValues[rowInd * cols + colInd] = 1;
// printf("\nat row=%d col=%d out araay=%d ", rowInd, colInd,
// outputArray_d[rowInd * cols + colInd]);
// printf(
// "\nat row=%d col=%d ;val=%f, rowsrcptr[colInd-1]=%f, rowsrcptr[colInd+1]=%f,rowsrcptrNxt =%f",
// rowInd, colInd, val, rowsrcptr[colInd - 1],
// rowsrcptr[colInd + 1], rowsrcptrNxt[colInd]);
} else {
edgesValues[rowInd * cols + colInd] = 0;
//
// outputArray_d[rowInd * cols + colInd] = inputArray_d[rowInd
// * cols + colInd];
}
}
}
for (int i = 0; i < rows * cols; i++) {
// printf("in loop=%d", i);
}
}
__global__ void funcKernel2(const float* srcptr, float* dstptr, size_t srcstep,
const size_t dststep, int cols, int rows, int* inputArray_d,
int* outputArray_d) {
int rowInd = blockIdx.y * blockDim.y + threadIdx.y;
int colInd = blockIdx.x * blockDim.x + threadIdx.x;
if (rowInd >= rows || colInd >= cols)
return;
// const float* rowsrcptr = (const float *) (((char *) srcptr)
// + rowInd * srcstep);
// float* rowdstPtr = (float *) (((char *) dstptr) + rowInd * dststep);
// printf("\nat row=%d col=%d inp array=%d ", rowInd, colInd,
// inputArray_d[rowInd * cols + colInd]);
// __shared__ int test[240];
for (int i = 0; i < 239; i++) {
if (edgesValues[i] == 1) {
printf("test contour at %d is %d \n", i, edgesValues[i]);
}
}
}
int divUp(int a, int b) {
return (a + b - 1) / b;
}
//extern "C"
//{
void func(const float* srcptr, float* dstptr, size_t srcstep,
const size_t dststep, int cols, int rows) {
dim3 blDim(32, 8);
dim3 grDim(divUp(cols, blDim.x), divUp(rows, blDim.y));
// size_t size = sizeof(int);
int inputArray_h[rows * cols];
int outputArray_h[rows * cols];
int* inputArray_d;
int* outputArray_d;
for (int j = 0; j < rows * cols; j++) {
inputArray_h[j] = 0;
}
// for (int i = rows * cols - 1; i >= 0; i--)
// cout << "==" << inputArray_h[i];
int ARRAY_BYTES = rows * cols * sizeof(int);
//allocate GPU memory
cudaMalloc((void**) &inputArray_d, ARRAY_BYTES);
cudaMalloc((void**) &outputArray_d, ARRAY_BYTES);
// cudaMalloc((void**) &inputMatrix_d, ARRAY_BYTES);
// cudaMalloc((void**) &outputMatrix_d, ARRAY_BYTES);
cudaMemcpy(inputArray_d, inputArray_h, ARRAY_BYTES, cudaMemcpyHostToDevice);
std::cout << "calling kernel from func\n";
funcKernel<<<grDim, blDim>>>(srcptr, dstptr, srcstep, dststep, cols, rows,
inputArray_d, outputArray_d);
cudaDeviceSynchronize();
funcKernel2<<<1, 1>>>(srcptr, dstptr, srcstep, dststep, cols, rows,
inputArray_d, outputArray_d);
cudaMemcpy(outputArray_d, outputArray_h, ARRAY_BYTES,
cudaMemcpyDeviceToHost);
// if (edgesValues[0]) {
// cout << "host: " << edgesValues[0] << endl;
// }
cout << "\n\nstarting output in host" << endl;
// for (int i = rows * cols - 1; i >= 0; i--)
// cout << "==" << (int) outputArray_h[i];
// int *test;
// cudaMemcpy(counter, test, sizeof(int));
cudaDeviceSynchronize();
// std::cout << "done with kernel call\n==" << counter << endl;
}
//}