[go: up one dir, main page]

Menu

[r9]: / trunk / cocolib / tc / tc.cu  Maximize  Restore  History

Download this file

308 lines (251 with data), 8.1 kB

  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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* -*-c++-*- */
/** \file tc_linear.cu
Algorithms to solve the TV model with linear data term.
Workspace handling and access code.
Copyright (C) 2010 Bastian Goldluecke,
<first name>AT<last name>.net
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include "tc_linear.h"
#include "tc.cuh"
#include "tc_arrays.cuh"
#include "tc_energies.cuh"
#include "../defs.h"
#include "../cuda/cuda_helper.h"
#include "../common/gsl_image.h"
#include "../common/gsl_matrix_derivatives.h"
#include "../common/menger_curvature.h"
#include "../common/profiler.h"
using namespace gov;
// Alloc PDE data with sensible defaults
coco::tc_data* coco::tc_data_alloc( size_t N, gsl_matrix *a )
{
tc_data *data = new tc_data;
size_t W = a->size2;
size_t H = a->size1;
data->_W = W;
data->_H = H;
data->_N = N;
data->_iteration = 0;
// Params
data->_alpha = 0.1;
data->_p = 2.0;
data->_lambda = 0.01;
data->_inner_iterations = 15;
// Workspace
data->_workspace = new tc_workspace;
memset( data->_workspace, 0, sizeof( tc_workspace ));
tc_workspace *w = data->_workspace;
w->_N2 = (N-1)/2;
assert( w->_N2 * 2 + 1 == (int)data->_N );
w->_N = data->_N;
w->_H = data->_H;
w->_W = data->_W;
// CUDA arrays
w->_Nf = W*H*sizeof(stcflt);
w->_Nv = N*N*N*N*w->_Nf;
w->_num_dual = NUM_DUAL;
// Compute total required memory (before allocating)
TRACE( "Allocating tc data " << W << " x " << H << " x " << N << " ..." << endl );
size_t bpM = 1048576;
// u,uq, a,f
size_t nprimal = ( w->_Nf * 4 ) / bpM;
TRACE( " " << nprimal << " Mb for primal and auxiliary variables." << endl );
// px, py
size_t ndual = ( 0 * w->_Nv + 2 * w->_Nf ) / bpM;
ndual += ( w->_num_dual * w->_Nv + w->_Nf ) / bpM;
TRACE( " " << ndual << " Mb for dual variables." << endl );
size_t ntotal_gpu = nprimal + ndual;
TRACE( "Total memory required (gpu): " << ntotal_gpu << " Mb." << endl );
// Primal variable
CUDA_SAFE_CALL( cudaMalloc( &w->_u, w->_Nf ));
CUDA_SAFE_CALL( cudaMalloc( &w->_uq, w->_Nf ));
CUDA_SAFE_CALL( cudaMalloc( &w->_u_star, w->_Nf ));
CUDA_SAFE_CALL( cudaMalloc( &w->_D, w->_Nf ));
// Data term
CUDA_SAFE_CALL( cudaMalloc( &w->_a, w->_Nf ));
CUDA_SAFE_CALL( cudaMalloc( &w->_f, w->_Nf ));
cuda_memcpy( w->_a, a );
cuda_memcpy( w->_f, a );
// Epigraph variable
// cvl_alloc_array( w, w->_v );
//cvl_alloc_array( w, w->_vq );
//cvl_alloc_array( w, w->_vf );
// Dual variables
TRACE( "Alloc dual variables." << endl );
CUDA_SAFE_CALL( cudaMalloc( &w->_px, w->_Nf ));
CUDA_SAFE_CALL( cudaMalloc( &w->_py, w->_Nf ));
for ( size_t i=0; i<w->_num_dual; i++ ) {
// for ( size_t j=0; j<2; j++ ) {
// for ( size_t k=0; k<2; k++ ) {
vector<stcflt*> &xiv = w->_p[i]; //cvl_get_dual_variable( w, i,j,k );
cvl_alloc_array( w, xiv );
}
TRACE( "Alloc complete." << endl );
// Precomputed curvature weights
int N2 = w->_N2;
int N4 = N*N*N*N;
w->_cp_cpu = new stcflt[ N4 ];
for ( int y0o=-N2; y0o<=N2; y0o++ ) {
for ( int y1o=-N2; y1o<=N2; y1o++ ) {
for ( int z0o=-N2; z0o<=N2; z0o++ ) {
for ( int z1o=-N2; z1o<=N2; z1o++ ) {
int idx = AIND( y0o+N2, y1o+N2, z0o+N2, z1o+N2 );
assert( idx >= 0 && idx < N4 );
w->_cp_cpu[ idx ] = menger_curvature_weight( y0o, y1o, z0o, z1o );
}
}
}
}
CUDA_SAFE_CALL( cudaMalloc( &w->_cp, N4*sizeof( stcflt ) ));
CUDA_SAFE_CALL( cudaMemcpy( w->_cp, w->_cp_cpu, N4*sizeof( stcflt ), cudaMemcpyHostToDevice ));
CUDA_SAFE_CALL( cudaThreadSynchronize() );
// Finalize
TRACE( "Finalizing init." << endl );
w->_sigma = 1.0;
w->_lambda = data->_lambda;
w->_rof_lambda = data->_lambda;
w->_L = 1.0;
w->_t = 1.0;
//
w->_mask = NULL;
w->_b = NULL;
w->_bq = NULL;
//
w->_energy_scale_u = 1.0 / double(W*H);
w->_energy_scale_v = 1.0 / double(W*H*N*N*N*N);
// Block sizes
assert( (W%CUDA_BLOCK_SIZE)==0 );
assert( (H%CUDA_BLOCK_SIZE)==0 );
w->_dimBlock = dim3(CUDA_BLOCK_SIZE, CUDA_BLOCK_SIZE);
w->_dimGrid = dim3(W / w->_dimBlock.x, H / w->_dimBlock.y);
CUDA_SAFE_CALL( cudaThreadSynchronize() );
return data;
}
// Set mask for inpainting model
bool coco::tc_set_inpainting_mask( tc_data *data, gsl_matrix *mask )
{
tc_workspace *w = data->_workspace;
CUDA_SAFE_CALL( cudaMalloc( &w->_mask, w->_Nf ));
cuda_memcpy( w->_mask, mask );
return true;
}
// Free up PDE data
bool coco::tc_data_free( tc_data *data )
{
// Free CUDA arrays
tc_workspace *w = data->_workspace;
CUDA_SAFE_CALL( cudaFree( w->_u ));
CUDA_SAFE_CALL( cudaFree( w->_uq ));
CUDA_SAFE_CALL( cudaFree( w->_u_star ));
CUDA_SAFE_CALL( cudaFree( w->_D ));
CUDA_SAFE_CALL( cudaFree( w->_a ));
CUDA_SAFE_CALL( cudaFree( w->_f ));
CUDA_SAFE_CALL( cudaFree( w->_cp ));
// Dual vars
CUDA_SAFE_CALL( cudaFree( w->_px ));
CUDA_SAFE_CALL( cudaFree( w->_py ));
for ( size_t i=0; i<w->_num_dual; i++ ) {
vector<stcflt*> &xi = w->_p[i];
cvl_free_array( w, xi );
}
// Kernels
#ifdef CUDA_DOUBLE
if ( w->_b != NULL ) {
cuda_kernel_dbl_free( w->_b );
//Copy cuda_kernel_dbl_free( w->_bq );
}
#else
if ( w->_b != NULL ) {
cuda_kernel_free( w->_b );
//Copy cuda_kernel_free( w->_bq );
}
#endif
// Free up workspace
delete[] w->_cp_cpu;
delete data->_workspace;
delete data;
return true;
}
// Get current solution
bool coco::tc_get_solution( tc_data *data,
gsl_matrix* u )
{
tc_workspace *w = data->_workspace;
cuda_memcpy( u, w->_u );
// Wait for GPU and return
CUDA_SAFE_CALL( cudaThreadSynchronize() );
return true;
}
// Initialize workspace with current solution
bool coco::tc_initialize( tc_data *data,
gsl_matrix* u )
{
// Copy u
tc_workspace *w = data->_workspace;
// TEST: Neutral init
//gsl_matrix_set_all( u, 0.0 );
// Copy current solution to GPU
data->_iteration = 0;
cuda_memcpy( w->_u, u );
cuda_memcpy( w->_uq, u );
// Init v
/*
tc_init_product_field( data, w->_vq, w->_u );
*/
// Clear dual variables
CUDA_SAFE_CALL( cudaMemset( w->_px, 0, w->_Nf ));
CUDA_SAFE_CALL( cudaMemset( w->_py, 0, w->_Nf ));
// Init dual for v
//reset_dual_variables( data, w->_v );
for ( size_t i=0; i<w->_num_dual; i++ ) {
cvl_clear_array( w, w->_p[i] );
}
// Wait for GPU and return
CUDA_SAFE_CALL( cudaThreadSynchronize() );
return false;
}
// Init kernel
bool coco::tc_set_separable_kernel( tc_data *data, gsl_vector *kernel )
{
tc_workspace *w = data->_workspace;
assert( w->_b == NULL );
assert( w->_bq == NULL );
#ifdef CUDA_DOUBLE
w->_b = coco::cuda_kernel_dbl_alloc_separable( kernel, kernel );
w->_bq = w->_b;
#else
w->_b = coco::cuda_kernel_alloc_separable( kernel, kernel );
w->_bq = w->_b;
#endif
// TODO: Assuming that kernel is symmetric
cout << "WARNING: ASSUMING SYMMETRIC KERNEL IN DEBLURRING." << endl;
return true;
}
// Init kernel
bool coco::tc_set_kernel( tc_data *data, gsl_matrix *kernel )
{
tc_workspace *w = data->_workspace;
assert( w->_b == NULL );
assert( w->_bq == NULL );
gsl_matrix *kq = gsl_matrix_alloc( kernel->size2, kernel->size1 );
gsl_matrix_transpose_memcpy( kq, kernel );
#ifdef CUDA_DOUBLE
w->_b = coco::cuda_kernel_dbl_alloc( kernel );
w->_bq = coco::cuda_kernel_dbl_alloc( kq );
#else
w->_b = coco::cuda_kernel_alloc( kernel );
w->_bq = coco::cuda_kernel_alloc( kq );
#endif
return true;
}