[go: up one dir, main page]

Menu

[r7]: / trunk / cocolib / tv / tv_l2.cu  Maximize  Restore  History

Download this file

197 lines (171 with data), 5.8 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
/* -*-c++-*- */
/** \file tv_l2.cu
Algorithms to solve the TV-L2 model:
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 "tv_l2.h"
#include "tv_l2.cuh"
#include "../defs.h"
#include "../cuda/cuda_helper.h"
// Alloc PDE data with sensible defaults
coco::tv_l2_data* coco::tv_l2_data_alloc( gsl_matrix* f, gsl_matrix *g )
{
tv_l2_data *data = new tv_l2_data;
// Texture sizes
data->_W = f->size2;
data->_H = f->size1;
data->_N = data->_W * data->_H;
// Smoothness parameter
data->_lambda = 1.0f;
// Maximum step size with proven convergence
data->_tau = 0.125;
// Relaxation variable
data->_alpha = 1.0;
// Workspace
data->_workspace = new tv_l2_workspace;
memset( data->_workspace, 0, sizeof( tv_l2_workspace ));
// Size of image matrices in bytes
data->_nfbytes = data->_N * sizeof(float);
// Alloc fields
tv_l2_workspace *w = data->_workspace;
// Primal variable components
CUDA_SAFE_CALL( cudaMalloc( &w->_u, data->_nfbytes ));
// Dual variable XI and relaxation states
CUDA_SAFE_CALL( cudaMalloc( &w->_x1, data->_nfbytes ));
CUDA_SAFE_CALL( cudaMalloc( &w->_x2, data->_nfbytes ));
CUDA_SAFE_CALL( cudaMalloc( &w->_x1e, data->_nfbytes ));
CUDA_SAFE_CALL( cudaMalloc( &w->_x2e, data->_nfbytes ));
CUDA_SAFE_CALL( cudaMalloc( &w->_y1, data->_nfbytes ));
CUDA_SAFE_CALL( cudaMalloc( &w->_y2, data->_nfbytes ));
CUDA_SAFE_CALL( cudaMalloc( &w->_f, data->_nfbytes ));
w->_g = NULL;
// Copy f to GPU
data->_f = gsl_matrix_alloc( data->_H, data->_W );
memcpy( data->_f->data, f->data, data->_nfbytes );
assert( f->size2 == data->_W );
assert( f->size1 == data->_H );
cuda_memcpy( w->_f, f );
// Copy TV weight to GPU
data->_g = NULL;
if ( g != NULL ) {
data->_g = gsl_matrix_alloc( data->_H, data->_W );
memcpy( data->_g->data, g->data, data->_nfbytes );
CUDA_SAFE_CALL( cudaMalloc( &w->_g, data->_nfbytes ));
assert( g->size2 == data->_W );
assert( g->size1 == data->_H );
cuda_memcpy( w->_g, g );
}
return data;
}
// Free up PDE data
bool coco::tv_l2_data_free( tv_l2_data *data )
{
// Free GPU fields
tv_l2_workspace *w = data->_workspace;
CUDA_SAFE_CALL( cudaFree( w->_u ));
CUDA_SAFE_CALL( cudaFree( w->_f ));
CUDA_SAFE_CALL( cudaFree( w->_x1 ));
CUDA_SAFE_CALL( cudaFree( w->_x2 ));
CUDA_SAFE_CALL( cudaFree( w->_x1e ));
CUDA_SAFE_CALL( cudaFree( w->_x2e ));
CUDA_SAFE_CALL( cudaFree( w->_y1 ));
CUDA_SAFE_CALL( cudaFree( w->_y2 ));
if ( w->_g != NULL ) {
CUDA_SAFE_CALL( cudaFree( w->_g ));
}
gsl_matrix_free( data->_f );
if ( data->_g != NULL ) {
gsl_matrix_free( data->_g );
}
delete data->_workspace;
delete data;
return true;
}
// Initialize workspace with current solution
bool coco::tv_l2_initialize( tv_l2_data *data,
gsl_matrix* u )
{
data->_alpha = 1.0;
tv_l2_workspace *w = data->_workspace;
if ( u != NULL ) {
cuda_memcpy( w->_u, u );
}
CUDA_SAFE_CALL( cudaMemset( w->_x1, 0, data->_nfbytes ));
CUDA_SAFE_CALL( cudaMemset( w->_x2, 0, data->_nfbytes ));
CUDA_SAFE_CALL( cudaMemset( w->_x1e, 0, data->_nfbytes ));
CUDA_SAFE_CALL( cudaMemset( w->_x2e, 0, data->_nfbytes ));
CUDA_SAFE_CALL( cudaMemset( w->_y1, 0, data->_nfbytes ));
CUDA_SAFE_CALL( cudaMemset( w->_y2, 0, data->_nfbytes ));
CUDA_SAFE_CALL( cudaThreadSynchronize() );
return true;
}
// Get current solution
bool coco::tv_l2_get_solution( tv_l2_data *data,
gsl_matrix* u )
{
tv_l2_workspace *w = data->_workspace;
assert( u->size2 == data->_W );
assert( u->size1 == data->_H );
cuda_memcpy( u, w->_u );
CUDA_SAFE_CALL( cudaThreadSynchronize() );
return true;
}
// Get dual variable XI (vector of dimension 2)
bool coco::tv_l2_get_dual_xi( tv_l2_data *data,
vector<gsl_matrix*> &XI )
{
tv_l2_workspace *w = data->_workspace;
assert( XI.size() == 2 );
for ( size_t i=0; i<2; i++ ) {
gsl_matrix *xi = XI[i];
assert( xi->size2 == data->_W );
assert( xi->size1 == data->_H );
}
cuda_memcpy( XI[0], w->_x1 );
cuda_memcpy( XI[1], w->_x2 );
CUDA_SAFE_CALL( cudaThreadSynchronize() );
return true;
}
double coco::tv_l2_energy( tv_l2_data *data )
{
// Slow: works on CPU
// Copy back fields
tv_l2_workspace *w = data->_workspace;
size_t W = data->_W;
size_t H = data->_H;
float *f = new float[W*H];
float *u = new float[W*H];
CUDA_SAFE_CALL( cudaMemcpy( f, w->_f, W*H*sizeof(float), cudaMemcpyDeviceToHost ));
CUDA_SAFE_CALL( cudaMemcpy( u, w->_u, W*H*sizeof(float), cudaMemcpyDeviceToHost ));
CUDA_SAFE_CALL( cudaThreadSynchronize() );
// Compute energy
double energy = 0.0;
double ef = 1.0 / (2.0 * data->_lambda);
size_t n = 0;
for ( size_t y=0; y<H-1; y++ ) {
for ( size_t x=0; x<W-1; x++ ) {
energy += hypotf( u[n+1]-u[n], u[n+W]-u[n] );
energy += ef * powf( f[n] - u[n], 2.0f );
n++;
}
// Skip one.
n++;
}
// Cleanup and return
delete[] u;
delete[] f;
return energy / double(W*H);
}