forked from jwetzl/MAPSuperresolution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSRCostFunction.cu
More file actions
180 lines (146 loc) · 5.29 KB
/
Copy pathSRCostFunction.cu
File metadata and controls
180 lines (146 loc) · 5.29 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
/**
* ___ _ _ ___ _ __ __ _ ___
* / __| | | | \ /_\ | \/ | /_\ | _ \
* | (__| |_| | |) / _ \ | |\/| |/ _ \| _/
* \___|\___/|___/_/_\_\_|_|__|_/_/_\_\_|_ ___
* / __| | | | _ \ __| _ \___| _ \ __/ __|
* \__ \ |_| | _/ _|| /___| / _|\__ \
* |___/\___/|_| |___|_|_\ |_|_\___|___/
* 2012
*
* by Jens Wetzl (jens.wetzl@fau.de)
* and Oliver Taubmann (oliver.taubmann@fau.de)
*
* This work is licensed under a Creative Commons
* Attribution 3.0 Unported License. (CC-BY)
* http://creativecommons.org/licenses/by/3.0/
*
**/
#include "SRCostFunction.h"
#include "ImageIO.h"
#include <iostream>
#include "cudalbfgs_error_checking.h"
using namespace std;
namespace gpu_SRCostFunction
{
__device__ float d_priorF;
__global__ void add(float *p, float *q) { *p += *q; }
}
SRCostFunction::SRCostFunction(size_t numUnknowns, const SRSystemMatrix &systemMatrix,
const LRImageStack &lrImages, const GPUHandles &gpuHandles,
cost_function *priorFunction)
: cost_function(numUnknowns)
, m_systemMatrix(systemMatrix)
, m_lrImages(lrImages)
, m_gpuHandles(gpuHandles)
, m_prior(priorFunction)
, m_residualSize(lrImages.getNumImagePixels() * lrImages.getNumImages())
{
CudaSafeCall( cudaMalloc((void**) &m_d_residual, m_residualSize * sizeof(float)) );
#ifdef SUPERRES_TIMING
m_evalTotalTimer = new timer("evalTotal");
m_evalFTimer = new timer("evalFTotal");
m_evalGradTimer = new timer("evalGradTotal");
m_evalPriorTimer = new timer("evalPriorTotal");
#endif
}
SRCostFunction::~SRCostFunction()
{
#ifdef SUPERRES_TIMING
m_evalTotalTimer->saveMeasurement();
m_evalFTimer->saveMeasurement();
m_evalGradTimer->saveMeasurement();
m_evalPriorTimer->saveMeasurement();
delete m_evalTotalTimer;
delete m_evalFTimer;
delete m_evalGradTimer;
delete m_evalPriorTimer;
#endif
CudaSafeCall( cudaFree(m_d_residual) );
}
void SRCostFunction::f_gradf(const float *d_x, float *d_f, float *d_gradf)
{
/**
* Mathematical notation:
* W: Super-resolution system matrix
* x: Super-resolution image
* y: Low resolution image stack
*
* f: Objective function value
* grad: Objective function gradient
*/
#ifdef SUPERRES_TIMING
timer evalTimer("eval");
evalTimer.start();
m_evalTotalTimer->start();
m_evalFTimer->start();
#endif
// static int evals = 0;
// char buf[4096];
// sprintf(buf, "high_%04d.png", evals++);
// ImageIO::saveGPUImage(std::string(buf), d_x, 512, 512);
// Compute residual
CudaSafeCall( cudaMemcpy(m_d_residual, m_lrImages.getPixels(),
m_residualSize * sizeof(float),
cudaMemcpyDeviceToDevice) );
// r = W*x - y
CusparseSafeCall( cusparseScsrmv(m_gpuHandles.cusparseHandle, CUSPARSE_OPERATION_NON_TRANSPOSE,
m_residualSize, m_numDimensions, 1.0f, m_gpuHandles.cusparseDescriptor,
m_systemMatrix.getValues(), m_systemMatrix.getRowPointers(),
m_systemMatrix.getColIndices(), d_x, -1.0f, m_d_residual) );
CudaCheckError();
cudaDeviceSynchronize();
// Compute squared norm of the residual
CublasSafeCall( cublasSetPointerMode(m_gpuHandles.cublasHandle, CUBLAS_POINTER_MODE_DEVICE) );
// f = r^T * r
CublasSafeCall( cublasSdot(m_gpuHandles.cublasHandle, m_residualSize, m_d_residual, 1, m_d_residual, 1, d_f) );
CudaCheckError();
cudaDeviceSynchronize();
#ifdef SUPERRES_TIMING
m_evalFTimer->stop();
m_evalPriorTimer->start();
#endif
// Add prior, compute prior gradient in d_gradf
float fval;
cudaMemcpy(&fval, d_f, sizeof(float), cudaMemcpyDeviceToHost);
if (m_prior)
{
float *d_priorFPtr;
CudaSafeCall( cudaGetSymbolAddress((void**) &d_priorFPtr, gpu_SRCostFunction::d_priorF) );
// grad = grad_prior
m_prior->f_gradf(d_x, d_priorFPtr, d_gradf);
// f += f_prior
gpu_SRCostFunction::add<<<1, 1>>>(d_f, d_priorFPtr);
CudaCheckError();
cudaDeviceSynchronize();
}
else
CudaSafeCall( cudaMemset(d_gradf, 0, m_numDimensions * sizeof(float)) );
#ifdef SUPERRES_TIMING
m_evalPriorTimer->stop();
m_evalGradTimer->start();
#endif
// Total gradient
// grad += W^T * r
#ifndef SUPERRES_STORE_TRANSPOSE
// Use CCS for transpose-multiply
CusparseSafeCall( cusparseScsrmv(m_gpuHandles.cusparseHandle, CUSPARSE_OPERATION_TRANSPOSE,
m_residualSize, m_numDimensions, 2.0f, m_gpuHandles.cusparseDescriptor,
m_systemMatrix.getValues(), m_systemMatrix.getRowPointers(),
m_systemMatrix.getColIndices(), m_d_residual, 1.0f, d_gradf) );
#else
// Use CRS (inefficient)
CusparseSafeCall( cusparseScsrmv(m_gpuHandles.cusparseHandle, CUSPARSE_OPERATION_NON_TRANSPOSE,
m_numDimensions, m_residualSize, 2.0f, m_gpuHandles.cusparseDescriptor,
m_systemMatrix.getValuesCCS(), m_systemMatrix.getColPointersCCS(),
m_systemMatrix.getRowIndicesCCS(), m_d_residual, 1.0f, d_gradf) );
#endif
CudaCheckError();
cudaDeviceSynchronize();
#ifdef SUPERRES_TIMING
m_evalGradTimer->stop();
m_evalTotalTimer->stop();
evalTimer.stop();
evalTimer.saveMeasurement();
#endif
}